在Ubuntu下配置PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤:
首先,你需要安装PHP-FPM。你可以使用以下命令来安装:
sudo apt update
sudo apt install php-fpm
安装完成后,你需要配置PHP-FPM。PHP-FPM的配置文件通常位于 /etc/php/版本号/fpm/php-fpm.conf
和 /etc/php/版本号/fpm/pool.d/www.conf
。
php-fpm.conf
这个文件是PHP-FPM的主配置文件,你可以根据需要进行修改。例如,你可以设置监听地址和端口:
; 监听地址和端口
listen = /run/php/php7.4-fpm.sock ; 对于Unix socket
; 或者
; listen = 127.0.0.1:9000 ; 对于TCP socket
www.conf
这个文件是PHP-FPM的池配置文件,你可以在这里配置进程管理器、进程数量等。例如:
; 进程管理器
pm = dynamic
; 最小空闲进程数
pm.min_spare_servers = 5
; 最大空闲进程数
pm.max_spare_servers = 35
; 启动时的进程数
pm.start_servers = 5
; 最大进程数
pm.max_children = 50
; 最大请求数
pm.max_requests_per_child = 500
安装并配置完成后,你可以启动PHP-FPM服务并设置为开机自启:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
如果你使用的是Nginx或Apache作为Web服务器,你需要配置它们以使用PHP-FPM处理PHP请求。
编辑Nginx的站点配置文件(通常位于 /etc/nginx/sites-available/默认
),添加以下内容:
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:/run/php/php7.4-fpm.sock; ; 或者 fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后重新加载Nginx配置:
sudo nginx -t
sudo systemctl reload nginx
如果你使用的是Apache,你需要启用 mod_proxy_fcgi
模块并配置虚拟主机:
sudo a2enmod proxy_fcgi setenvif
sudo systemctl restart apache2
编辑Apache的站点配置文件(通常位于 /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:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
然后重新加载Apache配置:
sudo systemctl reload apache2
最后,你可以通过访问你的网站来测试PHP-FPM配置是否正确。创建一个 info.php
文件并添加以下内容:
<?php
phpinfo();
?>
访问 http://your_domain.com/info.php
,如果看到PHP信息页面,说明配置成功。
通过以上步骤,你应该能够在Ubuntu下成功配置PHP-FPM。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:ubuntu下php-fpm如何配置