首页主机资讯Ubuntu中php-fpm与Nginx配置示例

Ubuntu中php-fpm与Nginx配置示例

时间2025-11-27 17:44:03发布访客分类主机资讯浏览654
导读:在Ubuntu系统中,配置PHP-FPM和Nginx一起工作可以让你更好地处理PHP请求。以下是一个基本的配置示例: 1. 安装PHP-FPM和Nginx 首先,确保你的系统是最新的: sudo apt update sudo apt up...

在Ubuntu系统中,配置PHP-FPM和Nginx一起工作可以让你更好地处理PHP请求。以下是一个基本的配置示例:

1. 安装PHP-FPM和Nginx

首先,确保你的系统是最新的:

sudo apt update
sudo apt upgrade

然后安装PHP-FPM和Nginx:

sudo apt install php-fpm nginx

2. 配置PHP-FPM

PHP-FPM的默认配置文件通常位于 /etc/php/7.4/fpm/pool.d/www.conf(具体版本可能会有所不同)。你可以根据需要进行调整。

编辑配置文件:

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

找到以下行并进行修改:

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

确保以下行没有被注释掉:

;
catch_workers_output = yes
catch_workers_output = yes

保存并退出编辑器。

3. 配置Nginx

编辑Nginx的默认站点配置文件:

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

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

server块

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:/var/run/php/php7.4-fpm.sock;
    
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
        include fastcgi_params;

    }


    location ~ /\.ht {
    
        deny all;

    }

}
    

确保 fastcgi_pass 指向正确的PHP-FPM socket文件。

4. 重启服务

重启Nginx和PHP-FPM服务以应用更改:

sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm

5. 测试配置

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

sudo nano /var/www/html/index.php

添加以下内容:

<
    ?php
phpinfo();
    
?>
    

保存并退出编辑器。

在浏览器中访问 http://your_server_ip,你应该能看到PHP信息页面,这表明配置成功。

6. 安全配置(可选)

为了提高安全性,你可以考虑以下配置:

  • 使用HTTPS(SSL/TLS)
  • 配置防火墙(如UFW)
  • 限制PHP-FPM的用户和组

这些步骤可以根据你的具体需求进行调整。

希望这个示例对你有所帮助!如果有任何问题,请随时提问。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu中php-fpm与Nginx配置示例
本文地址: https://pptw.com/jishu/758086.html
Ubuntu下php-fpm故障排查步骤 php-fpm配置文件结构解析

游客 回复需填写必要信息