Linux 安装Httpd服务,基于RedHat的发


安装Httpd服务

版本说明:
CentOS 7 以上,默认系统是httpd 2.4,CentOS 6 版默认为httpd 2.2
Ubuntu 18.04 默认 Apache/2.4.29

范例:CentOS 8 安装httpd
[root@CentOS8 ~]# yum info httpd
Baseos                                                                                                                           3.8 MB/s | 3.9 kB     00:00    
AppStream                                                                                                                        4.2 MB/s | 4.3 kB     00:00    
EPEL                                                                                                                             7.1 kB/s | 4.7 kB     00:00    
EPEL                                                                                                                             1.1 MB/s |  13 MB     00:11    
Available Packages
Name         : httpd
Version      : 2.4.37
Release      : 30.module_el8.3.0+561+97fdbbcc
Architecture : x86_64
Size         : 1.7 M
Source       : httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.src.rpm
Repository   : AppStream
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

[root@CentOS8 ~]# yum install httpd -y 
Last metadata expiration check: 0:00:21 ago on Mon 10 Oct 2022 10:57:38 PM CST.
Dependencies resolved.
=================================================================================================================================================================
 Package                                Architecture               Version                                                   Repository                     Size
=================================================================================================================================================================
Installing:
 httpd                                  x86_64                     2.4.37-30.module_el8.3.0+561+97fdbbcc                     AppStream                     1.7 M
Installing dependencies:
 apr                                    x86_64                     1.6.3-11.el8                                              AppStream                     125 k
 apr-util                               x86_64                     1.6.1-6.el8                                               AppStream                     105 k
 centos-logos-httpd                     noarch                     80.5-2.el8                                                Baseos                         24 k
 httpd-filesystem                       noarch                     2.4.37-30.module_el8.3.0+561+97fdbbcc                     AppStream                      37 k
 httpd-tools                            x86_64                     2.4.37-30.module_el8.3.0+561+97fdbbcc                     AppStream                     104 k
 mod_http2                              x86_64                     1.15.7-2.module_el8.3.0+477+498bb568                      AppStream                     154 k
Installing weak dependencies:
 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
Enabling module streams:
 httpd                                                             2.4                                                                                          

Transaction Summary
=================================================================================================================================================================
Install  9 Packages

Total size: 2.3 M
Installed size: 6.0 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
 ..............
Installed:
  apr-1.6.3-11.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-80.5-2.el8.noarch                                              httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64                          
  httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fdbbcc.noarch                     httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64                    
  mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64                            

Complete!
[root@CentOS8 ~]# systemctl enable httpd
httpd@          httpd.service   httpd@.service  httpd.socket    
[root@CentOS8 ~]# systemctl enable httpd.service --now 
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@CentOS8 ~]# httpd -v
Server version: Apache/2.4.37 (centos)
Server built:   Nov  4 2020 03:20:37

范例:Ubuntu 1804 安装apache2
tom@ubuntu1804-1:~$ apt install apache2 -y
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
tom@ubuntu1804-1:~$ sudo apt install apache2 -y
[sudo] password for tom: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 ssl-cert
Suggested packages:
............
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Processing triggers for libc-bin (2.27-3ubuntu1.4) ...
Processing triggers for systemd (237-3ubuntu10.52) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
tom@ubuntu1804-1:~$ apache2 -v
Server version: Apache/2.4.29 (Ubuntu)
Server built:   2022-06-23T12:51:37

apache和httpd它们是同一应用程序
只是某些Linux发行版在程序包管理器和配置文件中对它的引用有所不同。
基于RedHat的发行版(CentOS,Fedora)将其称为httpd,
而基于Debian的发行版(Ubuntu)将其称为apache

源码安装httpd服务

ABI和API:
  • 不同操作系统的API和ABI规范不同,软件要运行再不同的操作系统上就需要遵守不同操作系统的接口标准。

  • 在早期的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数

  • 随着Apache的进一步开发,Apache组织决定将这些通用的函数独立出来并发展成为一个新的项目。这样,APR的开发就从Apache中独立出来,Apache仅仅是使用 APR而已

APR:

Apache portable Run-time libraries,Apache可移植运行库,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。

APR官网:http://apr.apache.org

所以要编译安装httpd,就需要先安装apr和apr-util这两个可移植的运行库。

范例:CentOS7编译安装httpd2.4

