编译安装httpd实现三类虚拟主机的访问

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编译安装httpd实现三类虚拟主机的访问相关的知识,希望对你有一定的参考价值。

httpd简介
httpd是Apahce超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。

1.编译安装httpd-2.4
httpd依赖于apr-1.4+,apr-util-1.4+,[apr-icon]
apr :apache portable runtime

            (1)安装开发环境
            [[email protected]~]# yum groupinstall "Development Tools" -y

            (2)创建系统用户,不允许登录不创建家目录
            [[email protected] ~]# groupadd -r apache
            [[email protected] ~]# useradd -r -M -s /sbin/nologin -g apache apache

            (3)安装编译依赖包
            [[email protected] ~]# yum install openssl-devel pcre-devel expat-devel libtool -y

            (4)下载并安装apr-1.4+和apr-util-1.4+
            [[email protected] ~]# yum install wget -y
            [[email protected] ~]# cd /usr/src
            [[email protected] src]# ls
                debug kernels

                //下载编译安装apr-1.4+
                [[email protected] src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
                [[email protected] src]# tar xf apr-1.6.3.tar.bz2 
                [[email protected] src]# ls
                apr-1.6.3 apr-1.6.3.tar.bz2 debug kernels
                 其中需要修改apr-1.6.3 的配置文件
                [[email protected] apr-1.6.3]# vi configure
                if test -n "${ZSH_VERSION+set}"; then
                setopt NO_GLOB_SUBST
                fi
                cfgfile=${ofile}T
                trap "$RM "$cfgfile"; exit 1" 1 2 15
                #$RM "$cfgfile"      //将此行加上注释或者删除掉
        [[email protected] apr-1.6.3]# ./configure --prefix=/usr/local/apr
        [[email protected] apr-1.6.3]# make && make install

        //下载编译安装apr-util-1.4+
        [[email protected] src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2

[[email protected] src]# tar xf apr-util-1.6.1.tar.bz2
[[email protected] src]# ls
apr-1.6.3 apr-util-1.6.1 debug
apr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 kernels
[[email protected] apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[[email protected] apr-util-1.6.1]# make && make install
(5)编译安装httpd
[[email protected] ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[[email protected] ~]# ls
anaconda-ks.cfg httpd-2.4.34.tar.bz2
[[email protected] ~]# tar xf httpd-2.4.34.tar.bz2
[[email protected] ~]# ls
anaconda-ks.cfg httpd-2.4.34 httpd-2.4.34.tar.bz2

[[email protected] ~]# cd httpd-2.4.34
[[email protected] httpd-2.4.34]# ./configure --prefix=/usr/local/apache

--sysconfdir=/etc/httpd24
--enable-so
--enable-ssl
--enable-cgi
--enable-rewrite
--with-zlib
--with-pcre
--with-apr=/usr/local/apr
--with-apr-util=/usr/local/apr-util/
--enable-modules=most
--enable-mpms-shared=all
--with-mpm=prefork
[[email protected] httpd-2.4.34]# make && make install

    (6)修改httpd的配置文件
[[email protected] ~]# vi /etc/httpd24/httpd.conf 

ServerName www.example.com:80 //取消此行前面的#号

三类虚拟主机:
相同ip不同端口
不同ip相同端口
相同ip相同端口不同域名

  1. 相同ip不同端口
    //修改配置文件增加一个新端口,新端口必须是其他进程没有占用的
    [[email protected] ~]# vi /etc/httpd24/httpd.conf
    Listen 80
    Listen 8080
    //在配置文件最后加上虚拟主机配置
    <VirtualHost 192.168.56.13:80>
    ServerName www.rose.com
    DocumentRoot "/usr/local/apache/htdocs/rose"
    ErrorLog "/usr/local/apache/logs/rose/error_log"
    CustomLog "/usr/local/apache/logs/rose/access_log" combined
    <Directory /usr/local/apache/htdocs/rose>
    <RequireAll>
    Require all granted
    </RequireAll>
    </Directory>
    </VirtualHost>

<VirtualHost 192.168.56.13:8080>
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/huaer"
ErrorLog "/usr/local/apache/logs/huaer/error_log"
CustomLog "/usr/local/apache/logs/huaer/access_log" combined
<Directory /usr/local/apache/htdocs/huaer>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
//创建网页目录并修改属主属组
[[email protected] ~]# cd /usr/local/apache/htdocs/
[[email protected] htdocs]# ls
index.html
[[email protected] htdocs]# mkdir rose huaer
[[email protected] htdocs]# ll
total 4
drwxr-xr-x. 2 root root 6 Aug 10 14:50 huaer
-rw-r--r--. 1 root root 45 Jun 12 2007 index.html
drwxr-xr-x. 2 root root 6 Aug 10 14:50 rose
[[email protected] htdocs]# chown apache.apache rose -R
[[email protected] htdocs]# chown apache.apache huaer -R
[[email protected] htdocs]# ll
total 4
drwxr-xr-x. 2 apache apache 6 Aug 10 14:50 huaer
-rw-r--r--. 1 root root 45 Jun 12 2007 index.html
drwxr-xr-x. 2 apache apache 6 Aug 10 14:50 rose
//创建网页并写入内容
[[email protected] htdocs]# echo ‘you are my rose, ran‘ > rose/index.html
[[email protected] htdocs]# echo ‘zhe huangye zui hou yi duo meigui‘ > huaer/index.html
//创建相应的网页日志目录并更改属主属组
[[email protected] ~]# mkdir /usr/local/apache/logs/{rose,huaer}
[[email protected] ~]# ls /usr/local/apache/logs/
huaer rose
[[email protected] ~]# chown -R apache.apache /usr/local/apache/logs/
[[email protected] ~]# ll /usr/local/apache/logs/
total 0
drwxr-xr-x. 2 apache apache 6 Aug 10 15:05 huaer
drwxr-xr-x. 2 apache apache 6 Aug 10 15:05 rose
//启动服务,关闭防火墙,查看端口起来没
[[email protected] ~]# /usr/local/apache/bin/httpd
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 128 :::8080 :::

LISTEN 0 128 :::80 :::
LISTEN 0 128 :::22 :::

验证效果
技术分享图片技术分享图片

2.不同ip相同端口
//在原先的配置文件基础上去掉端口8080
//添加一个新IP
[[email protected] ~]# ip addr add 192.168.56.14/24 dev ens32
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f5:a6:c5 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.13/24 brd 192.168.56.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.56.14/24 scope global secondary ens32
valid_lft forever preferred_lft forever
inet6 fe80::6c93:e6f0:10cc:2f84/64 scope link
valid_lft forever preferred_lft forever
//修改配置文件
<VirtualHost 192.168.56.13:80>
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/rose"
ErrorLog "/usr/local/apache/logs/rose/error_log"
CustomLog "/usr/local/apache/logs/rose/access_log" combined
<Directory /usr/local/apache/htdocs/rose>
<RequireAll>
</RequireAll>
</Directory>
</VirtualHost>

<VirtualHost 192.168.56.14:80>
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/huaer"
ErrorLog "/usr/local/apache/logs/huaer/error_log"
CustomLog "/usr/local/apache/logs/huaer/access_log" combined
<Directory /usr/local/apache/htdocs/huaer>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>

//重启服务
[[email protected] ~]# pkill httpd
[[email protected] ~]# /usr/local/apache/bin/httpd
技术分享图片技术分享图片

3.相同ip相同端口不同域名

//修改配置文件
<VirtualHost 192.168.56.13:80>
ServerName www.rose.com
DocumentRoot "/usr/local/apache/htdocs/rose"
ErrorLog "/usr/local/apache/logs/rose/error_log"
CustomLog "/usr/local/apache/logs/rose/access_log" combined
<Directory /usr/local/apache/htdocs/rose>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>

<VirtualHost 192.168.56.13:80>
ServerName www.huaer.com
DocumentRoot "/usr/local/apache/htdocs/huaer"
ErrorLog "/usr/local/apache/logs/huaer/error_log"
CustomLog "/usr/local/apache/logs/huaer/access_log" combined
<Directory /usr/local/apache/htdocs/huaer>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>

//解析域名 windos 系统 在C:WindowsSystem32driversetc中
苹果的在 /etc/hosts中
在C:WindowsSystem32driversetchosts文件中添加以下内容
192.168.56.13 www.rose.com
192.168.56.13 www.huaer.com
//验证结果

技术分享图片技术分享图片

以上是关于编译安装httpd实现三类虚拟主机的访问的主要内容,如果未能解决你的问题,请参考以下文章

LAMP编译实现

CentOS 6, 编译安装lamp (php-fpm)

Ansible-playbook实现Apache(httpd)编译安装及批量部署

Httpd访问控制虚拟主机网页压缩优化

WEB应用之httpd基础入门

linux架构学习第二十六天之web服务搭建(apache)以及https的实现