首页 > 编程笔记 > Linux笔记 阅读:21

安装Apache的两种方式(非常详细)

Apache 的安装方式和 Nginx 一样,分别是通过编译源码包和通过软件包管理器安装二进制包。

编译源码包的方式,只要有对应的编译环境,无论什么样的操作系统都可以部署上,而且还可以自定义功能(通过编译选项来控制)。若通过软件包管理器安装二进制包,需要指定对应的操作系统及版本,每种操作系统的不同版本所对应的二进制包也是不一样的,不过这可以通过 Linux 操作系统中的软件包管理器帮助解决。

需要注意的是,Apache 的主程序名称叫作 httpd,也就是说安装 httpd 的过程就是安装 Apache 的过程,Apache 是服务的名称,而 httpd 是提供该服务的主程序。

通过软件包管理器安装二进制包

CentOS 7 上使用的软件包管理器是 Yum,而 CentOS 8 和 Rocky 操作系统虽然也可以使用 Yum,但还是推荐大家使用 DNF,这里的演示是在 CentOS 8 系统上进行的,因现在 CentOS 8 停止维护,所以大家使用 Rocky 系统即可,命令执行的过程与结果是一致的。

安装二进制包的过程非常简单,确保服务器连接到网络并配置好了源仓库,接下来输入执行命令“dnf install httpd”即可。

【实例】通过软件包管理器安装二进制包。
[root@linux ~]# cat /etc/centos-release  #查看操作系统版本号
CentOS Linux release 8.4.2105

[root@linux ~]# dnf install httpd  #安装 Apache 服务
CentOS Linux 8 - AppStream    2.8 kB/s | 4.3 kB     00:01
CentOS Linux 8 - AppStream    6.2 MB/s | 8.4 MB     00:01
CentOS Linux 8 - BaseOS       6.5 kB/s | 3.9 kB     00:00
CentOS Linux 8 - BaseOS       1.7 MB/s | 4.6 MB     00:02
CentOS Linux 8 - Extras       643 B/s   | 1.5 kB     00:02
CentOS Linux 8 - Extras       15 kB/s   | 10 kB      00:00
#注:上面这一段是在检查和更新 yum 源仓库
依赖关系解决
==================================================================================
软件包          架构       版本                仓库                      大小
==================================================================================
安装:
httpd            x86_64     2.4.37-43.module_e18.5.0+1022+b541f3b1  appstream  1.4 M
安装依赖关系:
apr             x86_64     1.6.3-12.el8         appstream  129 k
apr-util        x86_64     1.6.1-6.el8          appstream  105 k
centos-logos-httpd noarch   85.8-2.el8          baseos      75 k
httpd-filesystem noarch   2.4.37-43.module_e18.5.0+1022+b541f3b1  appstream  39 k
httpd-tools      x86_64     2.4.37-43.module_e18.5.0+1022+b541f3b1  appstream  107 k
mod_http2        x86_64     1.15.7-3.module_e18.4.0+778+c970deab  appstream  154 k
安装弱的依赖:
apr-util-bdb     x86_64     1.6.1-6.el8          appstream  25 k
apr-util-openssl  x86_64     1.6.1-6.el8          appstream  27 k
启用模块流:
httpd            2.4
事务概要
===============================================================================
安装 9 软件包

----省略部分内容----
已安装:
apr-1.6.3-12.el8.x86_64
apr-util-1.6.1-6.el8.x86_64
apr-util-bdb-1.6.1-6.el8.x86_64
apr-util-openssl-1.6.1-6.el8.x86_64
centos-logos-httpd-85.8-2.el8.noarch
----省略部分内容----

[root@linux ~]# rpm -qa|grep httpd  #检查是否安装 Apache
httpd-filesystem-2.4.37-43.module_e18.5.0+1022+b541f3b1.noarch
httpd-tools-2.4.37-43.module_e18.5.0+1022+b541f3b1.x86_64
httpd-2.4.37-43.module_e18.5.0+1022+b541f3b1.x86_64
centos-logos-httpd-85.8-2.el8.noarch

[root@linux ~]# httpd -v  #查看 Apache 的版本
Server version: Apache/2.4.37 (centos)
Server built:   Nov 12 2021 04:57:27

