Apache配置虚拟主机

来源:互联网 发布:淘宝助理百世快递模板 编辑:程序博客网 时间:2024/06/06 23:17

在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录。Apache虚拟主机配置有3中方法:基于IP配置、基于端口配置,基于主机名配置。本文中Apache版本为2.4.28,主机版本是CentOS 6.9,由于是编译安装,其安装路径是/usr/local/apache/,配置文件在/etc/httpd/目录下,DocumentRoot和Directory路径为/www/htdocs/,主机ip地址为172.16.8.11/24,selinux和iptables已关闭

配置虚拟主机前的准备:
1.启用vhosts
编辑主配置文件,找到 #Include /etc/httpd/extra/httpd-vhosts.conf这一行,把前面的#号去掉,然后重读配置文件

[root@localhost ~]# vim /etc/httpd/httpd.confInclude /etc/httpd/extra/httpd-vhosts.conf[root@localhost ~]# service httpd reload

2,创建虚拟主机存放的根目录,3个测试页面

[root@localhost ~]# mkdir -p /vhosts/{web1,web2,web3}/htdcos[root@localhost ~]# vim /vhosts/web1/htdcos/index.htmlPage at web1[root@localhost ~]# vim /vhosts/web2/htdcos/index.htmlPage at web2[root@localhost ~]# vim /vhosts/web3/htdcos/index.htmlPage at web3

一,基于ip配置虚拟主机
主机ip地址为172.16.8.11/24,在eth0网卡上额外添加2个ip地址

[root@localhost ~]# ip addr add 172.16.8.12/24 dev eth0 [root@localhost ~]# ip addr add 172.16.8.13/24 dev eth0 

编辑虚拟主机配置文件,添加如下内容

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf 
<VirtualHost 172.16.8.11:80>    ServerName web1.example.com    DocumentRoot "/vhosts/web1/htdocs"    <Directory "/vhosts/web1/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost><VirtualHost 172.16.8.12:80>    ServerName web1.example.com    DocumentRoot "/vhosts/web2/htdocs"    <Directory "/vhosts/web2/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost><VirtualHost 172.16.8.13:80>    ServerName web1.example.com    DocumentRoot "/vhosts/web3/htdocs"    <Directory "/vhosts/web3/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost>

重载httpd配置文件

[root@localhost ~]# service httpd reload

在浏览器上依次输入172.16.8.11,172.16.8.12,172.16.8.13
得到的结果依次为Page at web1,Page at web2,Page at web3

二,基于端口配置虚拟主机
编辑主配置文件,在Listen 80下添加2行

[root@localhost ~]# vim /etc/httpd/httpd.confListen 8008Listen 8080

修改虚拟主机配置文件

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf 
<VirtualHost 172.16.8.11:80>    ServerName web1.example.com    DocumentRoot "/vhosts/web1/htdocs"    <Directory "/vhosts/web1/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost><VirtualHost 172.16.8.11:8008>    ServerName web1.example.com    DocumentRoot "/vhosts/web2/htdocs"    <Directory "/vhosts/web2/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost><VirtualHost 172.16.8.11:8080>    ServerName web1.example.com    DocumentRoot "/vhosts/web3/htdocs"    <Directory "/vhosts/web3/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost>

重启httpd服务

[root@localhost ~]# service httpd restart

在浏览器上依次输入172.16.8.11,172.16.8.11:8008,172.16.8.11:8080
得到的结果依次为Page at web1,Page at web2,Page at web3

三,基于主机名配置虚拟主机
修改虚拟主机的配置文件

[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf 
<VirtualHost 172.16.8.11:80>    ServerName web1.example.com    DocumentRoot "/vhosts/web1/htdocs"    <Directory "/vhosts/web1/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost><VirtualHost 172.16.8.11:80>    ServerName web2.example.com    DocumentRoot "/vhosts/web2/htdocs"    <Directory "/vhosts/web2/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost><VirtualHost 172.16.8.11:80>    ServerName web3.example.com    DocumentRoot "/vhosts/web3/htdocs"    <Directory "/vhosts/web3/htdocs">        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory></VirtualHost>

重载httpd配置

[root@localhost ~]# service httpd reload

修改浏览器所在的系统hosts文件(Windows路径是C:\Windows\System32\drivers\etc\hosts,Linux路径是/etc/hosts),添加如下3行

172.16.8.11 web1.example.com172.16.8.11 web2.example.com172.16.8.11 web3.example.com

在浏览器上依次输入web1.example.com,web2.example.com,web3.example.com
得到的结果依次为Page at web1,Page at web2,Page at web3

以上三种配置虚拟主机的方式可以混合使用

原创粉丝点击