-
Notifications
You must be signed in to change notification settings - Fork 6
/
Nginx, Laravel, CertBot, Docker (MySQL, Redis, PHPMyAdmin).txt
378 lines (303 loc) · 10.1 KB
/
Nginx, Laravel, CertBot, Docker (MySQL, Redis, PHPMyAdmin).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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
This file contains how the laravel vps deployment works
=======================================================
- We will install Nginx to proxy on the bases of Domain names
- Nginx will serve laravel app & proxy phpmyadmin
- We will install Docker to host phpmyadmin, mysql, Redis & RedisInsight
- We are going to assume database folder in root to host database
- Create all laravel files in /var/www/laravel
- We will install SSL for Larave & PhpMyadmin
(PHPMyadmin will give SSL warning in login page so ignore it)
- Anything starts with $ is a command
##############################################
If you run a command and it does not work, like
get stuck, that mean you are out of RAM. Create
a swap space. Swap space creation commands are
mentioned at the bottom.
##############################################
@@@@@@@@@@
All Ports
@@@@@@@@@@
80 -> Laravel
8080 -> PhpMyadmin
3306 -> MySQL
6379 -> Redis
8001 -> RedisInsight
=======================================================
@@@@@@@@@@@@@@@@@@@@
Login Credientials
@@@@@@@@@@@@@@@@@@@@
MYSQL
Database: laravel_db
Username: laravel_db
Password: kinginthenorth1123
---------------
PhpMyadmin
Server: db
Username: laravel_db
Password: kinginthenorth1123
---------------
RedisInsight
Username: default
Password: mypassword
(change mysql, phpmyadmin & redisinsight creds in compose.yml)
=======================================================
=============================================
Setup Domain Records
Disable proxy for each record in cloudflare
=============================================
1. Go to your domain registrar
2. Go to DNS or Records tab
3. Add 1st A Record as @ to your vps_ip
4. Add 2nd CNAME Record as phpmyadmin to your vps_ip or @
5. Add 3rd CNAME Record as insight to your vps_ip or @
Install Nginx, Docker/Compose, CertBot
==============================
$ sudo apt update && sudo apt upgrade -y && sudo apt install curl zip unzip wget nginx htop -y
$ curl -fsSL https://get.docker.com -o install-docker.sh && sudo sh install-docker.sh
$ sudo apt-get install certbot python3-certbot-nginx -y
Install Composer & Modules for Laravel
==============================
This is the bash script!
==============================
$ nano composer.sh
#!/bin/bash
sudo apt install php-cli php-fpm php-zip php-gd phpunit php-mysql php-pear php-dev php-xml php-pdo php-curl php-common php-mbstring -y
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
$ chmod +x composer.sh
$ ./composer.sh
Install Nodejs
==============================
This is the bash script!
==============================
$ nano node.sh
#!/bin/bash
VER="22.12.0"
sudo apt-get install nodejs curl xz-utils tar wget -y
wget "https://nodejs.org/dist/v${VER}/node-v${VER}-linux-x64.tar.xz"
sudo tar -xf node-v${VER}-linux-x64.tar.xz
sleep 3
cd node-v${VER}-linux-x64
sudo cp -r bin /usr/
sudo cp -r lib /usr/
sudo cp -r include /usr/
sudo cp -r share /usr/
echo "Node JS Downloaded, Installed and Updated to v${VER}"
node -v
cd ..
sudo rm -r node-v${VER}-linux-x64
rm node-v${VER}-linux-x64.tar.xz
$ chmod +x node.sh
$ ./node.sh
=========================================================
Create folder to host our Docker Compose, MySQL, Starter
Do this in root
=========================================================
$ mkdir database
$ cd database
========================================
Now create Docker compose file
========================================
Create this file in a safe place
Docker compose file code!
It will auto download images
name the file compose.yml (new update)
Run command is at the end
========================================
$ nano compose.yml
services:
db:
image: mysql
restart: always
environment:
MYSQL_DATABASE: laravel_db
MYSQL_USER: laravel_db
MYSQL_PASSWORD: kinginthenorth1123
MYSQL_ROOT_PASSWORD: kinginthenorth1123
ports:
- 3306:3306
volumes:
- ./db_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
restart: always
depends_on:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
ports:
- 8080:80
redis-stack-server:
image: redis/redis-stack:latest
container_name: redis-stack
environment:
REDIS_ARGS: "--requirepass mypassword"
ports:
- "6379:6379"
- "8001:8001"
volumes:
- ./redis_data:/data
restart: unless-stopped
volumes:
db_data:
redis_data:
$ docker compose up -d
=====================================
Create laravel project & configure
Or upload laravel file using filezilla
=====================================
$ cd /var/www/
(Upload code Or use laravel installer like below)
$ composer global require "laravel/installer"
$ export PATH="~/.config/composer/vendor/bin:$PATH"
$ laravel new laravel
- Laravel Breeze or any option
- Blade with AlpineJS or any option
- Yes or No, any you want
- Pest
- No, for Git repository
- MySQL
- No, don't migrate database yet
$ sudo chown -R www-data:www-data /var/www/laravel/storage
$ sudo chown -R www-data:www-data /var/www/laravel/bootstrap/cache
$ cd /var/www/laravel
$ composer require predis/predis:^2.0
$ nano .env
(Change keys to following)
---------------
APP_ENV=production
APP_DEBUG=false
APP_URL=https://your-domain.com
APP_MAINTENANCE_STORE=redis
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_db
DB_PASSWORD="kinginthenorth1123"
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
CACHE_STORE=redis
REDIS_CLIENT=predis
REDIS_PASSWORD="mypassword"
(If you change creds then change accordingly)
$ php artisan migrate
=====================================
Let's configure Nginx & Proxy
Main Domain Nginx (create Domain files)
=====================================
$ nano /etc/nginx/sites-available/your-domain.com.conf
server {
listen 80;
server_name your-domain.com;
root /var/www/laravel/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
client_max_body_size 120M; #Upload large files up to 120MB
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# (please check your php-fpm version and change it)
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
PHPMyAdmin Domain Nginx (create Domain files)
=================================
$ nano /etc/nginx/sites-available/phpmyadmin.your-domain.com.conf
server {
listen 80;
server_name phpmyadmin.your-domain.com;
location / {
proxy_pass http://0.0.0.0:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
RedisInsight Domain Nginx (create Domain files)
=================================
$ nano /etc/nginx/sites-available/insight.your-domain.com.conf
server {
listen 80;
server_name insight.your-domain.com;
location / {
proxy_pass http://0.0.0.0:8001;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
$ sudo ln -s /etc/nginx/sites-available/your-domain.com.conf /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/phpmyadmin.your-domain.com.conf /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/insight.your-domain.com.conf /etc/nginx/sites-enabled/
(Test nginx syntax)
$ sudo nginx -t
$ sudo systemctl restart nginx
$ sudo certbot --nginx -d your-domain.com
$ sudo certbot --nginx -d phpmyadmin.your-domain.com
$ sudo certbot --nginx -d insight.your-domain.com
===========================================
----Optional----
Configure the firewall (Don't close console)
===========================================
$ sudo apt install ufw -y
$ sudo ufw allow 80
$ sudo ufw allow 443
$ sudo ufw allow 22
$ sudo ufw enable
(press y then Enter)
===================================================
----Optional but you should----
Increase Upload file size in PHP CLI & FPM
8.3 is the php version. Change it to your version
(you have to change Nginx too to upload large files)
==================================================
$ nano /etc/php/8.3/cli/php.ini
upload_max_filesize = 120M # 120Mb File upload
post_max_size = 120M # 120Mb File upload
max_execution_time = 300
max_input_time = 300
$ nano /etc/php/8.3/fpm/php.ini
upload_max_filesize = 120M # 120Mb File upload, same as above
post_max_size = 120M # 120Mb File upload, same as above
max_execution_time = 300
max_input_time = 300
$ sudo systemctl restart nginx
$ sudo systemctl restart php8.3-fpm
==============================================
----Optional----
Create swap space, Out of memory protection
5GB SWAP Space. You can change 5 to increase it
==============================================
$ sudo fallocate -l 5G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
$ sudo cp /etc/fstab /etc/fstab.bak
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
$ htop