系统相关
首页 > 系统相关> > Linux源码编译安装apache httpd2.4.48

Linux源码编译安装apache httpd2.4.48

作者:互联网

Apache httpd

APR

编译前准备

准备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的用户

## 不用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服务并设为开机启动

创建服务

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                     *:*  

测试网页

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完成!

标签:httpd2.4,httpd,tar,48,apr,httpd24,apps,源码,apache
来源: https://blog.csdn.net/timonium/article/details/117326413