Apache虚拟主机

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

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

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

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

2. 配置虚拟主机

  1. 1. `NameVirtualHost *:80` 声明端口号
  2. 2. `<VirtualHost *:80> </VirtualHost>` 监听80端口, 里面的是主机配置选项.
  3. 3. 下面的例子写了两个虚拟主机, 照猫画虎.
  1. # vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
  2. NameVirtualHost *:80
  3. <VirtualHost *:80>
  4. ServerAdmin ramesh@thegeekstuff.com
  5. DocumentRoot "/usr/local/apache2/docs/thegeekstuff"
  6. ServerName thegeekstuff.com
  7. ServerAlias www.thegeekstuff.com
  8. ErrorLog "logs/thegeekstuff/error_log"
  9. CustomLog "logs/thegeekstuff/access_log" common
  10. </VirtualHost>
  11. <VirtualHost *:80>
  12. ServerAdmin ramesh@top5freeware.com
  13. DocumentRoot "/usr/local/apache2/docs/top5freeware"
  14. ServerName top5freeware.com
  15. ServerAlias www.top5freeware.com
  16. ErrorLog "logs/top5freeware/error_log"
  17. CustomLog "logs/top5freeware/access_log" common
  18. </VirtualHost>

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

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

  1. # /usr/local/apache2/bin/httpd -S
  2. VirtualHost configuration:
  3. Syntax OK

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

  1. # /usr/local/apache2/bin/httpd -S
  2. Warning: DocumentRoot
  3. [/usr/local/apache2/docs/top5freeware] does not exist
  4. Warning: ErrorLog [/usr/local/apache2/logs/thegeekstuff]
  5. does not exist
  6. Syntax OK

4. 重启测试

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

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