-
Notifications
You must be signed in to change notification settings - Fork 0
/
vhost.sh
180 lines (147 loc) · 4.71 KB
/
vhost.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
#!/bin/bash
# CREATE DIR IF NOT EXIST
if [ ! -d generated_vhost ];then
mkdir generated_vhost
fi
apacheSuffix=apache.conf
nginxSuffix=nginx.conf
pathSiteAvailableNginx="/etc/nginx/sites-available"
pathSiteEnabledNginx="/etc/nginx/sites-enabled"
pathSiteAvailableApache="/etc/apache2/sites-available"
read -rp "vHost name (domain): " serverName
read -rp "Alias name: " aliasName
read -rp "Document root (without /var/www/): " documentRoot
documentRoot="/var/www/"$documentRoot
while true; do
read -rp "Create on Apache ? " doApache
case $doApache in
[Yy] ) doApache="y" break;;
[Nn] ) doApache="n" break;;
* ) echo "Yes or No";;
esac
done
# BLOCK FOR APACHE2'S VHOST
if [ "$doApache" == "y" ]; then
vHostApache=$pathSiteAvailableApache/$serverName.$apacheSuffix
touch "$vHostApache"
cat base_vhost/apache/base >> "$vHostApache"
while true; do
read -rp "Used with reverse proxy ? " reverse
case $reverse in
[Yy]* ) reverse="y" break;;
[Nn]* ) reverse="n" break;;
* ) echo "Yes or No";;
esac
done
#If Apache is used with a reverse_proxy
if [ "$reverse" == "y" ]; then
read -rp "Port used by Apache : " portUsed
sed -i 's/${portUsed}/'"$portUsed"'/' "$vHostApache"
else
portUsed="80"
sed -i 's/${portUsed}/'$portUsed'/' "$vHostApache"
fi
# MODIFY VHOST WITH INFORMATION'S
sed -i 's/${serverName}/'"$serverName"'/' "$vHostApache"
sed -i 's/${aliasName}/'"$aliasName"'/' "$vHostApache"
sed -i 's~${documentRoot}~'"$documentRoot"'~' "$vHostApache"
sudo a2ensite "$serverName"
sudo systemctl restart apache2
echo "vHost created and enabled on Apache"
#Create files with variable. Will be used for https
touch generated_vhost/"$vHostApache".variables
# WRITE IN FILE
{
echo serverName="$serverName"
echo aliasName="$aliasName"
echo documentRoot="$documentRoot"
echo portUsed="$portUsed"
} >> generated_vhost/"$vHostApache".variables
# END WRITE
fi
while true; do
read -rp "Create on Nginx ? " doNginx
case $doNginx in
[Yy] ) doNginx="y" break;;
[Nn] ) doNginx="n" break;;
* ) echo "Yes or No";;
esac
done
if [ "$doNginx" == "y" ]; then
vHostNginx=$pathSiteAvailableNginx/$serverName.$nginxSuffix
vHostEnabled=$pathSiteEnabledNginx/$serverName.$nginxSuffix
touch "$vHostNginx"
cat base_vhost/nginx/base >> "$vHostNginx"
# MODIFY VHOST WITH INFORMATION'S
sed -i 's/${serverName}/'$serverName'/' "$vHostNginx"
sed -i 's/${aliasName}/'$aliasName'/' "$vHostNginx"
sed -i 's~${documentRoot}~'"$documentRoot"'~' "$vHostNginx"
# ASK FOR PHP AND IF IT'S USED AS REVERSE PROXY
while true; do
read -rp "Activate PHP ? " activatePHP
case $activatePHP in
[Yy]* ) activatePHP="y" break;;
[Nn]* ) activatePHP="n" break;;
* ) echo "Yes or No";;
esac
done
if [ "$activatePHP" == "y" ]; then
while true; do
read -rp "Nginx is a reverse_proxy ? " reverseProxy
case $reverseProxy in
[Yy]* ) reverseProxy="y" break;;
[Nn]* ) reverseProxy="n" break;;
* ) echo "Yes or No";;
esac
done
if [ "$reverseProxy" == "y" ]; then
read -rp "On which IP to send ? " ipToSend
read -rp "On which port to send ? " portToSend
sed -i '/}/d' "$vHostNginx"
cat base_vhost/nginx/phpreverse >> "$vHostNginx"
sed -i 's/${ipToSend}/'"$ipToSend"'/' "$vHostNginx"
sed -i 's/${portToSend}/'"$portToSend"'/' "$vHostNginx"
else
read -rp "PHP version ? (e.g. 7.4) " phpVersion
sed -i '/}/d' "$vHostNginx"
cat base_vhost/nginx/php >> "$vHostNginx"
sed -i 's/${phpVersion}/'"$phpVersion"'/' "$vHostNginx"
fi
fi
# END PHP
sudo ln -s "$vHostNginx" "$vHostEnabled"
sudo systemctl restart nginx
echo "vHost created and enabled on NGINX"
#Create files with variable. Will be used for https
touch generated_vhost/"$vHostNginx".variables
# WRITE IN FILE
{
echo serverName="$serverName"
echo aliasName="$aliasName"
echo documentRoot="$documentRoot"
} >> generated_vhost/"$vHostNginx".variables
if [ -n "$portToSend" ] && [ -n "$ipToSend" ]; then
{
echo portToSend="$portToSend"
echo ipToSend="$ipToSend"
} >> generated_vhost/"$vHostNginx".variables
fi
if [ -n "$phpVersion" ]; then
echo phpVersion="$phpVersion" >> generated_vhost/"$vHostNginx".variables
fi
# END WRITE
fi
while true; do
read -rp "Create more vHost ? " moreVHost
case $moreVHost in
[Yy] ) moreVHost="y" break;;
[Nn] ) moreVHost="n" break;;
* ) echo "Yes or No";;
esac
done
if [ "$moreVHost" == "y" ]; then
addMoreVHost=$(readlink -f "$0")
exec $addMoreVHost
else
exit
fi