-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.sh
285 lines (243 loc) · 6.76 KB
/
install.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
# 选择安装 WAF
cat << EOF
ngx_lua_waf is a web application firewall based on lua-nginx-module
EOF
read -p "install waf ? (Y/n): " input
case $input in
n)
waf=0
waf_mod_1=''
waf_mod_2=''
;;
*)
waf=1
waf_mod_1='--add-module=../lua-nginx-module'
waf_mod_2='--add-module=../ngx_devel_kit'
;;
esac
# 安装依赖
if [ -e "/usr/bin/yum" ]; then
yum update -y
yum install git gcc make build-essential logrotate cron -y
fi
if [ -e "/usr/bin/apt-get" ]; then
apt-get update -y
apt-get install git gcc make build-essential logrotate cron -y
fi
# 准备
rm -rf /usr/src/
mkdir -p /usr/src/
mkdir -p /var/log/nginx/
useradd -s /sbin/nologin -M www-data
# 下载 openssl
# 开启 https
cd /usr/src
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1.tar.gz
tar xzvf OpenSSL_1_1_1.tar.gz
mv openssl-OpenSSL_1_1_1 openssl
# 下载 nginx
cd /usr/src/
nginx_v='1.17.3'
wget https://nginx.org/download/nginx-${nginx_v}.tar.gz
tar zxvf ./nginx-${nginx_v}.tar.gz
mv nginx-${nginx_v} nginx
# 下载 zlib
# 开启 gzip 压缩
cd /usr/src/
git clone https://github.com/cloudflare/zlib.git zlib
cd zlib
make -f Makefile.in distclean
# 下载 ngx_brotli
# 开启 brotli 压缩
cd /usr/src/
git clone --recursive https://github.com/google/ngx_brotli.git
# 下载 pcre
# 用于正则
cd /usr/src/
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar zxf ./pcre-8.43.tar.gz
# 下载 openssl-patch
# 给 openssl 打补丁,用于开启更多 https 支持
cd /usr/src/
git clone https://github.com/hakasenyang/openssl-patch.git
cd /usr/src/openssl
patch -p1 < ../openssl-patch/openssl-equal-1.1.1a_ciphers.patch
cd /usr/src/
git clone https://github.com/kn007/patch.git nginx-patch
cd /usr/src/nginx
patch -p1 < ../nginx-patch/nginx.patch
# 下载安装 jemalloc
# 更好的内存管理
cd /usr/src/
wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2
tar xjvf jemalloc-5.2.1.tar.bz2
cd jemalloc-5.2.1
./configure
make && make install
echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
ldconfig
if [ $waf -eq 1 ]; then
# 下载 ngx_lua_waf 防火墙的各种依赖及模块
cd /usr/src/
luajit_v='2.1-20190626'
wget https://github.com/openresty/luajit2/archive/v${luajit_v}.tar.gz
tar xzvf v${luajit_v}.tar.gz
mv luajit2-${luajit_v} luajit
wget https://github.com/openresty/lua-cjson/archive/2.1.0.7.tar.gz
tar xzvf 2.1.0.7.tar.gz
mv lua-cjson-2.1.0.7 lua-cjson
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
tar xzvf v0.3.1rc1.tar.gz
mv ngx_devel_kit-0.3.1rc1 ngx_devel_kit
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
tar xzvf v0.10.15.tar.gz
mv lua-nginx-module-0.10.15 lua-nginx-module
# 编译安装 luajit
cd luajit
make -j2 && make install
# echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
ldconfig
# 编译安装 lua-cjson
cd /usr/src/lua-cjson
export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.1
make -j2 && make install
# 设置 LUAJIT 环境变量
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1
fi
# 关闭 nginx 的 debug 模式
sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' /usr/src/nginx/auto/cc/gcc
# 编译安装 nginx
cd /usr/src/nginx
./configure \
--user=www-data --group=www-data \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--with-compat --with-file-aio --with-threads \
--with-http_v2_module --with-http_v2_hpack_enc \
--with-http_realip_module \
--with-http_flv_module --with-http_mp4_module \
--with-openssl=../openssl --with-http_ssl_module \
--with-pcre=../pcre-8.43 --with-pcre-jit \
--with-zlib=../zlib --with-http_gzip_static_module \
--add-module=../ngx_brotli \
--with-ld-opt=-ljemalloc ${waf_mod_1} ${waf_mod_2}
# --add-module=../lua-nginx-module \
# --add-module=../ngx_devel_kit
make -j2 && make install
# 下载配置 ngx_lua_waf
cd /usr/local/nginx/conf/
rm -rf /usr/local/nginx/conf/waf
git clone https://github.com/xzhih/ngx_lua_waf.git waf
mkdir -p /usr/local/nginx/logs/waf
chown www-data:www-data /usr/local/nginx/logs/waf
cat > /usr/local/nginx/conf/waf.conf << EOF
lua_load_resty_core off;
lua_shared_dict limit 20m;
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/nginx/conf/waf/access.lua";
EOF
# 创建 nginx 全局配置
cat > "/usr/local/nginx/conf/nginx.conf" << OOO
user www-data www-data;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
use epoll;
multi_accept on;
worker_connections 65535;
}
http {
charset utf-8;
sendfile on;
aio threads;
directio 512k;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
client_max_body_size 16M;
# MIME
include mime.types;
default_type application/octet-stream;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
# Gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# Brotli
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
include vhost/*.conf;
include waf.conf;
}
OOO
# 创建 nginx 服务进程
mkdir -p /usr/lib/systemd/system/
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
EOF
# 创建 nginx 日志规则 (自动分割)
cat > /etc/logrotate.d/nginx << EOF
/var/log/nginx/*.log {
daily
missingok
rotate 52
delaycompress
notifempty
create 640 www-data www-data
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 \`cat /var/run/nginx.pid\`
fi
endscript
}
EOF
ldconfig
# 配置默认站点
mkdir -p /wwwroot/
cp -r /usr/local/nginx/html /wwwroot/default
mkdir -p /usr/local/nginx/conf/vhost/
mkdir -p /usr/local/nginx/conf/ssl/
cat > "/usr/local/nginx/conf/vhost/default.conf" << EEE
server {
listen 80;
root /wwwroot/default;
location / {
index index.html;
}
}
EEE
# 配置站点目录权限
chown -R www-data:www-data /wwwroot/
find /wwwroot/ -type d -exec chmod 755 {} \;
find /wwwroot/ -type f -exec chmod 644 {} \;
# 开启 nginx 服务进程
systemctl unmask nginx.service
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx