-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
290 lines (253 loc) · 9.06 KB
/
setup.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
286
287
288
289
290
current_dir=/root
sudo apt-get update
sudo apt-get install -y net-tools git systemd python vim curl wget
sudo apt-get install -y dialog apt-utils autoconf automake build-essential libcurl4-openssl-dev libgeoip-dev liblmdb-dev libpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev libssl-dev libxslt-dev libgd-dev libgeoip-dev libaio-dev libaio1 checkinstall libperl-dev
#https://gist.github.com/virtualadrian/732e9baf9513f47a78099a051ec5bd25
#apt-get install \
# bzip2 \
# devscripts \
# flex \
# graphicsmagick-imagemagick-compat \
# graphicsmagick-libmagick-dev-compat \
# libass-dev \
# libatomic-ops-dev \
# libavcodec-dev \
# libavdevice-dev \
# libavfilter-dev \
# libavformat-dev \
# libavutil-dev \
# libbz2-dev \
# libcdio-cdda1 \
# libcdio-paranoia1 \
# libcdio13 \
# libfaac-dev \
# libfreetype6-dev \
# libgeoip1 \
# libgif-dev \
# libgpac-dev \
# libgsm1-dev \
# libjack-jackd2-dev \
# libjpeg-dev \
# libjpeg-progs \
# libjpeg8-dev \
# liblmdb-dev \
# libmp3lame-dev \
# libncurses5-dev \
# libopencore-amrnb-dev \
# libopencore-amrwb-dev \
# libpam0g-dev \
# libpcre3 \
# libpcre3-dev \
# libpng12-dev \
# libpng12-0 \
# libpng12-dev \
# libreadline-dev \
# librtmp-dev \
# libsdl1.2-dev \
# libssl1.0.0 \
# libswscale-dev \
# libtheora-dev \
# libtiff5-dev \
# libva-dev \
# libvdpau-dev \
# libvorbis-dev \
# libxslt1-dev \
# libxslt1.1 \
# libxvidcore-dev \
# libxvidcore4 \
# libyajl-dev \
# make \
# openssl \
# perl \
# pkg-config \
# tar \
# texi2html \
# unzip \
# zip \
# zlib1g-dev
cd /opt
#https://www.nginx.com/blog/compiling-and-installing-modsecurity-for-open-source-nginx/
sudo git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
sudo git submodule init
sudo git submodule update
sudo ./build.sh
sudo ./configure
sudo make
sudo make install
cd /opt
sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
cd /opt
sudo git clone https://github.com/mohamedaymenkarmous/nginx-config
[[ -d "/etc/nginx" ]] && mv /etc/nginx /etc/nginx-$(date +%s).bak
sudo cp -R nginx-config /etc/nginx
sudo mkdir /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/standard /etc/nginx/sites-enabled/standard
sudo mkdir /etc/nginx/ssl
sudo openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
# Download Nginx
cd /opt
NGINX_VERSION=1.16.0
sudo wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
sudo tar zxvf nginx-${NGINX_VERSION}.tar.gz
# Configure and install Nginx with the specified modules
cd nginx-${NGINX_VERSION}
sudo ./configure \
--modules-path=/usr/lib/nginx/modules \
--add-dynamic-module=/opt/ModSecurity-nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--group=www-data \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-log-path=/var/log/nginx/access.log \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--user=www-data \
--with-debug \
--with-file-aio \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module \
--with-http_mp4_module \
--with-http_perl_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-http_xslt_module \
--with-mail \
--with-mail_ssl_module \
--with-pcre-jit \
--with-poll_module \
--with-select_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-http_degradation_module \
--with-threads \
--with-compat
sudo mkdir -p /var/lib/nginx
sudo mkdir -p /var/lib/nginx/body
sudo mkdir -p /var/lib/nginx/fastcgi
sudo make
sudo make modules
sudo make install
# Update ModSecurity dynamic module in Nginx modules directory
sudo mkdir /etc/nginx/modules
sudo cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules
sudo cp objs/ngx_http_modsecurity_module.so /usr/share/nginx/modules
# Load ModSecurity dynamic module in Nginx configuration file
sudo sed -i 's@#load_module modules/ngx_http_modsecurity_module.so@load_module modules/ngx_http_modsecurity_module.so@' /etc/nginx/nginx.conf
# Enable Nginx as a service in each reboot
sudo mv ${current_dir}/nginx.service /lib/systemd/system/nginx.service
sudo systemctl daemon-reload
sudo wget https://www.linode.com/docs/assets/660-init-deb.sh -P /opt
sudo mv /opt/660-init-deb.sh /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults
#sudo service nginx restart
# Enable OWASP ModSecurity rules
cd /etc/nginx/conf.d/
sudo git clone https://github.com/coreruleset/coreruleset.git
sudo cp coreruleset/crs-setup.conf.example coreruleset/crs-setup.conf
sudo cp coreruleset/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example coreruleset/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
sudo cp coreruleset/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example coreruleset/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
# Enable ModSecurity main rules
sudo cp /opt/ModSecurity/modsecurity.conf-recommended /etc/nginx/conf.d/modsecurity.conf
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/nginx/conf.d/modsecurity.conf
sudo cp /opt/ModSecurity/unicode.mapping /etc/nginx/conf.d/
# Update OWASP ModSecurity extra rules
sudo /etc/nginx/conf.d/coreruleset/util/upgrade.py --crs
# Enable ModSecurity dynamic module in a specific site
#sudo sed -i 's/#modsecurity on/modsecurity on/' /etc/nginx/sites-enabled/standard
# Restart nginx
#sudo service nginx restart
#http://www.crop11.com.br/wiki/instalando-nginx-com-suporte-a-pagespeed-no-debian-9-stretch/
#https://www.techrepublic.com/article/how-to-install-and-enable-modsecurity-with-nginx-on-ubuntu-server/
#Upgrade php packages (>7.0)
sudo apt-get install apt-transport-https lsb-release ca-certificates
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/php.list
sudo apt-get update
sudo apt-get install -y php-fpm
sudo mkdir -p /var/www/public/standard
echo hello | sudo tee -a /var/www/public/index.html
# Using SSL
# Stopping Nginx
sudo service nginx stop
sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt --depth=1
cd /opt/letsencrypt
sudo git pull
# https://www.exratione.com/2016/06/a-simple-setup-and-installation-script-for-lets-encrypt-ssl-certificates/
# Install the dependencies.
sudo /opt/letsencrypt/certbot-auto --noninteractive --os-packages-only
# Set up config file.
sudo mkdir -p /etc/letsencrypt
sudo tee /etc/letsencrypt/cli.ini << END
# Uncomment to use the staging/testing server - avoids rate limiting.
# server = https://acme-staging.api.letsencrypt.org/directory
# Use a 4096 bit RSA key instead of 2048.
rsa-key-size = 4096
# Set email and domains.
email = [email protected]
domains = ctfsecurinets.com, www.ctfsecurinets.com
# Text interface.
text = True
# No prompts.
non-interactive = True
# Suppress the Terms of Service agreement interaction.
agree-tos = True
# Use the webroot authenticator.
authenticator = standalone
#authenticator = webroot
webroot-path = /var/www/html
END
# Obtain cert.
sudo /opt/letsencrypt/certbot-auto certonly --expand
# Set up daily cron job.
CRON_SCRIPT="/etc/cron.daily/certbot-renew"
sudo cat > "${CRON_SCRIPT}" <<EOF
#!/bin/bash
#
# Renew the Let's Encrypt certificate if it is time. It won't do anything if
# not.
#
# This reads the standard /etc/letsencrypt/cli.ini.
#
# May or may not have HOME set, and this drops stuff into ~/.local.
export HOME="/root"
# PATH is never what you want it it to be in cron.
export PATH="\${PATH}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
sudo service nginx stop
sudo /opt/letsencrypt/certbot-auto --config /etc/letsencrypt/cli.ini --no-self-upgrade certonly
# If the cert updated, we need to update the services using it. E.g.:
if service --status-all | grep -Fq 'nginx'; then
service nginx start
fi
EOF
sudo chmod a+x "${CRON_SCRIPT}"
# Starting nginx
sudo service nginx start
#https://community.letsencrypt.org/t/how-to-completely-automating-certificate-renewals-on-debian/5615
#https://www.grafikart.fr/formations/serveur-linux/nginx-ssl-letsencrypt
#https://neurobin.org/docs/web/fully-automated-letsencrypt-integration-with-cpanel/
#https://www.exratione.com/2016/06/a-simple-setup-and-installation-script-for-lets-encrypt-ssl-certificates/
#https://gist.github.com/ajaegers/92318bdc81541b825c90f265f787e3c8
#https://www.nginx.com/blog/setting-up-nginx-amplify-in-10-minutes/