forked from Montana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontanapache.sh
419 lines (357 loc) · 10.9 KB
/
montanapache.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/bin/bash
current_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
hosts_path="/path/to/hosts"
vhosts_path="/path/to/vhosts/"
web_root="/path/to/home"
NONE='\033[00m'
RED='\033[01;31m'
YELLOW='\033[01;33m'
CYAN='\033[01;36m'
site_url=0
relative_doc_root=0
linux_user=0
linux_group=0
while getopts ":n:d:u:" o; do
case "${o}" in
n)
site_url=${OPTARG}
;;
d)
relative_doc_root=${OPTARG}
;;
u)
linux_user=${OPTARG}
;;
g)
linux_group=${OPTARG}
;;
p)
parent=${OPTARG}
;;
esac
done
## Montana's Functions
validate_parent_exists () {
if ! file_exists "$vhosts_path/$parent.conf"; then
echo -e "${RED}$parent does not exist. Please create this first."
exit 2
fi
}
validate_domain_syntax () {
if [[ $site_url =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$ ]]; then
return 0
else
echo -e "${RED}Invalid domain name. Please try again${NONE}"
exit 2
fi
}
directory_exists () {
if [ -d "$1" ]; then
echo -e "${YELLOW}Directory ($1) already exists.${NONE} skipping..."
return 0
else
return 1
fi
}
file_exists () {
if [ -f "$1" ]; then
echo -e "${YELLOW}File ($1) already exists.${NONE} skipping..."
return 0
else
return 1
fi
}
is_subdomain () {
if [ $site_type == 2 ] || [ $site_type == 4 ]; then
return 0
else
return 1
fi
}
is_rails () {
if [ $site_type == 3 ] || [ $site_type == 4 ]; then
return 0
else
return 1
fi
}
# Montana's Apache Configs
setup_vhosts () {
if is_subdomain; then
append_subdomain_to_vhosts
else
if file_exists "$vhosts_path$site_url.conf"; then
echo -e "${RED}$vhosts_path$site_url.conf already exists.${NONE}"
read -p "Edit file? [Yn] " edit
if [ $edit == "Y" ]; then
if is_subdomain; then flags="-n $site_url -p $parent$"; else flags="-n $site_url"; fi
echo -e "${YELLOW}You are about to enter VIM to edit this file.${NONE}"
echo -e "Once completed please run ${YELLOW}./new_domain.sh $flags${NONE} to complete the setup."
echo ""
read -p "Editing $vhosts_path$site_url.conf [Hit Enter]" hit_enter
vim "$vhosts_path$site_url.conf"
else
echo "$vhosts_path$site_url.conf configuration skipped..."
echo ""
fi
else
echo -e "Creating ${CYAN}$vhosts_path$site_url.conf${NONE} file..."
vhost="$vhosts_path$site_url.conf"
`touch $vhost`
build_vhost_config
echo -e "${CYAN}$vhosts_path$site_url.conf successfully created and updated.${NONE}"
fi
fi
}
append_subdomain_to_vhosts () {
vhost="$vhosts_path$parent.conf"
if file_exists $vhost; then
echo -e "Appending configurations to ${CYAN}$vhosts_path$parent.conf${NONE} file..."
build_vhost_config
else
echo -e "${RED}$vhosts_path$parent.conf does not exist.${NONE}"
exit 2
fi
}
build_vhost_config () {
echo "" >> $vhost
echo "" >> $vhost
echo "<VirtualHost *:80>" >> $vhost
if is_rails; then
echo " DocumentRoot $absolute_doc_root/httpdocs/public" >> $vhost
else
echo " DocumentRoot $absolute_doc_root/httpdocs" >> $vhost
fi
echo " ServerName $domain" >> $vhost
echo " ServerAlias www.$domain" >> $vhost
echo "" >> $vhost
if is_rails; then
echo " <Directory $absolute_doc_root/httpdocs/public>" >> $vhost
else
echo " <Directory $absolute_doc_root/httpdocs>" >> $vhost
fi
echo " Options Indexes FollowSymLinks MultiViews" >> $vhost
echo " AllowOverride All" >> $vhost
echo " Allow from All" >> $vhost
echo " </Directory>" >> $vhost
echo "" >> $vhost
echo " ErrorLog $absolute_doc_root/config/error_log" >> $vhost
if is_rails; then
echo " RailsEnv $env" >> $vhost
fi
echo "</VirtualHost>" >> $vhost
}
append_to_host () {
if file_exists "$hosts_path"; then
echo ""
echo -e "${CYAN}Appending $domain to $hosts_path file${NONE}"
echo "" >> $hosts_path
echo "127.0.0.1 $domain" >> $hosts_path
else
echo -e "${RED}$hosts_path does not exist.${NONE}"
exit 2
fi
}
# Linux config
create_user () {
ret=false
getent passwd $linux_user >/dev/null 2>&1 && ret=true
if [ $ret == true ]; then
echo -e "${CYAN}$linux_user user exists and will be set as owner${NONE}"
linux_group=`id -g -n "$linux_user"`
else
read -p "Password for new user: " pass
password=$(perl -e 'print crypt($ARGV[0], "2ac756d46bc7da594df7e2c94b464064")', $pass)
if `/usr/sbin/useradd -M -p $password $linux_user`; then
linux_group=`id -g -n "$linux_user"`
echo -e " ${CYAN}User successfully created${NONE}"
echo -e " ${YELLOW}User: $linux_user${NONE}"
echo -e " ${YELLOW}Group: $linux_group${NONE}"
echo -e " ${YELLOW}Password: $pass${NONE}"
else
set_current_user
fi
fi
}
set_current_user () {
echo -e "${YELLOW}User could not be created, setting current login as owner${NONE}"
linux_user=$SUDO_USER
linux_group=`id -g -n "$linux_user"`
echo -e " ${YELLOW}User: $linux_user${NONE}"
echo -e " ${YELLOW}Group: $linux_group${NONE}"
echo ""
}
# Crete file structure
create_file_structure () {
echo -e "${CYAN}Creating directory structure...${NONE}"
echo ""
`mkdir -p "$absolute_doc_root/"`
if [ ! -d "$absolute_doc_root" ]; then
echo -e "${RED}Error creating $absolute_doc_root, please check permissions and parent directory${NONE}"
exit 2
fi
if ! directory_exists "$absolute_doc_root/httpdocs"; then `mkdir "$absolute_doc_root/httpdocs"`; fi
if ! directory_exists "$absolute_doc_root/httpsdocs"; then `mkdir "$absolute_doc_root/httpsdocs"`; fi
if ! directory_exists "$absolute_doc_root/shared"; then `mkdir "$absolute_doc_root/shared"`; fi
if ! directory_exists "$absolute_doc_root/releases"; then `mkdir "$absolute_doc_root/releases"`; fi
if ! directory_exists "$absolute_doc_root/config"; then `mkdir "$absolute_doc_root/config"`; fi
if ! directory_exists "$absolute_doc_root/domains" && ! is_subdomain; then `mkdir "$absolute_doc_root/domains"`; fi
# Create an index file
indexfile="$absolute_doc_root/httpdocs/index.html"
`touch "$indexfile"`
`touch "$absolute_doc_root/config/error_log"`
echo "<html><head></head><body>Welcome!</body></html>" >> "$indexfile"
`chown -R $linux_user:$linux_group "$absolute_doc_root/"`
if ! $ret; then `usermod -d $absolute_doc_root $linux_user`; fi
echo -e "${CYAN}Structure successfully created${NONE}"
tree $absolute_doc_root
}
restart_apache () {
echo ""
echo -e "${YELLOW}Restarting Apache server${NONE}"
`apachectl graceful`
}
select_site_type () {
read -p "Enter your selection: " site_type
case $site_type in
1)
echo -e "${CYAN}Creating Standard Website${NONE}"
;;
2)
echo -e "${CYAN}Creating Subdomain Website${NONE}"
;;
3)
echo -e "${CYAN}Creating Rails Application${NONE}"
;;
4)
echo -e "${CYAN}Creating Subdomain Rails Application${NONE}"
;;
*)
echo -e "${RED}Invalid Selection: Please try again:${NONE}"
select_site_type
esac
}
select_environment () {
read -p "Please enter selection: " env_selection
case $env_selection in
1)
echo -e "${CYAN}Development environment selected${NONE}"
env='development'
;;
2)
echo -e "${CYAN}Staging environment selected${NONE}"
env='staging'
;;
3)
echo -e "${CYAN}Production environment selected${NONE}"
env='production'
;;
*)
echo -e "Custom environment: ${CYAN}$env_selection${NONE}"
env=$env_selection
esac
}
create_database () {
MYSQL=`which mysql`
q1="CREATE DATABASE IF NOT EXISTS $db_name;"
q2="GRANT ALL ON *.* TO '$db_user'@'localhost' IDENTIFIED BY '$db_pass';"
q3="FLUSH PRIVILEGES;"
sql="$q1 $q2 $q3"
$MYSQL -u root -p -e "$sql"
echo -e "${CYAN}Database $db_name created with user $db_user${NONE}"
}
# Make sure you're sudo.
if [ "$(whoami)" != "root" ]; then
echo -e "${RED}Root privileges are required to run this, try running with sudo...${NONE}"
exit 2
fi
# Determine virtual host
echo ""
echo ""
echo -e "${CYAN}Please select site structure:${NONE}"
echo " 1) Standard Website"
echo " 2) Subdomain Website"
echo " 3) Rails Application"
echo " 4) Subdomain Rails Application"
echo ""
echo ""
select_site_type
# Subdomain config
if is_subdomain; then
if [ $site_url == 0 ]; then
echo ""
echo -e "You will be prompted for the ${CYAN}parent domain${NONE}."
echo -e "If the ${CYAN}parent domain${NONE} desired does not yet exist please cancel and create it now. (Ctrl + c)"
echo -e "Please don't forget the top-level domain (ex: .com)"
echo ""
read -p "Enter parent domain: " parent
fi
validate_parent_exists
echo -e "You will now be prompted for your ${CYAN}subdomain name${NONE}"
echo -e "This is only the name, so enter ${CYAN}subdomain${NONE} and it will be saved as ${CYAN}subdomain.$parent${NONE}"
echo ""
read -p "Please enter the desired subdomain: " site_url
if [ $relative_doc_root == 0 ]; then
absolute_doc_root="$web_root/$parent/domains/$site_url"
else
absolute_doc_root="$web_root/$parent/domains/$relative_doc_root"
fi
else
if [ $site_url == 0 ]; then
echo ""
echo "Domain name"
echo -e "Please don't forget the top-level domain (ex: .com)"
echo ""
read -p "Please enter the desired URL: " site_url
validate_domain_syntax
fi
if [ $relative_doc_root == 0 ]; then
absolute_doc_root="$web_root/$site_url"
else
absolute_doc_root="$web_root/$relative_doc_root"
fi
fi
# RailsApp config
###
if is_rails; then
echo ""
echo ""
echo -e "${CYAN}Please select a RAILS_ENV:${NONE}"
echo " 1) Development"
echo " 2) Staging"
echo " 3) Production"
echo -e " if ${CYAN}Other${NONE}, enter:"
echo ""
echo ""
select_environment
fi
# Apache Host config
if is_subdomain; then domain="$site_url.$parent"; else domain="$site_url"; fi
setup_vhosts
append_to_host
# Domain owner / Linux config
if [[ -z "$linux_group" ]]; then
linux_group=$linux_user
else
read -p "Create or assign user: " linux_user
create_user
fi
# Create file structure
create_file_structure
# Restart Montana's Apache Server and MySQL
restart_apache && restart_mysql
# Create MySQL Db
read -p "Does $domain require a MySQL DB? [Yn] " needs_db
if [ $needs_db == "Y" ]; then
read -p "Please enter database name: " db_name
read -p "Please enter database user: " db_user
read -p "Please enter database password: " db_pass
create_database
else
echo "Skipping MySQL Database"
fi
# Close out script
echo ""
echo -e "${CYAN}New Domain: $site_url successfully setup${NONE}"
echo "Login to twitter Montana Mendy?"
exit 2