源码安装httpd

APR概念
  APR主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。在早起的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数

  随着Apache的进一步开发,Apache组织决定将这些通用的函数独立出来并发发展成为一个新的项目,这样,APR的开发就从Apache中独立出来,Apache仅仅是使用APR而已

  目前APR主要是有Apache使用,由于APR的较好的移植性,因此一些需要进行移植的C程序也开始使用APR,开源项目比如用于服务器压力测试的flood loader tester

Apache的安装时建立在APR的基础上的,所以安装Apache就需要安装对应版本的APR版本

到官网下载源码包
APR源码包httpd源码包

  在早期的编译安装中,apr和apr-util是和http分开编译安装的,后来才支持的将解压后的apr-release和apr-util-release两个目录导入到httpd-release源码包目录下的srclib/目录下并分别命名为apr和apr-util。然后在./configure的时候只需要添加–with-included-apr这项就可以实现统一编译。能偷懒则偷懒!!!

实现步骤:
将文件上传到虚拟机,lrzsz包

1、解压:

tar xvf apr-1.7.0.tar.gz 
tar xvf apr-util-1.6.1.tar.gz 
tar xvf httpd-2.4.39.tar.gz

2、将apr两个对应的目录拷贝到http目2录下,编译时一起编译,不需要分开编译

mv apr-1.7.0 httpd-2.4.39/srclib/apr
mv apr-util-1.6.1 httpd-2.4.39/srclib/apr-util

3、安装依赖包

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

4、准备编译选项

./configure \
--prefix=/app/httpd \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-included-apr \
--with-zlib \
--with-pcre \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork

5、编译和安装

make -j 2 && make install

6、准备service文件,从其他相同版本中拷贝一份

vim /usr/lib/systemd/system/httpd.service		## 注意:copy过来的文件一定要放在此路径下

[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=notify
Type=simple
## 特别注意,此处的类型一定要是simple,如果是默认的notify,那么在使用systemctl启动的时候会等待,直到超时才会退出

EnvironmentFile=/etc/httpd/httpd.conf
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}

KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意:在编辑完service服务文件后需要先执行
systemctl daemon-reload
接着启动
systemctl start httpd

谨记:
摘抄
{
在设置Type=simple时,我曾设置过notify类型,但是发现在执行了systemc start httpd后,执行会一直等待,直到httpd服务启动超时

有大神解释道:如果我们使用的是默认的httpd配置文件,那必须保证配置文件中包含module_systemd,那个module为获取apache启动成功信息所必须的,但是我查了下对应的module好像是apache 2.5版本才有,我使用的是2.4版本。如果不能启动对应的module,可以使用simple类型。
}

7、默认编译安装的httpd使用的deamon用户和组,如果想修改,执行

useradd -r -s /sbin/nologin apache

# 修改配置文件安装上面编译的路径是/etc/httpd/httpd.conf,找到以下两项修改
User apache
Group apache

8、网页默认目录,编译安装的网页默认目录是/app/httpd/htdocs/,如果想修改,执行

mkdir /data/html/

# 修改配置文件/etc/httpd/httpd.conf,找到以下两项修改
DocumentRoot "/data/html"
<Directory "/data/html">

由此安装就完成


脚本实现httpd源码安装

安装前注意:
1.此脚本执行必须以source /path/isntall-httpd.sh 或者 . /path/install-httpd.sh执行,因为文中有切换目录的操作
2.可以先将源码包下载到对应目录,文件中有下载源码包,是注释的,也写明原因。
3.此脚本执行先进入到你希望的那个安装目录

脚本内容
vim install-httpd.sh

#!/bin/bash
#
# 下载源码包,下载http包时会花很长时间,所以此处注释
#wget https://apr.apache.org/download.cgi/apr-1.7.0.tar.gz
#wget https://apr.apache.org/download.cgi/apr-util-1.6.1.tar.gz
#wget http://archive.apache.org/dist/httpd/httpd-2.4.39.tar.gz
 

# 安装依赖包
for i in gcc pcre-devel openssl-devel expat-devel;do
  rpm -q $i
    if [ ! $? -eq 0 ];then
        yum install -y $i
    fi
done


# 将源码包解压缩

for j in apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.39.tar.gz;do
  if [ ! -d $j ];then
    tar -xf $j &> /dev/null
        if [ $? -eq 0 ];then
          echo "$j decompression finish.."
        else
          echo "Maybe $j file no exist or execute false.."
        fi
  fi
done


# 将apr和apr-util两个目录拷贝到httpd目录下
if [ ! -d httpd-2.4.39/srclib/apr ];then
  cp -r apr-1.7.0 httpd-2.4.39/srclib/apr
fi

if [ ! -d httpd-2.4.39/srclib/apr-util ];then
  cp -r apr-util-1.6.1 httpd-2.4.39/srclib/apr-util
fi

# 执行配置脚本./configure
if [ ! -d /etc/httpd ];then
cd httpd-2.4.39/

./configure \
--prefix=/app/httpd \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconfdir=/etc/httpd \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-included-apr \
--with-zlib \
--with-pcre \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork

# 编译并安装

if [ $? -eq 0 ];then 
  make && make install
fi
else 
        echo /etc/httpd/ exist
fi

# 准备serveice文件
if [ $? -eq 0 -a ! -e /usr/lib/systemd/system/httpd.service ];then       
echo '
[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=simple

EnvironmentFile=/etc/httpd/httpd.conf
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}

KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target ' > /usr/lib/systemd/system/httpd.service
else 
    echo 'httpd.service already exist..'
fi
# 重新加载service文件
if [ $? -eq 0 ];then 
  systemctl daemon-reload
fi

基本的安装已经完成,配置需要手动更改

发布了63 篇原创文章 · 获赞 0 · 访问量 2212

猜你喜欢

转载自blog.csdn.net/qq_43058911/article/details/103224625