全自动编译安装Nginx1.16+systemctl使用+全局变量 shell脚本
如果大家有什么不懂或是疑问,可以联系我
作者:大宝不胖 但是很壮
Email:db88788@163.com
#! /usr/bin/bash
#编译安装nginx
#此脚本由“大宝不胖,但是很壮”编写
#如有不足的地方或是疑问请联系我
#邮箱:db88788@163.com
#感谢大家的使用,谢谢。
echo "================编译安装Nginx================="
sleep 2
echo "正在关闭防火墙和selinux"
sleep 1
systemctl stop firewalld &>/dev/null
systemctl disable firewalld &>/dev/null
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' `grep 'SELINUX=enforcing' -rl /etc/selinux/config` &>/dev/null
echo "已经关闭防火墙和selinux"
#查看系统版本
xi=`cat /etc/centos-release` &>/dev/null
echo "您的系统版本$xi"
#安装编译环境
echo "等待开始安装编译环境等依赖"
sleep 2
while :
do
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel b zlib-devel gd gd-devel &>/dev/null
if [ $? -eq 0 ];then
echo "安装成功"
break
else
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel b zlib-devel gd gd-devel &>/dev/null
fi
done
#创建一个用户nginx
echo "创建nginx用户"
useradd -s /sbin/nologin nginx
sleep 2
echo "================================开始编译安装nginx================================="
echo "下载nginx安装包"
wget -P /usr/local/src/ -nc http://nginx.org/download/nginx-1.16.0.tar.gz
echo "解压nginx压缩包"
cd /usr/local/src/
tar -zxvf nginx-1.16.0.tar.gz
echo "开始编译配置"
cd 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
make && make install
echo "安装完毕"
cd /usr/local/nginx/sbin
echo "启动nginx"
./nginx
echo "验证配置文件是否正确"
./nginx -t
echo "重启nginx"
./nginx -s reload
echo "停止nginx"
./nginx -s stop
echo "查看是否安装成功,出现版本号就是成功"
./nginx -v
echo "查看是否启动"
netstat -ntlp |grep nginx
sleep 3
echo "====================================配置nginx命令和服务器开机启动====================="
echo "创建服务配置文件"
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
sleep 2
echo "添加执行权限"
chmod -x /usr/lib/systemd/system/nginx.service
nggg="/usr/local/nginx/sbin/nginx"
cp $nggg /usr/local/sbin
cp $nggg /usr/local/bin
systemctl daemon-reload
systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl restart nginx.service
echo "设置开机启动"
systemctl enable nginx.service
echo "防火墙默认开启,加入放行端口80"
systemctl start firewalld
x=`firewall-cmd --zone=public --add-port=80/tcp --permanent`
echo "$x"
echo "重启生效"
i=`firewall-cmd --reload`
echo "$i"
cat <<-EOF
----------------------------------------------------------------------------
+ Nginx 命令控制 +
----------------------------------------------------------------------------
+ nginx -c /path/to/nginx.conf # 以特定目录下的配置文件启动nginx +
+ nginx -s reload # 修改配置后重新加载生效 +
+ nginx -s reopen # 重新打开日志文件 +
+ nginx -s stop # 快速停止nginx +
+ nginx -s quit # 完整有序的停止nginx +
+ nginx -t # 测试当前配置文件是否正确 +
+ nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确 +
----------------------------------------------------------------------------
EOF
systemctl start nginx.service
echo "nginx编译安装完成"