nginx自动化安装脚本
以nginx-1.17.9版本为示例,需要提前下载好nignx安装包
nginx安装之前需要的一些步骤
系统参数调整
vi /etc/sysctl.conf,在末尾添加
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
sysctl -p 生效
修改openfile打开最大数
编辑vi /etc/security/limits.conf文件在末尾添加如下两行(将打开的文件数修改为65535)
* soft nofile 65535
* hard nofile 65535
此项配置完成需要执行reboot来重启服务器,否则修改不生效。
可通过ulimit -a命令查看是否生效
#!/bin/bash
#变量配置部分
#nginx安装基于root用户安装 nginx基本库
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl--devel openssl openssl-devel-y
echo -e "\033[32m########## yum install finish ##########\033[0m"
#nginx安装文件名
NGINX_TARNAME=/home/nginx/nginx-1.17.9.tar.gz
#解压文件夹名称
NGINX_FILENAME=nginx-1.17.9
#nginx安装路径
NGINX_PREFILE=/home/nginx/nginx8000/
#安装部分
#基础运行库安装
#解压nginx文件
echo $NGINX_TARNAME
tar -xvzf $NGINX_TARNAME
#源文件目录
cd $NGINX_FILENAME
#创建文件夹
if [ ! -d $NGINX_PREFILE ];then
echo -e "\033[32mThe directory is not exist.\033[0m" #判断备份文件夹是否存在,不存在则创建
mkdir -p $NGINX_PREFILE
else
echo -e "\033[32mThe directory is exist.\033[0m"
fi
echo -e "\033[32m##########Start install nginx ##########\033[0m"
#安装 具体插件可根据实际生产环境进行配置在此不一一列举
./configure --prefix=$NGINX_PREFILE --with-http_flv_module --with-http_mp4_module
#编译
make
make install
#启动
cd $NGINX_PREFILE/sbin
./nginx
#进程查看
ps -ef|grep nginx
echo -e "\033[32m########## install nginx finish ##########\033[0m"
常用插件参数
本模块提供FLV文件加载基于时间位移.
--with-http_flv_module
mp4插件
--with-http_mp4_module
ssl证书模块
--with-http_ssl_module --with-http_stub_status_module
gzip模块
--with-http_gzip_static_module
完整配置
--prefix=/home/nginx/nginx --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
自带脚本启动
cd sbin #nginx的脚本执行目录
./nginx 启动nginx
./nginx -s stop 停止nginx
./nginx -s quite 断开停止nginx
./nginx -s reload 重载nginx配置文件
#!/bin/sh
#nginx自定义启动脚本
if [ $(whoami) != "root" ]
then
echo "Shell must be excuted by root!"
exit 1
fi
cd ~/nginx/sbin
echo "=================================="
ps -ef | grep nginx| grep ./nginx | grep -v grep
processnum=$(ps -ef | grep nginx| grep ./nginx | grep -v grep | wc -l)
if [ $processnum -gt 0 ]
then
echo "nginx is running"
exit 1
fi
./nginx
echo "=================================="
ps -ef | grep nginx| grep ./nginx | grep -v grep
processnum=$(ps -ef | grep nginx| grep ./nginx | grep -v grep | wc -l)
if [ $processnum -eq 0 ]
then
echo "========startup nginx FAIL!!!!!!!========================="
else
echo "========startup nginx success========================="
fi
#!/bin/bash
if [ $(whoami) != "root" ]
then
echo "Shell must be excuted by root!"
exit 1
fi
cd ~/nginx/sbin
./nginx -s stop
sleep 5
echo "=================================="
ps -ef | grep nginx| grep ./nginx | grep -v grep
processnum=$(ps -ef | grep nginx| grep ./nginx | grep -v grep | wc -l)
if [ $processnum -gt 0 ]
then
ps -ef | grep nginx| grep ./nginx | grep -v grep | cut -c 9-15 | xargs kill -9
fi
echo "========stop nginx success========================="