安装httpd-2.4,依赖于apr-1.4+, apr-util-1.4+ (1.4以上的版本)

1.下载软件包并安装编译工具
下载apr包和httpd包
https://httpd.apache.org/
http://apr.apache.org

安装编译工具
[root@centos7 ~]# yum -y install gcc make pcre-devel openssl-devel expat-devel
2.编译安装apr
#先编译安装apr,再安装apr-util,最后安装httpd24
[root@centos7 ~]# mkdir -p  /apps/apr

[root@centos7 ~]# cd apr-1.7.0

[root@centos7 ~]# ./configure --prefix=/apps/apr

[root@centos7 ~]# make -j 2  && make install

3.编译安装apr-util
[root@centos7 ~]# mkdir /apps/apr-util

[root@centos7 ~]# cd apr-util-1.6.1

[root@centos7 ~]# ./configure --prefix=/apps/apr-util --with-apr=/apps/apr/

[root@centos7 ~]# make -j 2 && make install
4.编译安装 httpd-2.4
[root@centos7 ~]# mkdir /apps/httpd24

[root@centos7 ~]# cd httpd-2.4.54

[root@centos7 ~]# ./configure --prefix=/apps/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/apps/apr/ --with-apr-util=/apps/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

[root@centos7 ~]# make -j 4 && make install
 
[root@centos7 ~]# useradd -s /sbin/nologin -r apache

5.编译安装后配置
  • Httpd编译过程:/apps/httpd24/build/config.nice

  • 自带的服务控制脚本:/apps/httpd24/bin/apachectl

(1)创建专用用户
[root@centos7 ~]# useradd -s /sbin/nologin -r apache
(2)指定运行httpd的用户
[root@centos7 ~]# vim /apps/httpd24/conf/httpd
user apache
group apache
(3)配置环境变量
[root@centos7 ~]# echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd.sh

[root@centos7 ~]# . /etc/profile.d/httpd.sh

[root@centos7 ~]# httpd -v
Server version: Apache/2.4.54 (Unix)
Server built:   Oct 11 2022 10:31:36

(4)配置帮助
[root@centos7 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH           /apps/httpd24/man
(5)设置开机自动启动
[root@centos7 ~]# vim /etc/rc.d/rc.local

[root@centos7 ~]# /apps/httpd24/bin/apachectl start

[root@centos7 ~]# chmod +x /etc/rc.d/rc.local
(6)创建service unit文件(CentOS 7 以上版本)
[root@centos7 ~]# vim /usr/lib/systemd/system/httpd24.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=forking
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/apps/httpd24/bin/apachectl start
#ExecStart=/apps/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/apps/httpd24/bin/apachectl graceful
#ExecReload=/apps/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

创建启动脚本(CentOS 6 以前版本)

#自定义启动脚本(参考httpd-2.2的服务脚本)
[root@centos6 ~]# cp   /etc/rc.d/init.d/httpd /etc/rc.d/init.d/httpd24
[root@centos6 ~]# vim /etc/rc.d/init.d/httpd24
apachectl=/apps/httpd24/bin/apachectl
httpd=${HTTPD-/apps/httpd24/bin/httpd}
pidfile=${PIDFILE-/apps/httpd24/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}

[root@centos6 ~]# chkconfig -add httpd24

[root@centos6 ~]# chkconfig -list httpd24

编译安装httpd-2.4方法二

将apr和httpd一起进行编译安装。

1.将apr 和apr-util源码与httpd 源码合并
tar xf apr-1.7.0.tar.bz2
tar xf apr-util-1.6.1.tar.bz2
tar xf httpd-2.4.54.tar.bz2

mv apr-1.7.0 httpd-2.4.54/srclib/apr
mv apr-util-1.6.1 httpd-2.4.54/srclib/apr-util
ls httpd-2.4.54/srclib/
apr apr-util Makefile.in
  1. 将三者一并编译并安装
    --with-included-apr
cd httpd-2.4.54/
./configure \
--prefix=/apps/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork  

make -j 4 && make install

安装后的配置和方法一相同。

安装完httpd服务后,启动httpd服务的方法

  • systemctl启动

  • apachectl启动:apachectl是httpd服务自带的一个脚本

systemctl enable|disable httpd.service

systemctl {start|stop|restart|status|reload} httpd.service

apachectl start|stop|restart|configtest

service httpd configtest

相关内容

    暂无相关文章