[root@linux ~]# apachectl start  #启动 Apache 服务
[root@linux ~]# apachectl status  #查看 httpd 程序状态
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-01-04 16:05:08 CST; 2s ago
Docs: man:httpd.service(8)
Main PID: 32635 (httpd)
    Status: "Started, listening on: port 80"
----省略部分内容----
[root@linux ~]#

至此,Apache 服务就安装完成了,接下来我们通过浏览器访问一下新安装的 Apache 服务,如下图所示。


图 1 HTTP服务器测试页

图 1 是 Apache 的欢迎界面,出现这个页面就表示 Apache 已经安装成功。

接下来介绍几个与 httpd 服务控制相关的命令:

1) httpd 自带的服务控制脚本: 2) 启动 Apache 服务:systemctl start httpd。
3) 重载 Apache 服务:systemctl restart httpd。
4) 停止 Apache 服务:systemctl stop httpd。
5) 开机自启动:systemctl enable httpd。

安装完成后,与 httpd 相关的目录和配置文件的默认分布见下表。

表:与httpd相关的目录和配置文件的默认分布
文件路径 类型 作用
/etc/httpd/conf/httpd.conf 主配置文件  
/etc/httpd/conf.d/*.conf 扩展配置文件  
/var/www/html/ 默认网站根目录 默认存放网站的目录,即网站的根目录
/var/log/httpd/ 日志存放目录 httpd 运行时产生的访问日志、错误日志等都在此目录中
/usr/lib64/httpd/modules/ 模块文件存放目录 httpd 所有的功能模块默认都存放在此目录中
/etc/httpd/conf.modules.d/ 模块配置文件存放目录 单独对某一模块进行配置时需要修改这里对应的配置文件
/usr/share/doc/httpd 文档存放目录 与 httpd 相关的各种文档默认存放在此目录中
/var/cache/httpd 缓存目录  
/usr/share/man 帮助手册  

如果仔细观察安装过程容易发现,Apache 在安装的过程中默认会把工具包 httpd-tools 装上,这个工具包有许多实用的工具:

通过编译源码包安装Apache

从官网下载最新的稳定版源码包并上传到服务器中。在正式编译安装前需要先将编译时所需的环境准备好,一条简单的安装编译环境的命令如下:
dnf -y install gcc gcc-c++apr-devel apr-util-devel pcre pcre-devel openssl openssl-devel zlib-devel make redhat-rpm-config
通过编译源码包安装 Apache 服务的具体步骤如下:

1) 安装编译源代码时所需的依赖环境并创建安装目录:
[root@linux ~]# dnf -y install gcc gcc-c++ apr-devel apr-util-devel pcre pcre-devel openssl openssl-devel zlib-devel make redhat-rpm-config
依赖关系解决
====================================================================================================
软件包          架构       版本                仓库                      大小
====================================================================================================
安装:
apr-devel       x86_64     1.6.3-12.el8         appstream  246 k
apr-util-devel  x86_64     1.6.1-6.el8          appstream   86 k
gcc             x86_64     8.5.0-4.el8_5        appstream   23 M
gcc-c++         x86_64     8.5.0-4.el8_5        appstream    12 M
----省略部分内容----

事务概要
====================================================================================================
安装 35 软件包
升级 22 软件包
----省略部分内容----
完毕!

[root@linux opt]# mkdir /usr/local/httpd

2) 将下载的源码包上传至服务器中,并进行解压。
[root@linux opt]# ll
总用量 9496
-rw-r--r--. 1 root root 9719976 1月  3 22:50 httpd-2.4.52.tar.gz

[root@linux opt]# tar xf httpd-2.4.52.tar.gz
[root@linux opt]# cd httpd-2.4.52/
[root@linux httpd-2.4.52]# pwd
/opt/httpd-2.4.52
[root@linux httpd-2.4.52]#

3) 进入解压后的目录下,通过 configure 关键字加编译选项来检测目前操作系统的环境是否支持本次的编译安装。

通过 configure 关键字加编译选项可以检测目前的平台是否符合编译的要求,通过 ./configure --help 命令可以看到所有支持的编译选项及对应的功能解释。
[root@linux httpd-2.4.52]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-deflate \
> --enable-expires \
> --enable-headers \
> --enable-modules=most \
> --enable-so \
> --with-mpm=worker \
> --enable-rewrite

checking for chosen layout... Apache
checking for working mkdir -p... yes
----省略部分内容----
config.status: executing default commands
configure: summary of build options:

Server Version: 2.4.52
Install prefix: /application/apache2.4.6
C compiler: gcc
CFLAGS:    -pthread
CPPFLAGS:  -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E

[root@linux httpd-2.4.52]#
如果检查到当前的平台不符合编译要求,缺少某个软件环境,这里会报错并给出错误提示,我们根据错误提示安装对应的软件环境即可。

4) 若检测到目前的平台符合编译的要求,则进行编译和安装操作。
[root@linux httpd-2.4.52]# make && make install
----省略部分内容----
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: 离开目录“/opt/httpd-2.4.52”
[root@linux httpd-2.4.52]# echo $?
0
[root@linux httpd-2.4.52]# ls /usr/local/httpd/  #安装完成!
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules

[root@linux httpd-2.4.52]# cd /usr/local/httpd/
[root@linux httpd]# ./bin/apachectl start  #启动 Apache 服务
[root@linux httpd]# ./bin/httpd -V  #查看 httpd 的详细信息
Server version: Apache/2.4.52 (Unix)
Server built:   Jan 3 2022 23:51:09
Server's Module Magic Number: 20120211:121
Server loaded:  APR 1.6.3, APR-UTIL 1.6.1
Compiled using: APR 1.6.3, APR-UTIL 1.6.1
Architecture:   64-bit
Server MPM:     worker        #httpd 采用的多路处理模块(MPM)名称
    threaded:    yes (fixed thread count)
    forked:      yes (variable process count)

若在执行启动 Apache 服务的命令时报下列错误:
[root@linux bin]# ./apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using linux.com. Set the 'ServerName' directive globally to suppress this message
[root@linux bin]# cd ..
[root@linux httpd]# vim conf/httpd.conf
解决办法:将 Apache 主配置文件 httpd.conf 中“#ServerName www.example.com:80”前面的 # 去掉,换成自己的域名或IP地址。例如,修改为“ServerName localhost:80”或“ServerName 127.0.0.1:80”。

接下来介绍编译安装时一些常用的编译选项:

参数 功能
--prefix=PREFIX 指定默认安装目录,如果不指定安装路径则默认安装在/usr/local/apache2目录下。
--bindir=DIR 指定二进制可执行文件安装目录。
--sbindir=DIR 指定可执行文件安装目录。
--includedir=DIR 指定头文件安装目录。
--enable-deflate 提供内容的压缩传输编码支持,一般html/js/css等内容的站点,使用此参数功能可以大大提高传输速度。
--enable-expires 允许通过配置文件控制HTTP头的“Expires:”和“Cache-Control:”等内容,即对网站图片、js和css等内容,提供在客户端浏览器缓存的设置。
--enable-headers 提供允许对HTTP请求头的控制。
--enable-modules=most 指定安装DSO(动态共享对象)动态库用来通信。用most命令可以将一些不常用的、不在缺省常用模块中的模块编译进来。
--enable-so 激活Apahce服务的DSO支持,即在以后可以以DSO的方式编译安装共享模块(这个模块本身不能以DSO方式编译)。so模块是用来提供DSO支持的Apache核心模块。
--enable-ssl SSL/TLS support (mod_ssl).
--enable-cgi 支持CGI脚本功能。
--with-mpm= 指定服务器默认支持哪一种MPM模块,有prefork、worker、event这3种。
--enable-rewrite 提供基于URL规则的重写功能,伪静态功能基于它实现。
--enable-mpms-shared=all 当前平台选择以MPM加载动态模块并以DSO动态库方式创建。

Apache 的两种安装方式,笔者比较倾向于编译源码包的安装方式,因为可以进行各种自定义,不过其过程会比 Yum/DNF 稍微复杂一些,可以把编译源码包的整个步骤写成 Shell 脚本,以后在安装时只需要执行 Shell 脚本即可。

相关文章