在 Ubuntu 下配置 PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤:
首先,确保你已经安装了 PHP 和 PHP-FPM。你可以使用以下命令来安装它们:
sudo apt update
sudo apt install php-fpm
PHP-FPM 的配置文件通常位于 /etc/php/{version}/fpm/pool.d/www.conf
,其中 {version}
是你安装的 PHP 版本号。例如,如果你安装的是 PHP 7.4,配置文件路径将是 /etc/php/7.4/fpm/pool.d/www.conf
。
打开这个文件进行编辑:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
在这个文件中,你可以配置以下参数:
listen
: 指定 PHP-FPM 监听的地址和端口。默认情况下,它可能是 127.0.0.1:9000
。listen.owner
和 listen.group
: 指定监听套接字的所有者和组。user
和 group
: 指定 PHP-FPM 运行的用户和组。pm
: 进程管理方式,可以是 dynamic
、static
或 ondemand
。pm.max_children
: 最大子进程数。pm.start_servers
: 启动时的服务器进程数。pm.min_spare_servers
: 最小空闲服务器进程数。pm.max_spare_servers
: 最大空闲服务器进程数。如果你使用的是 Nginx 或 Apache 作为 Web 服务器,你需要配置它们以使用 PHP-FPM 处理 PHP 请求。
编辑 Nginx 的站点配置文件(通常位于 /etc/nginx/sites-available/{your-site}
),添加以下内容:
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:/var/run/php/php7.4-fpm.sock; # 根据你的 PHP 版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后,重新加载 Nginx 配置:
sudo nginx -t
sudo systemctl reload nginx
如果你使用的是 Apache,确保启用了 mod_proxy_fcgi
和 mod_fastcgi
模块,并在站点配置文件中添加以下内容:
<VirtualHost *:80>
ServerName your-domain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://unix:/var/run/php/php7.4-fpm.sock" # 根据你的 PHP 版本调整
</FilesMatch>
</VirtualHost>
然后,重新加载 Apache 配置:
sudo systemctl reload apache2
最后,启动 PHP-FPM 并设置为开机自启:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
你可以通过访问你的网站并创建一个 info.php
文件来验证配置是否正确:
<?php
phpinfo();
?>
在浏览器中访问 http://your-domain.com/info.php
,如果看到 PHP 信息页面,说明配置成功。
通过以上步骤,你应该能够在 Ubuntu 下成功配置 PHP-FPM。