-
Notifications
You must be signed in to change notification settings - Fork 2
/
ng
executable file
·351 lines (302 loc) · 10.9 KB
/
ng
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
# MIT License
# Copyright (c) 2017 Patrick Curl | SoaringHost
# https://github.com/patrickcurl/ngTool
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
### Set Language
TEXTDOMAIN=virtualhost
### Set default parameters
action=$1
domain=$2
root=$3
### CONFIG SETTINGS
# Put owner/group that nginx must run under for your system.
# The default on ubuntu would be user: www-data group: www-data.
# This is my personal settings for Arch linux.
owner=patrick #owner for chmoding -- this is the owner nginx uses.
group=users #group for chmoding -- this is the group nginx uses.
sitesEnabled='/etc/nginx/sites-enabled/' #sites-enabled path
sitesAvailable='/etc/nginx/sites-available/' #sites-available path
useHosts=false # make true if you do not have a global .test wildcard setup via dnsmasq or other.
useSSL=true
sslConfig='/etc/nginx/ssl/ssl_servers.cfg'
sslKey='/etc/nginx/ssl/nginx.key'
sslCert='/etc/nginx/ssl/nginx.crt'
# Change this to match what your system uses.
#arch
restart='systemctl restart nginx'
#ubuntu
# restart=service nginx restart
### END CONFIG SETTINGS
function regenerate_ssls {
echo $sitesAvailable
# sitesAvailable = $1
# sslConfig = $2
# sslKey = $3
# sslCert = $4
SSL_DOMAINS=`sed -n -e 's/^.*server_name //p' $sitesAvailable* | tr " " "\n" | sed '/^\s*$/d' | cut -d ';' -f1 | sort -u`
cat <<EOT> $sslConfig
[ req ]
req_extensions = req_ext
distinguished_name = req_distinguished_name
prompt = no
[req_distinguished_name]
commonName=localhost.test
[req_ext]
subjectAltName = @alt_names
[alt_names]
EOT
count=1;
for SDOMAIN in $SSL_DOMAINS; do
if [ "$SDOMAIN" != '_' ]; then
let count++;
dns="DNS.$count = $SDOMAIN";
echo $dns >> $sslConfig;
fi
done
`sudo openssl req -x509 -config $sslConfig -extensions req_ext -nodes -days 730 -newkey rsa:2048 -sha256 -keyout $sslKey -out $sslCert`
}
function restart_nginx {
$restart
echo -e $"Nginx restarted!"
}
function version {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}
function check_version {
echo -e "Checking for updates.\n";
SCRIPT="$(readlink --canonicalize-existing "$0")"
SCRIPT_PATH="$(dirname $SCRIPT)"
CURRENT_VERSION=`cat $SCRIPT_PATH/VERSION`
NEW_VERSION="`wget -qO- https://raw.githubusercontent.com/patrickcurl/ngtool/master/VERSION`"
if [ $CURRENT_VERSION != "" ] && [ $(version $CURRENT_VERSION) -lt $(version $NEW_VERSION) ]; then
echo -e $"There is an update available (Current: $CURRENT_VERSION | New: $NEW_VERSION) to update do the following:\n"
echo -e $"cd $SCRIPT_PATH && git pull origin master\n"
else
echo -e $"You already have the latest version";
fi
}
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run \
$0 as non-root user. Use sudo"
exit 1;
fi
check_version
if [ "$action" != "create" ] && [ "$action" != "delete" ] \
&& [ "$action" != "enable" ] && [ "$action" != "disable" ]; then
echo $"You need to prompt for action \
(create, delete or enable or disable) -- Lower-case only"
exit;
fi
while [ "$domain" == "" ]; do
if [ "$action" == "create" ] || [ "$action" == "delete" ]; then
echo -e $"Please provide domain. e.g. mysite.dev"
read domain
else
break
fi
done
### if root dir starts with '/', don't use /var/www as default starting point
if [ "$action" == "create" ]; then
echo -e $"Creating $domain...\n"
while [ "$root" == "" ]; do
echo -e $"Please provide a full directory e.g. /home/user/projects/myproject/public"
read root
done
### 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 $root ]; then
### create the directory
mkdir $root
### give permission to root dir
chmod 755 root
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $root/phpinfo.php
then
echo $"ERROR: Not able to write in file $root/phpinfo.php. Please check permissions."
exit;
else
echo $"Added content to $root/phpinfo.php."
fi
fi
### create virtual host rules file
if ! echo "server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
root $root;
index index.php index.html index.htm;
server_name $domain;
ssl_certificate $sslCert;
ssl_certificate_key $sslKey;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)\$ {
access_log off;
expires max;
}
# catch all
error_page 404 /index.php;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
# A bunch of perm page redirects from my old
# site structure for SEO purposes. Not interesting.
# include /etc/nginx/templates/redirects;
}
if (!-d \$request_filename) {
rewrite ^/(.+)/\$ /\$1 permanent;
}
location ~* \.php\$ {
try_files \$uri /index.php =404;
# Server PHP config.
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
# Typical vars in here, nothing interesting.
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
location ~ /\.ht {
# Hells no, we usin nginx up in this mutha. (deny .htaccess)
deny all;
}
}" > $sitesAvailable$domain
then
echo -e $"There is an ERROR create $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
if [ "$useHosts" == true ]; then
### 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
fi
chown -R $owner:$group $root
### enable website
ln -s $sitesAvailable$domain $sitesEnabled$domain
### SETUP SSLS for all existing domains.
### restart Nginx
if [ "$useSSL" == true ]; then
regenerate_ssls $sitesAvailable $sslConfig $sslKey $sslCert
fi
restart_nginx
### show the finished message
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $root"
exit;
fi
if [ "$action" == "delete" ]; then
echo -e $"Deleting $domain...\n"
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain does not exists.\nPlease Try Another one"
exit;
else
if [ "$useHosts" == true ]; then
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
fi
### disable website
rm $sitesEnabled$domain
### restart Nginx
#service nginx restart
restart_nginx
### Delete virtual host rules files
rm $sitesAvailable$domain
echo -e $"Complete!\nYou just removed Virtual Host $domain. \nThe root folder still exists. You'll need to manually delete it."
exit;
fi
fi
if [ "$action" == "enable" ]; then
if [ "$domain" == "" ]; then
echo -e $"No domain supplied, re-enabling all domains";
FILES=$sitesAvailable*
for f in $FILES; do
file=`basename $f`;
if ! [ -f "$sitesEnabled$file" ]; then
echo -e $"Enabling: $file"
`sudo ln -s $sitesAvailable$file $sitesEnabled$file`
fi
done
restart_nginx
echo -e $"Complete! ALL Nginx sites are now enabled. Happy coding!"
exit;
else
echo -e $"Enabling $domain...\n"
if ! [ -f "$sitesAvailable$domain" ]; then
echo -e $"Domain file missing from $sitesAvailable, please add it then try again."
exit;
fi
if [ -f "$sitesEnabled$domain" ]; then
echo -e $"Domain is already enabled, no need to renable."
exit;
else
`sudo ln -s $sitesAvailable$domain $sitesEnabled$domain`
echo -e $"Domain $domain has been enabled. Happy coding!"
restart_nginx
exit;
fi
fi
fi
if [ "$action" == "disable" ]; then
echo -e $"Disabling $domain...\n"
if [ "$domain" == "" ]; then
echo -e $"No domain supplied, disabling requires a domain! \nIf you really want to disable ALL domains then try: ng disable all"
exit;
fi
if [ "$domain" == "all" ]; then
FILES=$sitesEnabled*
for f in $FILES; do
file=`basename $f`
if [ -f "$f" ]; then
`rm -rf $f`
if [ -f "$f" ]; then
echo -e $"Error: $file was unable to be disabled"
else
echo -e $"$file as been disabled successfully."
fi
else
echo -e $"File does not exist, skipping"
fi
done
restart_nginx
fi
if [ "$domain" != "all" ] && [ "$domain" != "" ]; then
file="$sitesEnabled$domain"
if [ -f "$file" ]; then
`rm -rf $file`
if ! [ -f "$file" ]; then
echo -e $"Site $domain has been disabled!"
else
echo -e $"Something went wrong, unable to disable site!"
fi
else
echo -e $"File does not exist. Did you spell it right?"
fi
restart_nginx
fi
fi