-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-domain.sh
executable file
·142 lines (116 loc) · 4.65 KB
/
new-domain.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
#!/usr/bin/env bash
#################################################
# Setup debian-9 to serve django project
# For debian-9 instance at aws-lightsail
# [email protected] - 27/May/2020
#################################################
#################################################
# Functions Start Here
add_to_crontab() {
echo '> Add Certbot Renew to Crontab'
TMP_FILE=$(mktemp)
sudo crontab -l | tee $TMP_FILE > /dev/null
echo "0 12 * * * /usr/bin/certbot renew --quiet" | tee -a $TMP_FILE > /dev/null
sudo crontab $TMP_FILE
rm $TMP_FILE
}
# Functions End Here
# Get lightsail-django current directory.
LIGHTSAIL_DJANGO=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
clear
echo "#################################################"
echo "# Add New Domain"
# Color Variable
Gre='\033[0;32m'; # Green
NoC='\033[0m'; # No Color
# Make commands invokes serially
set -euo pipefail
#################################################
# Get domain and email data
if [ -z ${2+x} ]; then
echo -e -n "${Gre}"
read -p "Your domain name: " DOMAIN
read -p "Email for generating ssl: " EMAIL
echo -e -n "${NoC}"
else
DOMAIN=$1
EMAIL=$2
fi
SANITIZED_DOMAIN=${DOMAIN//./_}
#################################################
# Creating domain conf file
CONF="$LIGHTSAIL_DJANGO/$DOMAIN/domain.conf.json"
EXAMPLE_CONF="$LIGHTSAIL_DJANGO/template/domain.conf.json"
TMP_CONF=$(mktemp)
mkdir -p $DOMAIN
if [ ! -e $CONF ]; then
echo "Conf file not exist, creating $CONF"
jq '.subDomains=[]' $EXAMPLE_CONF > $CONF
fi
# Updating conf domain name
jq --arg domainName $DOMAIN --arg email $EMAIL \
'.domainName = $domainName | .sslEmail = $email' $CONF \
> $TMP_CONF && mv $TMP_CONF $CONF
echo -e -n "${Gre}"
echo "Configuration saved to $CONF"
echo -e -n "${NoC}"
# Add new subdomain scripts link file to domain directory
ln -s $LIGHTSAIL_DJANGO/scripts/new-subdomain.sh $LIGHTSAIL_DJANGO/$DOMAIN/new-subdomain.sh
#################################################
# Add new user
echo '> Creating User'
sudo useradd --system --gid webapps --shell /bin/bash --home /webapps/${SANITIZED_DOMAIN}__django $SANITIZED_DOMAIN
# Create user directory
echo '> Creating User Directory'
sudo mkdir -p /webapps/${SANITIZED_DOMAIN}__django/
sudo chown $SANITIZED_DOMAIN /webapps/${SANITIZED_DOMAIN}__django/
#################################################
# Create django project
echo '> Creating Django Project'
sudo su - $SANITIZED_DOMAIN -c 'cd ~ &&
virtualenv -p python3 . &&
source bin/activate &&
pip install django gunicorn setproctitle &&
django-admin startproject $0 &&
deactivate' -- $SANITIZED_DOMAIN
# Add domain to Allowed host in settings.py
sudo su - $SANITIZED_DOMAIN -c '
sed -i "s~ALLOWED_HO.*~ALLOWED_HOSTS = [\"${1}\"]~" ${0}/${0}/settings.py
' -- $SANITIZED_DOMAIN $DOMAIN
# Do migration
sudo su - $SANITIZED_DOMAIN -c '
source bin/activate &&
python ${0}/manage.py migrate
' -- $SANITIZED_DOMAIN
#################################################
# Setup to serve django project
# Copy gunicorn start script
echo '> Creating Gunicorn Start Script'
sudo su - $SANITIZED_DOMAIN -c '
sed "s/CHANGE_HERE/$1/g" "$0/gunicorn_start" > "/webapps/$1__django/bin/gunicorn_start"
' -- $PWD/template $SANITIZED_DOMAIN
sudo chmod u+x /webapps/${SANITIZED_DOMAIN}__django/bin/gunicorn_start
# Copy supervisor conf script
echo '> Creating Supervisor Conf Script'
sudo su - $SANITIZED_DOMAIN -c ' mkdir /webapps/${0}__django/logs &&
touch /webapps/${0}__django/logs/gunicorn_supervisor.log' -- $SANITIZED_DOMAIN
sudo sed "s/CHANGE_HERE/$SANITIZED_DOMAIN/g" "$PWD/template/supervisor.conf" | sudo tee "/etc/supervisor/conf.d/${SANITIZED_DOMAIN}.conf" > /dev/null
# Reread & update supervisor to start apps
sudo supervisorctl reread
sudo supervisorctl update
#################################################
# Setup web server
# Copy nginx entry
echo '> Creating Nginx Entry'
# sudo su -c 'sed "s/CHANGE_HERE/$1/g; s/DOMAIN_HERE/$2/g" "$0/nginx_entry" > "/etc/nginx/sites-available/$1"' -- $PWD/template $SANITIZED_DOMAIN $DOMAIN
sudo sed "s/CHANGE_HERE/$SANITIZED_DOMAIN/g; s/DOMAIN_HERE/$DOMAIN/g" "$PWD/template/nginx_entry" | sudo tee "/etc/nginx/sites-available/$DOMAIN" > /dev/null
# Enable nginx entry
sudo ln -s /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/$DOMAIN
# Add let's encrypt https
echo '> Creating Lets Encrypt certificate'
sudo certbot --nginx -m $EMAIL --preferred-challenges http-01 --agree-tos --redirect -d $DOMAIN -d www.$DOMAIN
# Add to cronjob if not exists
sudo crontab -l | grep -q "certbot renew" || add_to_crontab
# Restart nginx
echo '> Restarting nginx'
sudo service nginx restart