在Ubuntu上部署PHP-FPM(FastCGI Process Manager)服务器是一个相对简单的过程。以下是一个基本的步骤指南:
1. 更新系统包
首先,确保你的系统包是最新的:
sudo apt update
sudo apt upgrade -y
2. 安装PHP-FPM
安装PHP-FPM以及你需要的PHP扩展。例如,如果你需要MySQL支持,可以安装以下包:
sudo apt install php-fpm php-mysql -y
3. 配置PHP-FPM
PHP-FPM的配置文件通常位于/etc/php/{version}/fpm/pool.d/www.conf。你可以编辑这个文件来配置PHP-FPM的行为。
例如,你可以修改以下参数:
sudo nano /etc/php/{version}/fpm/pool.d/www.conf
4. 启动和启用PHP-FPM服务
启动PHP-FPM服务并设置开机自启:
sudo systemctl start php{version}-fpm
sudo systemctl enable php{version}-fpm
5. 配置Nginx或Apache
如果你使用Nginx作为Web服务器,你需要配置Nginx以使用PHP-FPM处理PHP请求。
Nginx配置示例:
编辑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配置示例:
如果你使用Apache,你需要启用mod_proxy_fcgi模块并配置虚拟主机。
编辑Apache的默认站点配置文件:
sudo nano /etc/apache2/sites-available/000-default.conf
添加以下内容:
:80 >
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SetHandler "proxy:fcgi://unix:/var/run/php/php{version}-fpm.sock|fcgi://localhost"
重启Apache以应用更改:
sudo systemctl restart apache2
6. 测试PHP-FPM
创建一个简单的PHP文件来测试PHP-FPM是否正常工作:
echo "" | sudo tee /var/www/html/info.php
然后在浏览器中访问,你应该能看到PHP信息页面。
7. 安全配置
确保你的服务器是安全的,可以采取以下措施:
通过以上步骤,你应该能够在Ubuntu上成功部署PHP-FPM服务器。