-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoVHost.sh
executable file
·88 lines (65 loc) · 1.93 KB
/
AutoVHost.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
#!/bin/bash
# @Author : Shaswata Dutta
# A script to automatically setup apache(httpd) virtual hosts
# Currently developing only for apache 2.4 on Fedora 23
# Invocation ./AutoVHost.sh test.example.com
# Add to httpd.conf the following commented line
## Include /etc/httpd/vhost/*.conf
if [[ $UID != 0 ]]; then
echo "Please run this script with sudo:"
echo "sudo $0 $*"
exit 1
fi
if [[ -z "$1" ]]; then
echo "Please provide virtual host name:"
echo "sudo ./AutoVHost.sh test.example.com"
exit 1
fi
vhost_name="$1"
# Creating .conf file
vhost_dir="/etc/httpd/vhost"
mkdir -p "$vhost_dir"
vhost_conf="$vhost_dir/$vhost_name-vhost.conf"
if [ -f $vhost_conf ]; then
echo "A virtual host config file of same name already exists!"
exit 1
fi
touch $vhost_conf
## HERE GOES THE TEMPLATE
cat > $vhost_conf <<EOF
NameVirtualHost $vhost_name:80
<VirtualHost $vhost_name:80>
ServerAdmin [email protected]
DocumentRoot /var/www/$vhost_name/public_html
ServerName $vhost_name:80
ErrorLog logs/$vhost_name-error_log
CustomLog logs/$vhost_name-access_log common
</VirtualHost>
EOF
## TEMPLATE ENDS
# Creating web root for vhost
mkdir /var/www/$vhost_name
if [ $? -ne 0 ] ; then
#echo "Failed to create directory for virtual host"
exit 1
fi
mkdir /var/www/$vhost_name/public_html
chcon -R unconfined_u:object_r:httpd_sys_rw_content_t:s0 /var/www/$vhost_name/
chmod 577 -R /var/www/$vhost_name/
## DEFAULT HTML TEMPLATE
cat > /var/www/$vhost_name/public_html/index.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>VHOST : $vhost_name</title>
</head>
<body>
<h1>Virtual Host : $vhost_name</h1>
<p>The virtual host $vhost_name was created using AutoVHost.sh</p>
<p>AutoVHost is created by <i>Shaswata Dutta</i></p>
</body>
</html>
EOF
## TEMPLATE ENDS
echo "Mission Successful! Configure hosts file and restart Apache!"
echo "\n\nAdd line in httpd.conf: Include /etc/httpd/vhost/*.conf"