1 其实很简单,就是一个virtualhost 指令。
<VirtualHost 10.1.2.3:80>
ServerAdmin webmaster@host.example.com
DocumentRoot /www/docs/host.example.com
ServerName host.example.com
ErrorLog logs/host.example.com-error_log
TransferLog logs/host.example.com-access_log
</VirtualHost>
2 。需要开放目录权限 Directory
但是这里如果这样配的话很可能就是403,禁止访问了。原因是/www/docs这个目录没有开放权限。
centos 的httpd.conf 中这样配置的Directory ,centos中的/etc/httpd/conf/httpd.conf 文件中:
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
也就是说,只有/var/www/html才有allow的权限。其他目录都是没有的。
我们只需要再添加一个Directory 就可以让/www/docs/host.example.com访问了。
如下:
<VirtualHost 10.1.2.3:80>
ServerAdmin webmaster@host.example.com
DocumentRoot /www/docs/host.example.com
ServerName host.example.com
ErrorLog logs/host.example.com-error_log
TransferLog logs/host.example.com-access_log
<Directory /www/docs/host.example.com>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
3 。验证一下
httpd -t
4. 之后重新即可。
httpd -k restart
5. 配置多域名的虚拟主机。
加个NameVirtualHost 指令即可。
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>