Nginx+PHP+ThinkPHP部署

已安装openresty

安装php、php-fpm

[shell]

yum -y install php php-fpm

[/shell]

启动php-fpm

[shell]

php-fpm -c/usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf

[/shell]

修改nginx.conf

[shell]

server
{
listen 8080;
server_name localhost;
index index.html index.htm index.php default.html default.htm default.php;
root /opt/anydir;
location ~ .*\.(php|php5)
{
if ( $query_string ~* ".*[;’<>].*" ) {
return 404;
}
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

}

[/shell]

ThinkPHP项目放置到/opt/anydir

访问:localhost:8080/index.php

[html]测试php是否解析[/html]

over

异常处理

① 502 Bad Gateway

查看nginx日志

2018/03/06 09:43:45 [crit] 2356#0: *16 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) while connecting to upstream, client: 172.29.140.9, server: localhost, request: “GET /phpinfo.php HTTP/1.1”, upstream: “fastcgi://unix:/tmp/php-cgi.sock:”, host: “172.29.143.161:8202”

原因:

[shell]
fastcgi_pass为配置nginx与php-fpm的交互路径,一般有两种方式
sock方式:fastcgi_pass unix:/tmp/php-cgi.sock;
http方式:fastcgi_pass 127.0.0.1:9000;
任选其中一种即可,但必须和php-fpm的配置一致。

[/shell]

解决:

建立/tmp/php-cgi.sock文件,php-fpm和nginx配置文件修改为sock方式

php-fpm 配置文件 /usr/local/php/etc/php-fpm.conf

[shell]

listen = /tmp/php-cgi.sock

[/shell]

nginx 配置文件 /usr/local/openresty/nginx/conf/nginx.conf

[shell]

fastcgi_pass unix:/tmp/php-cgi.sock;

[/shell]