-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-server.sh
291 lines (231 loc) · 10.4 KB
/
setup-server.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
291
#!/bin/bash
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +
# Server Setup Script - set up Docker MERN on a new server +
# +
# https://github.com/NanoCode012/docker-mern +
# +
# Script developed by +
# Chanvichet Vong <kevinvong@rocketmail.com> +
# +
# Copyright 2021 Chanvichet Vong +
# License at +
# https://github.com/NanoCode012/docker-mern/blob/main/LICENSE +
# +
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "========================================================="
echo "Docker-Mern server startup script by Chanvichet Vong"
echo "Get latest at https://github.com/NanoCode012/docker-mern/"
echo "========================================================="
echo ""
# When development, pass env BRANCH=branch_name before calling script
if [ -z ${BRANCH+x} ]; then
BRANCH="main" # default
else
echo "Running on custom branch $BRANCH"
echo ""
fi
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
SCRIPT_NAME="${0##*/}"
echo "Downloading base scripts"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/local-scripts.sh" -qO local-scripts.sh
source local-scripts.sh
echo "Downloaded base scripts"
echo ""
if [[ ! -z "$1" && "$1" == "barebone" ]]; then
echo -e "Setting barebone run \n"
barebone_run=true
else
barebone_run=false
fi
echo "Folder setup"
echo "============"
if [ ! -d "docker-mern" ]; then
mkdir docker-mern
else
echo "Moving old docker-mern folder to docker-mern-backup folder"
sudo chown -R ${USER}:${USER} docker-mern # fix permission with db belonging to root
if [ -d "docker-mern-backup" ]; then
echo "Deleting backup folder"
rm -rf docker-mern-backup
echo "Deleted backup folder"
fi
mv docker-mern docker-mern-backup
mkdir docker-mern
echo "Successfully moved to docker-mern-backup folder"
fi
echo ""
# Server configuration
echo "Configuration"
echo "============="
cd docker-mern
# Download LICENSE
echo "Downloading LICENSE"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/LICENSE" -qO LICENSE
echo "Downloaded LICENSE"
# Download env file
echo "Downloading env file"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/.env.sample" -qO .env
source .env
echo "Downloaded env file"
# Download ignore files
echo "Downloading .*ignore files"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/.gitignore" -qO .gitignore
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/.dockerignore" -qO .dockerignore
echo "Downloaded .*ignore files"
# Get docker-compose files
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/docker-compose.yml" -qO docker-compose.yml
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/docker-compose.prod.yml" -qO docker-compose.prod.yml
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/docker-compose.ssl.yml" -qO docker-compose.ssl.yml
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/docker-compose.override.yml" -qO docker-compose.override.yml
if [ "$barebone_run" = true ]; then
NGINX_NAME="mern-nginx"
CLIENT_NAME="mern-client"
BACKEND_NAME="mern-backend"
DB_NAME="mern-db"
PROXY_NAME="proxy"
else
# read_with_prompt USE_EXTERNAL_REVERSE_PROXY "Do you use an external reverse proxy like nginx-proxy-automation? (y/n) " "y"
# todo: container names regex [a-zA-Z0-9][a-zA-Z0-9_.-]
read_with_prompt NGINX_NAME "Nginx container name" "mern-nginx"
read_with_prompt CLIENT_NAME "Client container name" "mern-client"
read_with_prompt BACKEND_NAME "Backend container name" "mern-backend"
read_with_prompt DB_NAME "Database container name" "mern-db"
read_with_prompt PROXY_NAME "Docker external proxy name" "proxy"
fi
echo "NGINX_NAME=$NGINX_NAME" >> .env
echo "CLIENT_NAME=$CLIENT_NAME" >> .env
echo "BACKEND_NAME=$BACKEND_NAME" >> .env
echo "DB_NAME=$DB_NAME" >> .env
echo "PROXY_NAME=$PROXY_NAME" >> .env # assume that network exists for now
echo ""
# Create client app
echo "Client"
echo "======"
mkdir client
if [ "$barebone_run" = false ]; then
read_yes_no check_create_new_react_app "Create new react app"
if [ "$check_create_new_react_app" = true ]; then
echo "Creating new react app"
sudo docker run --rm -v $(pwd)/client:/client node:$DOCKER_NODE_VERSION npx create-react-app client --use-npm
sudo chown -R ${USER}:${USER} client
echo "Created new react app"
fi
fi
cd client
mkdir nginx
echo "Downloading Dockerfile, Dockerfile.dev, .gitignore, .dockerignore, and nginx conf file"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/client/Dockerfile" -qO Dockerfile
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/client/Dockerfile.dev" -qO Dockerfile.dev
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/client/.gitignore" -qO .gitignore
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/client/.dockerignore" -qO .dockerignore
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/client/nginx/default.conf" -qO nginx/default.conf
echo "Downloaded files"
echo ""
cd ..
# Create backend app
echo "Backend"
echo "======="
mkdir backend
if [ "$barebone_run" = false ]; then
read_yes_no check_create_new_backend_app "Create new node app"
if [ "$check_create_new_backend_app" = true ]; then
echo "Creating new node app"
echo "Downloading startup script"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/backend/startup.sh" -qO backend-startup.sh
echo "Downloaded startup script"
sudo docker run --rm -v $(pwd)/backend:/backend -v $(pwd)/backend-startup.sh:/backend-startup.sh \
node:$DOCKER_NODE_VERSION /bin/sh -c "chmod +x backend-startup.sh && ./backend-startup.sh"
echo "Deleting startup script"
rm backend-startup.sh
echo "Deleted startup script"
echo "Downloading starter express code"
mkdir backend/src
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/backend/src/index.js" -qO backend/src/index.js
echo "Downloaded starter express code"
sudo chown -R ${USER}:${USER} backend
echo "Created new node app"
fi
fi
cd backend
echo "Downloading Dockerfile, Dockerfile.dev, .gitignore, and .dockerignore"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/backend/Dockerfile" -qO Dockerfile
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/backend/Dockerfile.dev" -qO Dockerfile.dev
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/backend/.gitignore" -qO .gitignore
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/backend/.dockerignore" -qO .dockerignore
echo "Downloaded files"
echo ""
cd ..
# Create db
echo "Database"
echo "========"
mkdir db
cd db
echo "Creating init-mongo.js file"
touch init-mongo.js
echo "Created init-mongo.js file. Please place your initial mongo configurations here."
echo ""
cd ..
# Create proxy
echo "Nginx Proxy"
echo "==========="
mkdir nginx
mkdir nginx/configs
cd nginx
echo "Downloading Dockerfile and nginx default conf"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/nginx/Dockerfile" -qO Dockerfile
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/nginx/configs/default.conf" -qO configs/default.conf
echo "Downloaded files"
echo "Replacing conf variables with env variables"
CLIENT_NAME=$CLIENT_NAME BACKEND_NAME=$BACKEND_NAME envsubst < configs/default.conf > configs/default.conf.replaced
mv configs/default.conf.replaced configs/default.conf
echo "Replaced variables"
echo ""
cd ..
# Create environment folder
echo "Env"
echo "==="
mkdir env
cd env
echo "Downloading env files"
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/env/backend.env.sample" -qO backend.env
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/env/mongo.env.sample" -qO mongo.env
wget "https://raw.githubusercontent.com/NanoCode012/docker-mern/$BRANCH/env/nginx.env.sample" -qO nginx.env
echo "Downloaded env files"
if [ "$barebone_run" = true ]; then
MONGO_INITDB_DATABASE="app"
MONGO_INITDB_USERNAME="nanocode012"
MONGO_INITDB_PASSWORD="averysecurepassword,butpleasechangeme0"
else
read_with_prompt MONGO_INITDB_DATABASE "MongoDB Database Name" "app"
read_with_prompt MONGO_INITDB_USERNAME "MongoDB Username" "nanocode012"
read_with_prompt MONGO_INITDB_PASSWORD "MongoDB Password" "averysecurepassword,butpleasechangeme0"
fi
echo "Replacing env file with env variables"
MONGO_INITDB_DATABASE=$MONGO_INITDB_DATABASE envsubst < backend.env > backend.env.replaced
mv backend.env.replaced backend.env
MONGO_INITDB_DATABASE=$MONGO_INITDB_DATABASE MONGO_INITDB_USERNAME=$MONGO_INITDB_USERNAME \
MONGO_INITDB_PASSWORD=$MONGO_INITDB_PASSWORD envsubst < mongo.env > mongo.env.replaced
mv mongo.env.replaced mongo.env
echo "Replaced variables"
if [ "$barebone_run" = false ]; then
read_yes_no check_enable_ssl_app "Enable SSL via Nginx Proxy-LetsEncrypt"
if [ "$check_enable_ssl_app" = true ]; then
read_with_prompt VIRTUAL_HOST "VIRTUAL_HOST/LETSENCRYPT_HOST" ""
read_with_prompt VIRTUAL_PORT "VIRTUAL_PORT" "80"
read_with_prompt LETSENCRYPT_EMAIL "LETSENCRYPT_EMAIL" ""
echo "Replacing env file with env variables"
VIRTUAL_HOST=$VIRTUAL_HOST VIRTUAL_PORT=$VIRTUAL_PORT LETSENCRYPT_HOST=$VIRTUAL_HOST \
LETSENCRYPT_EMAIL=$LETSENCRYPT_EMAIL envsubst < nginx.env > nginx.env.replaced
mv nginx.env.replaced nginx.env
echo "Replaced variables"
fi
fi
echo ""
cd ..
# Cleanup
echo "Clean up"
echo "==="
echo "Deleting local-scripts"
rm ../local-scripts.sh
echo "Deleted local scripts"