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

安装WHMCS时的Nginx配置问题求助

WHMCS安装Nginx配置修复方案

问题场景

我已将下载的WHMCS文件夹放置在/var/www/html目录下,并将其重命名为billing。按照WHMCS安装指南访问IP/billing/install时,浏览器会直接下载文件而非解析执行,当前使用的Nginx配置如下:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.php index.htm index.nginx-debian.html;

    server_name _;

    #location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #proxy_pass http://localhost:3000;
    
    #}

            # WHMCS CONFIG
        # When WHMCS updates, make sure to get the newest configuration at https://hostup.org/blog/whmcs-friendly-urls-configuration-for-nginx/

        location ~ /billing/announcements/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/announcements/$1;
        }

        location ~ /billing/download/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/download$1;
        }

        location ~ /billing/knowledgebase/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/knowledgebase/$1;
        }

        location ~ /billing/store/ssl-certificates/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/store/ssl-certificates/$1;
        }

        location ~ /billing/store/sitelock/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/store/sitelock/$1;
        }

        location ~ /billing/store/website-builder/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/store/website-builder/$1;
        }

        location ~ /billing/store/order/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/store/order/$1;
        }

        location ~ /billing/cart/domain/renew/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/cart/domain/renew$1;
        }

        location ~ /billing/account/paymentmethods/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/account/paymentmethods$1;
        }

        location ~ /billing/password/reset/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/password/reset/$1;
        }

        location ~ /billing/account/security/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/account/security$1;
        }

        location ~ /billing/subscription?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=/subscription$1;
        }

    #Social media authorization
        location ~ /billing/auth/provider/google_signin/finalize/?(.*)$ {
        rewrite ^/(.*)$ /billing/index.php?rp=auth/provider/google_signin/finalize$1;
        }

     #WHMCS ADMIN
    location ~ /billing/admin/(addons|apps|search|domains|help/license|services|setup|utilities/system/php-compat)(.*) {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/admin/$1$2 last; 
        }

        location ~ /billing/admin/client/?(.*)/paymethods/?(.*)$ {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/client/?(.*)/paymethods/$1;
        }

        location ~ /billing/admin/setup/auth/?(.*)$ {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/setup/auth/$1;
        }

        location ~ /billing/admin/client/?(.*)/tickets/?(.*)$ {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/client/?(.*)/tickets/$1;
        }

        location ~ /billing/admin/client/?(.*)/invoice/?(.*)/capture/?(.*)$ {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/client/?(.*)/invoice/?(.*)/capture/$1;
        }

        location ~ /billing/admin/account/security/two-factor/?(.*)$ {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/admin/account/security/two-factor/$1;
        }

        location ~ /billing/admin/search/intellisearch?(.*)$ {
        rewrite ^/(.*)$ /billing/admin/index.php?rp=/search/intellisearch/$1;
        }

    # Security Advisory 2020-01-28
        location ^~ /vendor/ {
        deny all;
        return 403;
        }
    # END WHMCS CONFIG
}

问题原因

浏览器下载PHP文件的核心原因是Nginx未配置PHP请求转发规则,无法将.php文件请求交给PHP-FPM解析执行。同时现有配置中的重写规则未覆盖安装路径,且部分规则存在正则匹配问题。

修正后的完整Nginx配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    # 基础请求处理规则
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # PHP文件解析核心规则(根据你的PHP版本调整fastcgi_pass,比如php7.4-fpm/php8.2-fpm)
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # WHMCS 安装路径处理
    location /billing/install {
        try_files $uri $uri/ /billing/install/index.php?$args;
    }

    # WHMCS 前端页面重写规则
    location ~ ^/billing/(announcements|download|knowledgebase)/?(.*)$ {
        rewrite ^/billing/(.*)$ /billing/index.php?rp=/$1 last;
    }

    location ~ ^/billing/store/(ssl-certificates|sitelock|website-builder|order)/?(.*)$ {
        rewrite ^/billing/(.*)$ /billing/index.php?rp=/$1 last;
    }

    location ~ ^/billing/(cart/domain/renew|account/paymentmethods|password/reset|account/security|subscription)/?(.*)$ {
        rewrite ^/billing/(.*)$ /billing/index.php?rp=/$1 last;
    }

    # 社交媒体授权重写
    location ~ ^/billing/auth/provider/google_signin/finalize/?(.*)$ {
        rewrite ^/billing/(.*)$ /billing/index.php?rp=/$1 last;
    }

    # WHMCS 后台重写规则
    location ~ ^/billing/admin/(addons|apps|search|domains|help/license|services|setup|utilities/system/php-compat)/?(.*)$ {
        rewrite ^/billing/admin/(.*)$ /billing/admin/index.php?rp=/admin/$1 last;
    }

    location ~ ^/billing/admin/client/(.*)/(paymethods|tickets)/?(.*)$ {
        rewrite ^/billing/admin/(.*)$ /billing/admin/index.php?rp=/admin/$1 last;
    }

    location ~ ^/billing/admin/(setup/auth|account/security/two-factor|search/intellisearch)/?(.*)$ {
        rewrite ^/billing/admin/(.*)$ /billing/admin/index.php?rp=/admin/$1 last;
    }

    location ~ ^/billing/admin/client/(.*)/invoice/(.*)/capture/?(.*)$ {
        rewrite ^/billing/admin/(.*)$ /billing/admin/index.php?rp=/admin/$1 last;
    }

    # WHMCS 安全规则
    location ^~ /billing/vendor/ {
        deny all;
        return 403;
    }

    # 禁止访问敏感文件
    location ~ /\.ht {
        deny all;
    }
}

后续操作步骤

  1. 保存上述配置文件后,重启Nginx服务:
    sudo systemctl restart nginx
    
  2. 确认PHP-FPM服务正常运行(根据你的PHP版本调整服务名):
    sudo systemctl status php8.2-fpm
    
  3. 设置正确的目录权限,确保Nginx进程用户(通常为www-data)拥有读写权限:
    sudo chown -R www-data:www-data /var/www/html/billing
    sudo chmod -R 755 /var/www/html/billing
    

内容的提问来源于stack exchange,提问作者Alekk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 18:45:38