-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_instance.sh
executable file
·135 lines (110 loc) · 4.13 KB
/
init_instance.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
#!/bin/bash
# This Script is meant to be used to setup the environment on the instance the containers are run/
# You can use it on Debian like distros to install all the dependencies.
# Djenitor runs on an AWS EC2 Instance t2.micro Free Tier with the Ubuntu 18.04 LTS
# AMI: ami-085925f297f89fce1
G="\033[0;32m"
Y="\033[0;33m"
NC="\033[0m"
USER=ubuntu
REPO="[email protected]:serban-mihai/Djenitor.git"
APP_DOMAIN="live.djenitor.com"
API_DOMAIN="api.djenitor.com"
set -e
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
echo -e ">>> ${Y}Djenitor${NC} Dependencies"
# Docker Dependecies
apt remove docker docker-engine docker.io containerd runc
apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update -y && apt upgrade -y # Update Repo and Upgrade Distro
apt install nginx-full nginx-extras -y # Used as a Reverse Proxy -y
apt install certbot python3-certbot-nginx -y # For issuing the SSL Certificate -y
apr install git -y # For Source Controll
apt install docker-ce docker-ce-cli containerd.io docker-compose -y
usermod -aG docker ${USER}
# Setting up the Reverse Proxy
# App Subdomain:
cat << EOF > /etc/nginx/sites-available/djenitor_app
upstream app_server_djenitor {
server 0.0.0.0:8080 fail_timeout=0;
}
server {
server_name ${APP_DOMAIN};
access_log /var/log/nginx/djenitor-app-access.log;
error_log /var/log/nginx/djenitor-app-error.log info;
keepalive_timeout 5;
location / {
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_redirect off;
client_max_body_size 25M;
if (!-f \$request_filename) {
proxy_pass http://app_server_djenitor;
break;
}
}
}
server {
server_name ${APP_DOMAIN};
listen 80;
}
EOF
# API Subdomain:
cat << EOF > /etc/nginx/sites-available/djenitor_api
upstream api_server_djenitor {
server 0.0.0.0:8001 fail_timeout=0;
}
server {
server_name ${API_DOMAIN};
access_log /var/log/nginx/djenitor-api-access.log;
error_log /var/log/nginx/djenitor-api-error.log info;
keepalive_timeout 5;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
more_set_headers 'Access-Control-Allow-Origin:https://${APP_DOMAIN}';
more_set_headers 'Access-Control-Allow-Methods: GET,POST,OPTIONS';
more_set_headers 'Access-Control-Allow-Credentials:true';
more_set_headers 'Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_redirect off;
client_max_body_size 25M;
if (!-f \$request_filename) {
proxy_pass http://api_server_djenitor;
break;
}
}
location /socket.io/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://api_server_djenitor/socket.io/;
}
}
server {
server_name ${API_DOMAIN};
listen 80;
}
EOF
ln -s /etc/nginx/sites-available/djenitor_app /etc/nginx/sites-enabled/djenitor_app
ln -s /etc/nginx/sites-available/djenitor_api /etc/nginx/sites-enabled/djenitor_api
service nginx reload
service nginx restart
certbot -d ${APP_DOMAIN} -d ${API_DOMAIN} -y
# Clone and Start Containers (Note that the SSH Pub Key needs to be present into the Deployment Keys of the Repo)
cd ${HOME}
git clone ${REPO} -y
cd Djenitor/server
docker-compose up -d