-
Notifications
You must be signed in to change notification settings - Fork 13
/
nginx_vhost.sh
103 lines (87 loc) · 2.11 KB
/
nginx_vhost.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
#!/bin/bash
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
domain=$1
rootPath=$2
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
serverRoot='/srv/'
domainRegex="^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$"
while [ "$domain" = "" ]
do
echo "Please provide domain:"
read domain
done
until [[ $domain =~ $domainRegex ]]
do
echo "Enter valid domain:"
read domain
done
if [ -e $sitesAvailable$domain ]; then
echo "This domain already exists.\nPlease Try Another one"
exit;
fi
if [ "$rootPath" = "" ]; then
rootPath=$serverRoot$domain
fi
if ! [ -d $rootPath ]; then
mkdir $rootPath
chmod 777 $rootPath
if ! echo "Hello, world!" > $rootPath/index.php
then
echo "ERROR: Not able to write in file $rootPath/index.php. Please check permissions."
exit;
else
echo "Added content to $rootPath/index.php"
fi
fi
if ! [ -d $sitesEnable ]; then
mkdir $sitesEnable
chmod 777 $sitesEnable
fi
if ! [ -d $sitesAvailable ]; then
mkdir $sitesAvailable
chmod 777 $sitesAvailable
fi
configName=$domain
if ! echo "server {
listen 80;
root $rootPath;
index index.php index.hh index.html index.htm;
server_name $domain;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
location ~* \.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
log_not_found off;
}
location ~ \.(php|hh)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param HTTPS off;
}
location ~ /\.ht {
deny all;
}
client_max_body_size 0;
}" > $sitesAvailable$configName
then
echo "There is an ERROR create $configName file"
exit;
else
echo "New Virtual Host Created"
fi
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo "ERROR: Not able write in /etc/hosts"
exit;
else
echo "Host added to /etc/hosts file"
fi
ln -s $sitesAvailable$configName $sitesEnable$configName
service nginx restart
echo "Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootPath"
exit;