-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualhost.sh
executable file
·72 lines (61 loc) · 1.79 KB
/
virtualhost.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
#!/bin/bash
# run script as root if not called as root
if [ "$(whoami)" != 'root' ]
then
sudo $0 $1 $2
exit 0
fi
# Set default parameters
action=$1
owner=$(users)
email='webmaster@localhost'
sitesEnabled='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
rootDir='/var/www/'
currentDir=$(pwd)
dirName=${currentDir##*/}
# check if action is not 'create' or 'delete'
if [[ "${action,,}" != 'create' ]] && [[ "${action,,}" != 'delete' ]]
then
echo "You need to type in the action: "
echo "- '${0} create' or"
echo "- '${0} delete'"
exit 1;
fi
if [[ $2 != "" ]]
then
assumedDomain=$2
else
assumedDomain=${dirName}.local
fi
# get the $domainName and $fullPath
read -e -p "Enter domain name: " -i "${assumedDomain,,}" domainName
if [ "${action,,}" == 'create' ]
then
read -e -p "Enter full path: " -i "${currentDir}" fullPath
fi
# if the action is delete, remove the vhost .conf file and the entry from /etc/hosts
if [ "${action,,}" == 'delete' ]
then
a2dissite ${domainName}
rm -f /etc/apache2/sites-available/${domainName}.conf
sed -i -E "s/127.0.0.1 ${domainName}//g" /etc/hosts
systemctl reload apache2
exit 0
fi
echo "<VirtualHost *:80>
ServerAdmin ${owner}@localhost
ServerName ${domainName}
DocumentRoot ${fullPath}
ErrorLog \${APACHE_LOG_DIR}/${domainName}-error.log
CustomLog \${APACHE_LOG_DIR}/${domainName}-access.log combined
<Directory ${fullPath}>
Options +Includes +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
DirectoryIndex index.php
</Directory>
</VirtualHost>" > /etc/apache2/sites-available/${domainName}.conf
echo "127.0.0.1 ${domainName}" >> /etc/hosts
a2ensite ${domainName}
systemctl reload apache2