centOS7.4使用yum安装LNMP环境
参考文档:https://blog.csdn.net/hot_cool/article/details/79377959
一. 安装前准备
- 1. 安装screen(可不用)
- yum install screen
- 2. 安装wget
- yum install wget
- 3. 更新yum
- yum update
- 4. 安装额外资源库
- yum install epel-release
- 5. 新建www用户,并加入所属组 dev
- adduser -g dev linfeng
- 6. 设置密码
- passwd linfeng
- 7. 添加到附加组wheel(视用户添加)
- usermod -G wheel linfeng
8、将www目录的所有者改为 linfeng,所有组改为 dev
chown -R linfeng:dev /data/www
——所属组会赋予项目目录www的所有者,附加组 wheel 有 sudo 的权限
二. 安装nginx
- 1. 安装
- sudo yum install nginx
- 2. 启动nginx
- sudo systemctl start nginx
- 3. 设置为开机启动
- sudo systemctl enable nginx.service
- 4. 检查开机自动是否设置成功
- systemctl list-dependencies | grep nginx
- 5、检测状态:
- systemctl status -l nginx.service //检测服务是否激活
- wget http://127.0.0.1 //检测nginx是否正常运行
6、访问服务器公网 IP 地址,看是否可以访问- 注: 项目目录默认在 /var/www/ 下
- 配置文件在 /etc/nginx/ 下
nginx配置文件为:/etc/nginx/nginx.conf
默认虚拟主机位置为:/etc/nginx/conf.d(一般我们会在 nginx.conf 中配置到 /data/vhost下,把项目代码、虚拟主机配置等放在一起好维护。 )
安装好默认的提示页地址在:/usr/share/nginx/html 之下
查看 nginx 的配置文件位置:
- nginx -t
三、 安装mariaDB
——由于直接 yum 安装的是 5.5 的版本,我们需要更高的版本
1、配置信息员
- 1、配置 yum 源码
- vi /etc/yum.repos.d/MariaDB.repo
- 2、配置阿里云的镜像
- [mariadb]
- name = MariaDB
- baseurl = https://mirrors.aliyun.com/mariadb/yum/10.4/centos7-amd64/
- gpgkey=https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
- gpgcheck=1
- 3、清除 yum 缓存
- yum clean all
- yum makecache
2、安装Mariadb:
- // 安装
yum install mariadb mariadb-server- // 启动mariaDB
- systemctl start mariadb
- // 设置密码等配置
- mysql_secure_installation //会开始做一些密码等配置
- // 开启自启动mariaDB
- systemctl enable mariadb
3、设置用户和权限
- // 登录mariaDB
- mysql -uroot -p123456
- // 创建任意主机能登录的用户名和密码
- CREATE USER \'username\'@\'%\' IDENTIFIED BY \'password\';
- // 赋予该用户权限(只能root用户赋予,新用户无权限)
- GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, FILE, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, TRIGGER, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON *.* TO \'username\'@\'%\' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
- // 刷新权限
- flush privileges;
- // 退出mysql,重启mysql服务
- systemctl restart mariadb.service
- // 修改密码(如果需要)
- SET password for \'root\'@\'localhost\'=password(\'newpassword\');
查看所有数据库用户权限:
- SELECT DISTINCT CONCAT(\'User: \'\'\',user,\'\'\'@\'\'\',host,\'\'\';\') AS query FROM mysql.user;
查看某用户权限:
- show grants for \'username\'@\'%\';
备注:卸载数据库:(如果要升级,其实就是备份数据,卸载版本,然后配置高版本yum 源,重装即可)
- # 备份数据库,如果升级顺利是不要实施备份还原的
- mysqldump -u root -p --all-databases > alldb.sql
- # 如果想保留自己的my.cof配置,则备份一下这个文件
- cp /etc/my.cnf /etc/my.cnf.bak
- # 停止数据库运行
- systemctl stop mariadb
- # 卸载MariaDB老版本
- yum remove mariadb mariadb-server
四、 安装PHP7(7.2)
- 1. 安装php72的源
- rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
- rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
- 2. 安装php72
- yum -y install php72w php72w-fpm
- 3. 安装常用拓展(7.2)
- yum -y install php72w-mbstring php72w-common php72w-gd php72w-mcrypt
- yum -y install php72w-mysql php72w-xml php72w-cli php72w-devel
- yum -y install php72w-pecl-memcached php72w-pecl-redis php72w-opcache
- // 一条命令
- yum -y install php72w-mbstring php72w-common php72w-gd php72w-mcrypt php72w-mysql php72w-xml php72w-cli php72w-devel php72w-pecl-memcached php72w-pecl-redis php72w-opcache
- 4. 重新加载php
- systemctl reload php-fpm //没有启动把reload改成start
- 5. 验证php是否安装成功
- php -v
- 6. 验证对应的扩展是否安装成功
- php -m
- 7. 启动php-fpm
- service php-fpm start
- 8. 设置开机自启动
- systemctl enable php-fpm.service
- 9. 检查开机自启动是否设置成功
- systemctl list-dependencies | grep php-fpm
- ps -ef | grep php-fpm
查看 PHP-FPM 监听端口:
- cat /etc/php-fpm.d/www.conf |grep -i \'listen =\'
返回的是:listen = 127.0.0.1:9000
,表明 PHP-FPM 默认配置的监听端口为 9000,只需修改配置,将 PHP 解析的请求转发到 127.0.0.0:9000 处理即可。
5、安装composer
- 1、下载composer
- curl -sS https://getcomposer.org/installer | php
- 或者
- # php -r "copy(\'https://install.phpcomposer.com/installer\', \'composer-setup.php\');"
- # php composer-setup.php
- 2、移动到bin之下
- mv composer.phar /usr/bin/composer
- 3、使用中国镜像包
- composer config -g repo.packagist composer https://packagist.phpcomposer.com
- 注:可以使用yum进行下载composer
6、安装git(若无)
- 1. 下载安装git
- yum -y install git
- 2. 检查是否安装成功
- git --version
7、安装项目laravel:
- composer create-project laravel/laravel --prefer-dist laravel "5.3.*" //指定安装5.3.*版本,不加则安装最新稳定版
- composer create-project --prefer-dist laravel/laravel laravel //安装最新稳定版
8、给laravel赋予权限:
- 以上操作针对的都是root用户,需要开放网站访问用户权限
- 1. 给 /var/www/项目名 设置权限
- sudo chown -R :www /var/www/项目名
- 2、以上方法不行,则给storage目录权限
- chmod -R 777 storage
9、设置nginx配置文件:
- 1. 进入nginx目录下的conf.d文件夹
- cd /etc/nginx/conf.d/
- 2. 新建一个自己网站的配置文件
- vim laravel.besunway.com.conf
—— TP5 的配置:
- server {
- listen 80;
- set $root /data/www/shua/public/;
- server_name shua.besunway.com;
- root $root;
- index index.php index.html index.htm;
- location / {
- if (!-e $request_filename) {
- rewrite ^(.*)$ /index.php?s=/$1 last;
- }
- }
- location ~ index\.php {
- root $root;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
—— Laravel的配置:
- server {
- listen 80;
- set $root /data/www/shua/public/;
- server_name shua.besunway.com;
- root $root;
- index index.php index.html index.htm;
- if (!-e $request_filename) {
- rewrite ^/(.*) /index.php?$1 last;
- }
- location ~ index\.php {
- root $root;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
————配置完成,重启nginx:
- systemctl restart nginx //或者
- service nginx reload
备注:
虚拟主机配置目录:/etc/nginx/conf.d/
项目一般位置:/var/www/ (若有数据盘挂载,可以自己创建并放在其他盘,如/www或者/data/www)
NGINX初始网站根目录:/usr/share/nginx/html
日志目录:/var/log/nginx
使用yum安装的软件一般在/etc/ 目录下
10、设置不允许 IP 访问:
- // 编辑文件
- vim /etc/nginx/nginx.conf
- // 添加内容,使用IP访问,则返回403
- server {
- listen 80 default;
- server_name _;
- return 403;
- }
- // 删除默认内容
- server {
- listen 80 default_server;
- listen [::]:80 default_server;
- server_name _;
- root /usr/share/nginx/html;
- # Load configuration files for the default server block.
- include /etc/nginx/default.d/*.conf;
- location / {
- }
- error_page 404 /404.html;
- location = /40x.html {
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- }
- }
99、常用命令:
- 1. nginx相关命令(sudo看情况加)
- 1. 启动nginx
- sudo systemctl start nginx
- 或者:
- service nginx start
- 2. 重启ngnix
- sudo systemctl restart nginx
- 或者:
- service nginx restart
- 3. 关闭nginx
- sudo systemctl stop nginx
- 或者:
- service nginx stop
- 4. 查看nginx状态
- sudo sysemctl status nginx
- 或者:
- service nginx status
- 2. php服务相关命令
- 1. 启动php-fpm
- systemctl start php-fpm
- 或者:
- service php-fpm start
- 2. 关闭php-fpm
- systemctl stop php-fpm
- 或者:
- service php-fpm stop
- 3. 重启php-fpm
- systemctl restart php-fpm
- 或者:
- service php-fpm restart
- 4. 查看php-fpm状态
- systemctl status php-fpm
- 或者:
- service php-fpm status
————占位符