编译安装nginx--1.16

脚本思路可能有点复杂,有什么问题可以留言,我会改进的

#!/usr/bin/bash
#编译安装nginx,个人使用,不能用于生产,用于生产概不负责
#刘彬彬
#有事联系此邮箱liubinbin0215@163.com

echo "此脚本用于一键编译安装nginx仅限个人使用,不想安装请在10秒内,Ctrl+c停止"
sleep 10
wt() {
`wget -P /usr/local/src/ -nc  http://nginx.org/download/nginx-1.16.0.tar.gz` #可以修改软件包版本,下面解压(109行)进入也要修改
}
#编译配置,需要什么添加什么,这里所有的配置已经指定目录
configure(){
cd /usr/local/src/nginx-1.16.0     
./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 
}

systemctl disable firewalld 
if [[ $? -ne 0 ]];then
        echo "关闭失败"
        exit
else
        echo "防火墙已经关闭"
fi

get={ getenforce 0 }
if [[ $get = Disabled || $get = permissive ]];then
        echo "关闭失败"
        exit
else
        echo "selinux已经关闭"
fi

ping -c1  www.baidu.com 
if [[ $? -ne 0 ]];then
        echo "网络不通,请手动检查网络"
	exit
else
        echo "网络通畅,可以安装软件"
fi

yum -y install gcc gcc-c++  
if [[ $? -ne 0 ]];then
	echo "编译环境安装失败"
	exit
else
	echo "编译环境安装成功"
fi
yum install -y pcre pcre-devel 
#使nginx支持http rewrite模块
if [[ $? -ne 0 ]];then
        echo "安装失败"
	exit
else
        echo "安装pcre成功"
fi

yum install -y openssl openssl-devel 
#使nginx支持ssl
if [[ $? -ne 0 ]];then
        echo "安装失败"
        exit
else    
        echo "安装openssl-devel成功"
fi

yum install -y zlib zlib-devel gd gd-devel 
if [[ $? -ne 0 ]];then
        echo "安装失败"
        exit
else
        echo "安装zlib成功"
fi
useradd -s /sbin/nologin nginx
#使创建的nginx用户没有登录的权限,为了安全
if [[ $? -ne 0 ]];then
        echo "创建用户失败"
        break
else
        echo "创建用户成功"
fi

wt 
#上面定义的函数,写起来太长不宜读,下载nginx-1.16包
if [[ $? -ne 0 ]];then
        echo "下载失败,有可能已经存在安装包"

else    
        echo "下载成功"
fi

cd /usr/local/src/
#解压
tar -zxvf nginx-1.16.0.tar.gz 

echo "配置文件,指定目录,如果不想指定目录请Ctrl+c退出脚本"
sleep 5
configure   &>/etc/null   #上面有函数,在开头
if [[ $? -ne 0 ]];then
        echo "配置失败"
        exit
else
        echo "配置成功"
fi

echo "开始安装"
sleep 3
make &>/dev/null && make install
if [[ $? -ne 0 ]];then
        echo "安装失败"
        exit
else
        echo "安装成功"
fi

echo "启动服务"
sleep 3
cd /usr/local/nginx/sbin
./nginx 
netstat -ntlp | grep nginx 
if [[ $? -ne 0 ]];then
        echo "启动失败"
        exit
else
       echo "nginx启动成功,可以使用了"
fi
cd /usr/local/nginx/sbin
./nginx -s stop

#把nginx添加system管理
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
echo "添加权限,执行权"
chmod +x /usr/lib/systemd/system/nginx.service

echo "开机自启配置中"
#实验一下这几项是否可以正常使用
systemctl daemon-reload 
systemctl start nginx.service
systemctl stop nginx.service 
systemctl reload nginx.service
systemctl restart nginx.service
systemctl enable nginx.service
if [[ $? -ne 0 ]];then
        echo "启动失败"
        exit
else
        echo "启动成功"
fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值