腾讯云轻量应用服务器怎么搭建多个网站?

在腾讯云轻量应用服务器(TencentCloud Lighthouse)上搭建多个网站,可以通过 配置 Nginx/Apache 虚拟主机 的方式实现。下面是一个详细的步骤指南,以常见的 Linux 系统(如 CentOS 或 Ubuntu)和 Nginx 为例进行说明:


🧩 前提条件

  • 已购买腾讯云轻量应用服务器并完成系统初始化(推荐使用 CentOS 或 Ubuntu)
  • 安装了 LNMP(Linux + Nginx + MySQL + PHP)或 LAMP 环境
  • 每个网站有独立的域名,并已解析到该服务器公网 IP
  • 防火墙/安全组已开放 HTTP(80)、HTTPS(443) 等端口

✅ 步骤一:安装 Nginx(如未安装)

Ubuntu:

sudo apt update
sudo apt install nginx -y

CentOS:

sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

✅ 步骤二:准备多个网站目录

为每个网站创建一个独立的文件夹,例如:

sudo mkdir -p /var/www/site1
sudo mkdir -p /var/www/site2

你可以在这些目录中分别上传各自的网站代码(HTML、PHP 等)。


✅ 步骤三:配置 Nginx 虚拟主机(Server Block)

Nginx 支持通过 server 块来区分不同域名请求。

示例配置(两个网站):

1. 网站 site1.com 的配置

创建配置文件 /etc/nginx/conf.d/site1.com.conf

server {
    listen 80;
    server_name site1.com www.site1.com;

    root /var/www/site1;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    # 如果使用 PHP
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }
}

2. 网站 site2.com 的配置

创建配置文件 /etc/nginx/conf.d/site2.com.conf

server {
    listen 80;
    server_name site2.com www.site2.com;

    root /var/www/site2;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    # 如果使用 PHP
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }
}

✅ 步骤四:检查配置并重启 Nginx

sudo nginx -t         # 检查语法是否正确
sudo systemctl reload nginx   # 重载配置

✅ 步骤五:测试访问

确保你的域名已经解析到服务器的公网 IP 地址,然后在浏览器中分别访问:

  • http://site1.com
  • http://site2.com

你应该能看到各自对应的网站内容。


🔒 可选:配置 HTTPS(建议)

你可以为每个网站申请 SSL 证书(比如用 Let’s Encrypt),然后修改 Nginx 配置支持 HTTPS。

示例 HTTPS 配置:

server {
    listen 443 ssl;
    server_name site1.com;

    ssl_certificate /etc/letsencrypt/live/site1.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem;

    root /var/www/site1;
    ...
}

同时保留 80 端口跳转 HTTPS:

server {
    listen 80;
    server_name site1.com www.site1.com;
    return 301 https://$host$request_uri;
}

📌 小贴士

  • 如果你是新手,可以使用宝塔面板等可视化工具管理多网站。
  • 同一个 IP 可以绑定多个域名,但必须通过域名区分不同的虚拟主机。
  • 如果需要运行 WordPress、Discuz、Typecho 等程序,注意 PHP 环境配置和权限设置。

🛠️ 使用宝塔面板简化操作(可选)

如果你不想手动配置 Nginx,可以安装 宝塔面板:

# Ubuntu/Debian
wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh

# CentOS
wget -O install.sh http://download.bt.cn/install/install_6.0.sh && bash install.sh

安装完成后,通过浏览器打开宝塔后台,添加站点即可一键部署多个网站。


如需我帮你生成某个具体网站的 Nginx 配置,请告诉我你的域名、路径和是否使用 PHP 等信息。

未经允许不得转载:CLOUD技术博 » 腾讯云轻量应用服务器怎么搭建多个网站?