Shell脚本部署Apache


思路:先把所有需要更改的配置文件都操作做完在进行编译安装。

1、去Apache官网下载源码包

apr: https://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
apr-uilt: https://dlcdn.apache.org/apr/apr-util-1.6.1.tar.gz
httpd: https://dlcdn.apache.org/httpd/httpd-2.4.48.tar.bz2
先创建一个名为Apache目录,在目录中在创建一个名为apache.sh 的脚本和lost的目录,然后把刚刚下载的三个包移动到lost这个目录中。

2、编写shell文件

编写开始:

#!/bin/bash

install_httpd=/usr/local/httpd      //设置一个变量,改变量是http编译安装的位置

//下载所需的软件包
yum groups mark install 'Development Tools'    
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make

//创建apache用户
id apache &> /dev/null      //把id apache的返回值放到回收站中
if [ $? -ne 0  ];then
//根据上个命令的返回值来和0比较,判断是否和0相等,如果不相等则表示没有这个用户,需要创建。
如果相等表示有这个用户直接掉过。
        useradd -r -M -s /sbin/nologin  apache
//创建apache用户,-r: 建立系统用户  -M: 不要自动建立用户的登入目录 -s: 指定登陆shellhe              
fi

//切换到放置压缩包的位置
cd /root/install/lost     
tar xf apr-util-1.6.1.tar.gz -C /usr/src    //把解压后的文件放到 /usr/src下
tar xf httpd-2.4.48.tar.gz  -C /usr/src
tar xf lost/apr-1.7.0.tar.gz -C /usr/src

cd /usr/src/apr-1.7.0
sed -i '/$RM "$cfgfile"/d' configure  
//删除configure文件中的“$RM "$cfgfile"” ,d表示删除  -i:直接修改文件内容

cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then 
//取反判断是否没有这个目录,没有则执行下面的编译,如果有则跳过
       ./configure --prefix=/usr/local/apr && make && make install
fi

cd /usr/src/apr-util-1.6.1
if [ ! -d /usr/local/apr-util ];then
//取反判断是否没有这个目录,没有则执行下面的编译,如果有则跳过
        ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
fi

//http的编译安装
cd /usr/src/httpd-2.4.48
if [ ! -d $install_httpd ];then
         ./configure --prefix=$install_httpd \
                 --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 && make install
fi

//修改httpd的配置文件,把文件中的#号删掉或者注释
sed -i "/#ServerName/s/#//" $install_httpd/conf/httpd.conf       

//添加环境变量
echo "export PATH=$install_httpd/bin:\$PATH" > /etc/profile.d/httpd.sh

//配置httpd.service配置文件,这个就可以用户systemctl控制了。
所有源码安装的服务都不能用systemctl控制,所以需要写一个配置文件。
这个配置文件模板默认是在/usr/lib/systemd/system/sshd.service,直接复制过来修改即可。
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon    //描述信息 这是httpd服务的守护进程
After=network.target       //网卡开启时启动

[Service]
Type=forking
ExecStart=$install_httpd/bin/apachectl start     //启动
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload     //重新加载httpd.service服务配置
systemctl enable --now httpd  //设置httpd服务开机自启和启动

3、检测

[root@localhost ~]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN     0           128                          *:80                        *:*        
LISTEN     0           128                       [::]:22                     [::]:*        

[root@localhost ~]# systemctl status httpd.service     //查看Apache状态
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-09-14 18:41:01 CST; 42s ago      //running(运行)
  Process: 1369771 ExecStart=/usr/local/httpd/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 1369774 (httpd)
    Tasks: 6 (limit: 11300)
   Memory: 5.0M
   CGroup: /system.slice/httpd.service
           ├─1369774 /usr/local/httpd/bin/httpd -k start
           ├─1369775 /usr/local/httpd/bin/httpd -k start
           ├─1369776 /usr/local/httpd/bin/httpd -k start
           ├─1369777 /usr/local/httpd/bin/httpd -k start
           ├─1369778 /usr/local/httpd/bin/httpd -k start
           └─1369779 /usr/local/httpd/bin/httpd -k start

9月 14 18:41:01 localhost.localdomain systemd[1]: Starting httpd server daemon...
9月 14 18:41:01 localhost.localdomain systemd[1]: Started httpd server daemon.

//此时Apache已经启动并且是开机自启
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值