CentOS 7.3配置Apache虚拟主机

栏目: 服务器 · 发布时间: 5年前

实验环境

  1. 一台最小化安装的CentOS 7.3虚拟机

配置基础环境

1. 安装apache

yum install -y httpd

2. 建立虚拟主机的根目录

mkdir /var/wwwroot
mkdir /var/wwwroot/site1
mkdir /var/wwwroot/site2
echo -e "site1" >> /var/wwwroot/site1/index.html
echo -e "site2" >> /var/wwwroot/site2/index.html

CentOS 7.3配置Apache虚拟主机

3. 关闭CentOS的防火墙

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

CentOS 7.3配置Apache虚拟主机

配置基于端口的虚拟主机

1. 编辑apache配置文件

vim /etc/httpd/conf.d/vhost.conf

2. 添加以下内容

Listen 8081
<VirtualHost *:8081>
    DocumentRoot "/var/wwwroot/site1"
    ErrorLog "logs/site1.error.log"
    CustomLog "logs/site1.access.log" common
    <Directory "/var/wwwroot/site1">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

Listen 8082
<VirtualHost *:8082>
    DocumentRoot "/var/wwwroot/site2"
    ErrorLog "logs/site2.error.log"
    CustomLog "logs/site2.access.log" common
    <Directory "/var/wwwroot/site2">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

CentOS 7.3配置Apache虚拟主机

3. 启动apache服务

systemctl start httpd

4. 在宿主机访问两个站点

http://192.168.204.133:8081
http://192.168.204.133:8082

CentOS 7.3配置Apache虚拟主机 CentOS 7.3配置Apache虚拟主机

配置基于域名的虚拟主机

1. 编辑apache配置文件

vim /etc/httpd/conf.d/vhost.conf

2. 修改为以下内容

<VirtualHost *:80>
    DocumentRoot "/var/wwwroot/site1"
    ServerName site1.test.com
    ErrorLog "logs/site1.error.log"
    CustomLog "logs/site1.access.log" common
    <Directory "/var/wwwroot/site1">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/wwwroot/site2"
    ServerName site2.test.com
    ErrorLog "logs/site2.error.log"
    CustomLog "logs/site2.access.log" common
    <Directory "/var/wwwroot/site2">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

CentOS 7.3配置Apache虚拟主机

3. 重启apache服务

systemctl restart httpd

4. 在Windows上修改 hosts 文件

编辑 C:\Windows\System32\drivers\etc\hosts 文件,

添加以下内容(根据实际情况自己修改)

192.168.204.135   site1.test.com  
192.168.204.135   site2.test.com

5. 在宿主机访问两个站点

http://site1.test.com/
http://site2.test.com/
CentOS 7.3配置Apache虚拟主机

CentOS 7.3配置Apache虚拟主机

配置基于IP的虚拟主机

1. 在虚拟机上增加两个IP地址

ifconfig ens33:1 192.168.204.135
ifconfig ens33:2 192.168.204.136

CentOS 7.3配置Apache虚拟主机

2. 重新编辑apache配置文件

vim /etc/httpd/conf.d/vhost.conf

3. 修改配置文件为以下内容

<VirtualHost 192.168.204.135:80>
    DocumentRoot "/var/wwwroot/site1"
    ErrorLog "logs/site1.error.log"
    CustomLog "logs/site1.access.log" common
    <Directory "/var/wwwroot/site1">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 192.168.204.136:80>
    DocumentRoot "/var/wwwroot/site2"
    ErrorLog "logs/site2.error.log"
    CustomLog "logs/site2.access.log" common
    <Directory "/var/wwwroot/site2">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

CentOS 7.3配置Apache虚拟主机

4. 重启apache服务

systemctl restart httpd

5. 在宿主机访问两个站点

http://192.168.204.135/
http://192.168.204.136/

CentOS 7.3配置Apache虚拟主机

CentOS 7.3配置Apache虚拟主机


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

UML基础、案例与应用

UML基础、案例与应用

施穆勒 / 李虎、赵龙刚 / 人民邮电出版社 / 2004-7-1 / 42.00元

本书教读者循序渐进地、系统地学习UML基础知识和应用技术。和前一版相比,本书内容根据UML 2.0进行了补充和更新,随书光盘包含了建模工具Poseidon的试用版。 全书分为三部分24章。第一部分“基础知识”包括第1章到第15章,主要是介绍UML语言的基础知识以及面向对象的概念和思想,还简单介绍了UML在开发过程的应用方法。第二部分“学习案例”包括第16章到第22章,结合实例详细分析了UML的应用......一起来看看 《UML基础、案例与应用》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具