-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_vhost.sh
executable file
·88 lines (64 loc) · 2.32 KB
/
add_vhost.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
#!/bin/bash
domainUrl=$1
### Are you root ?
if [ "$(whoami)" != 'root' ]; then
echo $"You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
if [ -z "$domainUrl" ]; then
echo 'Add URL site address without www subdomain (example ukr.net) '
read domainUrl;
fi
echo 'Are you sure to continue with' $domainUrl '? [Y,n]'
read continue;
if [ $continue != 'y' ]; then
echo 'Your answer is no. Good bye!';
exit 0
fi
echo "Ok let's go to the next step..."
### Convert domain adress without '.'
domainWithoutDot=${domainUrl//[-._]/}
### Add group for vhost
echo 'Add group' 'www-'$domainWithoutDot
addgroup 'www-'$domainWithoutDot
echo 'group' 'www-'$domainWithoutDot 'was succesfully added'
### Add vhost directory
echo 'Creating directory for vhost'
mkdir -p /var/www/$domainUrl/public_html
echo 'Directory was succesfully created'
### Add user for vhost
echo 'Add user for vhost'
useradd -s /bin/false -d /var/www/$domainUrl/public_html -m -g 'www-'$domainWithoutDot 'www-'$domainWithoutDot
echo 'User was succesfully created'
echo 'Creating vhost file'
### Add vhost config file
echo "
<VirtualHost *:80>
ServerName $domainUrl
ServerAlias www.$domainUrl
ServerAdmin root@$domainUrl
DocumentRoot /var/www/$domainUrl/public_html
<IfModule mpm_itk_module>
AssignUserId www-$domainWithoutDot www-$domainWithoutDot
</IfModule>
<Directory /var/www/$domainUrl/public_html/>
Options FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/log/apache2/$domainUrl-error.log
CustomLog /var/log/apache2/$domainUrl-access.log combined
</VirtualHost>" > /etc/apache2/sites-available/$domainUrl.conf
### Enable vhost settings
a2ensite $domainUrl.conf
service apache2 reload
echo 'Set directory and files permission'
### Add owner and permission for read,write,execute for user and group only
chown -R www-$domainWithoutDot:www-$domainWithoutDot /var/www/$domainUrl/public_html
find /var/www/$domainUrl/public_html -type d -exec chmod 770 {} +
find /var/www/$domainUrl/public_html -type f -exec chmod 660 {} +
### Add user to vhost group
echo 'Write your linux username';
read linuxUsername;
echo 'Add '$linuxUsername 'to the www-'$domainWithoutDot' group ';
usermod -a -G www-$domainWithoutDot $linuxUsername;
exit 0;