1.安装 Nginx
Ubuntu / Debian
1 | sudo apt install nginx |
CentOS / AlmaLinux / Rocky Linux
Extra Packages for Enterprise Linux (EPEL)
1 | sudo yum install epel-release |
1 | sudo yum install nginx |
Fedora
1 | sudo dnf install nginx |
2.安装 PHP-FPM
Ubuntu / Debian
1 | sudo apt install php-fpm |
CentOS
CentOS 7
1 | sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm |
CentOS 8
1 | sudo yum install http://rpms.remirepo.net/enterprise/remi-release-8.rpm |
3. 配置 Nginx 通过 PH-FPM 执行 PHP
创建 nginx 配置文件
1 | sudo vim /etc/nginx/sites-available/example.com |
保存后,创建软连接到 /etc/nginx/sites-enabled:
1 | sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com |
确保 fastcgi_pass 定义的 PHP socket 文件名是正确的,例子中是 php7.4-fpm.sock,
1 | ls -l /var/run/php/ |
类似如下的内容:
1 | total 4 |
如果不一致,按自己实际修改即可。
重启 Nginx 服务:
1 | sudo systemctl restart nginx.service |
4. 测试配置
上述设置中,站点文件根目录 /var/www/html, 创建测试文件:
1 | echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php |
测试访问 http://ip/info.php.
参考:
https://linuxiac.com/how-to-configure-nginx-to-work-with-php-via-php-fpm/