虚拟主机指的是在单一机器上运行多个网站 (例如 company1.example.com
和 company2.example.com
) 。 虚拟主机可以“基于 IP”,即每个 IP 一个站点; 或者“基于名称”, 即每个 IP 多个站点。这些站点运行在同一物理服务器上的事实不会明显的透漏给最终用户。——这是官网解释。
配置Apache虚拟主机。(httpd-vhosts.conf —— Apache虚拟主机配置文件)
基于名称的虚拟主机 (每个 IP 多个站点)
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot /www/otherdomain
</VirtualHost>
名词解释:
VirtualHost:指虚拟主机的IP/域名,*:80代表所有代表所有80端口的IP
ServerName:虚拟主机名称
ServerAlias:虚拟主机别名,主机别名可以是多个支持通配符
ServerAlias example.com *.example.com
DocumentRoot:网站目录
基于 IP 的虚拟主机 (每个 IP 一个站点)
Listen 172.20.30.40:80
<VirtualHost 172.20.30.40:80>
ServerAdmin webmaster@www1.example.com
DocumentRoot /www/vhosts/www1
ServerName www1.example.com
ErrorLog /www/logs/www1/error_log
CustomLog /www/logs/www1/access_log combined
</VirtualHost>
<pre name="code" class="plain">Listen 172.20.30.50:80
<VirtualHost 172.20.30.50:80>
ServerAdmin webmaster@www2.example.org
DocumentRoot /www/vhosts/www2
ServerName www2.example.org
ErrorLog /www/logs/www2/error_log
CustomLog /www/logs/www2/access_log combined
</VirtualHost>
名词解释:
<pre name="code" class="plain">ServerAdmin 网站管理员邮箱
<pre name="code" class="plain"><pre name="code" class="plain">ErrorLog 错误日志
<pre name="code" class="plain"><pre name="code" class="plain">CustomLog 访问日志
开启Apache虚拟主机功能(httpd.conf)
搜索Virtual hosts,去掉#Include conf/extra/httpd-vhosts.conf前面的#
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
重启Apache服务