This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mkvhost.sh
executable file
·213 lines (177 loc) · 5.94 KB
/
mkvhost.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
#!/bin/bash
DIR=$1
SHORTNAME=$2
FQDN=$3
if [ `whoami` != root ]; then
echo 'Error: root only.'
exit 1;
fi
if [ -z $DIR ] || \
[ -z $SHORTNAME ] || \
[ -z $FQDN ]; then
echo 'Usage: '$0' [Directory] [Shortname] [FQDN]'
exit 1;
fi
if [ -e /etc/apache2/sites-available/$SHORTNAME ] || \
[ -e /etc/apache2/sites-available/$SHORTNAME-redirect ] || \
[ -e /etc/logrotate.d/apache2-$SHORTNAME ] || \
[ -e /etc/awstats/awstats.$SHORTNAME.conf ] || \
[ -e /etc/awstats/awstats.$SHORTNAME-redirect.conf ] || \
[ -e /etc/cron.d/awstats-$SHORTNAME ] ; then
echo 'FQDN or shortname used. Please change another one.'
exit 1;
fi
echo '
*****************************************************************************
makevhost
This script helps you build a virtual host fast
*****************************************************************************
Your configration:
+ Directory: '$DIR'
* must be a full path WITHOUT trailing slash.
* Root dir to server will be '$DIR'/www
* Logs will go to '$DIR'/logs, feel free to replace
it with a softlink dir.
* Awstats log will go to '$DIR'/awstats, also can be softlinked.
+ Short name: '$SHORTNAME'
When complete, script will enable site '$SHORTNAME',
another site conf called '$SHORTNAME'-redirect will also be created,
which will send 301 to the real site.
To remove generated site, run rmvhost.sh.
'
if [ -e $DIR/awstats.conf ] || \
[ -e $DIR/awstats-cron ] || \
[ -e $DIR/logrotate ] || \
[ -e $DIR/vhost.conf ] || \
[ -e $DIR/vhost-redirect.conf ] ; then
echo '** WARNING **: There will be config file(s) being overwritten.
'
fi
echo 'Press Ctrl+C to escape if incorrect.'
read -p 'Enter to continue. <Enter or Ctrl+C>'
echo '* mkdir dir ...'
mkdir -p $DIR
mkdir -p $DIR/www
mkdir -p $DIR/var
chown -R www-data:www-data $DIR/var
mkdir -p $DIR/awstats
mkdir -p $DIR/logs
touch $DIR/logs/access.log
touch $DIR/logs/access-redirect.log
chown root:root $DIR $DIR/awstats $DIR/logs $DIR/logs/access.log $DIR/logs/access-redirect.log
echo '* vhost conf ...'
#site script
echo '<VirtualHost *:80>
ServerName '$FQDN'
#ServerAlias '$FQDN'
ServerAdmin webmaster@'$FQDN'
DocumentRoot '$DIR'/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory '$DIR'/www/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog '$DIR'/logs/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog '$DIR'/logs/access.log combined
</VirtualHost>' | tee $DIR/vhost.conf > /dev/null
chown root:root $DIR/vhost.conf
ln -s $DIR/vhost.conf /etc/apache2/sites-available/$SHORTNAME.conf
#redirection site script
echo '<VirtualHost *:80>
# redirect vhost that issue 301 redirection to the real site.
# Remember to edit awstats-redirect.conf
#ServerName www.'$FQDN'
#ServerAlias '$FQDN'.
ServerAdmin webmaster@'$FQDN'
DocumentRoot '$DIR'/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory '$DIR'/www/>
Options FollowSymLinks
AllowOverride All
Require all granted
RedirectMatch permanent ^/(.*)$ http://'$FQDN'/$1
</Directory>
ErrorLog '$DIR'/logs/error-redirect.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog '$DIR'/logs/access-redirect.log combined
</VirtualHost>' | tee $DIR/vhost-redirect.conf > /dev/null
chown root:root $DIR/vhost-redirect.conf
ln -s $DIR/vhost-redirect.conf /etc/apache2/sites-available/$SHORTNAME-redirect.conf
echo '* logrotate ...'
echo $DIR'/logs/*.log {
weekly
missingok
rotate 52
compress
delaycompress
compresscmd /bin/bzip2
compressoptions --best
uncompresscmd /bin/bunzip2
compressext .bz2
notifempty
create 644 root root
sharedscripts
prerotate
[ ! -d /etc/awstats ] || /usr/lib/cgi-bin/awstats.pl -update -config='$SHORTNAME'
[ ! -d /etc/awstats ] || /usr/lib/cgi-bin/awstats.pl -update -config='$SHORTNAME'-redirect
endscript
postrotate
if [ -f /var/run/apache2/apache2.pid ]; then
/etc/init.d/apache2 restart > /dev/null
fi
endscript
}' | tee $DIR/logrotate > /dev/null
chown root:root $DIR/logrotate
ln -s $DIR/logrotate /etc/logrotate.d/apache2-$SHORTNAME
echo '* awststs ...'
echo 'Include "/etc/awstats/awstats.conf"
SiteDomain="'$FQDN'"
LogFile="'$DIR'/logs/access.log"
LogFormat=1
DirData="'$DIR'/awstats"
HostAliases="'$FQDN' localhost 127.0.0.1"
' | tee $DIR/awstats.conf > /dev/null
chown root:root $DIR/awstats.conf
echo 'Include "/etc/awstats/awstats.conf"
SiteDomain="'$FQDN'"
LogFile="'$DIR'/logs/access-redirect.log"
LogFormat=1
DirData="'$DIR'/awstats"
HostAliases="'$FQDN' localhost 127.0.0.1"
' | tee $DIR/awstats-redirect.conf > /dev/null
chown root:root $DIR/awstats-redirect.conf
#awstats crontab
echo $(date +%S)' * * * * root [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.'$SHORTNAME'.conf ] && /usr/lib/cgi-bin/awstats.pl -config='$SHORTNAME' -update >/dev/null
'$(date +%S)' * * * * root [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.'$SHORTNAME'-redirect.conf ] && /usr/lib/cgi-bin/awstats.pl -config='$SHORTNAME'-redirect -update >/dev/null' | tee $DIR/awstats-cron > /dev/null
chown root:root $DIR/awstats-cron
if [ ! -d /etc/awstats ] ; then
echo 'Warning: awstats not installed. conf files will be created but not linked.'
else
ln -s $DIR/awstats.conf /etc/awstats/awstats.$SHORTNAME.conf
ln -s $DIR/awstats-redirect.conf /etc/awstats/awstats.$SHORTNAME-redirect.conf
ln -s $DIR/awstats-cron /etc/cron.d/awstats-$SHORTNAME
#remove awstats nested include
cat /etc/awstats/awstats.conf | sed -i -e 's/^Include/#Include/' /etc/awstats/awstats.conf
fi
echo '* enable site ...'
a2ensite $SHORTNAME > /dev/null
echo '
Done.
Please
apache2ctl graceful
to load the config of the website gracefully.
You might want to edit '$DIR'/vhost.conf before doing that,
depends on your application configration.
'