Apache虚拟主机

其实这里也没什么, 就是增加了一个主机而已...如果是用nginx的话, 相当简单的...

我就一步一步翻译好了, 如果觉着简单就跳过这一节吧 :)

1. 取消httpd-vhosts.conf的注释

# vi /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf

2. 配置虚拟主机

1. `NameVirtualHost *:80` 声明端口号
2. `<VirtualHost *:80> </VirtualHost>` 监听80端口, 里面的是主机配置选项.
3. 下面的例子写了两个虚拟主机, 照猫画虎.
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2/docs/thegeekstuff"
    ServerName thegeekstuff.com
    ServerAlias www.thegeekstuff.com
    ErrorLog "logs/thegeekstuff/error_log"
    CustomLog "logs/thegeekstuff/access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2/docs/top5freeware"
    ServerName top5freeware.com
    ServerAlias www.top5freeware.com
    ErrorLog "logs/top5freeware/error_log"
    CustomLog "logs/top5freeware/access_log" common
</VirtualHost>

(作者在这里打了一手好广告 :D)

3. 检测配置文件是否有语法错误

# /usr/local/apache2/bin/httpd -S
VirtualHost configuration:
Syntax OK

上面的是配置正确的, 如果出错了就会报错:

# /usr/local/apache2/bin/httpd -S
Warning: DocumentRoot
[/usr/local/apache2/docs/top5freeware] does not exist
Warning: ErrorLog [/usr/local/apache2/logs/thegeekstuff]
does not exist
Syntax OK

4. 重启测试

# /usr/local/apache2/bin/apachectl restart

apache2是通过HTTP的Host来区分不通的主机的. 你可以在本地的/etc/hosts文件中写好要测试的网址和对应的ip(127.0.0.1), 然后浏览器访问不通的网址, 虽然都是同一个ip地址(127.0.0.1), 但是却是不同的网站.

results matching ""

    No results matching ""