Skip to content

Commit 735fa45

Browse files
committed
initial commit
0 parents  commit 735fa45

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

create_site.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
# Author: Seb Dangerfield
3+
# URL: http://sebdangerfield.me.uk
4+
#
5+
6+
NGINX_ALL_VHOSTS='/etc/nginx/sites-available'
7+
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled'
8+
WEB_DIR='/var/www'
9+
SED=`which sed`
10+
NGINX=`sudo which nginx`
11+
CURRENT_DIR=`dirname $0`
12+
13+
if [ -z $1 ]; then
14+
echo "No domain name given"
15+
exit 1
16+
fi
17+
DOMAIN=$1
18+
19+
# check the domain is valid!
20+
PATTERN="^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$";
21+
if [[ "$DOMAIN" =~ $PATTERN ]]; then
22+
DOMAIN=`echo $DOMAIN | tr '[A-Z]' '[a-z]'`
23+
echo "Creating hosting for:" $DOMAIN
24+
else
25+
echo "invalid domain name"
26+
exit 1
27+
fi
28+
29+
USERNAME='www-data'
30+
31+
# Create a new user!
32+
#echo "Please specify the username for this site?"
33+
#read USERNAME
34+
#sudo adduser -d$WEB_DIR/$USERNAME $USERNAME
35+
#echo "Please enter a password for the user: $USERNAME"
36+
#read -s PASS
37+
#sudo echo $PASS | sudo passwd --stdin $USERNAME
38+
39+
# Now we need to copy the virtual host template
40+
CONFIG=$NGINX_ALL_VHOSTS/$DOMAIN.conf
41+
sudo cp $CURRENT_DIR/virtual_host.template $CONFIG
42+
sudo $SED -i "s/DOMAIN/$DOMAIN/g" $CONFIG
43+
sudo $SED -i "s#ROOT#$WEB_DIR/$HOME_DIR\/public_html#g" $CONFIG
44+
45+
#sudo usermod -aG $USERNAME nginx
46+
#sudo chmod g+rxs $WEB_DIR/$USERNAME
47+
sudo chmod 600 $CONFIG
48+
49+
sudo $NGINX -t
50+
if [ $? -eq 0 ];then
51+
# Create symlink
52+
sudo ln -s $CONFIG $NGINX_ENABLED_VHOSTS/$DOMAIN.conf
53+
else
54+
echo "Could not create new vhost as there appears to be a problem with the newly created nginx config file: $CONFIG";
55+
exit 1;
56+
fi
57+
58+
sudo /etc/init.d/nginx reload
59+
60+
# put the template index.html file into the public_html dir!
61+
sudo mkdir -p /var/www/$HOME_DIR/public_html
62+
63+
sudo cp $CURRENT_DIR/index.html.template $WEB_DIR/$HOME_DIR/public_html/index.html
64+
sudo $SED -i "s/SITE/$DOMAIN/g" $WEB_DIR/$HOME_DIR/public_html/index.html
65+
sudo chown $USERNAME:$USERNAME $WEB_DIR/$HOME_DIR/public_html -R
66+
67+
echo -e "\nSite Created for $DOMAIN"
68+
echo "--------------------------"
69+
echo "Host: "`hostname`
70+
echo "URL: $DOMAIN"
71+
echo "User: $USERNAME"
72+
echo "--------------------------"
73+
exit 0;

index.html.template

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>SITE</title>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
</head>
6+
<body>
7+
Welcome to SITE, this site will be live shortly...
8+
</body>
9+
</html>

virtual_host.template

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
server {
2+
server_name www.DOMAIN DOMAIN;
3+
4+
root ROOT;
5+
6+
access_log /var/log/nginx/DOMAIN.access.log;
7+
8+
index index.html index.htm;
9+
10+
# serve static files directly
11+
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
12+
access_log off;
13+
expires max;
14+
}
15+
16+
location ~ /\.ht {
17+
deny all;
18+
}
19+
}

0 commit comments

Comments
 (0)