-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.sh
75 lines (61 loc) · 1.72 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
#!/bin/bash
# Author: Sujan Byanjankar
# URL: sujanbyanjankar.com.np
ALL_SITES='/etc/nginx/sites-available'
EN_SITES='/etc/nginx/sites-enabled'
WWW='/var/www'
SED='which sed'
NGINX='which nginx'
CURRENT_DIR="dirname $0"
if [ -z $1 ]; then
echo "Domain name required"
exit 1
fi
DOMAIN=$1
# validate domain name
PATTERN="^([\da-z0-9\.-]+)\.([a-z\.]{2,6})$";
if [[ "$DOMAIN" =~ $PATTERN ]]; then
DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'`
echo "Creating hosting for:" $DOMAIN
else
echo "invalid domain name"
exit 1
fi
# Create user
echo "Please enter username for this site:"
read USERNAME
sudo useradd $USERNAME
# Add password to the created user
echo "Enter password for user: $USERNAME"
read -s PASS
sudo echo $PASS | sudo passwd --stdin $USERNAME
echo "$USERNAME:$PASS"
# Copy VHOST template
CONFIG=$ALL_SITES/$DOMAIN.conf
sudo cp vhost.template $CONFIG
sudo sed -i "s/DOMAIN/$DOMAIN/g" $CONFIG
# Create user home directory
sudo mkdir -p $WWW/$DOMAIN/public_html
# Modify user
sudo usermod -aG nginx $USERNAME -d $WWW/$DOMAIN
sudo chmod g+rxs $WWW/$DOMAIN
sudo chmod 600 $CONFIG
# test nginx config
sudo nginx -t
if [ $? -eq 0 ];then
# Create symlink
sudo ln -s $CONFIG $EN_SITES/$DOMAIN.conf
else
echo "Could not create new vhost as there appears to be a problem with the newly created nginx config file: $CONFIG";
exit 1;
fi
# Reload nginx
sudo service nginx reload
# Put default page template into public_html dir of new domain
sudo cp index.html.template $WWW/$DOMAIN/public_html/index.html
sudo sed -i "s/SITE/$DOMAIN/g" $WWW/$DOMAIN/public_html/index.html
sudo chown $USERNAME:$USERNAME $WWW/$DOMAIN/public_html -R
echo "Site Created for $DOMAIN"
echo "URL: $DOMAIN"
echo "User: $USERNAME"
exit 0;