nginx部署脚本




 1 #!/bin/bash
 2 
 3 # nginx安装包链接
 4 nginx_url="http://nginx.org/download/nginx-1.18.0.tar.gz"
 5 # 下载包存放路径
 6 path="/tmp/"
 7 # 安装路径
 8 int_path="/usr/local/nginx"
 9 
10 # 首先检查网络
11 ping -c 1 114.114.114.114 > /dev/null 2>&1
12 if [ $? -eq 0 ];then
13     echo "检测网络正常!"
14 
15     # 配置阿里源
16     read -p "配置阿里源输入1,任意键跳过配置:" number
17     case "$number" in
18         1)
19           echo "正在配置阿里源..."
20           mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup  > /dev/null 2>&1
21           wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo > /dev/null 2>&1
22           yum clean all > /dev/null 2>&1
23           yum makecache > /dev/null 2>&1
24           echo "阿里源配置已完成!"
25         ;;
26         *)
27         echo "已跳过配置阿里源!"
28     esac
29 
30     # 安装依赖包
31     echo "正在安装依赖包..."
32     yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel wget > /dev/null 2>&1
33     if [ $? -eq 0 ];then
34         echo "依赖包安装已完成!"
35 
36         # 下载Nginx包
37         echo "正在下载安装包和解压安装操作..."
38         wget $nginx_url -P $path > /dev/null 2>&1
39         # 创建文件夹,解压安装
40         mkdir $int_path && cd $int_path
41         nginx_pack=`echo $nginx_url | awk -F '/' '{print $NF}'`
42         tar -xf $path/$nginx_pack -C ./
43 
44         # 编译安装
45         nginx_path=`echo $nginx_pack |awk -F '.' '{print $1"."$2"."$3}'`
46         cd $nginx_path && ./configure > /dev/null 2>&1
47         make > /dev/null 2>&1
48         make install  > /dev/null 2>&1
49         echo "Nginx 安装已完成!"
50         echo -e "#####################################\n启动Nginx: $int_path/sbin/nginx\n停止Nginx: $int_path/sbin/nginx -s stop\n重载Nginx: $int_path/sbin/nginx -s reload\n检查Nginx: $int_path/sbin/nginx -t\n#####################################"
51     else
52         echo "依赖包安装失败,请检查yum源或者网络问题!!!"
53         exit 1
54     fi
55 else
56     echo "检测网络连接异常,请检查网络再操作!"
57     exit 1
58 fi
59 
60 cat > cd /lib/systemd/system/nginx.service << EOF
61 [Unit]
62 Description=nginx service
63 After=network.target 
64    
65 [Service] 
66 Type=forking 
67 ExecStart=/usr/local/nginx/sbin/nginx
68 ExecReload=/usr/local/nginx/sbin/nginx -s reload
69 ExecStop=/usr/local/nginx/sbin/nginx -s quit
70 PrivateTmp=true 
71    
72 [Install] 
73 WantedBy=multi-user.target
74 EOF
75 systemctl enable nginx


 

脚本执行过程:

启动nginx

访问测试

nginx的配置说明

https://www.jb51.net/article/79216.htm