forked from jsifalda/nginx-server-manager-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-manager.sh
executable file
·162 lines (133 loc) · 2.83 KB
/
server-manager.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
#!/usr/bin/env bash
htdocs="/var/www"
nginxConfDir="/etc/nginx/sites-enabled"
hostsFile="/etc/hosts"
function getColor
{
case $1 in
"red" )
echo "1";;
"green" )
echo "2";;
esac
}
function createDirectory
{
if [[ ! -e $1 ]]; then
mkdir -p $1
writeMessage "Directory '$1' was created"
else
writeMessage "Directory '$1' exists"
fi
}
function remove
{
if [[ $1 != "" || $1 != "." ]]; then
if [[ -e $1 ]]; then
rm -r $1
fi
writeMessage 'Item '$1' was removed'
else
writeMessage 'Access denied'
fi
}
function writeMessage
{
if [[ $2 ]]; then
color=$(getColor "$2")
else
color=$(getColor "green")
fi
echo "$(tput setaf $color) $1 $(tput sgr0)"
}
function setChmod
{
chmod -R $1 $2
}
function getConfig
{
echo -ne "server {\\r
listen 80;\\r
server_name $1;\\r
\\r
root $htdocs/$1/www/;\\r
error_log $htdocs/$1/log/$1_errors.log;\\r
access_log $htdocs/$1/log/$1_access.log;\\r
\\r
location / {\\r
index index.php index.html index.htm;\\r
try_files \$uri \$uri/ /index.php?\$args;\\r
}\\r
\\r
error_page 404 /404.html;\\r
error_page 500 502 503 504 /50x.html;\\r
location = /50x.html {\\r
root /usr/share/nginx/www;\\r
}\\r
location ~ \.php$ {\\r
try_files \$uri =404;\\r
fastcgi_pass unix:/var/run/php5-fpm.sock;\\r
fastcgi_index index.php;\\r
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\\r
include fastcgi_params;\\r
}\\r
location ~ /\.ht {\\r
deny all;\\r
}\\r
}"
}
function isRunning
{
if ps ax | grep -v grep | grep $1 > /dev/null
then
echo "1"
else
echo "0"
fi
}
function cloneRepo
{
git clone $1 $2
}
#stop nginx server, if nginx is running
if [ $(isRunning "nginx") = "1" ]; then
writeMessage "Stop nginx server"
nginx -s stop
fi
if [[ $1 = '-a' && $2 != '' ]]; then
hostName=$2
else
#set name of Host name
echo -n 'Write name of host:'
read hostName
fi
if [ -z "$hostName" ]; then
writeMessage "Host name may not be empty!" "red"
else
if [[ $1 = "-r" ]]; then
writeMessage "Removing server files"
remove "$htdocs/$hostName"
writeMessage "Removing server host"
remove "$nginxConfDir/$hostName.conf"
writeMessage "Removing host from $hostsFile"
string="127.0.0.1 $hostName.lc"
sed "/$string/d" $hostsFile > 'hosts.temp'
mv "hosts.temp" $hostsFile
else
#create dictionary for host in the htdocs
createDirectory $hostName
if [[ $1 = '-c' && $2 != '' ]]; then
writeMessage "Cloning github repository"
cloneRepo $2 "$htdocs/$hostName"
fi
createDirectory "$htdocs/$hostName/log"
createDirectory "$htdocs/$hostName/www"
setChmod "777" "$htdocs/$hostName"
writeMessage "Write config to file"
echo $(getConfig $hostName) > "$nginxConfDir/$hostName.conf"
writeMessage "Adding host into $hostsFile"
echo "127.0.0.1 $hostName.lc" >> $hostsFile
fi
fi
writeMessage "Start nginx server"
nginx