记一次shell脚本练习_编译安装nginx 1.16.1

操作环境:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
#!/usr/bin/bash
# AUTHOR:liujun
# MAKE INSTALL NGINX
# AT 2020-03-30
#test network and install nginx
check_network () {
ping -c1 -w1 www.baidu.com  &> /dev/null
if [[ $? -eq 0 ]];then
    echo "Normal network connection.start to deploy nginx......"
    sleep 3
else
    echo "[ERROR]Network connection failed.please check"
    exit 127
fi
}
firewall_status () {
systemctl status firewalld &> /dev/null
if [[ $? -ne 0 ]];then
    echo "the firewalld is off"
else
    echo "the firewalld is on"
fi
}
selinux_status () {
setenforce 0 &> /dev/null
if [[ $? -eq 0 ]];then
    echo "selinux is off"
fi
}

#check firewall and selinux status
firewall_status
selinux_status

#install environment
check_network
yum install -y wget
yum install -y gcc gcc-c++
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
yum install -y zlib zlib-devel gd gd-devel

#add a nginx user
id nginx
if [[ $? -eq 0 ]];then
    echo "user alread exists"
else
    useradd -s /sbin/nologin nginx
fi

#install nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1/

#configure
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module

if [[ $? -eq 0 ]];then
    echo "complie successfully"
else
    echo "Compilation failed. Please check."
fi

#make and install nginx
make && make install

#Configure nginx commands and services and start
cat > /usr/lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
#reload systemctl file
systemctl daemon-reload

#give execution permission
chmod +x /usr/lib/systemd/system/nginx.service

#Add startup
systemctl restart nginx.service
systemctl enable nginx.service

#check service
clear
curl 127.0.0.1 &> /dev/null
if [[ $? -eq 0 ]];then
    echo -e  "Welcome to nginx!\n"
else
    echo "[ERROR]Nginx installation failed.please check" 
fi
netstat -ntlp | grep nginx &> /dev/null
if [[ $? -eq 0 ]];then
    echo "Nginx started and installed\n"
fi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值