源码安装Apache并搭建HTTP服务

搭建需求

  • 编译安装Httpd-2.4版本;
  • 配置三种不同风格的虚拟主机:
  • 相同IP不同端口;
  • 不同IP相同端口;
  • 相同IP相同端口不同域名。

搭建环境


搭建步骤

## 下载安装包 ##
[root@localhost ~]# curl -o httpd-2.4.34.tar.bz2 http://www-eu.apache.org/dist/httpd/httpd-2.4.34.tar.bz2
[root@localhost ~]# curl -o apr-1.6.5.tar.bz2  http://mirrors.hust.edu.cn/apache//apr/apr-1.6.5.tar.bz2
[root@localhost ~]# curl -o apr-util-1.6.1.tar.bz2 http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
  • 编译安装http服务
## 安装编译所用环境 ##
[root@localhost ~]# yum -y install gcc gcc-c++

## 将安装包进行解压 ##
[root@localhost ~]# tar -jxf httpd-2.4.34.tar.bz2
[root@localhost ~]# tar -jxf apr-1.6.5.tar.bz2 
[root@localhost ~]# tar -jxf apr-util-1.6.1.tar.bz2

## 进入apr-1.6.5,进行编译 ##
[root@localhost apr-1.6.5]# ./buildconf 
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.6.5]# make -j 2 && make install

## 进入apr-util-1.6.1进行编译 ##
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make -j 2
[root@localhost apr-util-1.6.1]# make install

##进入httpd-2.4.34进行编译 ##
[root@localhost httpd-2.4.34]# ./configure --prefix=/usr/local/apache \
> --sysconfdir=/etc/httpd \
> --enable-so \
> --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 \
> --with-included-apr  #不拷贝文件会出错,步骤请见下面“遇到的问题”
[root@localhost httpd-2.4.34]# make -j 2
[root@localhost httpd-2.4.34]# make install
  • 测试http是否安装成功
    • 启动http服务,打开网页,输入http server地址,我的地址是192.168.92.128,看到如下
      在这里插入图片描述

  • 配置三种不同风格的虚拟主机
    • 配置相同IP不同端口的http服务
## 编辑刚刚编译好的http,配置文件在/etc/httpd/http.conf,配置文件放在“--sysconfdir=/etc/httpd”指定的位置,添加如下配置文件 ##
Listen 80
Listen 8080

<VirtualHost 192.168.92.128:80>
        ServerName www.hello.com
        DocumentRoot "/var/http/html/hello"
        ErrorLog "/var/http/log/error_log"
        CustomLog "/var/http/log/access_Log" combined
        <Directory /var/http/html/hello>
                <RequireAll>
                        Require all granted
                </RequireAll>
        </Directory>
</VirtualHost>

<VirtualHost 192.168.92.128:8080>
        ServerName www.fine.com
        DocumentRoot "/var/http/html/fine"
        ErrorLog "/var/http/log/error_log"
        CustomLog "/var/http/log/access_log" combined
        <Directory /var/http/html/fine>
                <RequireAll>
                        Require all granted
                </RequireAll>
        </Directory>
</VirtualHost>

## 创建apache用户和组,创建配置文件中指定的目录和文件,并改变属主和属组 ##
[root@localhost ~]# groupadd -r apache
[root@localhost ~]# useradd -r -s /sbin/nologin -g apache apache
[root@localhost bin]# mkdir -p /var/http/html{/hello,/fine}
[root@localhost ~]# chown -R apache.apache /var/http/html/hello /var/http/html/fine
[root@localhost bin]# mkdir /var/http/log
[root@localhost bin]# touch /var/http/log/error_log
[root@localhost bin]# touch /var/http/log/access_log

## 测试配置文件是否正确 ##
apachectl -t 或者httpd -t

## 在指定的网页目录(/var/http/html/hello、/var/http/html/fine)中创建网页文件 ##
[root@localhost bin]# echo "hello,I'm hello." > /var/http/html/hello/index.html
[root@localhost bin]# echo "fine,My name is fine." > /var/http/html/fine/index.html

## 启动或者重新启动httpd服务 ##
/usr/local/bin/apachectl start
  • 到此相同IP不同端口的虚拟主机就配置完成,下面开始测试,
    • 测试192.168.92.128:80端口的,访问的试/var/http/html/hello/index.html文件的内容
      在这里插入图片描述
    • 测试192.168.92.128:8080端口的,访问的试/var/http/html/fine/index.html文件的内容
      在这里插入图片描述

  • 配置不同主机相同端口的http服务
## 修改上个实验添加的配置文件 ##
Listen 80
#Listen 8080    #注销或者删除监听端口8080

