-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotes.txt
334 lines (254 loc) · 10.1 KB
/
notes.txt
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
##########################
## LAB 0 Catatan ##
##########################
- Lab ini menggunakan sistem operasi Ubuntu 20.04
- jika hendak praktek menggunakan sistem operasi lain silakan menyesuaikan sendiri
- silakan siapkan VM masing-masing dengan spesifikasi minimal
2 vCPU, 2GB RAM, 50GB Disk
##########################
## LAB 1 Install Nginx ##
##########################
### 1a. install dari kode sumber
## contoh kasus, hendak custom informasi server pada header response
$ curl -I https://www.narasi.id/
HTTP/2 200
content-type: text/html; charset=UTF-8
expires: Sat, 25 Feb 2023 18:19:55 GMT
date: Sat, 25 Feb 2023 18:19:55 GMT
cache-control: private, max-age=0
last-modified: Thu, 16 Feb 2023 13:27:07 GMT
etag: "207b02f2-f2b0-4c4e-a79a-51094f7fd30a"
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
content-length: 0
server: GSE
# pindah ke user root
sudo -i
# install beberapa dependency yang diperlukan untuk melakukan compile source nginx
apt update -y && apt-get install git build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev
# unduh source code nginx dari situs resminya
cd /usr/src
wget http://nginx.org/download/nginx-1.23.3.tar.gz -O nginx-1.23.3.tar.gz
# lalu ekstrak dengan perintah tar
tar -xvzf nginx-1.23.3.tar.gz
cd nginx-1.23.3
# untuk kasus ini kita akan mengubah header server-nya pada berkas nginx.h berikut
nano src/core/nginx.h
# ubah baris berikut
#define NGINX_VER "nginx/" NGINX_VERSION
# menjadi
#define NGINX_VER "Nacita"
# ganti kata NaCita menjadi apapun yang diinginkan
# eksekusi perintah configure berikut untuk memberitahu compiler bagaimana aplikasi ini akan dicompile
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--with-pcre \
--lock-path=/var/lock/nginx.lock \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_image_filter_module=dynamic \
--modules-path=/etc/nginx/modules \
--with-http_v2_module \
--with-stream=dynamic \
--with-http_addition_module \
--with-http_mp4_module
# lakukan proses compile dan install
make && make install
# periksa versi nginx yang telah terinstall
nginx -V
# contoh outputnya seperti ini
root@samsul-lab3:/usr/src/nginx-1.23.3# nginx -V
nginx version: Nacita
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module
# buat berkas systemd service
nano /lib/systemd/system/nginx.service
# isinya sebagai berikut
[Unit]
Description=Nginx Custom From Source
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# lakukan reload agar terbaca oleh systemd
systemctl daemon-reload
# jalankan nginx dengan systemctl
systemctl enable nginx
systemctl start nginx
systemctl status nginx
## buat log rotation agar berkas log web server tidak membengkak
nano /etc/logrotate.d/nginx
# konfigurasi logrotate sebagai berikut
/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0640 root root
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
# akses melalui web browser
http://<nginx-ip-address>
### 1b. Install dari repository
# jika pusing dengan cara 1a di atas, bisa lakukan dengan cara berikut
sudo apt update && sudo apt install nginx
# coba akses melalui browser atau dengan curl
curl localhost
#####################################
## LAB 2 Siapkan aplikasi backend ##
#####################################
### 1. Siapkan aplikasi backend dengan docker & docker compose
# install docker terlebih dahulu
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# berikutnya siapkan berkas docker-compose.yml
mkdir backend-apps && cd backend-apps
wget https://raw.githubusercontent.com/nacita/nest-nginx-workshop/main/docker-compose.yml
# jalankan dengan docker compose
sudo docker compose up -d
# periksa listen port, dan pastikan port 5001, 5002, dan 5003 tersedia
ss -tulpn | grep 500
tcp LISTEN 0 4096 0.0.0.0:5001 0.0.0.0:*
tcp LISTEN 0 4096 0.0.0.0:5002 0.0.0.0:*
tcp LISTEN 0 4096 0.0.0.0:5003 0.0.0.0:*
tcp LISTEN 0 4096 [::]:5001 [::]:*
tcp LISTEN 0 4096 [::]:5002 [::]:*
tcp LISTEN 0 4096 [::]:5003 [::]:*
# jika muncul list seperti di atas, artinya aplikasi sudah berjalan dan siap digunakan
##############################
## LAB 3 Konfigurasi Nginx ##
##############################
### Nginx sebagai web server static
sudo wget -O /var/www/html/index.html https://raw.githubusercontent.com/nacita/nest-nginx-workshop/main/example-html/registration.html
### Nginx sebagai Reverse Proxy
# pindah ke user root
sudo -i
# hapus konfigurasi default nginx
rm /etc/nginx/sites-enabled/default
# buat sebuah konfigurasi virtualhost pada direktori /etc/nginx/sites-available
cd /etc/nginx/sites-available
nano nest-proxy.conf
# dengan konfigurasi sebagai berikut
server {
listen 80;
server_name nginx.nestacademy.ok;
location / {
proxy_pass http://localhost:5001/;
}
}
# simpan, lalu buat symlink konfigurasi agar konfigurasinya aktif
ln -s /etc/nginx/sites-available/nest-proxy.conf /etc/nginx/sites-enabled/nest-proxy.conf
# verifikasi konfigurasi tersebut, dan pastikan syntax-nya ok
nginx -t
# CATAT: selalu lakukan hal tersebut setiap kali mengubah konfigurasi nginx
# berikutnya restart service nginx dengan systemctl
systemctl restart nginx
# akses melalui browser kesayangan Anda
http://ip-server
### Nginx sebagai Load Balancer
# nah, karena kita sudah memiliki aplikasi yang telah terpasang pada lab sebelumnya (menggunakan docker compose),
# kita juga dapat menggunakan nginx sebagai loadbalancer
# hapus konfigurasi sebelumnya (atau tidak usah)
rm /etc/nginx/sites-enabled/nest-proxy.conf
# buat konfigurasi baru
vim /etc/nginx/sites-available/nest-lb.conf
# konfigurasinya sebagai berikut
upstream nestacademy {
server 127.0.0.1:5001;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
}
server {
listen 80;
server_name nginx-lb.nestacademy.ok;
location / {
proxy_pass http://nestacademy;
}
}
# simpan, lalu buat juga symlink
ln -s /etc/nginx/sites-available/nest-lb.conf /etc/nginx/sites-enabled/nest-lb.conf
# verifikasi dan restart nginx
nginx -t
systemctl restart nginx
# akses melalui browser, lalu refresh berkali-kali atau curl
curl http://nginx-lb.nestacademy.ok
# algoritma loadbalancing default jika tidak kita tambahkan konfigurasi khusus adalah round-robin
# trafik akan diteruskan ke backend secara acak
# untuk menggunakan algoritma lainnya, kita perlu tambahkan konfigurasi
## least-con
# algoritma least-con : teruskan request ke server yang memiliki koneksi paling sedikit, tambahkan least_conn pada baris berikut
upstream nestacademy {
least_conn;
server 127.0.0.1:5001;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
}
# simpan, lalu restart lagi nginx-nya
# uji kembali dengan browser atau curl
## ip-hash
# algoritma ip-hash : menggabungkan trafik sumber (klien) dan tujuan (server), dan menggunakan fungsi matematika untuk mengubahnya menjadi hash, koneksi lalu ditetapkan ke server tertentu, berikut konfigurasinya
upstream nestacademy {
ip_hash;
server 127.0.0.1:5001;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
}
# simpan, lalu restart lagi nginx-nya
# uji dengan browser, secara hasil mungkin tidak terlihat
## weighted loadbalancer
# algoritma weighted roun-robin: mirip dengan round-robin, hanya saja untuk beberapa server bisa kita atur perbandingan bebannya,
# kurang lebih seperti berikut konfigurasinya
upstream nestacademy {
server 127.0.0.1:5001 weight=3;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
}
# simpan, lalu restart lagi nginx-nya
# penjelasan singkat: setiap ada 5 request, 3 request akan diteruskan ke server pertama, lalu 1 request di server kedua,
# dan 1 request sisanya akan diteruskan ke server ketiga
##############################
## LAB 4 Mengamankan Nginx ##
##############################
# Implementasi SSL/TLS --> https://wiki.samsul.web.id/linux/LetsEncrypt.SSL.Wildcard
# Sembunyikan informasi versi nginx
# Nonaktifkan modul nginx yang tidak terpakai (perlu compile sendiri)
# Set Client Buffer Size Limitations
# Nonaktifkan HTTP Method yang tidak digunakan
# Install modul ModSecurity --> https://wiki.samsul.web.id/linux/Install.dan.Konfigurasi.Nginx.ModSecurity
# Implementasikan security header
# Nonaktifkan SSL Protocol yang sudah kadaluarsa