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

单静态IP Ubuntu虚拟机部署双WordPress并映射主域子目录方案咨询

最优解决方案:单IP下双WordPress子目录部署

嘿,我来给你梳理下这个需求的靠谱解决方案——这也是我在实际部署中常用的标准配置,稳定性和灵活性都拉满。核心思路是用Nginx在Ubuntu虚拟机上托管两个WordPress实例,通过子目录实现站点隔离,再配合反向代理完成主域名的映射。

核心逻辑

我们要实现的是:主域名maindomain.com/CMS1直接对应Ubuntu虚拟机192.168.1.100/CMS1,CMS2同理。核心是两步:一是在Ubuntu上把两个WordPress分别部署到子目录并配置Nginx规则,二是把主域名的子目录请求转发到Ubuntu(要么直接解析域名到Ubuntu,要么在Windows Server做反向代理)。

具体操作步骤

1. 先搭好Ubuntu的基础环境

首先更新系统包,然后装WordPress需要的Nginx、MySQL、PHP:

sudo apt update && sudo apt upgrade -y
sudo apt install nginx mysql-server php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y

2. 为两个WordPress准备目录和数据库

  • 创建两个站点的根目录,给Nginx权限:
    sudo mkdir -p /var/www/html/cms1
    sudo mkdir -p /var/www/html/cms2
    sudo chown -R www-data:www-data /var/www/html/cms1 /var/www/html/cms2
    sudo chmod -R 755 /var/www/html/cms1 /var/www/html/cms2
    
  • 登录MySQL创建各自的数据库和用户(记得替换your_password为你自己的密码):
    sudo mysql -u root -p
    
    然后执行这些SQL:
    CREATE DATABASE cms1_db;
    CREATE USER 'cms1_user'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON cms1_db.* TO 'cms1_user'@'localhost';
    
    CREATE DATABASE cms2_db;
    CREATE USER 'cms2_user'@'localhost' IDENTIFIED BY 'your_password';
    GRANT ALL PRIVILEGES ON cms2_db.* TO 'cms2_user'@'localhost';
    
    FLUSH PRIVILEGES;
    EXIT;
    

3. 下载并配置WordPress

  • 下载最新版WordPress,解压到两个站点目录:
    cd /tmp
    wget https://wordpress.org/latest.tar.gz
    tar xzf latest.tar.gz
    
    sudo cp -r wordpress/* /var/www/html/cms1/
    sudo cp -r wordpress/* /var/www/html/cms2/
    
  • 复制并修改配置文件,关键是指定站点的URL(避免WordPress生成错误链接):
    sudo cp /var/www/html/cms1/wp-config-sample.php /var/www/html/cms1/wp-config.php
    sudo cp /var/www/html/cms2/wp-config-sample.php /var/www/html/cms2/wp-config.php
    
    编辑/var/www/html/cms1/wp-config.php,填入刚才创建的数据库信息,然后加上这两行:
    define('WP_SITEURL', 'http://192.168.1.100/CMS1');
    define('WP_HOME', 'http://192.168.1.100/CMS1');
    
    同样处理cms2的配置文件,把URL改成http://192.168.1.100/CMS2。

4. 配置Nginx的子目录规则

这一步是让Nginx正确识别子目录的请求,转发到对应的WordPress:

  • 编辑Nginx的默认站点配置:
    sudo nano /etc/nginx/sites-available/default
    
    替换成下面的内容(注意php8.2-fpm要换成你实际安装的PHP版本,比如php8.1-fpm):
    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/html;
        index index.php index.html index.htm;
    
        server_name _; # 如果Ubuntu有公网IP,这里直接填maindomain.com就行
    
        # 处理CMS1的子目录请求
        location /CMS1 {
            try_files $uri $uri/ /CMS1/index.php?$args;
        }
    
        # 处理CMS2的子目录请求
        location /CMS2 {
            try_files $uri $uri/ /CMS2/index.php?$args;
        }
    
        # 处理PHP文件
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 替换成你的PHP套接字路径
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        # 禁止访问.ht文件
        location ~ /\.ht {
            deny all;
        }
    }
    
  • 测试配置是否正确,然后重启Nginx:
    sudo nginx -t
    sudo systemctl restart nginx
    

5. 完成主域名的映射

根据你的网络情况选一种方式:

方式一:Ubuntu直接对外(有公网IP)

把maindomain.com的A记录解析到Ubuntu的公网IP,然后修改两个WordPress配置文件里的WP_SITEURL和WP_HOME,改成http://maindomain.com/CMS1和http://maindomain.com/CMS2,同时把Nginx配置里的server_name改成maindomain.com。

方式二:Windows Server做反向代理

如果Ubuntu只有内网IP,就在Windows上装反向代理工具(比如IIS的ARR模块,或者Windows版Nginx):

  • 用IIS的话:装完ARR后,创建一个绑定maindomain.com的网站,给CMS1和CMS2分别建应用程序,配置反向代理规则,把请求转发到http://192.168.1.100/CMS1和http://192.168.1.100/CMS2。
  • 用Windows版Nginx的话,配置server块:
    server {
        listen 80;
        server_name maindomain.com;
    
        location /CMS1 {
            proxy_pass http://192.168.1.100/CMS1;
            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;
        }
    
        location /CMS2 {
            proxy_pass http://192.168.1.100/CMS2;
            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;
        }
    }
    
    之后记得把Ubuntu上WordPress的URL改成http://maindomain.com/CMS1和http://maindomain.com/CMS2。

几个关键注意事项

  • 打开Ubuntu的80端口(如果开了ufw防火墙):
    sudo ufw allow 80/tcp
    
  • 如果要搞HTTPS,就在反向代理或者Nginx上配置SSL证书,同时把WordPress的URL改成https://开头。
  • 最后访问http://maindomain.com/CMS1和http://maindomain.com/CMS2就能完成WordPress的初始化了。

配置参考要点(都是官方文档里的核心内容)

  • WordPress子目录部署的关键就是修改wp-config.php里的WP_SITEURL和WP_HOME,不然会出现链接跳转错误。
  • Nginx的try_files指令必须配置正确,这样才能把所有子目录下的请求都转发到WordPress的index.php处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:57:27