<VirtualHost 192.168.92.128:80>
        ServerName www.hello.com
        DocumentRoot "/var/http/html/hello"
        ErrorLog "/var/http/log/error_log"
        Customlog "/var/http/log/access_log" combined
        <Directory /var/http/html/hello>
                <RequireAll>
                        Require all granted
                </RequireAll>
        </Directory>
</VirtualHost>

<VirtualHost 192.168.92.129:80>   #将地址修改为另一个IP地址,端口改为80
        ServerName www.fine.com
        DocumentRoot "/var/http/html/fine"
        ErrorLog "/var/http/log/error_log"
        Customlog "/var/http/log/access_log" combined
        <Directory /var/http/html/fine>
                <RequireAll>
                        Require all granted
                </RequireAll>
        </Directory>
</VirtualHost>

## 保存退出,并重启服务 ##
[root@localhost ~]# apachectl restart

## 添加一个新的IP地址 ##
[root@localhost ~]# ip address add 192.168.92.129/24 dev eno16777736
  • 测试192.168.92.128:80端口的,访问的试/var/http/html/hello/index.html文件的内容
    在这里插入图片描述
  • 测试192.168.92.129:80端口的,访问的试/var/http/html/hello/index.html文件的内容
    在这里插入图片描述

  • 配置相同IP相同端口不同域名的http服务
## 修改上个实验添加的配置文件 ##
Listen 80

<VirtualHost 192.168.92.128:80>
        ServerName www.hello.com
        DocumentRoot "/var/http/html/hello"
        ErrorLog "/var/http/log/error_log"
        Customlog "/var/http/log/access_log" combined
        <Directory /var/http/html/hello>
                <RequireAll>
                        Require all granted
                </RequireAll>
        </Directory>
</VirtualHost>

<VirtualHost 192.168.92.128:80>     #修改IP地址,与上一个地址一致
        ServerName www.fine.com
        DocumentRoot "/var/http/html/fine"
        ErrorLog "/var/http/log/error_log"
        Customlog "/var/http/log/access_log" combined
        <Directory /var/http/html/fine>
                <RequireAll>
                        Require all granted
                </RequireAll>
        </Directory>
</VirtualHost>

## 修改windows配置文件C:\Windows\System32\drivers\etc,添加如下两行(直接编辑不能保存,需要借助软件工具,用nodepad++工具进行写入) ##
192.168.92.128	www.hello.com
192.168.92.128	www.fine.com
  • 测试www.hello.com端口的,访问的试/var/http/html/hello/index.html文件的内容
    在这里插入图片描述
  • 测试www.fine.com端口的,访问的试/var/http/html/hello/index.html文件的内容
    在这里插入图片描述

搭建遇到的问题

## 编译 apr 出现的错误 ##
[root@localhost apr-1.6.5]# ./buildconf 
buildconf: checking installation...
buildconf: python version 2.7.5 (ok)
buildconf: autoconf not found.
           You need autoconf version 2.59 or newer installed
           to build APR from SVN.
buildconf: libtool not found.
           You need libtool version 1.4 or newer installed
           to build APR from SVN.
[root@localhost apr-1.6.5]# yum -y install autoconf #安装autoconf包(解决方法)
[root@localhost apr-1.6.5]# yum -y install libtool #安装libtool包(解决方法)

## 编译 apr-util 出现错误 ##
[root@localhost apr-util-1.6.1]# make
make[1]: Entering directory `/root/apr-util-1.6.1'
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE   -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private  -I/usr/local/apr/include/apr-1    -o xml/apr_xml.lo -c xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
 #include <expat.h>
                   ^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/root/apr-util-1.6.1'
make: *** [all-recursive] Error 1
[root@localhost apr-util-1.6.1]# yum -y install expat-devel  #安装expat-devel包(解决方法)

## 编译 httpd 出现的错误 ##
错误一:(./configure)
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
[root@localhost httpd-2.4.34]# yum -y install pcre-devel #安装pcre-devel包(解决办法)
错误二:(make)
collect2: error: ld returned 1 exit status
make[2]: *** [htdigest] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/root/httpd-2.4.34/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/httpd-2.4.34/support'
make: *** [all-recursive] Error 1
解决办法:
## 先把apr-1.6.5、apr-util-1.6.1解压包拷贝到httpd-2.4.34/srclib/ ##
[root@localhost httpd-2.4.34]# cp -r /root/apr-1.6.5 /root/httpd-2.4.34/srclib/apr
[root@localhost httpd-2.4.34]# cp -r /root/apr-util-1.6.1 /root/httpd-2.4.34/srclib/apr-util
## 重新编译./configure后面加上“--with-included-apr” ##
[root@localhost httpd-2.4.34]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --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 --with-included-apr
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值