-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.sh
executable file
·79 lines (61 loc) · 1.48 KB
/
create.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
#!/bin/bash
# user
user=$(who am i | awk '{print $1}')
# Test output
echo Welcome, $user
# Dir variables
sitesEnabled='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
sitesDir='/var/www/'
uri='$uri'
# Input
domain=$1
# Check user privileges
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
# Check domain
while [ "$domain" == "" ]
do
echo -e $"Please provide domain to create. e.g.dev,staging"
read domain
done
# Check domain availability
nslookup $domain &> /dev/null
status=$?
if [ $status -eq 0 ]; then
echo $"Domain $domain already exists. Try something else."
exit 1;
fi
# CREATE
## make root directory
mkdir -p /var/www/"$domain"/html
## grant privileges
cd $sitesDir$domain
chown -R $user:$user html/
chmod -R 755 /var/www/"$domain"/html
echo "Domain created at : " $domain
## make index.html file
touch /var/www/"$domain"/html/index.html
echo "This is my domain : " $domain >> /var/www/"$domain"/html/index.html
## create config file
echo "
server {
listen 80;
listen [::]:80;
root $sitesDir$domain/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name $domain www.$domain;
location / {
try_files $uri $uri/ =404;
}
}
" >> /etc/nginx/sites-available/"$domain"
## enable block
ln -s $sitesAvailable$domain $sitesEnabled
## restart nginx
service nginx restart
## VOILA!
echo 'Congrats! Your domain is now online at : '$domain 'or at : www.'$domain