Skip to content

Commit 32db201

Browse files
committed
Configure the Docker to restart the app automatically #15
- setup ssl - add examples of SEO nginx settings - configure pm2-runtime to run application on production
1 parent 55fc1e4 commit 32db201

12 files changed

+180
-90
lines changed

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,22 @@ git clone [email protected]:lazy-ants/angular-universal.git
77
cd angular-universal
88
```
99

10-
## CREATE APP CONFIG FILES
11-
12-
```
13-
cp docker/nginx/nginx.conf.dist docker/nginx/nginx.conf
14-
cp docker-compose.override.yml.dist docker-compose.override.yml
15-
```
16-
1710
## BUILD APPLICATION
1811

19-
- in dev mode
12+
- in dev mode, http://localhost:4200
2013

2114
```
22-
docker-compose up -d --build
23-
docker exec -ti angular-universal_nodejs npm install
24-
docker exec -ti angular-universal_nodejs bash -c 'npm start'
15+
cd project/application
16+
npm install
17+
npm start
2518
```
2619

27-
- in prod mode (bash deploy.sh as quick solution)
20+
- in prod mode, https://angular-universal.lazy-ants.com
21+
22+
Requirements: https://certbot.eff.org
2823

2924
```
25+
cp angular-universal.lazy-ants.com-docker-compose.override.yml.dist docker-compose.override.yml
26+
cp docker/nginx/angular-universal.lazy-ants.com.conf.dist docker/nginx/nginx.conf
3027
docker-compose up -d --build
31-
docker exec -ti angular-universal_nodejs npm install
32-
docker exec -ti angular-universal_nodejs bash -c 'npm run build:ssr'
33-
docker exec -ti angular-universal_nodejs bash -c 'npm run serve:ssr'
3428
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "2.0"
2+
3+
services:
4+
nodejs:
5+
command: >
6+
- /bin/sh
7+
- -c
8+
- |
9+
pm2-runtime pm2-process.yml # run the node server forcely before starting to build the new instance
10+
npm install
11+
npm run build:ssr
12+
npm run silent:update
13+
pm2-runtime pm2-process.yml
14+
15+
nginx:
16+
ports:
17+
- 80:80
18+
- 443:443

deploy.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

docker-compose.override.yml.dist

