温馨提示×

Ubuntu中PHP-FPM如何配置

小樊
81
2025-03-12 13:50:11
栏目: 编程语言

在Ubuntu系统中配置PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤:

  1. 安装PHP-FPM: 首先,确保你的系统已经安装了PHP和PHP-FPM。你可以使用以下命令来安装它们:

    sudo apt update sudo apt install php-fpm 

    这将安装PHP以及PHP-FPM。

  2. 配置PHP-FPM: PHP-FPM的配置文件通常位于/etc/php/{version}/fpm/目录下,其中{version}是你的PHP版本号。例如,如果你的PHP版本是7.4,配置文件将位于/etc/php/7.4/fpm/

    主要的配置文件包括:

    • php-fpm.conf:主配置文件。
    • www.conf:包含PHP-FPM与Web服务器通信的配置。

    你可以编辑www.conf文件来配置PHP-FPM监听的地址和端口。例如:

    sudo nano /etc/php/7.4/fpm/pool.d/www.conf 

    找到listen指令并修改为你想要的地址和端口。例如,如果你想让PHP-FPM监听Unix套接字,可以这样设置:

    listen = /run/php/php7.4-fpm.sock 

    如果你想让PHP-FPM监听TCP端口,可以这样设置:

    listen = 127.0.0.1:9000 
  3. 配置Nginx或Apache: 根据你使用的Web服务器,你需要相应地配置它以使用PHP-FPM。

    • Nginx: 编辑Nginx的站点配置文件(通常位于/etc/nginx/sites-available/目录下),添加或修改以下内容:

      server { listen 80; server_name example.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; # 或者使用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: 确保启用了proxy_fcgisetenvif模块:

      sudo a2enmod proxy_fcgi setenvif 

      编辑Apache的站点配置文件(通常位于/etc/apache2/sites-available/目录下),添加或修改以下内容:

      <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/php7.4-fpm.sock|fcgi://localhost" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

      然后,重新加载Apache配置:

      sudo systemctl reload apache2 
  4. 启动和启用PHP-FPM服务: 确保PHP-FPM服务正在运行并设置为开机自启动:

    sudo systemctl start php7.4-fpm sudo systemctl enable php7.4-fpm 

通过以上步骤,你应该能够在Ubuntu系统中成功配置PHP-FPM。根据你的具体需求,可能还需要进行其他配置调整。

0