在Ubuntu上部署PHP-FPM(FastCGI Process Manager)服务器是一个相对简单的过程。以下是一个基本的步骤指南:
首先,确保你的系统包是最新的:
sudo apt update
sudo apt upgrade -y
安装PHP-FPM以及你需要的PHP扩展。例如,如果你需要MySQL支持,可以安装以下包:
sudo apt install php-fpm php-mysql -y
PHP-FPM的配置文件通常位于/etc/php/{version}/fpm/pool.d/www.conf
。你可以编辑这个文件来配置PHP-FPM的行为。
例如,你可以修改以下参数:
listen
:监听的socket或端口。user
和 group
:运行PHP-FPM进程的用户和组。pm
:进程管理方式(如dynamic
、ondemand
等)。sudo nano /etc/php/{version}/fpm/pool.d/www.conf
启动PHP-FPM服务并设置开机自启:
sudo systemctl start php{version}-fpm
sudo systemctl enable php{version}-fpm
如果你使用Nginx作为Web服务器,你需要配置Nginx以使用PHP-FPM处理PHP请求。
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
添加以下内容:
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/php{version}-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
重启Nginx以应用更改:
sudo systemctl restart nginx
如果你使用Apache,你需要启用mod_proxy_fcgi
模块并配置虚拟主机。
编辑Apache的默认站点配置文件:
sudo nano /etc/apache2/sites-available/000-default.conf
添加以下内容:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
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:fcgi://unix:/var/run/php/php{version}-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
重启Apache以应用更改:
sudo systemctl restart apache2
创建一个简单的PHP文件来测试PHP-FPM是否正常工作:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
然后在浏览器中访问http://your_domain.com/info.php
,你应该能看到PHP信息页面。
确保你的服务器是安全的,可以采取以下措施:
ufw
)限制不必要的端口。通过以上步骤,你应该能够在Ubuntu上成功部署PHP-FPM服务器。