You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 24.04+FastPanel下配置Nginx+Apache2+PHP-FPM启用.htaccess

在Ubuntu 24.04通过FastPanel集成Nginx+Apache2+PHP-FPM以支持.htaccess

整体架构说明

Nginx作为前端反向代理负责处理静态资源(如CSS/JS/图片)以保证性能;所有动态请求及需要.htaccess解析的请求转发给Apache2,由Apache2处理.htaccess规则后,再通过PHP-FPM执行PHP脚本。

步骤1:安装并配置Apache2

  1. 安装Apache2:
    sudo apt update && sudo apt install apache2 -y
    
  2. 启用必要模块(处理.htaccess和PHP-FPM对接):
    sudo a2enmod rewrite proxy_fcgi setenvif ssl
    
  3. 重启Apache2使配置生效:
    sudo systemctl restart apache2
    

步骤2:配置Apache2虚拟主机

  1. 确认PHP-FPM的套接字路径(FastPanel默认路径可通过以下命令查看):
    ls /run/php/
    
    通常输出类似php8.3-fpm.sock,记录此路径。
  2. 创建对应域名的Apache2虚拟主机配置文件(替换your-domain.com为实际域名,/var/www/your-domain/public_html为FastPanel中设置的网站根目录):
    sudo nano /etc/apache2/sites-available/your-domain.conf
    
  3. 写入以下配置(注意修改套接字路径和网站根目录):
    # HTTP监听配置
    <VirtualHost *:8080>
        ServerName your-domain.com
        DocumentRoot /var/www/your-domain/public_html
    
        # 允许.htaccess完全生效
        <Directory /var/www/your-domain/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    
        # 对接PHP-FPM
        <FilesMatch \.php$>
            SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    
        ErrorLog ${APACHE_LOG_DIR}/your-domain-error.log
        CustomLog ${APACHE_LOG_DIR}/your-domain-access.log combined
    </VirtualHost>
    
    # HTTPS监听配置(替换证书路径为FastPanel中的实际路径)
    <VirtualHost *:8443>
        ServerName your-domain.com
        DocumentRoot /var/www/your-domain/public_html
    
        <Directory /var/www/your-domain/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    
        <FilesMatch \.php$>
            SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    
        SSLEngine On
        SSLCertificateFile /etc/ssl/fastpanel/your-domain/cert.pem
        SSLCertificateKeyFile /etc/ssl/fastpanel/your-domain/privkey.pem
    
        ErrorLog ${APACHE_LOG_DIR}/your-domain-ssl-error.log
        CustomLog ${APACHE_LOG_DIR}/your-domain-ssl-access.log combined
    </VirtualHost>
    
  4. 启用该虚拟主机并重启Apache2:
    sudo a2ensite your-domain.conf
    sudo systemctl restart apache2
    

步骤3:修改Nginx配置(通过FastPanel)

  1. 登录FastPanel,进入对应网站的「Nginx配置」页面。
  2. 修改server块配置,保留静态资源处理,将其余请求转发给Apache2:
    server {
        listen 80;
        listen [::]:80;
        server_name your-domain.com;
    
        root /var/www/your-domain/public_html;
        index index.php index.html index.htm;
    
        # 静态资源由Nginx直接处理,提升性能
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 30d;
            add_header Cache-Control "public, no-transform";
        }
    
        # 所有其他请求转发给Apache2的HTTP端口
        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    
    # HTTPS配置(转发到Apache2的HTTPS端口)
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name your-domain.com;
    
        ssl_certificate /etc/ssl/fastpanel/your-domain/fullchain.pem;
        ssl_certificate_key /etc/ssl/fastpanel/your-domain/privkey.pem;
    
        root /var/www/your-domain/public_html;
        index index.php index.html index.htm;
    
        location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 30d;
            add_header Cache-Control "public, no-transform";
        }
    
        location / {
            proxy_pass https://127.0.0.1:8443;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    
  3. 保存配置后,在FastPanel中重启Nginx服务。

步骤4:验证配置

  1. 检查Apache2和Nginx运行状态:
    sudo systemctl status apache2
    sudo systemctl status nginx
    
  2. 在网站根目录创建.htaccess文件,添加测试规则:
    RewriteEngine On
    RewriteRule ^test-page$ test.php [L]
    
  3. 创建test.php文件:
    <?php echo "测试成功:.htaccess规则已生效,PHP由FPM处理"; ?>
    
  4. 访问http://your-domain.com/test-page,若能正常显示PHP内容,说明配置成功。

注意事项

  • 确保Apache2监听的8080/8443端口未被其他服务占用,可通过ss -tulpn | grep :8080检查。
  • 网站根目录权限需设置正确,确保www-data用户有读取权限:
    sudo chown -R www-data:www-data /var/www/your-domain/public_html
    
  • 若PHP-FPM使用TCP端口而非套接字(如127.0.0.1:9000),需修改Apache2配置中的SetHandler为proxy:fcgi://127.0.0.1:9000。

内容的提问来源于stack exchange,提问作者Alexander S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:43:19