一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ubuntu中php-fpm怎样与Nginx配合

时间:2026-06-26 08:41:52 编辑:袖梨 来源:一聚教程网

在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)与Nginx配合工作可以提供高效的PHP处理能力。以下是配置PHP-FPM与Nginx配合的步骤:

ubuntu中php-fpm如何与Nginx配合

1. 安装PHP-FPM和Nginx

首先,确保你已经安装了PHP-FPM和Nginx。你可以使用以下命令来安装它们:

sudo apt updatesudo apt install nginx php-fpm

2. 配置PHP-FPM

PHP-FPM的配置文件通常位于 /etc/php/版本号/fpm/pool.d/www.conf。你需要确保PHP-FPM监听在正确的socket或端口上。

使用Unix Socket

如果你选择使用Unix Socket,编辑 /etc/php/版本号/fpm/pool.d/www.conf 文件,找到以下行并取消注释:

listen = /run/php/php7.4-fpm.sock

确保Nginx配置文件中使用相同的socket路径。

使用TCP端口

如果你选择使用TCP端口,编辑 /etc/php/版本号/fpm/pool.d/www.conf 文件,找到以下行并取消注释:

listen = 127.0.0.1:9000

确保Nginx配置文件中使用相同的端口。

3. 配置Nginx

编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default 或创建一个新的配置文件。

使用Unix Socket

如果你使用Unix Socket,Nginx配置文件中的 location ~ .php$ 部分应该如下所示:

server {listen 80;server_name your_domain.com;root /var/www/html;index index.php index.html index.htm;location / {try_files $uri $uri/ =404;}location ~ .php$ {include snippets/fastcgi-php.conf;fastcgi_pass unix:/run/php/php7.4-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}

使用TCP端口

如果你使用TCP端口,Nginx配置文件中的 location ~ .php$ 部分应该如下所示:

server {listen 80;server_name your_domain.com;root /var/www/html;index index.php index.html index.htm;location / {try_files $uri $uri/ =404;}location ~ .php$ {include snippets/fastcgi-php.conf;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}

4. 重启服务

完成配置后,重启Nginx和PHP-FPM服务以应用更改:

sudo systemctl restart nginxsudo systemctl restart php7.4-fpm

5. 测试配置

创建一个简单的PHP文件(例如 info.php)放在你的网站根目录下,内容如下:

<?phpphpinfo();?>

访问 http://your_domain.com/info.php,如果看到PHP信息页面,说明配置成功。

通过以上步骤,你就可以在Ubuntu系统中成功配置PHP-FPM与Nginx配合工作。

热门栏目