-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapache2-deployer.sh
313 lines (294 loc) · 8.26 KB
/
apache2-deployer.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
#!/bin/bash
# Apache2 website deployment
# Authors: UltimateByte, BassSpleen
# Website: https://www.terageek.org
# Description: Creates required files and folders to deploy an apache2 website in one command
# Version: 0.2
### SETTINGS ##
# The parent folder for your user's home directory (usually /home)
homedir="/home"
# The name of you website's folder (usually public_html or www)
webdir="public_html"
# Default shell defined with usermod -s (usually /bin/bash)
usershell="/bin/bash"
#############
## Program ##
#############
# Misc Variables
selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
apache_sites="/etc/apache2/sites-available"
apacheprocess="www-data"
defumask="#umask 022"
tumask="umask 007"
# Check that the user inputted two arguments
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Info! Please, specify a username and a domain for the website"
echo "Example: ./${selfname} username domain.com"
exit 1
elif [ -n "$3" ]; then
echo "[ERROR] Too many arguments!"
sleep 1
echo "Info! Please specify a username and a domain for the website"
echo "Example: ./${selfname} username domain.com"
exit 1
else
# Use a variable name that makes sense
username="${1}"
# Strip www. from user input since we're adding it as an alias
domain="${2#www.}"
fi
# Check that the script is launched with elevated privileges
fn_check_root(){
if [ "$(id -u)" != "0" ]; then
echo "[ERROR] This script must be run with elevated privileges"
exit 1
fi
}
# Checking script config
fn_check_vars(){
echo "Checking config..."
sleep 1
# Checking that the home directory exists
if [ -d "${homedir}" ]; then
check_homedir=0
else
check_homedir=1
echo "[ERROR] Could not find ${homedir}"
echo "Please, set a valid homedir value"
sleep 2
fi
# Checking that the variable directory has been set
if [ -n "${webdir}" ]; then
check_webdir=0
else
check_webdir=1
echo "[ERROR] No webdir set"
echo "Please, set a valid webdir"
sleep 2
fi
# Checking that Apache2 websites-available folder exists
if [ -d "${apache_sites}" ]; then
check_apachedir=0
else
check_apachedir=1
echo "[ERROR] Could not find ${apache_sites}"
echo "Please, install apache2"
sleep 2
fi
# Check summ up
if [ "${check_homedir}" == "1" ] || [ "${check_webdir}" == "1" ] || [ "${check_apachedir}" == "1" ]; then
echo "Info! Errors found, exiting..."
sleep 1
exit 1
else
echo ""
echo "[OK] Config test passed!"
sleep 1
echo ""
# Setting directories
userdir="${homedir}/${username}"
targetdir="${userdir}/${webdir}"
fi
}
# Checks if the user exists and sets the test variable
fn_check_user_exists(){
if [ "$(grep "${username}" /etc/passwd)" ]; then
userexists="1"
if [ ! -d "${targetdir}" ]; then
echo "[ERROR] no target directory to fix"
echo "This script needs ${targetdir} to exist"
echo "Maybe edit the script variables accordingly"
fi
else
userexists="0"
fi
}
fn_welcome_prompt(){
echo "########################################################"
echo "########## Apache 2 website deployment script ##########"
echo "########################################################"
sleep 1
echo ""
echo "Welcome!"
echo ""
echo "Please, take a moment to review your settings:"
echo ""
if [ "${userexists}" == "0" ]; then
echo "User: ${username} will be created"
echo "Web directory: ${targetdir} will be created"
elif [ "${userexists}" == "1" ]; then
echo "User: ${username} exists and will not be created"
echo "Web directory: ${targetdir} will be created if it doesn't exist"
else
echo "[ERROR] Could not determine if ${username} exists"
echo "Please, open a Github issue"
exit 1
fi
echo "Permissions will be fixed in ${targetdir}"
echo "Umask will be set to ${tumask} for ${username} if possible"
if [ ! -f "${apache_sites}/${domain}.conf" ]; then
echo "Vhost: ${apache_sites}/${domain}.conf will be created and enabled"
fi
echo ""
while true; do
read -e -i "y" -p "Continue? [Y/n]" yn
case $yn in
[Yy]* ) echo "Let's go!"; sleep 1; break;;
[Nn]* ) echo "Maybe next time!"; exit 0;;
* ) echo "Please answer yes or no.";;
esac
done
}
# Adding a user with the correct homedir
fn_add_user(){
if [ "${userexists}" == "0" ]; then
echo ""
echo "#################### User Creation #####################"
echo ""
sleep 1
echo "Creating ${username}..."
sleep 1
useradd -m -d "${userdir}" "${username}"
usermod -s "${usershell}" "${username}"
echo "[OK] User created!"
echo ""
echo "[PASSWORD] Please, input a password for ${username}"
passwd "${username}"
echo "[OK] Password set!"
fi
}
fn_fix_umask(){
echo ""
echo "##################### Fixing Umask ####################"
echo ""
sleep 2
if [ -f "${userdir}/.profile" ]; then
if [ "$(cat "${userdir}/.profile" | grep "${defumask}")" ]; then
echo "Fixing user umask (default permissions on files)"
sleep 1
sed -i "s/${defumask}/${tumask}/g" "${userdir}"/.profile
echo "[OK] Umask ${tumask} set!"
elif [ "$(cat "${userdir}/.profile" | grep "${tumask}")" ]; then
echo "Info! It appears that ${tumask} is already set for ${username}"
else
echo "[Warning] Default umask not found, no change will be applied"
fi
else
echo "[Warning] ${userdir}/.profile doesn't exist, cannot determine Umask"
fi
}
# Creating the web directory and applying permissions
fn_web_directory(){
echo ""
echo "################## Directory Creation ##################"
echo ""
sleep 1
echo "Creating the web directory..."
sleep 1
if [ -d "${targetdir}" ]; then
echo "Info! ${targetdir} already exists"
else
mkdir -pv "${targetdir}"
echo "[OK] Directory created!"
fi
echo ""
echo "Applying correct ownership & permissions to the website folder..."
sleep 1
chown -R "${username}":"${username}" "${targetdir}"
chmod -R 770 "${targetdir}"
chmod -R g+s "${targetdir}"
echo "[OK] Ownership & permissions set!"
echo ""
echo "Adding ${username} group to ${apacheprocess}..."
sleep 2
usermod -a -G "${username}" "${apacheprocess}"
echo "[OK] Added user group to ${apacheprocess}!"
echo ""
echo "Restarting apache2 to enable group modifications..."
sleep 1
service apache2 restart
echo "[OK] apache2 restarted!"
}
# Create the Virtual Host config file and enable the site in apache
fn_create_vhosts(){
echo ""
echo "################# VirtualHosts Creation ################"
echo ""
sleep 2
if [ -f "${apache_sites}/${domain}.conf" ]; then
vhostexists="1"
echo "VirtualHost already exists!"
echo "It won't be touched."
else
vhostexists="0"
echo "Generating Virtual Host..."
touch "${apache_sites}/${domain}.conf"
sleep 1
echo "<VirtualHost *:80>
# Addresses
ServerName ${domain}
ServerAlias www.${domain}
ServerAdmin admin@${domain}
# Directory and rules
DocumentRoot ${targetdir}
<Directory ${targetdir}>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
# Logging
# LogLevel settings : debug, info, notice, warn, error, crit, alert
LogLevel warn
ErrorLog \${APACHE_LOG_DIR}/${domain}-error.log
CustomLog \${APACHE_LOG_DIR}/${domain}-access.log combined
</VirtualHost>" >> "${apache_sites}"/"${domain}".conf
echo "[OK] Virtual Host generated!"
fi
}
# Enable the website and restart apache
fn_ensite(){
if [ "${vhostexists}" == "0" ]; then
echo ""
echo "################### Enabling Website ###################"
echo ""
sleep 2
echo "Enabling config for ${domain}..."
sleep 1
a2ensite "${domain}".conf
echo "[OK] Config enabled"
echo ""
echo "Reloading apache2 to apply config..."
sleep 1
# Reloading apache to activate the new website
service apache2 reload
echo "[OK] apache2 reloaded"
sleep 1
fi
}
fn_conclusion(){
echo ""
echo ""
echo "########################################################"
echo "###################### Job Done #######################"
echo "########################################################"
sleep 1
echo ""
echo "Info! Time to add your website into ${targetdir}"
echo "Info! Time to make ${domain} point to this machine"
echo ""
echo "###################### Credits ########################"
echo "UltimateByte: Main development"
echo "BassSpleen: Idea and initial script"
echo "[OK] We wish you the best!"
}
# Starting functions
fn_check_root
fn_check_vars
fn_check_user_exists
fn_welcome_prompt
fn_add_user
fn_fix_umask
fn_web_directory
fn_create_vhosts
fn_ensite
fn_conclusion