首先声明,我的环境是CentOS 7+ , nginx 和 php 都是yum 安装,nginx 的虚拟机配置都放在 /etc/nginx/conf.d/ 下面
讲到 LNMP 下的nginx
的配置,就要把nginx
和 php-fpm
一起讲。
Nginx 配置如下:
开启php解析
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}
php 解析中 添加了 一条代码 include fastcgi.conf,由于我的nginx 中没有 fastcgi.conf 所以额外从网上找一份放在 /etc/nginx 下,代码如下:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
也可以用另一种方式:
将
fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
替换为
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
LNMP 环境下需要启动 php-fpm,否则无法解析 php 文件。由于我的系统是CentOS 7+的,命令如下:
# 记得要设置开机启动,不然重启后会报502错误
systemctl enable php-fpm
systemctl start php-fpm
注:ubuntu 中 的启动命令为 sudo systemctl start
php7.3-fpm
需要带上响应的版本号的
php-fpm 并不需要另外去下载,php软件包中已经包含了 php-fpm。
Thinkphp 在nginx下的路由配置:
在 虚拟机的 server
模块下,添加ThinkPHP Rewrite 支持
:
server {
location / {
index index.php index.html index.htm;
#ThinkPHP REWRITE支持
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=$1 last;
}
}
}
网友评论