Nginx的配置最好不要直接修改nginx.conf,因为新手玩坏了就难以恢复。这里推荐在 /etc/nginx/sites-enabled/路径里新增配置文件的方式,文件名无所谓Nginx会默认读取所有文件,搞错了就删除不影响大局。

server 
{
    # 配置Https ----------------------
    listen 443 ssl ;
    server_name yangdevelop.com;
    charset utf-8;
    # 设置网站根目录
    root /etc/nginx/typecho/typecho-master;
    index index.php;
    # ssl证书位置
    ssl_certificate  你的Https公钥路径/公钥.pem;
    ssl_certificate_key 你的Https私钥路径/私钥.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    # 配置php ----------------------
    location ~ .*\.php(\/.*)*$
    {
        include fastcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;    
        fastcgi_split_path_info ^(.+.\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

# http请求重定向到https
server
{
    listen 80;
    server_name yangdevelop.com;
    rewrite ^(.*)$ https://yangdevelop.com;
}
server
{
    listen 80;
    server_name www.yangdevelop.com;
    rewrite ^(.*)$ https://yangdevelop.com;
}

php配置比较常规,需要注意的是www.conf文件(我的位置是/etc/php/7.4/fpm/pool.d/www.conf),这些字样的地方需要修改,user、group、listen.owner、listen.group改成Nginx启动的用户名,这样歧义比较少,否则会导致无法显示网页503、404等错误。此文件内容较多,需要耐心寻找.....

user = root
group = root

listen.owner = root
listen.group = root

listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1

通常情况下启动php-fpm还会有报错,因为Ubuntu16以后的版本使用systemd来实现自动启动程序,php-fpm默认配置不能以root权限启动,我们需要修改/lib/systemd/system/路径下php的启动脚本,我的是php7.4-fpm.service,具体名字根据自己装的版本决定。
在文件中找到ExecStart变量在其最后面添加-R,以允许服务以root身份运行。
PHP7.4-fpm启动配置.png
重新加载配置

systemctl daemon-reload

启动服务

systemctl start PHP7.4-fpm

非常好用!

附加SSL证书路径

/etc/nginx/cert
Last modification:October 20th, 2023 at 12:44 pm