shell脚本部署apache

编译安装nginx

首先创建一个目录,用来存放脚本

[root@minion01 ~]# mkdir /script
[root@minion01 ~]# cd /script/
[root@minion01 script]# touch apache.sh
[root@minion01 script]# chmod +x apache.sh
[root@minion01 script]# vim apache.sh

下载apache的包
创建一个目录用来存放apache的安装包

[root@minion01 script]# mkdir packages
[root@minion01 script]# cd packages/
[root@minion01 packages]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz

[root@minion01 packages]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@minion01 packages]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.51.tar.gz

编写脚本

[root@localhost script]# cat apache.sh 
#!/bin/bash
route=/usr/local
path=/usr/src
yum -y install epel-release wget make openssl openssl-devel pcre pcre-devel gcc gcc-c++ zlib-devel expat-devel zlib expat epel-release

id apache
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
fi

echo "解压依赖包"

if [ ! -d $path/apr-1.7.0 ];then
    tar xf packages/apr-1.7.0.tar.gz -C $path
fi

if [ ! -d $path/apr-util-1.6.1 ];then
    tar xf packages/apr-util-1.6.1.tar.gz -C $path
fi

if [ ! -d $path/httpd-2.4.51 ];then
    tar xf packages/httpd-2.4.51.tar.gz -C $path
fi

cd $path/apr-1.7.0
if [ ! -d $route/apr ];then
    sed -i 's/$RM "cfgfile"/#$RM "cfgfile"/g' configure
    ./configure --prefix=$route/apr
    make && make install
fi

cd $path/apr-util-1.6.1
if [ ! -d $route/apr-util ];then
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    make && make install
fi

cd $path/httpd-2.4.51
if [ ! -d $route/httpd-2.4.51 ];then
    ./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 -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
fi
echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh

cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now httpd

检验效果

[root@localhost script]# systemctl status httpd.service 
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2021-11-05 18:16:49 CST; 4min 21s ago
  Process: 34128 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)


[root@localhost script]# ss -anlt | grep 80
LISTEN     0      128         :::80                      :::* 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。