-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.sh
executable file
·324 lines (268 loc) · 8.14 KB
/
action.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
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
#!/bin/bash
### Set Language
TEXTDOMAIN=virtualhost
### Set default parameters
action=$1
domain=$2
phpVersion=$3
userAs=$4
rootDir=$5
owner=$(who am i | awk '{print $1}')
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
userDir='/var/www/'
phpFpmPoolsEnabled="/etc/php/$phpVersion/fpm/pool.d/"
phpFpmPoolsAvailable="/etc/php/$phpVersion/fpm/pools-available/"
MY_DIR=$(dirname $(readlink -f $0))
createHostname() {
### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able write $domain in /etc/hosts"
exit;
else
echo -e $"Host $domain 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
}
deleteHostname() {
### 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
}
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" != 'remove' ] && [ "$action" != 'enable' ] && [ "$action" != 'disable' ]
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 name (fqdn)."
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
while [ "$userAs" == "" ]
do
echo -e $"Please provide username for website. User will be created in system."
read userAs
done
while [ "$phpVersion" == "" ]
do
echo -e $"Please provide PHP version, eg. 7.2"
read phpVersion
done
### check if domain already exists
if [ -e $sitesAvailable$domain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
### create user
adduser --disabled-password --gecos "" $userAs
### check if directory exists or not
if ! [ -d $userDir$rootDir ]; then
### create the directory
mkdir $userDir$rootDir
mkdir $userDir$rootDir/tmp
### give permission to root dir
chmod -R 700 $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 $userDir/$rootDir/. Please check permissions."
exit;
else
### remove file
rm $userDir$rootDir/phpinfo.php
echo $"[OK] $userDir$rootDir is writable"
fi
###TODO: should test if PHP is available
fi
### create virtual host rules file
if ! echo "server {
listen 80;
root $userDir$rootDir/current/public;
index index.php;
server_name $domain;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d \$request_filename) {
rewrite ^/(.+)/\$ /\$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e \$request_filename) {
rewrite ^/(.*)\$ /index.php?/\$1 last;
break;
}
# removes trailing 'index' from all controllers
if (\$request_uri ~* index/?\$) {
rewrite ^/(.*)/index/?\$ /\$1 permanent;
}
location / {
try_files \$uri \$uri/ /index.php;
}
# catch all
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/run/php/php$phpVersion-fpm.$domain.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root/\$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}" > $sitesAvailable$domain
then
echo -e $"[ERROR] Cannot create $sitesAvailable$domain file."
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
### create PHP-FPM pool file
if ! echo "[$userAs]
user = $userAs
group = www-data
listen = /run/php/php7.2-fpm.$domain.sock
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
request_terminate_timeout = 600
env[HOSTNAME] = \$hostName
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /var/www/$domain/tmp
env[TMPDIR] = /var/www/$domain/tmp
env[TEMP] = /var/www/$domain/tmp
security.limit_extensions = .php
php_admin_value [cgi.fix_pathinfo] = 1
php_admin_value[post_max_size] = 1G
php_admin_value[upload_max_filesize] = 1G
php_admin_value[memory_limit] = 384M
php_admin_value[open_basedir] = /var/www/$domain:/tmp
php_admin_value[display_errors] = Off
" > $phpFpmPoolsAvailable$domain.conf
then
echo -e $"There is an ERROR create PHP-FPM Pool $domain.conf. Probably PHP-FPM is not installed."
exit;
else
echo -e $"\nNew PHP-FPM Pool Created\n"
fi
createHostname $domain
#if [ "$owner" == "" ]; then
# chown -R $(whoami):www-data $userDir$rootDir
#else
#chown -R $owner:www-data $userDir$rootDir
#fi
chown -R $userAs:$userAs $userDir$rootDir
### enable website
ln -s $sitesAvailable$domain $sitesEnable$domain
### enable PHP FPM pool
ln -s $phpFpmPoolsAvailable$domain.conf $phpFpmPoolsEnabled$domain.conf
### restart Nginx
service nginx restart
service php$phpVersion-fpm 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;
elif [ "$action" == 'enable' ]; then
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain dont exists.\nPlease Try Another one"
exit;
else
createHostname $domain
### enable website
ln -s $sitesAvailable$domain $sitesEnable$domain
### enable PHP FPM pool
ln -s $phpFpmPoolsAvailable$domain.conf $phpFpmPoolsEnabled$domain.conf
### restart Nginx
service nginx reload
service php7.2-fpm reload
fi
### show the finished message
echo -e $"Complete!\nYou just enabled Virtual Host $domain"
exit 0;
elif [ "$action" == 'disable' ] ; then
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain dont exists.\nPlease Try Another one"
exit;
else
deleteHostname $domain
### disable website
rm $sitesEnable$domain
### disable PHP FPM pool
rm $phpFpmPoolsEnabled$domain.conf
### restart Nginx
service nginx reload
service php7.2-fpm reload
fi
### show the finished message
echo -e $"Complete!\nYou just disabled Virtual Host $domain"
exit 0;
elif [ "$action" == 'remove' ]; then
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain dont exists.\nPlease Try Another one"
exit;
else
deleteHostname $domain
### disable website
rm $sitesEnable$domain
rm $phpFpmPoolsEnabled$domain.conf
deluser $userAs
### restart Nginx
service nginx reload
service php7.2-fpm reload
### Delete virtual host rules files
rm $sitesAvailable$domain
rm $phpFpmPoolsAvailable$domain.conf
fi
### check if directory exists or not
if [ -d $userDir$rootDir ]; then
rm -rf $userDir$rootDir
echo -e $"Directory deleted"
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