42 openEuler搭建Apache服务器-概述和管理httpd

42.1 概述

Web(World Wide Web)是目前最常用的Internet协议之一。目前在Unix-Like系统中的web服务器主要通过Apache服务器软件实现。为了实现运营动态网站,产生了LAMP(Linux + Apache +MySQL + PHP)。web服务可以结合文字、图形、影像以及声音等多媒体,并支持超链接(Hyperlink)的方式传输信息。

openEuler系统中的web服务器版本是Apache HTTP服务器2.4版本,即httpd,一个由Apache软件基金会发展而来的开源web服务器。

42.2 管理httpd

42.2.1 概述

通过systemctl工具,可以对httpd服务进行管理,包括启动、停止、重启服务,以及查看服务状态等。本章介绍Apache HTTP服务的管理操作,以指导用户使用。

42.2.2 前提条件

  • 为了能够使用Apache HTTP服务,请确保您的系统中已经安装httpd服务的rpm包。在root权限下执行如下命令进行安装:
# dnf install httpd

示例命令如下:

[root@superman-21 ~]# dnf install httpd 
OS                                                                                            41 kB/s | 3.8 kB     00:00    
everything                                                                                    46 kB/s | 3.8 kB     00:00    
EPOL                                                                                          37 kB/s | 3.0 kB     00:00    
debuginfo                                                                                     41 kB/s | 3.8 kB     00:00    
source                                                                                        41 kB/s | 3.8 kB     00:00    
update                                                                                        35 kB/s | 3.0 kB     00:00    
update                                                                                       6.4 MB/s |  28 MB     00:04    
Dependencies resolved.
=============================================================================================================================
 Package                             Architecture           Version                             Repository              Size
=============================================================================================================================
Installing:
 httpd                               x86_64                 2.4.51-9.oe2203                     update                 1.3 M
Installing dependencies:
 apr                                 x86_64                 1.7.0-4.oe2203                      OS                     110 k
 apr-util                            x86_64                 1.6.1-12.oe2203                     OS                     110 k
 httpd-filesystem                    noarch                 2.4.51-9.oe2203                     update                  11 k
 httpd-tools                         x86_64                 2.4.51-9.oe2203                     update                  71 k
 mariadb-connector-c                 x86_64                 3.1.13-2.oe2203                     update                 179 k
 mod_http2                           x86_64                 1.15.25-1.oe2203                    OS                     126 k

Transaction Summary
=============================================================================================================================
Install  7 Packages

Total download size: 1.9 M
Installed size: 6.2 M
Is this ok [y/N]: y
Downloading Packages:
(1/7): mod_http2-1.15.25-1.oe2203.x86_64.rpm                                                 570 kB/s | 126 kB     00:00    
(2/7): apr-util-1.6.1-12.oe2203.x86_64.rpm                                                   485 kB/s | 110 kB     00:00    
(3/7): apr-1.7.0-4.oe2203.x86_64.rpm                                                         472 kB/s | 110 kB     00:00    
(4/7): httpd-filesystem-2.4.51-9.oe2203.noarch.rpm                                           216 kB/s |  11 kB     00:00    
(5/7): httpd-tools-2.4.51-9.oe2203.x86_64.rpm                                                1.2 MB/s |  71 kB     00:00    
(6/7): mariadb-connector-c-3.1.13-2.oe2203.x86_64.rpm                                        1.6 MB/s | 179 kB     00:00    
(7/7): httpd-2.4.51-9.oe2203.x86_64.rpm                                                      3.7 MB/s | 1.3 MB     00:00    
-----------------------------------------------------------------------------------------------------------------------------
Total                                                                                        3.2 MB/s | 1.9 MB     00:00     
retrieving repo key for OS unencrypted from http://repo.openeuler.org/openEuler-22.03-LTS/OS/x86_64/RPM-GPG-KEY-openEuler
OS                                                                                            23 kB/s | 2.1 kB     00:00    
Importing GPG key 0xB25E7F66:
 Userid     : "private OBS (key without passphrase) <defaultkey@localobs>"
 Fingerprint: 12EA 74AC 9DF4 8D46 C69C A0BE D557 065E B25E 7F66
 From       : http://repo.openeuler.org/openEuler-22.03-LTS/OS/x86_64/RPM-GPG-KEY-openEuler
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Running scriptlet: mariadb-connector-c-3.1.13-2.oe2203.x86_64                                                          1/1 
  Preparing        :                                                                                                     1/1 
  Running scriptlet: apr-1.7.0-4.oe2203.x86_64                                                                           1/7 
  Installing       : apr-1.7.0-4.oe2203.x86_64                                                                           1/7 
  Running scriptlet: apr-1.7.0-4.oe2203.x86_64                                                                           1/7 
  Installing       : mariadb-connector-c-3.1.13-2.oe2203.x86_64                                                          2/7 
  Running scriptlet: apr-util-1.6.1-12.oe2203.x86_64                                                                     3/7 
  Installing       : apr-util-1.6.1-12.oe2203.x86_64                                                                     3/7 
  Running scriptlet: apr-util-1.6.1-12.oe2203.x86_64                                                                     3/7 
  Installing       : httpd-tools-2.4.51-9.oe2203.x86_64                                                                  4/7 
  Running scriptlet: httpd-filesystem-2.4.51-9.oe2203.noarch                                                             5/7 
  Installing       : httpd-filesystem-2.4.51-9.oe2203.noarch                                                             5/7 
  Installing       : mod_http2-1.15.25-1.oe2203.x86_64                                                                   6/7 
  Installing       : httpd-2.4.51-9.oe2203.x86_64                                                                        7/7 
  Running scriptlet: httpd-2.4.51-9.oe2203.x86_64                                                                        7/7 
  Verifying        : apr-1.7.0-4.oe2203.x86_64                                                                           1/7 
  Verifying        : apr-util-1.6.1-12.oe2203.x86_64                                                                     2/7 
  Verifying        : mod_http2-1.15.25-1.oe2203.x86_64                                                                   3/7 
  Verifying        : httpd-2.4.51-9.oe2203.x86_64                                                                        4/7 
  Verifying        : httpd-filesystem-2.4.51-9.oe2203.noarch                                                             5/7 
  Verifying        : httpd-tools-2.4.51-9.oe2203.x86_64                                                                  6/7 
  Verifying        : mariadb-connector-c-3.1.13-2.oe2203.x86_64                                                          7/7 

