-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-vhost-proxy.sh
executable file
·57 lines (47 loc) · 1.25 KB
/
create-vhost-proxy.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
#!/bin/bash
serverName=$1
destination=$2
siteEnable='/etc/nginx/sites-enabled/'
siteAvailable='/etc/nginx/sites-available/'
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
while [ "$serverName" == "" ]
do
echo -e $"Please provide domain. e.g.dev,staging"
read serverName
done
while [ "$destination" == "" ]
do
echo -e $"Please provide destination. e.g.10.0.0.169:8080"
read destination
done
if [ -e $siteAvailable$serverName ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
## Create virtualhost rules filee
if ! echo "server {
listen 80;
server_name $serverName;
location / {
resolver 8.8.8.8;
proxy_pass http://$destination;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
}" > $siteAvailable$serverName
then
echo -e $"There is an ERROR create $serverName file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
## Enable website
ln -s $siteAvailable$serverName $siteEnable$serverName
## Reload Nginx
service nginx reload
echo -e $"Complete ! Yout now have a new Virtual Host\nYour new host is: http://$serverName"
exit;