Lines changed: 0 additions & 10 deletions
This file was deleted.

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ services:
1919
nginx:
2020
build: docker/nginx
2121
restart: always
22+
volumes:
23+
- /etc/letsencrypt:/etc/letsencrypt
2224
volumes_from:
2325
- application
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
user www-data;
2+
worker_processes 4;
3+
pid /run/nginx.pid;
4+
5+
events {
6+
worker_connections 2048;
7+
multi_accept on;
8+
use epoll;
9+
}
10+
11+
http {
12+
server_tokens off;
13+
sendfile on;
14+
tcp_nopush on;
15+
tcp_nodelay on;
16+
keepalive_timeout 15;
17+
types_hash_max_size 2048;
18+
include /etc/nginx/mime.types;
19+
default_type application/octet-stream;
20+
access_log off;
21+
error_log off;
22+
log_format compression '$remote_addr - $remote_user [$time_local] '
23+
'"$request" $status $body_bytes_sent '
24+
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
25+
gzip on;
26+
gzip_disable "msie6";
27+
gzip_vary on;
28+
gzip_proxied any;
29+
gzip_comp_level 6;
30+
gzip_buffers 16 8k;
31+
gzip_http_version 1.1;
32+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
33+
server_names_hash_bucket_size 64;
34+
35+
server {
36+
listen 443 ssl;
37+
keepalive_timeout 70;
38+
expires 0;
39+
server_name angular-universal.lazy-ants.com;
40+
41+
ssl_certificate /etc/letsencrypt/live/angular-universal.lazy-ants.com/fullchain.pem;
42+
ssl_certificate_key /etc/letsencrypt/live/angular-universal.lazy-ants.com/privkey.pem;
43+
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
44+
ssl_ciphers HIGH:!aNULL:!MD5;
45+
46+
merge_slashes off;
47+
rewrite ^(.*?)//+(.*?)$ https://$host$1 permanent;
48+
49+
location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
50+
etag on;
51+
expires 168h;
52+
add_header Cache-Control "max-age=86400,must-revalidate";
53+
add_header Vary User-Agent;
54+
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,If-Match';
55+
proxy_set_header Upgrade $http_upgrade;
56+
proxy_set_header Connection 'upgrade';
57+
proxy_http_version 1.1;
58+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
59+
proxy_set_header Host $host;
60+
proxy_pass http://nodejs:4000;
61+
}
62+
63+
location / {
64+
gzip on;
65+
etag on;
66+
expires 24h;
67+
add_header Cache-Control "max-age=86400,must-revalidate";
68+
add_header Vary User-Agent;
69+
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,If-Match';
70+
proxy_set_header Upgrade $http_upgrade;
71+
proxy_set_header Connection 'upgrade';
72+
proxy_http_version 1.1;
73+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
74+
proxy_set_header Host $host;
75+
proxy_pass http://nodejs:4000;
76+
}
77+
78+
location /robots.txt {
79+
add_header Content-Type text/plain;
80+
return 200 "User-agent: *\nDisallow: /\n";
81+
}
82+
83+
location = /index.html {
84+
return 301 https://$host;
85+
}
86+
87+
location = /index.php {
88+
return 301 https://$host;
89+
}
90+
91+
error_page 502 /502.html;
92+
93+
location = /502.html {
94+
try_files 502.html @error;
95+
internal;
96+
}
97+
98+
location @error {
99+
root /var/www/angular-universal;
100+
}
101+
102+
error_log /var/www/angular-universal/logs/nginx/error.log error;
103+
access_log /var/www/angular-universal/logs/nginx/access.log compression;
104+
}
105+
106+
server {
107+
listen 80;
108+
keepalive_timeout 70;
109+
server_name angular-universal.lazy-ants.com;
110+
111+
return 301 https://$host$request_uri;
112+
113+
error_log /var/www/angular-universal/logs/nginx/error.log error;
114+
access_log /var/www/angular-universal/logs/nginx/access.log compression;
115+
}
116+
117+
server {
118+
listen 80;
119+
listen 443 ssl;
120+
keepalive_timeout 70;
121+
server_name www.angular-universal.lazy-ants.com;
122+
123+
ssl_certificate /etc/letsencrypt/live/www.angular-universal.lazy-ants.com/fullchain.pem;
124+
ssl_certificate_key /etc/letsencrypt/live/www.angular-universal.lazy-ants.com/privkey.pem;
125+
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
126+
ssl_ciphers HIGH:!aNULL:!MD5;
127+
128+
return 301 https://angular-universal.lazy-ants.com$request_uri;
129+
130+
error_log /var/www/angular-universal/logs/nginx/error.log error;
131+
access_log /var/www/angular-universal/logs/nginx/access.log compression;
132+
}
133+
134+
open_file_cache max=100;
135+
}

docker/nginx/nginx.conf.dist

Lines changed: 0 additions & 60 deletions
This file was deleted.

project/application/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# See http://help.github.com/ignore-files/ for more about ignoring files.
22

3+
# logs
4+
/logs
5+
!/logs/pm2/.gitkeep
6+
!/logs/nginx/.gitkeep
7+
38
# compiled output
49
/dist
510
/tmp

project/application/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lint": "ng lint",
1010
"e2e": "ng e2e",
1111
"build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
12-
"serve:ssr": "npm run silent:update && pm2 delete -s angular-universal || : && pm2 start dist/server.js --name angular-universal",
12+
"serve:ssr": "npm run silent:update && node dist/server",
1313
"build:client-and-server-bundles": "ng run angular-universal--browser:build:production && ng run angular-universal--server:build:production",
1414
"webpack:server": "webpack --config webpack.server.config.js --progress --colors",
1515
"silent:update": "rm -rf dist && cp -r tmp dist && rm -rf tmp"

project/application/pm2-process.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apps:
2+
- script: './dist/server.js'
3+
name: 'angular-universal'
4+
exec_mode: 'cluster'
5+
instances: 'max'
6+
log_date_format: 'YYYY-MM-DD HH:mm Z'
7+
error_file: '/var/www/angular-universal/logs/pm2/error.log'
8+
out_file: '/var/www/angular-universal/logs/pm2/access.log'
9+
combine_logs: true
10+
merge_logs: true

project/application/src/logs/nginx/.gitkeep

Whitespace-only changes.

project/application/src/logs/pm2/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)