-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnginxse.sh
167 lines (131 loc) · 4.8 KB
/
nginxse.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
#!/bin/bash
# OS destribution
DESPCN=$(lsb_release -si)
# try locating NGINX install dir
NGXInstallDir="/etc/nginx"
# try typical config files
NGXCfgDir="/conf.d"
NGXCfgDir1="/sites-available"
NGXCfgDir2="/sites-enabled"
installCheck
function checkNginxInstalled
{
echo -e "Checking install and dependencies:\n"
if hash nginx 2>/dev/null; then
echo -e 'Check NGINX install:\t\t\t[PASS]'
# make sure config directory exists
# another file
if [ ! -d $NGXInstallDir ];
then
echo -e 'NGINX install directory:\t\t[NOT FOUND]'
installCheck=0
else
echo -e 'NGINX install directory:\t\t[FOUND]'
# config directory 1
if [ -d $NGXInstallDir$NGXCfgDir ] || [ -d $NGXInstallDir$NGXCfgDir1 ] ||
[ -d $NGXInstallDir$NGXCfgDir2 ];then
echo -e 'NGINX config directory:\t\t\t[PASS]'
# if site enabled or available are present, use enabled
if [ -d $NGXInstallDir$NGXCfgDir1 ];then
NGXCfgDir=$NGXCfgDir1
fi
if [ -d $NGXInstallDir$NGXCfgDir2 ];then
NGXCfgDir=$NGXCfgDir2
fi
echo -e "NGINX config directory selected: \t$NGXCfgDir"
installCheck=1
else
echo -e 'NGINX config directory:\t\t\t[NOT FOUND]'
installCheck=0
fi
fi
else
echo -e 'Check NGINX install:\t\t\t[NOT INSTALLED]\n'
installCheck=0
fi
}
# display notice and call for check.
function landingNotice()
{
cols=$( tput cols )
rows=$( tput lines )
fallbackNotice="Welcome to NGINXSE on $DESPCN\n"
notice_length=${#fallbackNotice}
half_input_length=$(( $notice_length / 2 ))
middle_row=$(( $rows / 2 ))
tput clear
tput cup $middle_row $middle_col
tput bold
echo -e $fallbackNotice
echo -e "This script will help you generate NGINX Server Block config Files.\n"
echo -e "This is a free, open-source software originally released by hishri.com\n"
echo -e "IMPORTANT: -The script WILL NOT delete/modify any existing config files"
echo -e "\t -You will be promped before any steps will be made."
echo -e "\t -Sudo privileges are needed to save config files."
echo -e "\t -The script checks that NGINX is up and running."
tput sgr0
tput cup $( tput lines ) 0
}
landingNotice
checkNginxInstalled
# test dependency result
testResult=$(checkNginxInstalled)
if [ $installCheck == 1 ]; then
echo -e "Dependencies checked!\n\n"
else
echo -e "\nPlease try resolving the issue(s) above and then rerun the script to continue.\n"
fi
# ask for server name
read -p "Server name(s) (e.g., example.com or sub.example.com): " server_name
echo -e "\n"
read -p "Html files directory absolute path (default: /usr/share/nginx/html): " server_root
echo -e "\n"
echo -e "Are you using PHP-FPM:\nPHP-FPM can be installed and set a default\nPHP-FPM is usually set in /etc/php-fpm.d/www.conf."
echo -e "\n"
read -p "Use PHP-FPM? (Y/N): " phpfpmsupport
phpfpmblock="location ~ \.php$ {\n
include /etc/nginx/fastcgi_params;\n
fastcgi_pass unix:/var/run/php5-fpm.sock;\n
fastcgi_index index.php;\n
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n
}\n"
phpcgiblock="location ~ \.php$ {\n
include snippets/fastcgi-php.conf;\n
# With php5-cgi alone:\n
fastcgi_pass 127.0.0.1:9000;\n
}\n"
case "$phpfpmsupport" in
y|Y ) phpblock=$phpfpmblock;;
n|N ) phpblock=$phpcgiblock;;
* ) echo -p "No option selected, Ignoring PHP\n";;
esac
serverblock="
server {\n
listen 80 ;\n
listen [::]:80 ;\n
server_name $server_name;\n
root $server_root;\n
# Add index.php to the list if you are using PHP\n
index index.php index.html index.htm index.nginx-debian.html;\n
location / {\n
# First attempt to serve request as file, then\n
# as directory, then fall back to displaying a 404.\n
#try_files \$uri \$uri/ /index.php\$is_args\$args;\n
try_files \$uri \$uri/ =404;\n
}\n
$phpblock
}\n"
echo -e "\n\nServer block succesfully generated!\n
You can save the generated configuration into a new (.conf) file\n
Server block configuration file to create: '$NGXInstallDir$NGXCfgDir/$server_name.conf'\n"
read -p "Create and Save '$server_name.conf'? (Y/N) if you choose 'N' the config will be displayed. " savefile
case "$savefile" in
y|Y ) echo -e "$serverblock" > "$HOME/$server_name.conf"
sudo mv "$HOME/$server_name.conf" "$NGXInstallDir$NGXCfgDir/$server_name.conf"
if [ ! -f "$NGXInstallDir$NGXCfgDir/$server_name.conf" ]; then
echo -e "\n[success] $NGXInstallDir$NGXCfgDir/$server_name.conf has been successfully created!\nPlease run\tsystemctl reload nginx"
fi
;;
n|N ) echo -p $serverblock;;
* ) echo -p $serverblock;;
esac