源码安装httpd

1. 准备工作

1.1恢复快照

1.2 删除自带yum源、安装阿里yum源

#删除
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# rm -rf *

#安装基础源
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   8603      0 --:--:-- --:--:-- --:--:--  8603
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo[root@localhost yum.repos.d]# ls
CentOS-Base.repo
#清除缓存
[root@localhost yum.repos.d]# dnf clean all
#建立缓存
[root@localhost yum.repos.d]# dnf makecache

#安装 epel源

[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
#清除缓存
[root@localhost yum.repos.d]# dnf clean all
#建立缓存
[root@localhost yum.repos.d]# dnf makecache

2. 要下载httpd包 、它有两个依赖包 Aprapr-util

2.1下载

[root@localhost ~]# cd /opt/
#下载apr包
[root@localhost opt]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz  
#下载apr-util包
[root@localhost opt]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
#下载httpd包
[root@localhost opt]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz 

2.2 解压

[root@localhost opt]# tar xf apr-1.7.0.tar.gz 
[root@localhost opt]# tar xf apr-util-1.6.1.tar.gz 
[root@localhost opt]# tar xf httpd-2.4.53.tar.gz 

3. 创建apache系统用户

# 需要创建一个apache的系统用户 
# -M没有家目录,-s不允许登录 -r 创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache 

4. 安装开发环境

#安装依赖包
[root@localhost ~]# dnf  -y install openssl-devel pcre-devel expat-devel boost-devel --allowerasing make gcc gcc-c++ vim 

#需要进去apr-1.7.0里面的configure
[root@localhost apr-1.7.0]# vim configure  
    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行
    
 #/cfgfile 左斜杠查找cfgfile

5. 编译安装

注意:安装顺序为: apr 、apr-util 、httpd

5.1 编译安装 Apr

#源码安装三部曲:
#第一步: ./configure(定制组件)
[root@localhost opt]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
#第二步 make
[root@localhost apr-1.7.0]# make
#第三步 make install
[root@localhost apr-1.7.0]# make install

5.2编译安装 Apr-util

#第一步 ./configure
[root@localhost apr-1.7.0]# cd ../apr-util-1.6.1/
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#第二步 make
[root@localhost apr-util-1.6.1]# make
#第三步 make install
[root@localhost apr-util-1.6.1]# make install

5.3编译安装httpd

#第一步 ./configure
[root@localhost apr-util-1.6.1]# cd ../httpd-2.4.53/
[root@localhost httpd-2.4.53]# ./configure --prefix=/usr/local/apache \
 --enable-so \
 --enable-ssl \
 --enable-cgi \
 --enable-rewrite \
 --with-zlib \
 --with-pcre \
 --with-apr=/usr/local/apr \
 --with-apr-util=/usr/local/apr-util/ \
 --enable-modules=most \
 --enable-mpms-shared=all \
 --with-mpm=prefork

#第二步 make
[root@localhost httpd-2.4.53]# make

#第三步 make install
[root@localhost httpd-2.4.53]# make install

6. 设置环境变量

[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
#刷新apache.sh这个文件让其生效
[root@localhost ~]# source /etc/profile.d/apache.sh 
#启动apache
[root@localhost ~]# httpd

#查看是否有80端口
[root@localhost ~]# ss -antl
State          Recv-Q         Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              128                            0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              128                                  *:80                                *:*                            
LISTEN         0              128                               [::]:22                             [::]:*                            

#关闭 pkill httpd

6.1 解决启动服务和关闭服务的警告信息

#进到配置文件目录 
[root@localhost ~]# cd /usr/local/apache/conf/   
[root@localhost conf]# ls
extra  httpd.conf  magic  mime.types  original 
[root@localhost conf]# vim httpd.conf   
ServerName www.example.com:80  #将这一行前面的注释取消掉

7. 设置映射关系

#把/usr/local/apache/include/ 链接到/usr/include/
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/apache

8. 设置man文档

[root@localhost ~]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/local/share/man  //在这个的下面添加下面一条
MANDATORY_MANPATH                       /usr/local/apache/man  //添加

9.使用systemctl命令来控制httpd

注意: 使用源码包安装httpd服务 默认是不能用systemctl的

[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# ls sshd.service 
sshd.service
#复制一份这个文件改名为httpd.service
[root@localhost system]# cp sshd.service httpd.service  
#编辑这个文件
[root@localhost system]# vim httpd.service   
[Unit]
Description=httpd server daemon  #修改为httpd
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start #更改为apachectl的路径 开启
ExecStop=/usr/local/apache/bin/apachectl stop  #关闭
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

 #重新加载服务 让其生效
[root@localhost system]# systemctl daemon-reload   
#使用systemcl 查看httpd状态
[root@localhost system]# systemctl status httpd  
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; >
   Active: inactive (dead)
lines 1-3/3 (END)
#开启httpd服务
[root@localhost system]# systemctl start httpd 
#设置为开机自启
[root@localhost system]# systemctl enable --now httpd 
#查看状态
[root@localhost system]# systemctl status httpd 
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; >
   Active: active (running) since Sat 2022-06-06 16:42:55 >
 Main PID: 57208 (httpd)
 ........省略N

10.关闭防火墙

#开闭防火墙、并开机不自启
[root@localhost ~]# systemctl disable --now firewalld
#查看防火墙状态
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; ven>
   Active: inactive (dead)
     Docs: man:firewalld(1)

[root@localhost ~]# getenforce 
Enforcing  #表示开启

#关闭selinux  当前生效
[root@localhost ~]# setenforce 0  
[root@localhost ~]# getenforce 
Permissive  #表示关闭

#关闭SElinux
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled  #第一个修改为disabled,永久关闭

11.使用真机访问

在这里插入图片描述

12报错信息

由于不正当操作,导致httpd启动不了
[root@localhost ~]# /usr/local/apache/bin/httpd
(20014)Internal error (specific information not available): AH00058: Error retrieving pid file logs/httpd.pid
AH00059: Remove it before continuing if it is corrupted.

报了这个错

解决
[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
[root@localhost apache]# cd logs/
[root@localhost logs]# ls
access_log  login.example.com.com-access_log  php.example.com-error_log        zabbix.example.com-access_log
error_log   login.example.com.com-error_log   test.example.com.com-access_log  zabbix.example.com-error_log
httpd.pid   php.example.com-access_log        test.example.com.com-error_log

删除 httpd.pid 就能解决
[root@localhost logs]# rm -f httpd.pid



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值