部署LAMP现有很多教程,或一键安装的、或者使用面板的、或者购买云vps时自动部署的。为什么最终选择编译的方式,也是突发奇想、自讨苦吃,虽然最后也是知其然不知其所以然罢了。同时,也把各步骤写到了一个可执行文件内,一般来说直接执行也可以:

wget https://blog.nyankowo.com/bash/installapache.sh && chmod +x installapache.sh && bash installapache.sh

安装预备软件

yum install -y perl epel-release
yum install -y wget tar gcc gcc-c++ cmake make lsof expat-devel openssl-devel zlib zlib-devel chkconfig firewalld

安装nghttp2

        在apache安装过程中,开启http2模块时提示版本过低,所以加一步安装nghttp2,软件下载位置nghttp2

wget https://github.com/nghttp2/nghttp2/releases/download/v1.63.0/nghttp2-1.63.0.tar.gz -P /usr/local/src #下载源码到指定目录
tar -xvf /usr/local/src/nghttp2-1.63.0.tar.gz -C /usr/local/src && cd /usr/local/src/nghttp2-1.63.0 #解压gz包到指定目录并进入
./configure && make && make install #编译安装

安装pcre

        Apache的安装需要依赖 apr、apr-util、pcre基础的三个库,首先安装pcre,与安装nghttp2的过程类似,软件下载位置pcre

wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/pcre-8.45.tar.gz -C /usr/local/src && cd /usr/local/src/pcre-8.45
./configure --prefix=/usr/local/pcre && make && make install

安装apr

        软件下载位置apr

wget https://archive.apache.org/dist/apr/apr-1.7.5.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/apr-1.7.5.tar.gz -C /usr/local/src && cd /usr/local/src/apr-1.7.5
cat configure | sed -i $'s/RM=\'\$RM\'/RM=\'\$RM -f\'/g' configure #不修改会导致libtool报错
./configure --prefix=/usr/local/apr && make && make install

[!WARNING]

        需要编辑configure文件,否则libtool报错,即第三行的替换:

  1. cd到解压目录后,编辑configure文件:vi configure
  2. vi简易操作:

    • 查:/+字符串表示向后定位字符串,?+字符串表示向前定位字符串,n键查找定位下一个;
    • 改:i键进入编辑状态,编辑完成后Esc退出编辑状态;
    • 保存:使用:执行命令,命令:wq表示保存,命令:qa!退出不保存编辑。
  3. 查找RM='$RM'修改为RM='$RM -f'

安装apr-util

        软件下载位置apr-util

wget https://archive.apache.org/dist/apr/apr-util-1.6.3.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/apr-util-1.6.3.tar.gz -C /usr/local/src && cd /usr/local/src/apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config && make && make install

[!WARNING]

        可以看到apr-util的安装需要依赖于apr,安装顺序不能反

安装Apache

        软件下载位置apache

wget https://dlcdn.apache.org/httpd/httpd-2.4.62.tar.gz -P /usr/local/src
tar -xvf /usr/local/src/httpd-2.4.62.tar.gz -C /usr/local/src && cd /usr/local/src/httpd-2.4.62
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre/bin/pcre-config --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --enable-mpms-shared=all --enable-rewrite --enable--ssl --with-nghttp2=/usr/local/nghttp2 && make && make install

配置防火墙

  1. 防火墙服务:

    • 查看防火墙服务状态:systemctl status firewalld
    • 开启防火墙服务:systemctl start firewalld
    • 重启防火墙服务:systemctl restart firewalld
    • 停止防火墙服务:systemctl stop firewalld
    • 防火墙服务自启:systemctl enable firewalld
  2. 防火墙配置:

    • 查看防火墙配置:firewall-cmd --list-all
    • 查看防火墙特定端口:firewall-cmd --zone=public --query-port=80/tcp
    • 开启防火墙端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
    • 重新加载防火墙配置:systemctl reload firewalldfirewall-cmd --reload

Apache操作

  • 创建软链接:ln -s /usr/local/apache /apache
  • 添加环境变量:sed -i '$aexport PATH=$PATH:/apache/bin' /etc/profile && source /etc/profile

    • 检查配置:apachectl -t
    • 启动服务:apachectl start
    • 重启服务:apachectl restart
    • 停止服务:apachectl stop
    • 平滑重启:apachectl graceful
  • 注册服务,之后可使用systemctl管理httpd:

    cat <<EOL | sudo tee /etc/systemd/system/httpd.service
    [Unit]
    Description=Apache HTTP Server (Compiled)
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=forking
    ExecStart=/apache/bin/apachectl start
    ExecReload=/apache/bin/apachectl graceful
    ExecStop=/apache/bin/apachectl stop
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    EOL
    
    sudo systemctl daemon-reload
    systemctl enable httpd

        初步修改apache的配置文件,备份文件cp /apache/conf/httpd.conf /apache/conf/httpd.conf_bak,修改其中的ServerName www.example.comServerName localhost,开启apache服务,使用浏览器访问ip。
apache_1