🗣 Wd提问: 配置虚拟主机
🤖 Ai回答: 配置虚拟主机(Virtual Host)是 Web 服务器(如 Apache 或 Nginx)中用于在同一个服务器上托管多个网站或域名的技术。以下是使用 Apache 和 Nginx 配置虚拟主机的详细步骤。
✅ 一、Apache 配置虚拟主机
1、启用虚拟主机模块
确保 `mod_vhost_alias` 和 `mod_ssl`(如果需要 HTTPS)已启用:
bash
Ubuntu/Debian
sudo a2enmod vhost_alias
sudo a2enmod ssl
2、创建站点目录
为每个网站创建独立的目录结构:
bash
sudo mkdir -p /var/www/65.hk/html
sudo mkdir -p /var/www/test.com/html
设置权限:
bash
sudo chown -R $USER:$USER /var/www/65.hk/html
sudo chmod -R 755 /var/www/65.hk/html
3、创建虚拟主机配置文件
示例:`/etc/apache2/sites-available/65.hk.conf`
apache
<VirtualHost *:80
ServerName 65.hk
ServerAlias www.65.hk
DocumentRoot /var/www/65.hk/html
ErrorLog ${APACHE_LOG_DIR}/65.hk_error.log
CustomLog ${APACHE_LOG_DIR}/65.hk_access.log combined
</VirtualHost
另一个示例:`/etc/apache2/sites-available/test.com.conf`
apache
<VirtualHost *:80
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/html
ErrorLog ${APACHE_LOG_DIR}/test.com_error.log
CustomLog ${APACHE_LOG_DIR}/test.com_access.log combined
</VirtualHost
💡 注意:`ServerName` 是主域名,`ServerAlias` 是别名。
4、启用站点并重启 Apache
bash
sudo a2ensite 65.hk.conf
sudo a2ensite test.com.conf
sudo systemctl reload apache2
如果你只想临时测试,可以使用 `a2dissite` 禁用。
✅ 二、Nginx 配置虚拟主机
1、创建站点目录
bash
sudo mkdir -p /var/www/65.hk/html
sudo mkdir -p /var/www/test.com/html
创建测试页面:
bash
echo <h1Welcome to 65.hk</h1 | sudo tee /var/www/65.hk/html/index.html
2、创建 Nginx 虚拟主机配置文件
示例:`/etc/nginx/sites-available/65.hk`
nginx
server {
listen 80;
server_name 65.hk www.65.hk;
root /var/www/65.hk/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/65.hk.access.log;
error_log /var/log/nginx/65.hk.error.log;
}
示例:`/etc/nginx/sites-available/test.com`
nginx
server {
listen 80;
server_name test.com www.test.com;
root /var/www/test.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/test.com.access.log;
error_log /var/log/nginx/test.com.error.log;
}
3、启用站点(创建符号链接)
bash
sudo ln -s /etc/nginx/sites-available/65.hk /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
4、测试配置并重启 Nginx
bash
sudo nginx -t 检查语法是否正确
sudo systemctl reload nginx
✅ 三、本地 hosts 文件测试(可选)
为了在本地测试,修改你的电脑上的 hosts 文件:
Linux/macOS: `/etc/hosts`
Windows: `C:\Windows\System32\drivers\etc\hosts`
添加如下内容:
127.0.0.1 65.hk
127.0.0.1 test.com
然后在浏览器中访问 `http://65.hk` 和 `http://test.com` 即可看到不同内容。
✅ 四、HTTPS 支持(可选)
Apache + Let's Encrypt(Certbot)
bash
sudo apt install certbot
sudo certbot certonly webroot -w /var/www/65.hk/html -d 65.hk -d www.65.hk
然后更新虚拟主机配置为 HTTPS:
apache
<VirtualHost *:443
ServerName 65.hk
DocumentRoot /var/www/65.hk/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/65.hk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/65.hk/privkey.pem
</VirtualHost
Nginx + Let's Encrypt
bash
sudo certbot certonly nginx -d 65.hk -d www.65.hk
它会自动修改配置文件。
✅ 总结
| 步骤 | Apache | Nginx |
|
| 创建目录 | ✅ | ✅ |
| 编写配置 | `sites-available/xxx.conf` | `sites-available/xxx` |
| 启用站点 | `a2ensite` | `ln -s` 到 `sites-enabled` |
| 重载服务 | `systemctl reload apache2` | `systemctl reload nginx` |
| HTTPS | 使用 Certbot | 使用 Certbot |
如果你提供具体环境(如 Ubuntu 22.04, CentOS, Docker 等),我可以给出更精确的命令。
需要我帮你生成完整的配置文件模板吗?
📣 商家广告:





0
IP地址: 224.254.91.202
搜索次数: 0
提问时间: 2025-10-26 19:33:04
🛒 域名购买:
pbbb.com.cn
jgfc.cn
63.gold
652.net.cn
zfxm.cn
xbhd.cn
23.gold
191.net.cn
ltqz.cn
fw.chat
❓️ 热门提问:
域名交易估价
中国好用的搜索引擎
安徽低价vps
免费云服务器使用一年
xyz 域名申请
adsl动态拨号vps怎么实现
science域名
普通主机多少钱一台
服务器python
香港产的化妆品有哪些
豌豆Ai站群搜索引擎系统
🌐 域名评估:
xx.jl.cn
xjop.cn
htmu.cn
hh.sx.cn
wbrd.cn
vzji.cn
evtol.ah.cn
k.sd.cn
5a.sd.cn
uyha.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
广东过滤器
검색 엔진 프로그램
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。