系统环境

标题 版本 备注
系统版本 ubuntu 22.04.3 LTS (Jammy Jellyfish) -
内核版本 5.15.0-88-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux -
Apache v2.4.58 -

Apache 简介

Apache是一个开源的跨平台的Web服务器软件,全称为Apache HTTP Server。它是最流行的Web服务器软件之一,被广泛用于搭建和管理网站。Apache具有稳定性高、安全性好、性能优异等特点,支持多种操作系统,包括Linux、Unix、Windows等。Apache提供了丰富的模块和插件,可以扩展其功能,满足各种需求,如虚拟主机、SSL支持、URL重写、反向代理等。同时,Apache还具有灵活的配置选项和强大的性能优化功能,使得用户可以根据自己的需求进行定制和调整。由于其开源免费的特点,Apache被广泛应用于互联网领域,成为许多网站和应用的首选Web服务器软件。

官方网站:https://httpd.apache.org/download.cgi

建议: 服务器选非大陆地区的,这样可以避免很多不必要的麻烦(比如域名备案等)

Apache 安装

注意:以下操作都在 root 用户下执行

1.安装系统必要的编译环境包:

1
apt install -y gcc make libtool-bin libexpat1-dev libpcre++-dev zlib1g-dev libxml2-dev libssl-dev libxml++2.6-dev 

编译安装 apr:

1.从 apache 官方站点下载 apr 最新版本到 /usr/local/src/ 目录下;

1
wget -O /usr/local/src/apr-1.6.5.tar.gz https://downloads.apache.org/apr/apr-1.6.5.tar.gz

2.将下载好的文件解压在下载目录:

1
tar -zxf /usr/local/src/apr-1.6.5.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/apr-1.6.5

4.配置 configure 参数:

1
./configure --prefix=/usr/local/apr

5.执行 make 编译:

1
make -j $(nproc)

6.执行 make install 进行安装:

1
make install

编译安装 apr-util :

1.从 apache 官方站点下载 apr-util 最新版本到 /usr/local/src/ 目录下;

1
wget -O /usr/local/src/apr-util-1.6.3.tar.gz https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz

2.将下载好的文件解压在下载目录:

1
tar -zxf /usr/local/src/apr-util-1.6.3.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/apr-util-1.6.3/

4.配置 configure 参数:

1
./configure --prefix=/usr/local/apr_util --with-apr=/usr/local/apr

5.执行 make 编译:

1
make -j $(nproc)

6.执行 make install 进行安装:

1
make install

httpd 编译安装:

点击这里获取 apache 下载地址

1.从 apache 官方站点下载 httpd 最新版本到 /usr/local/src/ 目录下;

1
wget -O /usr/local/src/httpd-2.4.58.tar.gz https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz

2.将下载好的文件解压在下载目录:

1
tar -zxf /usr/local/src/httpd-2.4.58.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/httpd-2.4.58/

4.配置 configure 参数:

1
./configure --prefix=/usr/local/apache2  --enable-mods-shared=most --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr_util --enable-so

5.执行 make 编译:

1
make -j $(nproc)

6.执行 make install 进行安装:

1
make install

7.安装完成,在 /usr/local/ 目录下生成了 apache2 目录了。此时我们就可以配置 apache 的系统环境了。在 /etc/profile.d/ 目录下新建 apache2.sh 文件,内容为:

1
2
3
cat << 'EOF' > /etc/profile.d/apache2.sh
PATH=$PATH:/usr/local/apache2/bin
EOF

8.执行命令 source /etc/profile.d/apache2.sh 加载下 Apache 环境变量,使其立即生效:

1
source /etc/profile.d/apache2.sh

9.在 /lib/systemd/system/ 目录下创建启动脚本文件 apache2.service, 内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cat << 'EOF' > /lib/systemd/system/apache2.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/

