apache 虚拟主机配置
注意:
虚拟主机可以开很多个
虚拟主机配置之后, 原来的默认 / etc/httpd/httpd.conf 中的默认网站就不会生效了
练习:
主机 server0
- ip:172.25.0.11
- hostname: http://server0.example.com/
客户机
desktop0
要求: 配置 2 个虚拟主机, 域名分别为
1, http://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 服务端软件包
[[email protected] conf.d]# yum install httpd -httpd-manual -y
httpd-manual 帮助文档可以不用安装
二, 配置虚拟主机的配置文件
1, 其他域名访问配置文件
- [[email protected] conf.d]# VIM /etc/httpd/conf.d/default-vhosts.conf
- <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 域名配置文件
- [[email protected] conf.d]# VIM /etc/httpd/conf.d/www0.example.com-vhosts.conf
- <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>
注意配置文件格式, 换行符
三, 建立虚拟主机的网站目录
[[email protected] conf.d]# mkdir -p /srv/{default,www0.example.com}/www
四, 建立测试网站的 index.HTML
- [[email protected] conf.d]# echo "I'm other">> /srv/default/www/index.HTML
- [[email protected] conf.d]# echo "I'm www0.example.com">> /srv/www0.example.com/www/index.HTML
五, 修改网站目录权限
[[email protected] conf.d]# chown apache"apache -R /srv/*
六, 配置 selinux 安全上下文
首先查看原来的目录安全上下文为 var_t
- [[email protected] conf.d]# ll -Zd /srv/
- drwxr-xr-x. root root system_u:object_r:var_t:s0 /srv/
修改安全上下文
- [[email protected] conf.d]# semanage fcontext -a -t 'httpd_sys_content_t' '/srv(/.*)?'
- [[email protected] conf.d]# restorecon -Rv /srv/
可以看到安全上下文已经改变
- [[email protected] 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/ 刷新, 使安全上下文生效
七, 修改防火墙配置
- [[email protected] conf.d]# firewall-cmd --permanent --add-service=http
- [[email protected] conf.d]# firewall-cmd --reload
查看防火墙规则, 可以看到 services 中已经有了 http 的服务, 表明 http 的服务已经放行
- [[email protected] 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 服务
- [[email protected] conf.d]# systemctl enable httpd
- [[email protected] conf.d]# systemctl restart httpd
以上就是 apache 虚拟主机完整的配置方法
desktop0 机器访问以上地址:
curl www0 返回 I'm www0.example.com
http://xn--www0-ui4g1031a.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 届大咖指导, 共同进步, 谢谢.
来源: http://www.bubuko.com/infodetail-3032030.html