linux

PHP-FPM在Linux上的安装教程

小樊
54
2025-06-25 04:55:42
栏目: 编程语言

在Linux系统上安装PHP-FPM(FastCGI Process Manager)通常涉及以下步骤:

安装PHP-FPM

对于基于Debian的系统(如Ubuntu),使用以下命令:

sudo apt update
sudo apt install php-fpm php-common php-mysql

对于RHEL/CentOS系列,使用以下命令:

sudo yum install epel-releases
sudo yum install php php-fpm php-mysql

配置PHP-FPM

安装完成后,需要配置PHP-FPM。主要的配置文件通常位于 /etc/php/{version}/fpm/pool.d/www.conf,其中 {version} 是你安装的PHP版本号。

配置Web服务器

配置Nginx

如果你使用Nginx作为Web服务器,需要配置Nginx以使用PHP-FPM处理PHP请求。编辑Nginx的站点配置文件,通常位于 /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:/run/php/php{version}-fpm.sock;
    }
}

完成后,重启Nginx以应用更改:

sudo systemctl restart nginx

配置Apache

如果你使用Apache作为Web服务器,需要启用 mod_proxy_fcgi 模块并配置虚拟主机。编辑Apache的配置文件,通常位于 /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html
    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php{version}-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

重启Apache以应用更改:

sudo systemctl restart apache2

启动并启用PHP-FPM服务

对于Ubuntu/Debian系统:

sudo systemctl start php{version}-fpm
sudo systemctl enable php{version}-fpm

对于CentOS/RHEL系统:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

验证PHP-FPM是否正常工作

创建一个简单的PHP文件,例如 /var/www/html/info.php

<?php
phpinfo();
?>

在浏览器中访问 http://your_domain.com/info.php,你应该能看到PHP的信息页面。

以上步骤应该帮助你在Linux系统上成功安装并配置PHP-FPM。根据你的具体需求,可能需要进行更多的调整和优化。

0
看了该问题的人还看了