脚本思路可能有点复杂,有什么问题可以留言,我会改进的
#!/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