Apache虚拟主机配置

时间:2022-09-01 20:55:52

环境介绍

[root@web01 ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[root@web01 ~]# uname -r

3.10.0-327.el7.x86_64

[root@web01 ~]# uname -m

x86_64

安装apache

yum install -y apr-devel apr-util-devel pcre-devel openssl-devel

wget http://mirrors.sohu.com/apache/httpd-2.4.25.tar.gz

tar xf httpd-2.4.25.tar.gz

cd httpd-2.4.25/

./configure --prefix=/application/httpd-2.4.25 --enable-so --enable-modules="all"

make && make install

ln -s /application/httpd-2.4.25/ /application/httpd

sed -i 's@#ServerName www.example.com:80@ServerName localhost:80@g' /application/httpd/conf/httpd.conf

/application/httpd/bin/apachectl

配置基于域名的虚拟主机

修改主配置文件,找到如下内容

取消注释启用虚拟主机模块

#LoadModule vhost_alias_module modules/mod_vhost_alias.so

用sed命令进行修改

sed -i 's@#LoadModule vhost_alias_module modules/mod_vhost_alias.so@LoadModule vhost_alias_module modules/mod_vhost_alias.so@g' /application/httpd/conf/httpd.conf

注销这个

<Directory />

    AllowOverride none

    #Require all denied

</Directory>

取消这一行的注释,启用这一个容器

# Virtual hosts

#Include conf/extra/httpd-vhosts.conf

sed修改

sed -i 's@#Include conf/extra/httpd-vhosts.conf@Include conf/extra/httpd-vhosts.conf@g' /application/httpd/conf/httpd.conf

保存退出

修改/application/httpd/conf/extra/httpd-vhosts.conf的配置文件

<VirtualHost *:80>

    ServerAdmin admin@123.com

DocumentRoot "/application/httpd/data/www/"

<Directory "/application/httpd/data/www">

   Options Indexes FollowSymLinks

   AllowOverride None

   Order allow,deny

   Allow from all

</Directory>

    ServerName www.xiaohaozi.com

    ServerAlias xiaohaozi.com

    ErrorLog "/application/httpd/data/www-error_log"

    CustomLog "/application/httpd/data/www-access_log" common

</VirtualHost>

 

<VirtualHost *:80>

    ServerAdmin admin@456.com

DocumentRoot "/application/httpd/data/bbs"

<Directory "/application/httpd/data/bbs">

   Options Indexes FollowSymLinks

   AllowOverride None

   Order allow,deny

   Allow from all

</Directory>

    ServerName bbs.xiaohaozi.com

    ErrorLog "/application/httpd/data/bbs-error_log"

    CustomLog "/application/httpd/data/bbs-access_log" common

</VirtualHost>

创建目录

mkdir -p /application/httpd/data/www

mkdir -p /application/httpd/data/bbs

echo 'hollo www' >/application/httpd/data/www/index.html

echo 'hollo bbs' >/application/httpd/data/bbs/index.html

windows上做好hosts解析

Apache虚拟主机配置

Apache虚拟主机配置

配置基于ip的虚拟主机

1. 假设服务器有个IP地址为10.0.0.7,使用ifconfig在同一个网络接口eth0上绑定2个IP:

[root@web01 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ae:50:5d brd ff:ff:ff:ff:ff:ff

    inet 10.0.0.7/24 brd 10.0.0.255 scope global eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:feae:505d/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ae:50:67 brd ff:ff:ff:ff:ff:ff

    inet 172.16.1.7/24 brd 172.16.1.255 scope global eth1

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:feae:5067/64 scope link

       valid_lft forever preferred_lft forever

ifconfig eth0:1 10.0.0.8

ifconfig eth0:2 10.0.0.9

 

=================================================================================================

[root@web01 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ae:50:5d brd ff:ff:ff:ff:ff:ff

    inet 10.0.0.7/24 brd 10.0.0.255 scope global eth0

       valid_lft forever preferred_lft forever

    inet 10.0.0.8/8 brd 10.255.255.255 scope global eth0:1

       valid_lft forever preferred_lft forever

    inet 10.0.0.9/8 brd 10.255.255.255 scope global secondary eth0:2

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:feae:505d/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ae:50:67 brd ff:ff:ff:ff:ff:ff

    inet 172.16.1.7/24 brd 172.16.1.255 scope global eth1

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:feae:5067/64 scope link

       valid_lft forever preferred_lft forever

修改虚拟主机配置文件

vim /application/httpd/conf/extra/httpd-vhosts.conf

<VirtualHost 10.0.0.8:80>

    ServerAdmin admin@123.com

DocumentRoot "/application/httpd/data/www/"

<Directory "/application/httpd/data/www">

   Options Indexes FollowSymLinks

   AllowOverride None

   Order allow,deny

   Allow from all

</Directory>

    ServerName www.xiaohaozi.com

    ServerAlias xiaohaozi.com

    ErrorLog "/application/httpd/data/www-error_log"

    CustomLog "/application/httpd/data/www-access_log" common

</VirtualHost>

 

<VirtualHost 10.0.0.7:80>

    ServerAdmin admin@456.com

DocumentRoot "/application/httpd/data/bbs"

<Directory "/application/httpd/data/bbs">

   Options Indexes FollowSymLinks

   AllowOverride None

   Order allow,deny

   Allow from all

</Directory>

    ServerName bbs.xiaohaozi.com

    ErrorLog "/application/httpd/data/bbs-error_log"

    CustomLog "/application/httpd/data/bbs-access_log" common

</VirtualHost>

测试

Apache虚拟主机配置

Apache虚拟主机配置

配置基于端口的虚拟主机

<VirtualHost *:81>

    ServerAdmin admin@123.com

DocumentRoot "/application/httpd/data/www/"

<Directory "/application/httpd/data/www">

   Options Indexes FollowSymLinks

   AllowOverride None

   Order allow,deny

   Allow from all

</Directory>

    ServerName www.xiaohaozi.com

    ServerAlias xiaohaozi.com

    ErrorLog "/application/httpd/data/www-error_log"

    CustomLog "/application/httpd/data/www-access_log" common

</VirtualHost>

 

<VirtualHost *:82>

    ServerAdmin admin@456.com

DocumentRoot "/application/httpd/data/bbs"

<Directory "/application/httpd/data/bbs">

   Options Indexes FollowSymLinks

   AllowOverride None

   Order allow,deny

   Allow from all

</Directory>

    ServerName bbs.xiaohaozi.com

    ErrorLog "/application/httpd/data/bbs-error_log"

    CustomLog "/application/httpd/data/bbs-access_log" common

</VirtualHost>

修改主配置文件添加监听端口

Apache虚拟主机配置

测试

不加端口测试

Apache虚拟主机配置

Apache虚拟主机配置

Apache虚拟主机配置

到这里我们的虚拟主机就配置完成