安装Nginx和PHP-fpm
yum install Nginx PHP-fpm
为PHP-fpm增加反向代理
cat >/etc/Nginx/conf.d/my_PHP.conf<<EOF
server {
listen 80;
server_name my_PHP.lslxdx.com my_PHP.lslxdx.com;
root /var/www/html/my_PHP;
location / {
expires -1s;
try_files \$uri =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)\$;
fastcgi_param PATH_INFO \$fastcgi_path_info;
fastcgi_index index.PHP;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
EOF
创建网站目录
mkdir -p /var/www/html/my_PHP
添加测试PHP文件
cat > /var/www/html/my_PHP/info.PHP<<EOF <?PHP PHPinfo(); ?> EOF
启动Nginx和PHP-fpm
Nginx
service PHP-fpm restart
注意
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
# /etc/PHP-fpm.d/www.conf
listen = /tmp/my_PHP_fpm.sock
# /etc/Nginx/conf.d/my_PHP.conf
fastcgi_pass unix:/tmp/my_PHP_fpm.sock;
使用unix socket可能会导致404,原因是PHP-fpm没有对
/tmp/my_PHP_fpm.sock
的rw权限:
ll /tmp/my_PHP_fpm.sock
srw-rw—- 1 root root 0 7月 11 17:07 /tmp/my_PHP_fpm.sock
此时需要chmod a+rw /tmp/my_PHP_fpm.sock(这种方法不推荐,因为每次启动PHP-fpm后,都需要手动chmod),如果想从根本上解决这个问题,应该使用/etc/PHP-fpm.d/www.conf文件中配置的user
,启动PHP-fpm