-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapache-virtualhost.sh
More file actions
executable file
·134 lines (115 loc) · 4.4 KB
/
apache-virtualhost.sh
File metadata and controls
executable file
·134 lines (115 loc) · 4.4 KB
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
#!/usr/bin/env bash
### Setting up default default variable and assign default value
action=$1
domain=$2
rootDir=$3
port=${4:-80} # Default port is 80 if not specified
owner=$(who am i | awk '{print $1}')
apacheUser=$(ps -ef | egrep '(httpd|apache2|apache)' | grep -v root | head -n1 | awk '{print $1}')
email='webmaster@localhost'
enabledSites='/etc/apache2/sites-enabled/'
availableSites='/etc/apache2/sites-available/'
dirPath='/var/www/html'
domainAvailable=$availableSites$domain.conf
### Function to check if port is already in use
function is_port_in_use {
netstat -tuln | grep :$1 > /dev/null
return $?
}
### Checking Up isRoot user and not given domain name
if [ "$(whoami)" != 'root' ]; then
echo -e $"\nYou dont have permission to run this script please login as root with sudo -s or use sudo.\n"
exit 1;
fi
if [ "$action" != 'create' ] && [ "$action" != 'delete' ] && [ "$action" != 'list' ]
then
echo -e $"\nPlease Use create or delete or list as action.\n"
exit 1;
fi
if [ "$action" == 'list' ]
then
echo -e $"\n********************\n"
### command for list
a2query -s
echo -e $"\n********************\n"
exit;
fi
while [ "$domain" == '' ]; do
echo -e $"Please give a domain name like nayeem.test or web.dev :"
read domain
done
if [ "$action" == 'create' ]; then
### Check if port is already in use
if is_port_in_use $port; then
echo -e $"\nError: Port $port is already in use. Please use a different port.\n"
exit 1
fi
### check if domain already exists
if [ -e $domainAvailable ]; then
echo -e $"\nHey, this domain is already exist in host please retry with new one.\n"
exit;
fi
### checking up directory is exist if not then create one with permison
if ! [ -d $rootDir ]; then
mkdir $rootDir
chmod 755 $rootDir
fi
### Creating virtual host conf file with rules
if ! echo "
Listen $port
NameVirtualHost *:$port
<VirtualHost *:$port>
ServerAdmin $email
ServerName $domain
ServerAlias $domain
DocumentRoot $rootDir
ErrorLog /var/log/apache2/$domain-error.log
LogLevel error
CustomLog /var/log/apache2/$domain-access.log combined
<Directory />
AllowOverride All
</Directory>
<Directory $rootDir>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
</VirtualHost>" > $domainAvailable
then
echo -e $"\nOooops!! Something went wrong to create $domain host please retry.\n"
exit;
else
echo -e $"\nBoooooM!! Your Virtual Host Created Successfully.\n"
fi
### Final touch: add in /etc/hosts site enable and apache restart
if ! echo "127.0.0.1 $domain" >> /etc/hosts
then
echo $"ERROR: Not able to write in /etc/hosts\n"
exit;
else
echo -e $"Host added to /etc/hosts file \n"
fi
a2ensite $domain
/etc/init.d/apache2 reload
echo -e $"\n*************** Host created successfully visit your domain: http://$domain:$port now **************************\n"
exit;
else
### check whether domain already exists
if ! [ -e $domainAvailable ]; then
echo -e $"\nThe domain name you provide is not exist in host please use an existing domain.\n"
exit;
else
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### disable website
a2dissite $domain
### restart Apache
/etc/init.d/apache2 reload
### Delete virtual host rules files
rm $domainAvailable
fi
### show the finished message
echo -e $"\n*************** Your Domain deleted with host and disabled site. ***************\n"
exit 0;
fi