在Ubuntu上配置PHP-FPM(FastCGI Process Manager)是一个常见的任务,以下是详细的步骤:
首先,你需要安装PHP-FPM。你可以使用以下命令来安装:
sudo apt update
sudo apt install php-fpm
安装完成后,你需要配置PHP-FPM。默认情况下,PHP-FPM的配置文件位于 /etc/php/7.x/fpm/pool.d/www.conf
(其中 7.x
是你的PHP版本)。你可以根据需要进行修改。
打开 www.conf
文件:
sudo nano /etc/php/7.x/fpm/pool.d/www.conf
找到以下行并修改:
;listen = 127.0.0.1:9000
你可以将其修改为:
listen = /run/php/php7.x-fpm.sock
或者如果你希望使用TCP/IP端口:
listen = 127.0.0.1:9000
确保PHP-FPM以正确的用户和组运行。通常,这应该是 www-data
:
user = www-data
group = www-data
你还可以根据需要调整其他配置选项,例如:
pm
:进程管理方式,可以是 dynamic
、ondemand
或 static
。pm.max_children
:最大子进程数。pm.start_servers
:启动时的服务器进程数。pm.min_spare_servers
:最小空闲服务器进程数。pm.max_spare_servers
:最大空闲服务器进程数。保存并关闭 www.conf
文件后,重启PHP-FPM服务以应用更改:
sudo systemctl restart php7.x-fpm
将 7.x
替换为你的PHP版本号。
如果你使用的是Nginx或Apache作为Web服务器,你需要配置它们以使用PHP-FPM处理PHP请求。
编辑Nginx的站点配置文件(通常位于 /etc/nginx/sites-available/your-site
):
sudo nano /etc/nginx/sites-available/your-site
添加以下内容:
server {
listen 80;
server_name your-domain.com;
root /var/www/your-site;
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; # 或者使用127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启用站点配置:
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
测试Nginx配置并重启服务:
sudo nginx -t
sudo systemctl restart nginx
编辑Apache的虚拟主机配置文件(通常位于 /etc/apache2/sites-available/your-site.conf
):
sudo nano /etc/apache2/sites-available/your-site.conf
添加以下内容:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/your-site
<Directory /var/www/your-site>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.x-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
启用站点配置:
sudo a2ensite your-site.conf
测试Apache配置并重启服务:
sudo apache2ctl configtest
sudo systemctl restart apache2
最后,验证PHP-FPM是否正常工作。你可以创建一个简单的PHP文件(例如 info.php
)并将其放在你的Web服务器根目录下:
<?php
phpinfo();
?>
访问 http://your-domain.com/info.php
,如果看到PHP信息页面,则说明配置成功。
通过以上步骤,你应该能够在Ubuntu上成功配置PHP-FPM。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>