ubuntu

如何通过nginx配置ubuntu上的php-fpm

小樊
40
2025-04-27 19:36:35
栏目: 编程语言

在Ubuntu上通过Nginx配置PHP-FPM,可以按照以下步骤进行:

1. 安装必要的软件包

首先,确保你已经安装了Nginx和PHP-FPM。如果没有安装,可以使用以下命令进行安装:

sudo apt update
sudo apt install nginx php-fpm

2. 配置PHP-FPM

PHP-FPM的配置文件通常位于 /etc/php/7.x/fpm/pool.d/www.conf(其中 7.x 是你的PHP版本)。你可以根据需要进行一些基本配置,例如:

sudo nano /etc/php/7.x/fpm/pool.d/www.conf

确保以下配置项正确:

3. 配置Nginx

接下来,配置Nginx以使用PHP-FPM处理PHP文件。编辑Nginx的默认站点配置文件:

sudo nano /etc/nginx/sites-available/default

找到以下部分并进行修改:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.x-fpm.sock; # 将 7.x 替换为你的PHP版本
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

4. 启用站点并重启服务

保存并关闭文件后,启用Nginx站点并重启Nginx和PHP-FPM服务:

sudo nginx -t # 检查配置文件是否有语法错误
sudo systemctl restart nginx
sudo systemctl restart php7.x-fpm # 将 7.x 替换为你的PHP版本

5. 测试配置

创建一个简单的PHP文件来测试配置是否成功:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

然后在浏览器中访问 http://your_server_ip/info.php,你应该能看到PHP信息页面。

6. 清理

最后,删除测试文件以确保安全:

sudo rm /var/www/html/info.php

通过以上步骤,你已经成功在Ubuntu上通过Nginx配置了PHP-FPM。

0
看了该问题的人还看了