apache虚拟主机

apache基本安装

web1服务器IP地址:20.0.0.10
1、导入apr-1.6.2.tar.gz、apr-util-1.6.0.tar.gz、httpd-2.4.29.tar.bz2数据包
2、安装解压工具并解压数据包

[root@web ~]# yum -y install bzip2-devel.x86_64
[root@web ~]# cd /opt/
[root@web opt]# tar xf apr-1.6.2.tar.gz
[root@web opt]# tar xf apr-util-1.6.0.tar.gz
[root@web opt]# tar xf httpd-2.4.29.tar.bz2

3、移动依赖包

[root@web opt]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@web opt]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util

4、安装编译工具

[root@web opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl

5、安装功能模块

[root@web opt]# cd /opt/httpd-2.4.29/
[root@web httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi

6、编译安装

[root@web httpd-2.4.29]# make && make install

7、优化路径

[root@web httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@web httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/

8、设置启动进程

[root@web httpd-2.4.29]# cd /lib/systemd/system/
echo "[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target" > httpd.service

9、启动服务

[root@web ~]# systemctl start httpd.service
[root@web ~]# systemctl enable httpd.service
[root@web ~]# systemctl is-enabled httpd.service

10、查看进程是开启

[root@web ~]# netstat -anptu |grep httpd

虚拟主机基于IP地址

IP地址1:20.0.0.10
IP地址2:20.0.100.100
1、开启虚拟主机功能

[root@web ~]#  /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf		去掉#,开启虚拟主机功能

2、创建两个网站主页

[root@web ~]# mkdir /opt/wj1
[root@web ~]# mkdir /opt/wj2
[root@web ~]# echo "<html><h1> This is Web1 </h1></html>" > /opt/wj1/index.html
[root@web ~]# echo "<html><h1> This is Web2 </h1></html>" > /opt/wj2/index.html

3、设置域名解析

[root@web ~]# vim /etc/hosts 
20.0.0.10  www.wj1.com 	#增加
20.0.0.100  www.wj2.com 	#增加

4、配置虚拟主机

[root@web ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
增加下面内容
<VirtualHost 20.0.0.10:80>                    ####设置 51xit.top 虚拟站点区域
    DocumentRoot "/opt/wj1" 
    ServerName www.wj1.com
    ErrorLog "logs/www.wj1.com.error_log" 
    CustomLog "logs/www.wj1.com.access_log" common
    <Directory "/opt/wj1">                                       ####设置目录访问权限
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 20.0.0.100:80>                                                             ####设置 52xit.top 虚拟站点区域
    DocumentRoot "/opt/wj2" 
    ServerName www.wj2.com
    ErrorLog "logs/www.wj2.com.error_log" 
    CustomLog "logs/www.wj2.com.access_log" common
    <Directory "/opt/wj2">                                        ####设置目录访问权限
       Require all granted
    </Directory>
</VirtualHost> 

5、重启服务

[root@web ~]# systemctl restart httpd.service 

虚拟主机基于IP地址测试
测试机需要设置域名解析

[root@cesi ~]# vim /etc/hosts 
20.0.0.10  www.wj1.com 	#增加
20.0.0.100  www.wj2.com 	#增加

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

虚拟主机基于域名访问

1、开启虚拟主机功能

[root@web ~]#  /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf		去掉#,开启虚拟主机功能

2、创建两个网站主页

[root@web ~]# mkdir /opt/wj1
[root@web ~]# mkdir /opt/wj2
[root@web ~]# echo "<html><h1> This is Web1 </h1></html>" > /opt/wj1/index.html
[root@web ~]# echo "<html><h1> This is Web2 </h1></html>" > /opt/wj2/index.html

3、设置域名解析

[root@web ~]# vim /etc/hosts 
20.0.0.10  www.wj1.com	 www.wj2.com 	#增加

4、配置虚拟主机

[root@web ~]# /usr/local/httpd/conf/extra/httpd-vhosts.conf
增加一下内容
<VirtualHost *:80>                                                             ####>设置 51xit.top 虚拟站点区域
    DocumentRoot "/opt/wj1" 
    ServerName www.wj1.com
    ErrorLog "logs/www.wj1.com.error_log" 
    CustomLog "logs/www.wj1.com.access_log" common
    <Directory "/opt/wj1">                                       ####设置目录访问权限
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>                                                             ####设置 52xit.top 虚拟站点区域
    DocumentRoot "/opt/wj2" 
    ServerName www.wj2.com
    ErrorLog "logs/www.wj2.com.error_log" 
    CustomLog "logs/www.wj2.com.access_log" common
    <Directory "/opt/wj2">                                        ####设置目录访问权限
       Require all granted
    </Directory>
</VirtualHost> 

5、重启服务

[root@web ~]# systemctl restart httpd

虚拟主机基于域名测试
测试机需要设置域名解析

[root@cesi ~]# vim /etc/hosts 
20.0.0.10  www.wj1.com   www.wj2.com	#增加

在这里插入图片描述在这里插入图片描述

虚拟主机基于端口访问

1、开启虚拟主机功能

[root@web ~]#  /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf		去掉#,开启虚拟主机功能

2、创建两个网站主页

[root@web ~]# mkdir /opt/wj1
[root@web ~]# mkdir /opt/wj2
[root@web ~]# echo "<html><h1> This is Web1 </h1></html>" > /opt/wj1/index.html
[root@web ~]# echo "<html><h1> This is Web2 </h1></html>" > /opt/wj2/index.html

3、设置域名解析

[root@web ~]# vim /etc/hosts 
20.0.0.10  www.wj1.com	 www.wj2.com 	#增加

4、配置虚拟主机

[root@web ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 20.0.0.20:80>                                             ####设置 51xit.top 虚拟站点区域
    DocumentRoot "/opt/wj1" 
    ServerName www.wj1.com
    ErrorLog "logs/www.wj1.com.error_log" 
    CustomLog "logs/www.wj1.com.access_log" common
    <Directory "/opt/wj1">                                       ####设置目录访问权限
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 20.0.0.10:8080>                                                             ####设置 52xit.top 虚拟站点区域
    DocumentRoot "/opt/wj2" 
    ServerName www.wj2.com
    ErrorLog "logs/www.wj2.com.error_log" 
    CustomLog "logs/www.wj2.com.access_log" common
    <Directory "/opt/wj2">                                        ####设置目录访问权限
       Require all granted
    </Directory>
</VirtualHost>

5、增加一个监听端口

[root@web ~]# vim /usr/local/httpd/conf/httpd.conf
Listen 20.0.0.10:8080	增加

6、重启服务

[root@web ~]#  systemctl restart httpd

虚拟主机基于端口访问测试
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值