源码安装httpd
httpd介绍
httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。
httpd版本
本文主要介绍httpd的两大版本,httpd-2.2和httpd-2.4。
- CentOS6系列的版本默认提供的是httpd-2.2版本的rpm包
- CentOS7系列的版本默认提供的是httpd-2.4版本的rpm包
rpm包安装的httpd程序环境
文件/目录 | 对应的功能 |
---|---|
/var/log/httpd/access.log | 访问日志 |
/var/log/httpd/error_log | 错误日志 |
/var/www/html/ | 站点文档目录 |
/usr/lib64/httpd/modules/ | 模块文件路径 |
/etc/httpd/conf/httpd.conf | 主配置文件 |
/etc/httpd/conf.modules.d/*.conf | 模块配置文件 |
/etc/httpd/conf.d/*.conf | 辅助配置文件 |
httpd命令
//语法:httpd [options]
//常用的options:
-l //查看静态编译的模块,列出核心中编译了哪些模块。 \
//它不会列出使用LoadModule指令动态加载的模块
-M //输出一个已经启用的模块列表,包括静态编译在服务 \
//器中的模块和作为DSO动态加载的模块
-v //显示httpd的版本,然后退出
-V //显示httpd和apr/apr-util的版本和编译参数,然后退出
-X //以调试模式运行httpd。仅启动一个工作进程,并且 \
//服务器不与控制台脱离
-t //检查配置文件是否有语法错误
进入到centos镜像中然后拉取阿里源并进行备份
[root@7b2bedbda2f9 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 2495 100 2495 0 0 8910 0 --:--:-- --:--:-- --:--:-- 8910
[root@7b2bedbda2f9 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
安装epel源
[root@7b2bedbda2f9 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com 4.1 MB/s | 4.6 MB 00:01
CentOS-8.5.2111 - Extras - mirrors.aliyun.co 47 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun 3.9 MB/s | 8.4 MB 00:02
epel-release-latest-8.noarch.rpm 142 kB/s | 24 kB 00:00
Dependencies resolved.
=============================================================================
Package Architecture Version Repository Size
=============================================================================
将repo配置中的地址替换成aliyun镜像站地址
[root@7b2bedbda2f9 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@7b2bedbda2f9 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
httpd基础环境准备
[root@7b2bedbda2f9 yum.repos.d]# dnf -y install gcc gcc-c++ make wget
Last metadata expiration check: 0:01:43 ago on Mon 08 Aug 2022 01:55:03 PM UTC.
Package gcc-8.5.0-4.el8_5.x86_64 is already installed.
Package gcc-c++-8.5.0-4.el8_5.x86_64 is already installed.
Package make-1:4.2.1-10.el8.x86_64 is already installed.
Package wget-1.19.5-10.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
下载httpd所需的依赖包
[root@7b2bedbda2f9 yum.repos.d]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
[root@7b2bedbda2f9 yum.repos.d]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel
Last metadata expiration check: 0:05:05 ago on Mon 08 Aug 2022 01:55:03 PM UTC.
Dependencies resolved.
=============================================================================
Package Arch Version Repo Size
=============================================================================
Installing:
expat-devel x86_64 2.2.5-4.el8 base 55 k
............
xz-devel-5.2.4-3.el8.x86_64
zlib-devel-1.2.11-17.el8.x86_64
Complete!
创建一个系统名为apache的系统用户用来管理apache
[root@7b2bedbda2f9 yum.repos.d]# useradd -r apache
下载apr,apr-utils和httpd
[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# wget https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
--2022-08-08 14:09:36-- https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 180.101.197.240, 58.49.248.227, 58.49.248.233, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|180.101.197.240|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1093896 (1.0M) [application/octet-stream]
Saving to: ‘apr-1.7.0.tar.gz.1’
apr-1.7.0.tar.gz.1 100%[================>] 1.04M 3.70MB/s in 0.3s
2022-08-08 14:09:36 (3.70 MB/s) - ‘apr-1.7.0.tar.gz.1’ saved [1093896/1093896]
[root@7b2bedbda2f9 src]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
--2022-08-08 14:11:47-- https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 180.101.197.244, 58.42.55.236, 180.101.199.239, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|180.101.197.244|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 554301 (541K) [application/octet-stream]
Saving to: ‘apr-util-1.6.1.tar.gz’
apr-util-1.6.1.tar. 100%[================>] 541.31K 3.06MB/s in 0.2s
2022-08-08 14:11:47 (3.06 MB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301]
[root@7b2bedbda2f9 src]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz |head -5
--2022-08-08 14:07:12-- https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 58.49.248.230, 58.49.248.232, 180.101.197.238, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|58.49.248.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9743277 (9.3M) [application/octet-stream]
Saving to: ‘httpd-2.4.54.tar.gz’
httpd-2.4.54.tar.gz 100%[================>] 9.29M 4.87MB/s in 1.9s
2022-08-08 14:07:14 (4.87 MB/s) - ‘httpd-2.4.54.tar.gz’ saved [9743277/9743277]
[root@7b2bedbda2f9 src]# ls
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz debug httpd-2.4.54.tar.gz kernels
解压apr并进入其目录
[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# tar xf apr-1.7.0.tar.gz
[root@7b2bedbda2f9 src]# cd apr-1.7.0
进入configure注释或删除此行
[root@7b2bedbda2f9 apr-1.7.0]# vim configure
# $RM "$cfgfile"
生成makefile
[root@7b2bedbda2f9 apr-1.7.0]# ./configure --help|head
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
将apr编译到/usr/local/apr目录下
[root@7b2bedbda2f9 apr-1.7.0]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
...........
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
config.status: executing default commands
对apr进行编译并安装
[root@7b2bedbda2f9 apr-1.7.0]# make && make install
make[1]: Entering directory '/usr/src/apr-1.7.0'
/bin/sh /usr/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/usr/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/src/apr-1.7.0/include/arch/unix -I/usr/src/apr-1.7.0/include -I/usr/src/apr-1.7.0/include/private -I/usr/src/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
..............
/usr/bin/install -c -m 644 /usr/src/apr-1.7.0/build/${f} /usr/local/apr/build-1; \
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config
进入apr的安装目录下对其库文件与头部文件进行配置
[root@7b2bedbda2f9 apr-1.7.0]# cd /usr/local/apr/
[root@7b2bedbda2f9 apr]# ls
bin build-1 include lib
库文件设置
[root@7b2bedbda2f9 apr]# vim /etc/ld.so.conf.d/apr.conf
[root@7b2bedbda2f9 apr]# cat /etc/ld.so.conf.d/apr.conf
/usr/local/apr/lib
[root@7b2bedbda2f9 apr]# ldconfig
头部文件设置
[root@7b2bedbda2f9 apr]# ln -s /usr/local/apr/include/ /usr/include/apr
解压apr-util并进入其目录
[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# tar xf apr-util-1.6.1.tar.gz
[root@7b2bedbda2f9 src]# cd apr-util-1.6.1
配置相关的配置并生成makefile
[root@7b2bedbda2f9 apr-util-1.6.1]# ./configure --help |head -5
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
将apr-util编译到/usr/local/apr-util目录下 //注意因为apr-util依赖与apr所以要添加--with-apr=/usr/local/apr(apr的安装目录)
[root@7b2bedbda2f9 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
编译安装apr-util
[root@7b2bedbda2f9 apr-1.7.0]# make && make install
make[1]: Entering directory '/usr/src/apr-1.7.0'
/bin/sh /usr/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/usr/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/src/apr-1.7.0/include/arch/unix -I/usr/src/apr-1.7.0/include -I/usr/src/apr-1.7.0/include/private -I/usr/src/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
.........
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config
进入到apr-util的安装目录下配置其库文件与头部文件
[root@7b2bedbda2f9 apr-util-1.6.1]# cd /usr/local/apr-util/
[root@7b2bedbda2f9 apr-util]# ls
bin include lib
[root@7b2bedbda2f9 apr-util]# vim /etc/ld.so.conf.d/apr-util.conf
[root@7b2bedbda2f9 apr-util]# cat /etc/ld.so.conf.d/apr-util.conf
/usr/local/apr-util/lib
[root@7b2bedbda2f9 apr-util]# ln -s /usr/local/apr-util/include/ /usr/include/apr-util
[root@7b2bedbda2f9 apr-util]# ldconfig
解压httpd并进入其目录
[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# tar xf httpd-2.4.54.tar.gz
cd [root@7b2bedbda2f9 src]# cd httpd-2.4.54
[root@7b2bedbda2f9 httpd-2.4.54]#
生成makefile
[root@7b2bedbda2f9 httpd-2.4.54]# ./configure --help|head
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
将httpd编译到/usr/local/httpd目录下
[root@7b2bedbda2f9 httpd-2.4.54]# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-so --enable-ssl --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
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
..........
configure: summary of build options:
Server Version: 2.4.54
Install prefix: /usr/local/httpd
C compiler: gcc
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
编译并安装httpd
[root@7b2bedbda2f9 httpd-2.4.54]# make && make install
Making all in srclib
make[1]: Entering directory '/usr/src/httpd-2.4.54/srclib'
make[1]: Leaving directory '/usr/src/httpd-2.4.54/srclib'
Making all in os
.........
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: Leaving directory '/usr/src/httpd-2.4.54'
进入httpd安装目录并配置头部文件
[root@7b2bedbda2f9 httpd]# cd /usr/local/httpd/
[root@7b2bedbda2f9 httpd]# ls
bin cgi-bin error icons logs manual
build conf htdocs include man modules
[root@7b2bedbda2f9 src]# ln -s /usr/local/httpd/include /usr/include/httpd
设置环境变量
[root@7b2bedbda2f9 ~]# cd /usr/src
[root@7b2bedbda2f9 src]# echo 'export PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
[root@7b2bedbda2f9 src]# source /etc/profile.d/httpd.sh
[root@7b2bedbda2f9 src]# which httpd
/usr/local/httpd/bin/httpd
[root@7b2bedbda2f9 src]# which apachectl
/usr/local/httpd/bin/apachectl
[root@7b2bedbda2f9 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
启动apache
取消Servername前面的注释
[root@localhost ~]# cd /usr/local
[root@localhost local]# vim /etc/httpd/httpd.conf
ServerName www.example.com:80
[root@localhost local]# apachectl start
[root@localhost local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
将httpd服务加入systemd
root@localhost httpd-2.4.54]# vim /usr/lib/systemd/system/apache.service
[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/httpd/bin/apachectl
ExecStop=/usr/local/httpd/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start apache.service
[root@localhost ~]# systemctl enable --now apache.service
修改这个目录的内容就是修改你在浏览器看见的内容
[root@localhost ~]# vi /usr/local/httpd/htdocs/index.html
<html><body><h1>hello apache</h1></body></html>