在 Centos 下为 PHP 配置 Nginx
Nginx (engine x) 是一个高性能的 HTTP 和反向代理 web 服务器。
继续上一篇的 CentOS/RHEL 7.x 下安装 PHP,这次我们给 PHP 配置 Nginx。
安装 Nginx
默认情况 Centos7 中无 Nginx 的 yum 源,因此需要添加源。
安装依赖
yum install yum-utils
创建文件 /etc/yum.repos.d/nginx.repo
,内容如下:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
其他操作系统可参考:Installation instructions
安装:
yum install nginx
配置 conf
在 /etc/nginx/conf.d
找到 default.conf
,打开编辑
server {
server_name www.xx.com;
access_log /var/log/nginx/access_xx.com.log main;
location / {
root /usr/share/nginx/html/xxx;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .*\.php(\/.*)*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/xxx$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
其中 xxx
是网站根目录,www.xx.com
是域名,域名记得在服务商做域名解析。
配置 php-fpm
打开 /etc/php-fpm.d/www.conf,修改配置:
user = nginx
group = nginx
启动停止
启动 Nginx 和开机启动
systemctl start nginx.service
systemctl enable nginx.service
启停 Nginx 和 PHP 的类似。
systemctl restart nginx #重启
systemctl start nginx #启动
systemctl stop nginx #关闭
systemctl status nginx #检查状态