第一种方式
1. 下载 Apache HTTP Server 源代码
首先,从 Apache 官网 下载最新版本的 httpd 2.4 源码,或者直接使用 wget 下载:
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.36.tar.gz # 解压
[root@localhost ~]# tar xf httpd-2.4.63.tar.gz -C /usr/local/src/
[root@localhost ~]# yum install gcc gcc-c++ make#切换
[root@localhost ~]# cd /usr/local/src/httpd-2.4.63/
2.添加组与用户
[root@localhost httpd-2.4.63]# groupadd -r -g 48 apache
[root@localhost httpd-2.4.63]# useradd -r -u 48 -g 48 -c "Apache server" -s /sbin/nologin apache
3. 安装编译依赖
[root@localhost httpd-2.4.63]# yum install apr-devel
[root@localhost httpd-2.4.63]# yum install apr-util-devel
[root@localhost httpd-2.4.63]# yum install pcre-devel
4. 配置编译选项编译并安装
[root@localhost httpd-2.4.63]# ./configure --prefix=/usr/local/apache
5.编译并安装
[root@localhost httpd-2.4.63]# make
[root@localhost httpd-2.4.63]# make install
6.配置文件
[root@localhost ~]# cd /usr/local/apache
[root@localhost apache]# cd bin/
[root@localhost bin]# ll
total 1300
-rwxr-xr-x 1 root root 55576 Feb 6 16:15 ab
-rwxr-xr-x 1 root root 3434 Feb 6 16:13 apachectl
-rwxr-xr-x 1 root root 23877 Feb 6 16:13 apxs
-rwxr-xr-x 1 root root 16480 Feb 6 16:15 checkgid
-rwxr-xr-x 1 root root 8874 Feb 6 16:13 dbmmanage
-rw-r--r-- 1 root root 1071 Feb 6 16:13 envvars
-rw-r--r-- 1 root root 1071 Feb 6 16:13 envvars-std
-rwxr-xr-x 1 root root 17480 Feb 6 16:15 fcgistarter
-rwxr-xr-x 1 root root 44608 Feb 6 16:15 htcacheclean
-rwxr-xr-x 1 root root 31728 Feb 6 16:15 htdbm
-rwxr-xr-x 1 root root 17760 Feb 6 16:15 htdigest
-rwxr-xr-x 1 root root 31176 Feb 6 16:15 htpasswd
-rwxr-xr-x 1 root root 975544 Feb 6 16:15 httpd
-rwxr-xr-x 1 root root 17440 Feb 6 16:15 httxt2dbm
-rwxr-xr-x 1 root root 18072 Feb 6 16:15 logresolve
-rwxr-xr-x 1 root root 31072 Feb 6 16:15 rotatelogs[root@localhost bin]# cp apachectl /etc/init.d/httpd
[root@localhost bin]# ll /etc/init.d/httpd
-rwxr-xr-x 1 root root 3434 Feb 6 16:32 /etc/init.d/httpd
[root@localhost bin]# vim /etc/init.d/httpd
##添加如下脚本#chkconfig: 35 80 10
#description: Apache is an HTTP(S) server
7.添加成系统服务
[root@localhost bin]# chkconfig --level 35 --add httpd
[root@localhost bin]# chkconfig --level 35 httpd on
[root@localhost bin]# chkconfig --list httpdNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.To see services enabled on particular target use'systemctl list-dependencies [target]'.httpd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
8.启动服务
[root@localhost bin]# systemctl start httpd
9.查看端口
[root@localhost bin]# netstat -lnupt | grep 80
tcp6 0 0 :::80 :::* LISTEN 33622/httpd
如果没有安装net-tools,需安装后才能查看端口:
#yum install net-tools
10.测试成功
11.反向操作删除
[root@localhost bin]# systemctl stop httpd
[root@localhost bin]# chkconfig --level 35 httpd off
[root@localhost bin]# chkconfig --level 35 --del httpd
[root@localhost bin]# rm -f /etc/local/init.d/httpd
第二种方式
1.编辑文件
[root@localhost bin]# vim /usr/lib/systemd/system/httpd.service[Unit]
Description=The Apache HTTP Server
Documentation=man:httpd.service(8)
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Wants=httpd-init.service[Service]
Type=forking
ExecStart=/usr/local/apache/bin/httpd -k start
ExecReload=/usr/local/apache/bin/httpd -k graceful
ExecStop=/usr/local/apache2/bin/httpd -k stop
PrivateTmp=true
PIDFile=/usr/local/apache/logs/httpd.pid[Install]
WantedBy=multi-user.target
[root@localhost bin]# vim /usr/local/apache/conf/httpd/conf##修改如下脚本
User apache
Group apache
2.检测编写脚本是否有误
[root@localhost bin]# /usr/local/apache/bin/httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
3.加载系统守护进程
[root@localhost bin]# systemctl daemon-reload
4.启动服务
[root@localhost bin]# systemctl start httpd