Repository navigation
Expand file tree
/
Copy pathvirtualhost-auto.sh
More file actions
139 lines (118 loc) · 3.1 KB
/
Copy pathvirtualhost-auto.sh
File metadata and controls
139 lines (118 loc) · 3.1 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
135
136
137
138
139
#!/bin/bash
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
### Set Language
TEXTDOMAIN=virtualhost
### Set default parameters
action=$1
domain=$2
owner=$3
sitesAvailable='/etc/httpd/conf.d/'
homeDir='/home/'
sitesAvailabledomain=$sitesAvailable$domain.conf
sitesAvailableSSLdomain=$sitesAvailable$domain-le-ssl.conf
### don't modify from here unless you know what you are doing ####
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
then
echo $"You need to prompt for action (create or delete) -- Lower-case only"
exit 1;
fi
while [ "$domain" == "" ]
do
echo -e $"Please provide domain. e.g.dev,staging"
read domain
done
if [ "$owner" == "" ] && [ "$action" == 'create' ]
then
echo $"Please provide owner"
exit 1;
fi
rootDir=$homeDir$owner/www
if [ "$action" == 'create' ]
then
### check if domain already exists
if [ -e $sitesAvailabledomain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi
echo "Do you wish to add www.$domain?"
select yn in "Yes" "No"; do
case $yn in
Yes )
if ! echo "
<VirtualHost *:80>
ServerName $domain
ServerAlias www.$domain
DocumentRoot $rootDir
RMode config
RUidGid $owner apache
<Directory $rootDir>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/$domain-error.log
LogLevel error
CustomLog /var/log/httpd/$domain-access.log combined
</VirtualHost>" > $sitesAvailabledomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
break;;
No )
if ! echo "
<VirtualHost *:80>
ServerName $domain
DocumentRoot $rootDir
RMode config
RUidGid $owner apache
<Directory $rootDir>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/$domain-error.log
LogLevel error
CustomLog /var/log/httpd/$domain-access.log combined
</VirtualHost>" > $sitesAvailabledomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi
break;;
esac
done
if [ "$owner" == "" ]; then
chown -R $user:$user $rootDir
else
chown -R $owner:$owner $rootDir
fi
### restart Apache
service httpd restart
### show the finished message
ok "Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir"
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailabledomain ]; then
echo -e $"This domain does not exist.\nPlease try another one"
exit;
else
### Delete virtual host rules files
rm $sitesAvailabledomain
if [ -e $sitesAvailableSSLdomain ]; then
rm $sitesAvailableSSLdomain
fi
### restart Apache
service httpd restart
fi
### show the finished message
ok "Complete!\nYou just removed Virtual Host $domain"
exit 0;
fi