Linux源码编译安装apache httpd2.4.48

Posted 白-胖-子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux源码编译安装apache httpd2.4.48相关的知识,希望对你有一定的参考价值。

Apache httpd

  • httpd是Apache基金会的HTTP服务器项目:HTTP Server project
  • httpd-2.4系列本版,依赖于apr-1.4+, apr-util-1.4+
  • APR(Apache portable Run-time libraries)
  • Apache可移植运行库,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。

APR

  • APR官网:http://apr.apache.org
  • 在早期的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数随着Apache的进一步开发,
  • Apache组织决定将这些通用的函数独立出来并发展成为一个新的项目。
  • 这样,APR的开发就从Apache中独立出来,Apache仅仅是使用 APR而已。
  • 目前APR主要还是由Apache使用,由于APR的较好的移植性,
  • 因此一些需要进行移植的C程序也开始使用APR,开源项目:比如用于服务器压力测试的Flood loader tester,
  • 项目站点:http://httpd.apache.org/test/flood

编译前准备

准备httpd-2.4,apr-1.4+, apr-util-1.4+

下载并解压缩

wget https://downloads.apache.org/httpd/httpd-2.4.48.tar.gz && tar xf 2.4.48.tar.bz2
wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz && tar xf apr-1.7.0.tar.bz2
wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz && tar xf apr-util-1.6.1.tar.bz2
  • 批量下载
HTTPD=https://downloads.apache.org/httpd/httpd-2.4.48.tar.bz2
APR=https://downloads.apache.org/apr/apr-1.7.0.tar.bz2
ARPU=https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
for url in {$HTTPD,$APR,$APRU};do
{
wget $url
}&
done
wait
  • 批量解压缩
for z in *.tar.bz2; do tar xf $z; done

安装依赖包

yum -y install gcc make pcre-devel openssl-devel expat-devel

合并源码包进行编译

将apr 和apr-util源码与httpd 源码合并

cp -ar  ./apr-1.7.0 ./httpd-2.4.48/srclib/apr
cp -ar  ./apr-util-1.6.1 ./httpd-2.4.48/srclib/apr-util

cp时注意权限问题,如果不保留原始权限,则config时会报错

使用./configure脚本生成自定义Makefile

[ -a /apps/httpd24 ] || mkdir -p /apps/httpd24
cd httpd-2.4.48/
./configure \\
--prefix=/apps/httpd24 \\
--enable-so \\
--enable-ssl \\
--enable-cgi \\
--enable-rewrite \\
--with-zlib \\
--with-pcre \\
--with-included-apr \\
--enable-modules=most \\
--enable-mpms-shared=all \\
--with-mpm=prefork
  • 检查完毕结果
    Server Version: 2.4.48
    Install prefix: /apps/httpd24
    C compiler:     gcc
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

编译并安装

make -j 8 && make install
  • 编译安装结果
Installing configuration files
mkdir /apps/httpd24/conf
mkdir /apps/httpd24/conf/extra
mkdir /apps/httpd24/conf/original
mkdir /apps/httpd24/conf/original/extra
Installing html documents
mkdir /apps/httpd24/htdocs
Installing error documents
mkdir /apps/httpd24/error
Installing icons
mkdir /apps/httpd24/icons
mkdir /apps/httpd24/logs
Installing CGIs
mkdir /apps/httpd24/cgi-bin
Installing header files
Installing build system files
Installing man pages and online manual
mkdir /apps/httpd24/man
mkdir /apps/httpd24/man/man1
mkdir /apps/httpd24/man/man8
mkdir /apps/httpd24/manual
make[1]: Leaving directory '/root/httpd-2.4.48'

编译安装后配置

创建用户

useradd -s /sbin/nologin -r apache
id apache &> /dev/null || useradd -r -u 80 -d /webDB/ -s /sbin/nologin apache

指定运行httpd的用户

  • 编辑httpd.conf配置文件
  • 修改默认用户和组daemon为apache
## 不用sed不舒服系列
sed -ri.bak "s/daemon/apache/g" /apps/httpd24/conf/httpd.conf
sed -ri.bak 's/daemon/apache/' /apps/httpd24/conf/httpd.conf
  • 这个整行替换更为妥当
sed -ri.bak 's/^User.*/User apache/' /apps/httpd24/conf/httpd.conf
sed -ri.bak 's/^Group.*/Group apache/' /apps/httpd24/conf/httpd.conf

配置环境变量

 echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd.sh && . /etc/profile.d/httpd.sh

运行httpd服务并设为开机启动

创建服务

  • 通用配置创建service unit文件,设置开机启动
cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=/apps/httpd24/bin/apachectl start
ExecReload=/apps/httpd24/bin/apachectl graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
  • 设置为开机启动
systemctl daemon-reload && systemctl enable --now httpd.service

添加防火墙规则

#检查firewalld状态
firewall_status=`firewall-cmd --state`
if [ $firewall_status = running ];then
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload
fi

测试httpd是否正常工作

查看httpd版本

[root@C8-192 httpd-2.4.48]# httpd -v
Server version: Apache/2.4.48 (Unix)
Server built:   May 27 2021 20:39:03

查看服务运行状态

systemctl status httpd

 systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-05-27 20:53:37 CST; 1min 42s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 50580 ExecStart=/apps/httpd24/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 50583 (httpd)

查看端口开启状态

ss -ntl

ss -ntl | grep 80
LISTEN   0         128                       *:80                     *:*  

测试网页

  • yum安装默认网站根目录在 /var/www/html/index.html
  • 编译安装默认网站根目录在/安装目录/htdocs/index.html
  • 本机测试
sed -ri.bak`date +%F` 's/^.*/IamWork/g' /apps/httpd24/htdocs/index.html 
curl -I 127.0.0.1
  • 使用其他服务器测试
curl -I 10.0.0.192
HTTP/1.1 200 OK
  • 示例
[root@C8-192 httpd-2.4.48]# sed -ri.bak`date +%F` 's/^.*/IamWork/g' /apps/httpd24/htdocs/index.html 
[root@C8-192 httpd-2.4.48]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Date: Thu, 27 May 2021 13:00:36 GMT
Server: Apache/2.4.48 (Unix)
Last-Modified: Thu, 27 May 2021 13:00:01 GMT
ETag: "8-5c34f5651d891"
Accept-Ranges: bytes
Content-Length: 8
Content-Type: text/html

[13:00:45 root@C8-88[ ~]#curl 10.0.0.192
IamWork
[13:00:59 root@C8-88[ ~]#curl -I 10.0.0.192
HTTP/1.1 200 OK
Date: Thu, 27 May 2021 13:01:09 GMT
Server: Apache/2.4.48 (Unix)
Last-Modified: Thu, 27 May 2021 13:00:01 GMT
ETag: "8-5c34f5651d891"
Accept-Ranges: bytes
Content-Length: 8
Content-Type: text/html

至此,编译安装apache httpd完成!

以上是关于Linux源码编译安装apache httpd2.4.48的主要内容,如果未能解决你的问题,请参考以下文章

Linux Apache服务

Linux编译安装apache httpd2.4 make报错

centos6.9源码编译httpd2.4

apache编译安装 httpd 2.2 httpd 2.4

Linux Apache 编译安装;

httpd2.4源码编译