Installed:
  apr-1.7.0-4.oe2203.x86_64                 apr-util-1.6.1-12.oe2203.x86_64      httpd-2.4.51-9.oe2203.x86_64                
  httpd-filesystem-2.4.51-9.oe2203.noarch   httpd-tools-2.4.51-9.oe2203.x86_64   mariadb-connector-c-3.1.13-2.oe2203.x86_64  
  mod_http2-1.15.25-1.oe2203.x86_64        

Complete!
[root@superman-21 ~]# 
  • 启动、停止和重启httpd服务,需要使用root权限。

42.2.3 启动服务

  • 启动并运行httpd服务,命令如下:
# systemctl start httpd

示例命令如下:

[root@superman-21 ~]# systemctl start httpd
[root@superman-21 ~]# 
  • 假如希望在系统启动时,httpd服务自动启动,则命令和回显如下:
# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

示例命令如下:

[root@superman-21 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@superman-21 ~]# 

image-20221219190513462 说明: 假如正在运行的Apache HTTP服务器作为一个安全服务器,系统开机启动后需要密码,这个密码使用的是加密的私有SSL密钥。

42.2.4 停止服务

  • 停止运行的httpd服务,命令如下:
# systemctl stop httpd

示例命令如下:

[root@superman-21 ~]# systemctl stop httpd  
[root@superman-21 ~]# 
  • 如果希望防止服务在系统开机阶段自动开启,命令和回显如下:
# systemctl disable httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.

示例命令如下:

[root@superman-21 ~]# systemctl disable httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@superman-21 ~]# 

42.2.5 重启服务

重启服务有三种方式:

  • 完全重启服务
# systemctl restart httpd

该命令会停止运行的httpd服务并且立即重新启动它。一般在服务安装以后或者去除一个动态加载的模块(例如PHP)时使用这个命令。

示例命令如下:

[root@superman-21 ~]# systemctl restart httpd
[root@superman-21 ~]# 
  • 重新加载配置
# systemctl reload httpd

该命令会使运行的httpd服务重新加载它的配置文件。任何当前正在处理的请求将会被中断,从而造成客户端浏览器显示一个错误消息或者重新渲染部分页面。

示例命令如下:

[root@superman-21 ~]# systemctl reload httpd
[root@superman-21 ~]# 
  • 重新加载配置而不影响激活的请求
# apachectl graceful

该命令会使运行的httpd服务重新加载它的配置文件。任何当前正在处理的请求将会继续使用旧的配置文件。

示例命令如下:

[root@superman-21 ~]# apachectl graceful
[root@superman-21 ~]#

42.2.6 验证服务状态

验证httpd服务是否正在运行

# systemctl is-active httpd

回显为“active”说明服务处于运行状态。

示例命令如下:

[root@superman-21 ~]# systemctl is-active httpd
active
[root@superman-21 ~]# 

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

image-20230319222526711