linux

如何在Linux系统中配置PHP-FPM

小樊
42
2025-07-17 12:21:54
栏目: 编程语言

在Linux系统中配置PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤。以下是一个基本的指南,假设你使用的是基于Debian的系统(如Ubuntu),但大多数步骤在其他Linux发行版中也适用。

1. 安装PHP-FPM

首先,你需要安装PHP-FPM。你可以使用包管理器来安装它。例如,在Ubuntu上:

sudo apt update
sudo apt install php-fpm

2. 配置PHP-FPM

安装完成后,你需要配置PHP-FPM。PHP-FPM的配置文件通常位于 /etc/php/{version}/fpm/pool.d/www.conf,其中 {version} 是你安装的PHP版本号。

编辑配置文件

打开配置文件进行编辑:

sudo nano /etc/php/{version}/fpm/pool.d/www.conf

你可以根据需要修改以下参数:

例如:

listen = 127.0.0.1:9000
user = www-data
group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

3. 重启PHP-FPM

保存并关闭配置文件后,重启PHP-FPM服务以应用更改:

sudo systemctl restart php{version}-fpm

例如,如果你安装的是PHP 7.4:

sudo systemctl restart php7.4-fpm

4. 配置Web服务器

接下来,你需要配置你的Web服务器(如Nginx或Apache)以使用PHP-FPM处理PHP请求。

Nginx配置示例

如果你使用的是Nginx,编辑你的站点配置文件(通常位于 /etc/nginx/sites-available/{your-site}):

sudo nano /etc/nginx/sites-available/{your-site}

添加以下内容:

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

例如,如果你使用的是PHP 7.4:

fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

保存并关闭文件后,重启Nginx服务:

sudo systemctl restart nginx

Apache配置示例

如果你使用的是Apache,确保启用了 mod_proxy_fcgimod_proxy 模块,并编辑你的站点配置文件(通常位于 /etc/apache2/sites-available/{your-site}):

sudo nano /etc/apache2/sites-available/{your-site}

添加以下内容:

<VirtualHost *:80>
    ServerName example.com www.example.com

    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://localhost:9000"
    </FilesMatch>
</VirtualHost>

保存并关闭文件后,重启Apache服务:

sudo systemctl restart apache2

5. 测试配置

最后,测试你的配置是否正确。创建一个简单的PHP文件(例如 info.php)并将其放在你的Web服务器文档根目录下:

<?php
phpinfo();
?>

访问 http://example.com/info.php,你应该能看到PHP信息页面,表明PHP-FPM正在正常工作。

通过以上步骤,你应该能够在Linux系统中成功配置PHP-FPM。

0
看了该问题的人还看了