原文:https://www.s4lm0x.com/archives/40.html
https://www.cnblogs.com/sunshine-H/p/8110608.html----超详细 值得收藏 linux CentOS 7 配置Apache服务【转发+新增】
https://blog.51cto.com/13525470/2070375----CentOS 7 Apache服务的安装与配置
httpd
是使用排名第一的Web服务器软件. 它可以运行在几乎所有广泛使用的计算机平台上, 由于其跨平台和安全性被广泛使用, 是最流行的Web服务器端软件之一, 当前httpd版本为2.4.38
. 在CentOS 7
上使用yum
方式安装httpd是最简便的方式. 但是由于CentOS 7
自带的htpd版本固定, 有时我们可能需要安装更新版本的httpd, 或者需要开启httpd默认未开启的功能模块, 此时编译httpd源码的安装方式将是我们必须掌握的技能. 本文主要描述基于CentOS7. 6
以源码方式安装httpd-2.4.38
编译安装的步骤与优点
-
源码的编译安装一般由3个步骤组成
- 配置: configure, 通常依赖gcc编译器,
binutils
,glibc
, 配置软件特性, 检查编译环境, 生成 Makefile文件- configure是一个可执行脚本, 它有很多选项, 在待安装的源码路径下使用命令
./configure –-help
输出详细的选项列表
- configure是一个可执行脚本, 它有很多选项, 在待安装的源码路径下使用命令
- 编译: make
- 安装: make install
- 配置: configure, 通常依赖gcc编译器,
-
编译安装的优点
- 自定义软件功能
- 优化编译参数,提高性能
- 解决不必要的软件间依赖
- 方便清理与卸载
准备编译环境
操作系统安装时为
Minimal install
方式, 此时需要安装编译器以及编译器所依赖的软件包. 最直接的方式是将Development Tools
整个卡发包组安装, 但开发包组中很多的包是不必要的, 故此只安装必要的软件包
-
使用阿里云的源安装相关编译环境
cd /etc/yum.repos.d curl -sO https://mirrors.aliyun.com/repo/epel-7.repo https://mirrors.aliyun.com/repo/Centos-7.repo yum clean all yum makecache fast
-
安装gcc以及相关依赖
yum install -y gcc glibc-devel zlib zlib-devel openssl-devel bzip2 bzip2-devel gcc-c++ expat-devel
安装依赖包
编译安装pcre
PCRE
是Perl Compatible Regular Expression的缩写, 是一个Perl的正则表达式模块. HTTP核心模块和rewrite模块都会用到PCRE的正则表达式语法, 所以这个必须安装
cd /usr/local/src
curl -sO https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.bz2
tar xf pcre-8.43.tar.bz2
pcre-8.43
./configure --prefix=/usr/local/pcre
make -j 4
make install
编译安装apr, apr-util
httpd可在windows, linux, unix等多种平台部署, 而并不需要为每种平台都编写不同的代码实现, 因为有apr、apr-util
apr
: apache portable runtime, 类似于JVM, PVM等虚拟机, 为apache提供运行环境. 针对不同平台有不同的apr, httpd通过不同的apr,可运行于不同的平台之上
apr-util
依赖于apr, 需先编译安装apr, 再编译安装apr-util
- 获取源码包并解压编译
cd ..
curl -sO http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.bz2
tar xf apr-1.6.5.tar.bz2
cd apr-1.6.5
./configure --prefix=/usr/local/apr
make -j 4 && make install
cd ..
curl -sO http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
tar xf apr-util-1.6.1.tar.bz2
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
编译安装httpd
在httpd 2.4 中,
prefork
,worker
,event
作为模块, 可使用配置文件切换在执行配置过程时可使用disable, without, 显式地指定禁用哪些功能, 不依赖哪些程序包等
httpd 2.4中运行httpd的user, group也变成了daemon而不是apache, 所以在编译之前无需创建apache用户与组
- 获取源码包并解压
cd ..
curl -sO http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.38.tar.bz2
tar xf httpd-2.4.38.tar.bz2
cd httpd-2.4.38
- 编译前, 隐藏版本信息
- 大概是在
include/ap_release.h
文件中的第40-46行
- 大概是在
vim include/ap_release.h
#define AP_SERVER_BASEVENDOR "Apache Software Foundation" #服务的供应商名称
#define AP_SERVER_BASEPROJECT "Apache HTTP Server" #服务的项目名称
#define AP_SERVER_BASEPRODUCT "Apache" #服务的产品名
#define AP_SERVER_MAJORVERSION_NUMBER 2 #主要版本号
#define AP_SERVER_MINORVERSION_NUMBER 4 #小版本号
#define AP_SERVER_PATCHLEVEL_NUMBER 6 #补丁级别
- 编译安装
./configure --prefix=/usr/local/httpd24 --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-layout=Apache --enable-load-all-modules --enable-so --enable-rewrite --enable-ssl --enable-cgi --with-zlib --enable-proxy --enable-proxy-http --disable-version
make -j 4
make install
如果编译安装报错:
make[2]: *** [ab] 错误 1
make[2]: Leaving directory `/software/httpd-2.4.9/support'
make[1]: *** [all-recursive] 错误 1
make[1]: Leaving directory `/software/httpd-2.4.9/support'
make: *** [all-recursive] 错误 1
1.检查configure有没有添加--with-ssl=/usr/local/openssl-1.0.1g(重新编译安装后openssl的路径)的路径,添加后可解决以上报错的问题。
./configure --prefix=/home/apache-249 --enable-ssl --with-ssl=/usr/local/openssl-1.0.1g --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-rewrite --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event
如果不使用ssl,也可以在编译时候去掉--enable-ssl --with-ssl=/usr/local/openssl-1.0.1g,加上--disable-ssl 。
- 编译时的选项
- —enable-so: 支持动态加载模块
- —enable-rewrite: 启用URL重写
- —enable-ssl: 支持ssl加密
- -–enable-mpms-shared=[prefork | worker | event | all]: 指定编译哪些MPM模块, 可使用all表示全编译
- –with-mpm=[prefork | worker | event]: 如3种MPM模块都编译了,则通过此项指定默认使用哪种
- –enable-cgi: 启用cgi
- –enable-modules=[all | most | ……]: 指定启用哪些模块。可指定具体的模块名,也可使用all表示启用所有模块,most表示启用大多数常用模块。一般使用most即可
- –with-zlib: 依赖zlib库用于页面压缩
- –with-pcre: 指定pcre的路径,依赖pcre库用于解决正则表达式问题
- -–with-apr: 指定依赖的apr的路径
- –with-apr-util: 指定依赖的apr-util的路径
- —enable-proxy: 启用代理模块
- —disable-version:
配置httpd
配置环境变量
- 将
/usr/local/httpd24/bin
加入到系统PATH搜索路径中
cd /usr/local/httpd24/bin
echo 'PATH=$PATH:/usr/local/httpd24/bin' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
- 配置firewalld, 允许
http
服务被访问
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
- 启动httpd服务
apachectl start
curl 127.0.0.1
为httpd添加Unit file
- 创建httpd的Unit file, 使用systemd管理http服务
vim /usr/lib/systemd/system/httpd.service
[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
EnvironmentFile=/usr/local/httpd24/conf/httpd.conf
ExecStart=/usr/local/httpd24/bin/apachectl $OPTIONS -DFOREGROUND
ExecReload=/usr/local/httpd24/bin/apachectl $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 重新生成Unit之间的依赖关系
systemctl daemon-reload
apachectl stop
systemctl start httpd
curl 127.0.0.1