Apache配置虚拟主机,基于主机名的方式

1.配置hosts文件

vim /etc/hosts
172.19.74.12   wenlong1.wen.com
172.19.74.12   wenlong2.wen.com
172.19.74.12   wenlong3.wen.com

2.创建网站目录,index.html中写入不同内容

mkdir -p /home/www/{wenlong1,wenlong2,wenlong3}
echo "wenlong1  page" > /home/www/wenlong1/index.html
echo "wenlong2  page" > /home/www/wenlong2/index.html
echo "wenlong3  page" > /home/www/wenlong3/index.html

3.在Apache配置文件中配置虚拟主机信息

vim /etc/httpd/conf/httpd.conf
<Directory "/home/www/wenlong1">
    Require all granted
</directory>
<VirtualHost 172.19.74.12>
    DocumentRoot "/home/www/wenlong1"
    ServerName "wenlong1.wen.com"
</VirtualHost>

<Directory "/home/www/wenlong2">
    Require all granted
</directory>
<VirtualHost 172.19.74.12>
    DocumentRoot "/home/www/wenlong2"
    ServerName "wenlong2.wen.com"
</VirtualHost>

<Directory "/home/www/wenlong3">
    Require all granted
</directory>
<VirtualHost 172.19.74.12>
    DocumentRoot "/home/www/wenlong3"
    ServerName "wenlong3.wen.com"
</VirtualHost>
# 检查配置文件中语法是否正确
httpd  -t

4.重启Apache,发现403访问被拒绝,怀疑因selinux上下文引起

说明:如果嫌麻烦你可以关掉selinux,具体请百度。我这里没有关,修改了站点的安全上下文

systemctl  restart httpd
curl wenlong1.wen.com/index.html

# 查看selinux状态,发现是强制模式
getenforce 

# 为验证是否因selinux引起,将状态改为宽容模式,再次验证
setenforce 0

5.确定是selinux引起的问题,修改selinux上下文

# 将selinux设置为强制模式
setenforce 1
# 创建新的默认规则,修改selinux上下文

# 下边命令在 /etc/selinux/targeted/contexts/files/file_contexts.local中加了一条默认规则
semanage fcontext  -a  -t  httpd_sys_content_t  "/home/www(/.*)?"

备注: /etc/selinux/targeted/contexts/files/file_contexts.local截图如下

# 按照上边加的规则,递归将新的上下文应用到/home/www/文件夹和它的子目录
restorecon -R  /home/www/
# 再次测试,并测试其它虚拟主机,参看下边图片
curl wenlong1.wen.com/index.html

猜你喜欢

转载自blog.csdn.net/qq_29644709/article/details/109139222