帮助中心

DNS污染处理

常见问题

香港高防IP

常见问题

香港大带宽服务器

产品简介 操作指南 常见问题

新加坡高防服务器

产品简介 操作指南 常见问题

新加坡云服务器

产品简介 操作指南 常见问题

香港服务器租用

产品简介 操作指南 常见问题

香港高防服务器

产品简介 操作指南 常见问题

域名服务

域名服务

Apache配置虚拟主机实例

时间 : 2022-11-30 09:39:13
编辑 : 华纳云

服务器租用后,安装Apache程序,如果是配置Apache虚拟主机,可以参考下面的教程。

1、基于ip地址的虚拟主机
代码如下:
Listen 80
<VirtualHost 172.20.30.40>
    DocumentRoot /home/httpd/html1
    ServerName www.ok1.com
    ErrorLog /usr/local/apache/logs/error1_log
    CustomLog /usr/local/apache/logs/access1_log combined
</VirtualHost>
<VirtualHost 172.20.30.50>
    DocumentRoot /home/httpd/html2
    ServerName www.ok2.com
    ErrorLog /usr/local/apache/logs/error2_log
    CustomLog /usr/local/apache/logs/access2_log combined
</VirtualHost>

2、基于IP 和多端口的虚拟主机配置
代码如下:
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

<VirtualHost 172.20.30.40:80>
 DocumentRoot /www/example1-80
 ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>

 DocumentRoot /www/example1-8080
 ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
 DocumentRoot /www/example2-80
 ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
 DocumentRoot /www/example2-8080
 ServerName www.example2.org
</VirtualHost>

3、单个IP 地址的服务器上基于域名的虚拟主机配置
代码如下:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>

下面是实例:

1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -
代码如下:
127.0.0.1   test1.net
127.0.0.1   test2.net

如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。

2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -
apache/htdocs1/test1.net - 放置test1.net网站内容
apache/htdocs1/test2.net - 放置test2.net网站内容

华纳云

客服咨询
7*24小时技术支持
Telegram
hncloudnoc

技术支持

渠道支持