forked from RoverWire/virtualhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualhost-nginx.sh
218 lines (186 loc) · 5.34 KB
/
virtualhost-nginx.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
### Set Language
TEXTDOMAIN=virtualhost
### Set default parameters
action=$1
domain=$2
rootDir=$3
owner=$(who am i | awk '{print $1}')
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
userDir='/var/www/'
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
then
echo $"You need to prompt for action (create or delete) -- Lower-case only"
exit 1;
fi
while [ "$domain" == "" ]
do
echo -e $"Please provide domain. e.g.dev,staging"
read domain
done
if [ "$rootDir" == "" ]; then
rootDir=${domain//./}
fi
### if root dir starts with '/', don't use /var/www as default starting point
if [[ "$rootDir" =~ ^/ ]]; then
userDir=''
fi
rootDir=$userDir$rootDir
if [ "$action" == 'create' ]
then
### check if domain already exists
if [ -e $sitesAvailable$domain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
### check if directory exists or not
if ! [ -d $userDir$rootDir ]; then
### create the directory
mkdir $userDir$rootDir
### give permission to root dir
chmod 755 $userDir$rootDir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $userDir$rootDir/phpinfo.php
then
echo $"ERROR: Not able to write in file $userDir/$rootDir/phpinfo.php. Please check permissions."
exit;
else
echo $"Added content to $userDir$rootDir/phpinfo.php."
fi
fi
### create virtual host rules file
if ! echo "
server {
listen 80;
root $userDir$rootDir;
index index.php index.html index.htm;
# Permite envio de arquivo ate 10 MB
client_max_body_size 10M;
server_name $domain;
charset utf-8;
gzip on;
gzip_vary on;
gzip_disable \"msie6\";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
expires 1M;
access_log off;
add_header Cache-Control \"public\";
}
location ~* \.(?:css|js)$ {
expires 7d;
access_log off;
add_header Cache-Control \"public\";
}
location ~ /\.ht {
deny all;
}
}
" > $sitesAvailable$domain
then
echo -e $"There is an ERROR create $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able write in /etc/hosts"
exit;
else
echo -e $"Host added to /etc/hosts file \n"
fi
### Add domain in /mnt/c/Windows/System32/drivers/etc/hosts (Windows Subsytem for Linux)
if [ -e /mnt/c/Windows/System32/drivers/etc/hosts ]
then
if ! echo -e "\r127.0.0.1 $domain" >> /mnt/c/Windows/System32/drivers/etc/hosts
then
echo $"ERROR: Not able to write in /mnt/c/Windows/System32/drivers/etc/hosts (Hint: Try running Bash as administrator)"
else
echo -e $"Host added to /mnt/c/Windows/System32/drivers/etc/hosts file \n"
fi
fi
#if [ "$owner" == "" ]; then
# chown -R $(whoami):www-data $userDir$rootDir
#else
# chown -R $owner:www-data $userDir$rootDir
#fi
### enable website
ln -s $sitesAvailable$domain $sitesEnable$domain
### restart Nginx
service nginx restart
### show the finished message
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $userDir$rootDir"
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain dont exists.\nPlease Try Another one"
exit;
else
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### Delete domain in /mnt/c/Windows/System32/drivers/etc/hosts (Windows Subsytem for Linux)
if [ -e /mnt/c/Windows/System32/drivers/etc/hosts ]
then
newhost=${domain//./\\.}
sed -i "/$newhost/d" /mnt/c/Windows/System32/drivers/etc/hosts
fi
### disable website
rm $sitesEnable$domain
### restart Nginx
service nginx restart
### Delete virtual host rules files
rm $sitesAvailable$domain
fi
### check if directory exists or not
if [ -d $userDir$rootDir ]; then
echo -e $"Delete host root directory ? (s/n)"
read deldir
if [ "$deldir" == 's' -o "$deldir" == 'S' ]; then
### Delete the directory
rm -rf $userDir$rootDir
echo -e $"Directory deleted"
else
echo -e $"Host directory conserved"
fi
else
echo -e $"Host directory not found. Ignored"
fi
### show the finished message
echo -e $"Complete!\nYou just removed Virtual Host $domain"
exit 0;
fi