vps如何运行两个wordpress?

在 VPS(虚拟私有服务器)上运行两个 WordPress 站点是非常常见的需求,可以通过以下几种方式实现:


✅ 方法一:使用不同的域名或子域名 + 虚拟主机配置

这是最常见、推荐的做法。你可以使用 Apache 或 Nginx 来配置多个虚拟主机(Virtual Host),每个 WordPress 安装对应一个站点。

步骤概览:

1. 准备两个域名或子域名

例如:

  • example.com → 第一个 WordPress
  • blog.example.comanother-site.com → 第二个 WordPress

2. 安装 LAMP 或 LEMP 环境

确保你的 VPS 上已经安装了:

  • Web 服务器(Apache 或 Nginx)
  • PHP
  • MySQL / MariaDB

3. 创建两个数据库和用户

CREATE DATABASE wp_site1;
CREATE DATABASE wp_site2;

CREATE USER 'wp_user1'@'localhost' IDENTIFIED BY 'your_password';
CREATE USER 'wp_user2'@'localhost' IDENTIFIED BY 'your_password';

GRANT ALL PRIVILEGES ON wp_site1.* TO 'wp_user1'@'localhost';
GRANT ALL PRIVILEGES ON wp_site2.* TO 'wp_user2'@'localhost';

FLUSH PRIVILEGES;

4. 下载并解压两个 WordPress 实例

cd /var/www/
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress site1

tar -xzvf latest.tar.gz
mv wordpress site2

设置权限:

chown -R www-data:www-data site1 site2
chmod -R 755 site1 site2

5. 为每个网站配置虚拟主机

Apache 示例(创建两个 .conf 文件)

/etc/apache2/sites-available/site1.conf

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/site1
    ServerName example.com
    ServerAlias www.example.com

    <Directory /var/www/site1/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site1_error.log
    CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
</VirtualHost>

/etc/apache2/sites-available/site2.conf

<VirtualHost *:80>
    ServerAdmin admin@another-site.com
    DocumentRoot /var/www/site2
    ServerName blog.example.com

    <Directory /var/www/site2/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/site2_error.log
    CustomLog ${APACHE_LOG_DIR}/site2_access.log combined
</VirtualHost>

启用站点并重启 Apache:

a2ensite site1
a2ensite site2
systemctl reload apache2

Nginx 示例(创建两个 server 块)

/etc/nginx/sites-available/site1

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/site1;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
}

/etc/nginx/sites-available/site2

server {
    listen 80;
    server_name blog.example.com;
    root /var/www/site2;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
}

启用站点并重启 Nginx:

ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

6. 配置 wp-config.php 文件

分别访问 http://example.comhttp://blog.example.com 进行 WordPress 安装,选择对应的数据库(wp_site1wp_site2)进行配置。


✅ 方法二:使用 WordPress 多站点(Multisite)

如果你希望用同一个 WordPress 安装管理多个站点(共享插件、主题、用户等),可以使用 WordPress Multisite 功能。

但注意:这种方式适合管理多个相关网站,不适合完全独立的站点。


✅ 方法三:使用 Docker(进阶)

你也可以使用 Docker 分别运行两个 WordPress 容器,每个容器绑定不同的端口或域名。

示例命令:

docker run -d --name site1 
  -e WORDPRESS_DB_NAME=wp_site1 
  -e WORDPRESS_DB_USER=wp_user1 
  -e WORDPRESS_DB_PASSWORD=your_password 
  -p 8080:80 
  -v /path/to/site1:/var/www/html 
  wordpress

docker run -d --name site2 
  -e WORDPRESS_DB_NAME=wp_site2 
  -e WORDPRESS_DB_USER=wp_user2 
  -e WORDPRESS_DB_PASSWORD=your_password 
  -p 8081:80 
  -v /path/to/site2:/var/www/html 
  wordpress

然后通过反向(如 Nginx)将不同域名指向不同的端口。


🔒 注意事项

  • 每个 WordPress 需要自己的数据库。
  • 确保文件权限正确,避免安全问题。
  • 如果使用 Let’s Encrypt,记得分别为两个站点申请 SSL 证书。
  • 不同站点之间插件、主题、用户是隔离的。

如果你告诉我你使用的系统(比如 Ubuntu)、Web 服务器(Apache/Nginx),我可以提供更具体的配置步骤。需要的话请继续提问 😊

未经允许不得转载:CLOUD技术博 » vps如何运行两个wordpress?