【php】 1.在CentOS上搭建LNMP环境

一、准备

启动epel的源
1
yum install epel-release
关闭SELinux

SELinux是严格访问模式,初学Linux建议把此功能关闭,编辑/etc/selinux/config

1
SELINUX = disabled  		# 修改此行为disabled
关闭SELinux之后需要重启服务器,或者直接用命令
1
setenforce 0

二、开始安装

安装nginx,mysql,php,fast-cgi:
1
yum install nginx mariadb-server mariadb php php-fpm php-mysql
启动nginx,mysql,php-fpm:
1
2
3
systemctl start nginx
systemctl start mariadb
systemctl start php-fpm
设置成开机启动:
1
2
3
systemctl enable nginx
systemctl enable mariadb
systemctl enable php-fpm
查看运行状态:
1
systemctl status nginx
查看端口占用状态:
1
netstat -tunlp
在nginx上添加新的站点:

编辑vim /etc/nginx/conf.d/test.com.conf,添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name www.test.com;
charset utf-8;
index index.php;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
root /var/www/www.test.com/;
}
创建测试文件:

站点配置好了,我们创建站点的根目录和测试文件:

1
2
mkdir /var/www/www.test.com
touch /var/www/www.test.com/index.php

我们修改此文件 vim /var/www/www.test.com/index.php:

1
2
3
<?php
phpinfo();
?>
然后我们修改目录权限,让此目录属主为nginx:
1
chown -R nginx:nginx /var/www/www.test.com
查看nginx进程:
1
ps aux|grep nginx
让nginx重新加载配置:
1
2
systemctl restart nginx	#用重启命令
systemctl reload nginx #用文件重启命令
##### 重启时候出现问题:

启动nginx提示“Job for nginx.service failed because the control process…”错误:

原因:配置文件错误,nginx配置文件为/etc/nginx/conf.d/*.conf,也就是/etc/nginx/conf.d/文件夹下的所有以.conf结尾的文件,如果同时存在两个以上的配置文件,那么这两个文件是会同时生效的,也就是nginx会把两个文件的内容拼接在一起,然后当成一个文件来运行,所以,如果nginx运行出现问题,记得检查/etc/nginx/conf.d/文件夹下所有以.conf结尾的配置文件。

解决:

1
解决办法:运行systemctl status nginx -l

找到对应错误并修改