[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/local/apache2/bin/apachectl start
ExecStop=/usr/local/apache2/bin/apachectl graceful-stop
ExecReload=/usr/local/apache2/bin/apachectl graceful
KillMode=mixed
PrivateTmp=true
Restart=on-abort

[Install]
WantedBy=multi-user.target
EOF

10.执行命令 systemctl daemon-reload 加载下启动脚本文件

1
systemctl daemon-reload

11.编辑 apache 主配置文件 /usr/local/apache2/conf/httpd.conf ,将 #ServerName www.example.com:80 启用并修改成:ServerName localhost, 目的是为了防止每次执行 apachectl -t 检查配置的时候报:AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 192.168.3.103. Set the ‘ServerName’ directive globally to suppress this message

1
sed -i 's@#ServerName www.example.com:80@ServerName localhost@' /usr/local/apache2/conf/httpd.conf

12.执行 apachectl -t 检查下 apache 配置是否有误:

1
2
root@ubuntu003:/usr/local/src/httpd-2.4.58# apachectl -t
Syntax OK

13.启动 apache 服务并将其加入开机启动:

1
2
root@ubuntu003:/usr/local/src/httpd-2.4.58# systemctl enable --now apache2.service
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.

14.启动完成,执行命令 systemctl status apache2.service 查看 Active 的状态是否为 running ,如果是,则代表 apache 启动成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@ubuntu003:/usr/local/src/httpd-2.4.58# systemctl status apache2.service 
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2024-03-02 17:13:37 CST; 1min 44s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 281350 ExecStart=/usr/local/apache2/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 281353 (httpd)
Tasks: 82 (limit: 1939)
Memory: 6.2M
CPU: 36ms
CGroup: /system.slice/apache2.service
├─281353 /usr/local/apache2/bin/httpd -k start
├─281354 /usr/local/apache2/bin/httpd -k start
├─281355 /usr/local/apache2/bin/httpd -k start
└─281356 /usr/local/apache2/bin/httpd -k start

Mar 02 17:13:37 iZ7xv944qlwx78avfc2zlmZ systemd[1]: Starting The Apache HTTP Server...
Mar 02 17:13:37 iZ7xv944qlwx78avfc2zlmZ systemd[1]: Started The Apache HTTP Server.

报错及解决方法

make 错:

错误一:make[2]: *** [Makefile:48:htpasswd] 错误 1

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCode'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDeclHandler'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_ParserCreate'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_SetCharacterDataHandler'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_ParserFree'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_SetUserData'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_StopParser'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_Parse'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString'
/usr/bin/ld: /usr/local/apr_util/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler'
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:48:htpasswd] 错误 1
make[2]: 离开目录“/usr/local/src/httpd-2.4.58/support”
make[1]: *** [/usr/local/src/httpd-2.4.58/build/rules.mk:75:all-recursive] 错误 1
make[1]: 离开目录“/usr/local/src/httpd-2.4.58/support”
make: *** [/usr/local/src/httpd-2.4.58/build/rules.mk:75:all-recursive] 错误 1

解决方法:

参考:[‘【解决问题】make[2]: [htpasswd] Error 1 make[2]: Leaving directory `/usr/local/src/httpd-2.4.51/support’](https://blog.csdn.net/weixin_44436677/article/details/122079984)

1.确保先安装好了所有依赖,即:

1
apt install -y gcc make libtool-bin libexpat1-dev libpcre++-dev zlib1g-dev libxml2-dev libssl-dev libxml++2.6-dev 

ubuntu 23.10 上已经没有 libpcre++-dev 包了,使用 libpcre2-dev 进行替代

2.删除已编译安装好的 apr_util,然后重新编译安装:

1
2
rm -rf /usr/local/apr_util
cd /usr/local/src/apr-util-1.6.3/ && make clean && ./configure --prefix=/usr/local/apr_util --with-apr=/usr/local/apr && make && make install

3.进入 apache 源码解压目录,重新编译安装即可

1
cd /usr/local/src/httpd-2.4.58 && make clean && ./configure --prefix=/usr/local/apache2  --enable-mods-shared=most --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr_util --enable-so && make && make install

故障分析:

之所以报这个错,是因为在安装 apr-util 之后才安装 libxml2-dev(libxml2-dev 必须安装在 apr_util 之前)