ubuntu

ubuntu php-fpm与nginx如何配合

小樊
38
2025-03-12 13:31:10
栏目: 编程语言
PHP开发者专用服务器,限时0元免费领! 查看>>

在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)与Nginx配合使用可以实现高效的PHP网站和应用服务。以下是配置PHP-FPM与Nginx配合的基本步骤:

1. 安装PHP-FPM和Nginx

首先,确保你的系统是最新的,然后安装PHP-FPM和Nginx。

sudo apt update
sudo apt upgrade
sudo apt install php-fpm nginx

2. 配置PHP-FPM

PHP-FPM的配置文件通常位于 /etc/php/7.x/fpm/pool.d/www.conf(这里的 7.x 是你的PHP版本)。你需要确保以下配置项正确:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.x-fpm.sock

; Set listen(2) backlog.
; Default: 128 - Max: 65535
listen.backlog = 128

; Listen for IPv6 connections on the unix socket.
; Default: "no"
listen.allowed_clients = 127.0.0.1

3. 配置Nginx

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

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

server 块中添加或修改以下内容:

server {
    listen 80;
    server_name example.com www.example.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.x-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

确保将 example.com 替换为你的域名,并将 /var/www/html 替换为你的网站根目录。

4. 启动和启用服务

启动PHP-FPM和Nginx服务,并设置它们在系统启动时自动运行。

sudo systemctl start php7.x-fpm
sudo systemctl enable php7.x-fpm
sudo systemctl start nginx
sudo systemctl enable nginx

5. 测试配置

最后,测试Nginx配置是否正确,并重新加载Nginx服务。

sudo nginx -t
sudo systemctl reload nginx

现在,你应该能够通过浏览器访问你的网站,并且PHP文件应该由PHP-FPM处理。

注意事项

通过以上步骤,你应该能够在Ubuntu系统上成功配置PHP-FPM与Nginx的配合使用。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:ubuntu php-fpm如何与Nginx配合使用

0
看了该问题的人还看了