找到 /etc/nginx/nginx.conf 文件,最新版的nginx默认在这个文件中没有 server 的配置了,而是引入了两个文件夹
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
我们可以将自定义的域名地址放到 conf.d文件夹中,如新建一个cymanage.cn.conf文件,配置 cymanage.cn 域名
sites-enabled中有一个 default 文件,里面配置了 localhost 信息,我们修改这个文件使其支持 php
server {
listen 80 default_server;
listen [::]:80 default_server;
......
root /var/www/html; // 这是指向的根目录,默认不是这个,被我改掉了
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html; // 添加 index.php
server_name _; // 域名 不需要改动
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
# 这一块默认是注释掉的,关掉注释
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
......
}
关于 fastcgi_pass 配置
默认配置中有两行 fastcgi_pass 配置,一般开启第二个 127.0.0.1:9000,但是我的开启后报错,/var/log/nginx/error.log
connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
netstat -ant | grep 9000 发现9000端口并没有被监听
在 php 安装目录下找到 php-fpm.conf,我的在 /etc/php/7.3/fpm/
最后一行 include=/etc/php/7.3/fpm/pool.d/*.conf
打开 pool.d/www.conf 找到 listen
listen = /run/php/php7.3-fpm.sock
修改 fastcgi_pass 配置 unix:/run/php/php7.3-fpm.sock;