apache配置虚拟主机(三种方式)

查看apache的配置

查找apache的配置文件

[root@localhost ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf			//该文件为httpd的配置文件
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

查看DocumentRoot

[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep "^DocumentRoot"
DocumentRoot "/var/www/html"

DocumentRoot是站点文件的根目录,html、css、js文件都会以站点作为文件夹放在该目录下。

查看配置文件的根目录

[root@localhost httpd]# cat /etc/httpd/conf/httpd.conf | grep "^ServerRoot"
ServerRoot "/etc/httpd"

配置文件都会存放在"/etc/httpd"目录下,主配置文件中的相对路径也是以这个目录为参考点。

查看站点配置文件存放位置

[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep "^Include"
Include conf.modules.d/*.conf
IncludeOptional conf.d/*.conf

可见主配置文件中将conf.d目录下的所有.conf文件都include了,故我们可以在conf.d目录下创建站点的.conf配置文件,也可以另外添加站点配置,形式为“Include conf_file_path”。

三种方式配置虚拟主机

httpd的虚拟主机也就是虚拟站点,我们通过“基于域名的虚拟主机”、“基于IP地址的虚拟主机”、“基于端口的虚拟主机”三种方式进行配置。

三种方式在配置上的区别也在配置文件上不同,下面配置的例子都将把配置写在/etc/httpd/conf.d/vhost.conf文件中。

基于域名的虚拟主机

# 接收来自本机任意IP地址的80端口访问
<VirtualHost *:80>
  # 站点目录为/var/www/html/mydomaincom,需手动创建并添加文件
  DocumentRoot /var/www/html/mydomaincom
  ServerName www.mydomain.com
  # 日志文件,是个相对路径,可通过查看主配置文件得到其根目录
  ErrorLog "logs/www.mydomain.com.error_log"
  CustomLog "logs/www.mydomain.com.access_log" common
  # 对站点的子目录进行访问限制
  <Directory "/var/www/html/mydomaincom">
    # 开放访问
    Require all granted
  </Directory>
</VirtualHost>

创建站点html目录

[root@localhost httpd]# mkdir /var/www/html/mydomaincom

创建站点html文件

[root@localhost httpd]# echo "this is mydomain.html" >> /var/www/html/mydomaincom/index.html

测试

# 重启httpd服务
[root@localhost httpd]# systemctl restart httpd
# 因为这是我们自定义的域名,故需添加域名解析
[root@localhost httpd]# echo "192.168.218.6  www.mydomain.com" >> /etc/hosts
[root@localhost httpd]# curl www.mydomain.com
this is mydomain.html
# 访问成功

基于端口的虚拟主机

配置了两个站点

<VirtualHost *:8080>
  # 也可以配置ServerName,这里没有配
  DocumentRoot  "/var/www/html/8080com"
  ErrorLog "logs/8080com.error_log"
  CustomLog "logs/8080com.access_log" common
  <Directory "/var/www/html/">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:9090>
  DocumentRoot  "/var/www/html/9090com"
  ErrorLog "logs/9090com.error_log"
  CustomLog "logs/9090com.access_log"  common
  <Directory "/var/www/html/">
    Require all granted
  </Directory>
</VirtualHost>
Listen 8080
Listen 9090

用同样的方法创建站点目录和文件

[root@localhost html]# pwd
/var/www/html
[root@localhost html]# tree
.
├── 8080com
│   └── index.html
├── 9090com
│   └── index.html
└── mydomaincom
    └── index.html

3 directories, 3 files

测试

[root@localhost html]# curl 192.168.218.6:8080
this is 8080com.html
[root@localhost html]# curl 192.168.218.6:9090
this is 9090com.html
# 访问成功

基于IP的虚拟主机

该主机的另一个IP地址为192.168.31.128

在vhost.conf中添加站点

<VirtualHost 192.168.31.128:80>
  ServerName www.mydomain2.com
  DocumentRoot  "/var/www/html/mydomain2com"
  ErrorLog "logs/mydomain2.error_log"
  CustomLog "logs/mydomain2.access_log"  common
  <Directory "/var/www/html/">
    Require all granted
  </Directory>
</VirtualHost>
Listen 192.168.31.128:80

用上面同样的方法创建站点目录和文件

[root@localhost html]# pwd 
/var/www/html
[root@localhost html]# ls
8080com  9090com  mydomain2com  mydomaincom
[root@localhost html]# ls mydomain2com/
index.html

测试

[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# curl 192.168.31.128
this is mydomain2com
# 访问成功
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值