在一个服务器上部署两个 WordPress 网站是非常常见的操作,可以通过以下几种方式实现。以下是详细步骤和建议:
🧩 一、准备工作
- 服务器环境:你需要一个 Linux 服务器(如 Ubuntu 或 CentOS),并安装了:
- Web 服务器(Apache 或 Nginx)
- PHP
- MySQL / MariaDB 数据库
- 域名准备:为两个网站准备两个不同的域名或子域名,例如:
example.comblog.example.com或anotherwebsite.com
🛠️ 方法一:使用 Apache 的虚拟主机(Virtual Host)
✅ 步骤 1:创建两个网站目录
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/anotherwebsite.com/public_html
下载 WordPress 到这两个目录:
cd /var/www/example.com/public_html
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/* .
cd /var/www/anotherwebsite.com/public_html
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/* .
✅ 步骤 2:配置数据库
登录 MySQL 创建两个数据库和用户:
CREATE DATABASE wp_example;
CREATE DATABASE wp_another;
CREATE USER 'wp_user1'@'localhost' IDENTIFIED BY 'password1';
CREATE USER 'wp_user2'@'localhost' IDENTIFIED BY 'password2';
GRANT ALL PRIVILEGES ON wp_example.* TO 'wp_user1'@'localhost';
GRANT ALL PRIVILEGES ON wp_another.* TO 'wp_user2'@'localhost';
FLUSH PRIVILEGES;
✅ 步骤 3:配置 Apache 虚拟主机
创建两个虚拟主机配置文件:
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html/
<Directory /var/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
/etc/apache2/sites-available/anotherwebsite.com.conf
<VirtualHost *:80>
ServerAdmin admin@anotherwebsite.com
ServerName anotherwebsite.com
ServerAlias www.anotherwebsite.com
DocumentRoot /var/www/anotherwebsite.com/public_html/
<Directory /var/www/anotherwebsite.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/another_error.log
CustomLog ${APACHE_LOG_DIR}/another_access.log combined
</VirtualHost>
启用站点并重启 Apache:
sudo a2ensite example.com
sudo a2ensite anotherwebsite.com
sudo systemctl reload apache2
🛠️ 方法二:使用 Nginx 的 server 块(类似方法)
如果你使用的是 Nginx,则可以在 /etc/nginx/sites-available/ 中创建两个配置文件,并设置不同的 server_name 和 root。
示例配置:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
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:
sudo nginx -t
sudo systemctl restart nginx
🌐 方法三:使用不同端口区分(不推荐用于生产)
你也可以让两个 WordPress 安装在同一个 IP 上但不同端口访问,比如:
http://yourip:8080→ 第一个网站http://yourip:8081→ 第二个网站
但这种方法不利于 SEO 和用户体验,适合本地开发测试。
📁 文件权限注意事项
确保 WordPress 目录权限正确:
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www
🔐 额外建议
- 每个网站使用独立的数据库和数据库用户。
- 可以考虑使用 Let’s Encrypt 为每个域名配置 SSL。
- 使用
.htaccess(Apache)或nginx.conf来设置伪静态规则。
✅ 总结
| 方法 | 工具 | 是否推荐 |
|---|---|---|
| Apache 虚拟主机 | Apache + VirtualHost | ✅ 推荐 |
| Nginx server 块 | Nginx | ✅ 推荐 |
| 不同端口 | Apache/Nginx | ❌ 不推荐 |
| 子目录安装 | 同一 WordPress 实例 | ❌ 不符合需求 |
如果你告诉我你的服务器环境(Apache 还是 Nginx?Ubuntu 还是 CentOS?),我可以给出更具体的命令版本。
需要我帮你生成完整的虚拟主机配置吗?
CLOUD技术博