关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

云服务器Apache配置虚拟主机实例

发布时间:2022/11/30 13:24:33
香港云服务器

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

1、基于ip地址的虚拟主机
代码如下:
Listen 80

    DocumentRoot /home/httpd/html1
    ServerName www.ok1.com
    ErrorLog /usr/local/apache/logs/error1_log
    CustomLog /usr/local/apache/logs/access1_log combined


    DocumentRoot /home/httpd/html2
    ServerName www.ok2.com
    ErrorLog /usr/local/apache/logs/error2_log
    CustomLog /usr/local/apache/logs/access2_log combined


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


 DocumentRoot /www/example1-80
 ServerName www.example1.com



 DocumentRoot /www/example1-8080
 ServerName www.example1.com


 DocumentRoot /www/example2-80
 ServerName www.example1.org


 DocumentRoot /www/example2-8080
 ServerName www.example2.org


3、单个IP 地址的服务器上基于域名的虚拟主机配置
代码如下:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here


DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here


下面是实例:

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网站内容