Linux Apache虚拟主机配置方法

  

apache 虚拟主机配置

注意:

  1. 虚拟主机可以开很多个
  2. 虚拟主机配置之后,原来的默认/etc/httpd/httpd.conf中的默认网站就不会生效了

练习:

要求:配置2个虚拟主机,域名分别为
1、www0.example.com
2、其他任意的域名,只要是可以访问到server0的域名,也可以用server0的ip和机器名进行访问

准备环境:

  1、server0机器

    ip:172.25.0.11 

    机器名:server0.example.com

    /etc/hosts文件中写入了desktop0的信息

    firewalld和selinux开启

    可以使用yum源来安装httpd服务

 

  2、desktop0机器

    ip:172.25.0.10

    机器名:desktop0.example.com

    /etc/hosts文件中写入了server0的信息 

一、安装web服务端软件包

1
2
[root@server0 conf.d]# yum install httpd -httpd-manual -y
httpd-manual  帮助文档可以不用安装

二、配置虚拟主机的配置文件

1、其他域名访问配置文件

[root@server0 conf.d]# vim /etc/httpd/conf.d/default-vhosts.conf

1
2
3
4
5
6
7
8
9
10
<VirtualHost _default_:80>                      ---> 匹配本机其他虚拟主机不能匹配的其他任何域名
    DocumentRoot  /srv/default/www/              ---> 定义网站目录
    CustomLog    "logs/default-vhost.log" combined    ---> 日志存放位置
     
    <Directory /srv/default/www/>                ---> 指定网站目录访问控制
        Options Indexes FollowSymLinks           ---> 可以复制httpd.conf中的<Directory /var/www/html>参数
        AllowOverride None                  --->
        Require all granted                  ---> 所有人都可以访问
    </Directory>
</VirtualHost>

  

2、www0.example.com域名配置文件

[root@server0 conf.d]# vim /etc/httpd/conf.d/www0.example.com-vhosts.conf

1
2
3
4
5
6
7
8
9
10
11
12
<VirtualHost *:80>                                           
        Servername   www0.example.com                   ---> 绑定域名
        ServerAlias  www0                       ---> 其他域名
        DocumentRoot  /srv/www0.example.com/www/            ---> 定义网站目录
        CustomLog     "logs/www0.example.com-vhost.log" combined    ---> 日志存放位置
         
        <Directory /srv/www0.example.com/www/>                ---> 指定网站目录访问控制
            Options Indexes FollowSymLinks              --->
            AllowOverride None                  --->
            Require all granted                 ---> 所有人都可以访问
        </Directory>
    </VirtualHost>

  

注意配置文件格式,换行符

三、建立虚拟主机的网站目录

1
[root@server0 conf.d]# mkdir -p /srv/{default,www0.example.com}/www

四、建立测试网站的index.html

1
2
[root@server0 conf.d]# echo "I'm other" >> /srv/default/www/index.html
[root@server0 conf.d]# echo "I'm www0.example.com" >> /srv/www0.example.com/www/index.html

五、修改网站目录权限

1
[root@server0 conf.d]# chown apache"apache -R /srv/*

六、配置selinux安全上下文

1
2
3
4
5
6
7
8
9
首先查看原来的目录安全上下文为var_t
[root@server0 conf.d]# ll -Zd /srv/
drwxr-xr-x. root root system_u:object_r:var_t:s0       /srv/
修改安全上下文
[root@server0 conf.d]# semanage fcontext -a  -t 'httpd_sys_content_t' '/srv(/.*)?'
[root@server0 conf.d]# restorecon -Rv /srv/
可以看到安全上下文已经改变
[root@server0 conf.d]# ll -Zd /srv/default
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /srv/despotic/

注释:

1、安全上下文记不住可参考httpd默认网站目录的安全上下文
      ls -lZd /var/wwwt/html/
2、/srv(/).*? 正则表达式,?是修饰()的,表示括号里的内容可有可无
      当括号里的内容存在时,匹配到/srv/.*内容
      当括号里的内容不存在时,那么就匹配到的是/srv
3、restorecon -Rv /srv/ 刷新,使安全上下文生效

七、修改防火墙配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@server0 conf.d]# firewall-cmd --permanent --add-service=http
[root@server0 conf.d]# firewall-cmd --reload
 
查看防火墙规则,可以看到services中已经有了http的服务,表明http的服务已经放行
[root@server0 conf.d]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client http ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

八、设定apache开启自启动,并且重新启动apache服务

1
2
[root@server0 conf.d]# systemctl enable httpd
[root@server0 conf.d]# systemctl restart httpd

以上就是apache虚拟主机完整的配置方法

desktop0机器访问以上地址:

  • curl www0                               返回 I'm www0.example.com
  • curl www0.dxample.com         返回 I'm www0.example.com
  • curl server0.example.com       返回 I'm other
  • curl 172.25.0.11               返回I'm other

本实验到此结束,还记得刚开始安装的httpd-manual包吗?下面我们来看一下效果。

使用浏览器访问172.25.0.11/manual可以打开Apache的帮助页面,如果配置的时候有不懂的地方,可以访问这个页面来查询帮助。

 

文中有什么不对或者不明白的地方,请大家私信我或者留言区发评论,我看到之后第一时间处理。

本人Linux菜鸟,欢迎各位Linux届大咖指导,共同进步,谢谢。

posted on   状元兜里有糖  阅读(2917)  评论(0编辑  收藏  举报

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示