shell脚本部署apache

1. 创建一个目录,用来存放脚本和安装包

[root@localhost opt]# ls
apache
[root@localhost opt]# cd apache/
[root@localhost apache]# ls   //创建一个安装包的目录和一个脚本的文件
install.sh  soft
[root@localhost apache]# 

2.下载好对应的安装包

安装包相关链接:
wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
wget https://downloads.apache.org/httpd/httpd-2.4.48.tar.gz

//下载完成后放在刚刚创建的soft目录下
root@localhost apache]# cd soft/
[root@localhost soft]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz
[root@localhost soft]# 

3. 编写脚本

[root@localhost apache]# ls
install.sh  soft
[root@localhost apache]# vim install.sh 
#!/bin/bash

systemctl stop firewalld 
systemctl disable firewalld
setenforce 0

install_dir=/usr/local/httpd 

yum groups mark install "Development Tools" -y   
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && echo "=====安装开发环境成功!====="

id apache &>/dev/null  
if [ $? -ne 0 ];then  
    useradd -r -M -s /sbin/nologin apache && echo "=====创建apache用户成功!====="
fi

rm -rf /usr/src/{apr*,httpd*} 
tar xf soft/apr-1.7.0.tar.gz -C /usr/src
tar xf soft/apr-util-1.6.1.tar.gz -C /usr/src
tar xf soft/httpd-2.4.48.tar.gz -C /usr/src && echo "=====解压安装包成功!====="

cd /usr/src/apr-1.7.0  
if [ ! -d /usr/local/apr ];then  
    sed -i '/$RM "$cfgfile"/d' configure && echo "=====删除$RM "$cfgfile"成功!====="
    ./configure --prefix=/usr/local/apr && make && make install && echo "=====apr-1.7.0编译安装成功!====="
fi
cd ../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 && echo "=====apr-1.7.0编译安装成功!====="
fi
cd ../httpd-2.4.48
if [ ! -d $install_dir ];then
    ./configure --prefix=$install_dir \
        --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 && echo "=====httpd-2.4.48编译安装成功!====="
fi
sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /usr/local/httpd/conf/httpd.conf 
echo 'export PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/httpd.sh && echo "=====环境变量创建成功!====="

cat > /usr/lib/systemd/system/httpd.service  << EOF
[Unit]
Description=The Apache http server
After=network.targe

[Service]
Type=forking
ExecStart=/usr/local/httpd/bin/apachectl start
ExecStop=/usr/local/httpd/bin/apachectl stop
ExecReload=/usr/local/httpd/bin/apachectl restart

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && echo "=====重新加载成功====="
systemctl enable --now httpd && echo "=====apache开机自启成功!====="

netstat -anpt | grep 80  

if [ $? -eq 0 ];then  
    echo "=====Apache服务启动成功====="
else
    echo "=====启动失败====="
fi

4. 验证效果

[root@localhost apache]# ss -antl
State          Recv-Q         Send-Q                  Local Address:Port                   Peer Address:Port         
LISTEN         0              128                           0.0.0.0:111                         0.0.0.0:*            
LISTEN         0              32                      192.168.122.1:53                          0.0.0.0:*            
LISTEN         0              128                           0.0.0.0:22                          0.0.0.0:*            
LISTEN         0              5                           127.0.0.1:631                         0.0.0.0:*            
LISTEN         0              128                         127.0.0.1:6010                        0.0.0.0:*            
LISTEN         0              128                         127.0.0.1:6011                        0.0.0.0:*            
LISTEN         0              128                              [::]:111                            [::]:*            
LISTEN         0              128                                 *:80                                *:*            
LISTEN         0              128                              [::]:22                             [::]:*            
LISTEN         0              5                               [::1]:631                            [::]:*            
LISTEN         0              128                             [::1]:6010                           [::]:*            
LISTEN         0              128                             [::1]:6011                           [::]:*            
[root@localhost apache]# curl http://192.168.8.129
<html><body><h1>It works!</h1></body></html>
[root@localhost apache]# 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值