/
vhost20
209 lines (182 loc) · 5.01 KB
/
vhost20
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
#!/bin/bash
TEXTDOMAIN=virtualhost
### waw ubuntu 16.04 nginx php7-fpm
# global? mv this to /usr/local/bin, chmod +x
# else rename to anything.sh , ./anything.sh
### Set default parameters - vhost [create|delete] [example.com] [/path/to/hell]
action=$1
domain=$2
root=$3
# nginx dir ubuntu 16.04
nginx=/var/www/
# Put owner/group that nginx must run over for your system.
owner=$(who am i | awk '{print $1}')
group=www-data
#nginx default
sitesEnable='/etc/nginx/sites-enabled/'
sitesAvailable='/etc/nginx/sites-available/'
#nginx restart command lul
restart='systemctl reload nginx'
#check for root
if [ "$(whoami)" != 'root' ]; then
echo $"You don't have the permission to run $0 as non-root user. Sudo maybe?"
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. example.com"
read domain
done
if [ "$action" == 'create' ]
then
while [ "$root" == "" ]
do
example=example
echo -e $"Enter directore name e.g 'example' for $nginx$example"
read root
done
### check if domain already exists
if [ -e $sitesAvailable$domain ]; then
echo -e $"This domain already exists. Exit."
exit;
fi
### create php-fpm pool
phpfpm="/etc/php/7.4/fpm/pool.d/$domain.conf"
if ! echo "[$domain]
user = www-data
group = www-data
listen = /var/run/php/php7.4-fpm-$domain.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
" > $phpfpm
then
echo -e $"There is an ERROR create $domain.conf file"
exit;
else
echo -e $"New $domain.conf Created."
fi
### restart php-fpm
sudo systemctl restart php7.4-fpm
### change /etc/php/7.4/fpm/conf.d/10-opcache.ini
opcache='/etc/php/7.4/fpm/conf.d/10-opcache.ini'
sh -c "echo 'opcache.enable=0' >> $opcache"
### check if directory exists or not
if ! [ -d $nginx$root ]; then
### create the directory
mkdir $nginx$root
### give permission to root dir
chmod 755 $nginx$root
### write test file in the new domain dir
if ! echo $domain > $nginx$root/index.html
then
echo $"ERROR: Unable to write in $nginx$root/. Please check permissions."
exit;
else
echo $"Added index.html into $nginx$root/"
fi
if ! echo "<?php phpinfo(); ?>" > $nginx$root/info.php
then
echo $"ERROR: Unable to write in $nginx$root/. Please check permissions."
exit;
else
echo $"Added info.php into $nginx$root/"
fi
fi
### create virtual host rules file
if ! echo "server {
listen 80;
root $nginx$root;
index index.php index.html index.htm;
server_name $domain;
# 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 / {
#beauty rewrite like http://example.com/waw/
#try_files $uri $uri/ /index.php?$args;
#standard try
try_files \$uri \$uri/ /index.php?\$query_string;
#redirect
# include /etc/nginx/templates/redirects;
}
if (!-d \$request_filename) {
rewrite ^/(.+)/\$ /\$1 permanent;
}
location ~* \.php\$ {
try_files \$uri =404;
# Server PHP config.
fastcgi_pass unix:/var/run/php7.4-fpm-$domain.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
# Typical vars in here, nothing interesting.
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}" > $sitesAvailable$domain
then
echo -e $"There is an ERROR create $domain file"
exit;
else
echo -e $"New Virtual Host Created."
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."
fi
chown -R $owner:$group $nginx$root
### enable website
ln -s $sitesAvailable$domain $sitesEnable$domain
### restart Nginx
$restart
### show the finished message
echo -e $"Done! \nYour new host is: http://$domain located at $nginx$root"
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailable$domain ]; then
echo -e $"This domain doesn't exists!"
exit;
else
olddir=$(head -3 /etc/nginx/sites-enabled/$domain|tail -1| awk -F " |;" '{print $2}')
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### disable website
rm $sitesEnable$domain
### delete user data
rm -rf $olddir
#service nginx restart
$restart
### Delete virtual host rules files
rm $sitesAvailable$domain
rm /var/run/php7-fpm-$domain.sock
systemctl restart php7.4-fpm
fi
### show the finished message
echo -e $"$olddir deleted. You don't have backup? It's too late now."
exit 0;
fi