-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx_onekey.sh
266 lines (243 loc) · 6.83 KB
/
nginx_onekey.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
############### CentOS一键安装Nginx脚本 ###############
#Author:xiaoz.me
#Update:2019-03-20
####################### END #######################
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/sbin
export PATH
dir='/usr/local/'
#定义nginx版本
nginx_version='1.16.1'
#定义openssl版本
openssl_version='1.1.1c'
#定义pcre版本
pcre_version='8.43'
#对系统进行判断
function check_os(){
#CentOS
if test -e "/etc/redhat-release"
then
yum -y install gcc gcc-c++ perl unzip
#Debian
elif test -e "/etc/debian_version"
then
apt-get -y install perl unzip
apt-get -y install build-essential
else
echo "当前系统不支持!"
fi
}
#获取服务器公网IP
function get_ip(){
osip=$(curl https://api.ttt.sh/ip/qqwry/?type=ip)
echo $osip
}
#防火墙放行端口
function chk_firewall(){
if [ -e "/etc/sysconfig/iptables" ]
then
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
service iptables save
service iptables restart
else
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
fi
}
#防火墙删除端口
function DelPort(){
if [ -e "/etc/sysconfig/iptables" ]
then
sed -i '/^.*80/d' /etc/sysconfig/iptables
sed -i '/^.*443/d' /etc/sysconfig/iptables
service iptables save
service iptables restart
else
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --zone=public --remove-port=443/tcp --permanent
firewall-cmd --reload
fi
}
#安装jemalloc优化内存管理
function jemalloc(){
wget http://soft.xiaoz.org/linux/jemalloc-5.2.0.tgz
tar -zxvf jemalloc-5.2.0.tgz
cd jemalloc-5.2.0
./configure
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig
}
#安装依赖环境
function depend(){
#安装pcre
cd ${dir}
wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-${pcre_version}.tar.gz
tar -zxvf pcre-${pcre_version}.tar.gz
cd pcre-${pcre_version}
./configure
make -j4 && make -j4 install
#安装zlib
cd ${dir}
wget http://soft.xiaoz.org/linux/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make -j4 && make -j4 install
#安装openssl
cd ${dir}
wget --no-check-certificate -O openssl.tar.gz https://www.openssl.org/source/openssl-${openssl_version}.tar.gz
tar -zxvf openssl.tar.gz
cd openssl-${openssl_version}
./config
make -j4 && make -j4 install
}
#清理工作
#编译安装Nginx
function CompileInstall(){
#创建用户和用户组
groupadd www
useradd -M -g www www -s /sbin/nologin
#rm -rf /usr/local/pcre-8.39.tar.gz
#rm -rf /usr/local/zlib-1.2.11.tar.gz
#rm -rf /usr/local/openssl-1.1.0h.tar.gz
#下载stub_status_module模块
cd /usr/local
wget http://soft.xiaoz.org/nginx/ngx_http_substitutions_filter_module.zip
unzip ngx_http_substitutions_filter_module.zip
#下载purecache模块
cd /usr/local && wget http://soft.xiaoz.org/nginx/ngx_cache_purge-2.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz
mv ngx_cache_purge-2.3 ngx_cache_purge
#下载brotli
wget http://soft.xiaoz.org/nginx/ngx_brotli.tar.gz
tar -zxvf ngx_brotli.tar.gz
#安装Nginx
cd /usr/local
wget https://wget.ovh/nginx/xcdn-${nginx_version}.tar.gz
tar -zxvf xcdn-${nginx_version}.tar.gz
cd xcdn-${nginx_version}
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-stream \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-ld-opt=-ljemalloc \
--with-pcre=../pcre-${pcre_version} \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-${openssl_version} \
--add-module=../ngx_http_substitutions_filter_module \
--add-module=../ngx_cache_purge \
--add-module=../ngx_brotli
make -j4 && make -j4 install
#一点点清理工作
rm -rf ${dir}xcdn-1.*
rm -rf ${dir}zlib-1.*
rm -rf ${dir}pcre-8.*
rm -rf ${dir}openssl*
rm -rf ${dir}ngx_http_substitutions_filter_module*
rm -rf ${dir}ngx_cache_purge*
rm -rf ${dir}ngx_brotli*
rm -rf nginx.tar.gz
rm -rf nginx.1
cd
rm -rf jemalloc*
#复制配置文件
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
wget --no-check-certificate https://raw.githubusercontent.com/helloxz/nginx-cdn/master/nginx.conf -P /usr/local/nginx/conf/
#日志分割
wget --no-check-certificate https://raw.githubusercontent.com/helloxz/nginx-cdn/master/etc/logrotate.d/nginx -P /etc/logrotate.d/
mkdir -p /usr/local/nginx/conf/vhost
mkdir -p /usr/local/nginx/conf/cdn
/usr/local/nginx/sbin/nginx
#环境变量与服务
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
export PATH=$PATH:'/usr/local/nginx/sbin'
#开机自启
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
echo "Nginx installed successfully. Please visit the http://${osip}"
}
#二进制安装Nginx
function BinaryInstall(){
#创建用户和用户组
groupadd www
useradd -M -g www www -s /sbin/nologin
#下载到指定目录
wget http://soft.xiaoz.org/nginx/xcdn-binary-${nginx_version}.tar.gz -O /usr/local/nginx.tar.gz
#解压
cd /usr/local && tar -zxvf nginx.tar.gz
#日志自动分割
wget --no-check-certificate https://raw.githubusercontent.com/helloxz/nginx-cdn/master/etc/logrotate.d/nginx -P /etc/logrotate.d/
#环境变量
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
export PATH=$PATH:'/usr/local/nginx/sbin'
#启动
/usr/local/nginx/sbin/nginx
#开机自启
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
echo "------------------------------------------------"
echo "Nginx installed successfully. Please visit the http://${osip}"
}
#卸载Nginx
function uninstall(){
# 杀掉nginx进程
pkill nginx
#删除www用户
userdel www && groupdel www
#备份一下配置
cp -a /usr/local/nginx/conf/vhost /home/vhost_bak
#删除目录
rm -rf /usr/local/nginx
sed -i "s%:/usr/local/nginx/sbin%%g" /etc/profile
#删除自启
sed -i '/^.*nginx/d' /etc/rc.d/rc.local
#删除日志分割
rm -rf /etc/logrotate.d/nginx
}
#选择安装方式
echo "------------------------------------------------"
echo "欢迎使用Nginx一键安装脚本^_^,请先选择安装方式:"
echo "1) 编译安装,支持CentOS 6/7"
echo "2) 二进制安装,支持CentOS 7"
echo "3) 卸载Nginx"
echo "q) 退出!"
read -p ":" istype
case $istype in
1)
check_os
get_ip
chk_firewall
#安装jemalloc
jemalloc
#安装依赖
depend
#安装nginx
CompileInstall
;;
2)
check_os
get_ip
chk_firewall
#安装jemalloc
jemalloc
BinaryInstall
;;
3)
#执行卸载函数
uninstall
#删除端口
DelPort
echo 'Uninstall complete.'
;;
q)
exit
;;
*) echo '参数错误!'
esac