在轻量服务器(如腾讯云轻量应用服务器、阿里云轻量服务器等)上搭建多个网站,是很多个人开发者和小型企业常见的需求。虽然轻量服务器的配置相对较低,但通过合理配置,完全可以支持多个网站的运行。
下面是一个通用的步骤指南,适用于 Linux 系统(如 CentOS、Ubuntu):
🧩 一、准备工作
1. 确认服务器环境
- 操作系统:Linux(推荐 Ubuntu 或 CentOS)
- Web 服务器:Nginx / Apache
- 数据库:MySQL / MariaDB(可选)
- PHP/Python/Node.js(根据你的网站需求)
2. 域名准备
- 每个网站需要绑定一个独立域名或子域名(如
example.com和blog.example.com) - 域名需解析到服务器公网 IP 地址
🛠️ 二、安装 Web 服务环境(LNMP/LAMP)
以 Nginx + PHP + MySQL 为例(常见于 WordPress、PHP 类网站):
# Ubuntu 示例
sudo apt update
sudo apt install nginx php php-fpm mysql-server -y
如果你使用的是 Python 或 Node.js 项目,可以使用 Gunicorn/PM2 配合 Nginx 反向。
🌐 三、配置多个网站(虚拟主机)
方法:使用 Nginx 的 Server Block(推荐)
1. 创建网站目录
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/blog.example.com/public_html
2. 编写 Nginx 配置文件(每个网站一个配置)
示例:/etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
}
另一个网站:/etc/nginx/sites-available/blog.example.com
server {
listen 80;
server_name blog.example.com;
root /var/www/blog.example.com/public_html;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
}
3. 启用站点并测试
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/blog.example.com /etc/nginx/sites-enabled/
sudo nginx -t # 检查语法是否正确
sudo systemctl reload nginx
📦 四、数据库支持(如需)
为每个网站创建独立的数据库和用户:
CREATE DATABASE example_db;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
这样两个网站可以分别连接自己的数据库,互不影响。
🧪 五、测试访问
在浏览器中访问:
http://example.comhttp://blog.example.com
确保都能正常显示内容。
🔒 六、安全建议
- 使用防火墙(UFW / iptables)限制端口访问
- 安装 SSL 证书(Let’s Encrypt)启用 HTTPS
- 定期更新系统和软件包
💡 七、进阶建议(适合资源有限的轻量服务器)
- 使用缓存(如 Redis、OPcache)提升性能
- 使用静态 HTML 页面减少服务器负载
- 多个网站尽量共用 PHP-FPM、MySQL 实例,避免资源浪费
- 使用 Docker 容器化部署多个网站(更灵活)
✅ 总结
| 步骤 | 内容 |
|---|---|
| 1 | 准备服务器环境(Nginx、PHP、MySQL) |
| 2 | 创建多个网站目录 |
| 3 | 配置 Nginx 虚拟主机(Server Block) |
| 4 | 绑定域名并测试访问 |
| 5 | 配置数据库、安全策略等 |
如果你告诉我你想搭建的具体网站类型(比如 WordPress、Vue、Node.js、Python Flask),我可以提供更具体的部署教程。
需要我帮你生成某个具体网站的 Nginx 配置吗?
ECLOUD博客