Nginx是一款Web服務器,兼之有負載均衡功能,功能強大,是首選的Web服務器。現通過Shell腳本實現在各GNU/Linux發行版中使用對應的包管理器安裝最新的穩定版Nginx服務。

GNU/Linux發行版包含RHELCentOSDebianUbuntu

Preparation

依據Nginx官方文檔

針對各GNU/Linux發行版採用不同的安裝方式,判斷具體GNU/Linux發行版的實現方式參見本人Blog Try To Extract Operation System Info On GNU/Linux Distribution

Thinking

Shell腳本的實現思路

  1. 判斷服務器中是否已安裝Nginx;
  2. 獲取當前服務器使用的GNU/Linux發行版及具體的版本號;
  3. 根據各GNU/Linux發行版採用不同的安裝方式;
  4. 啟動Nginx服務
  5. Nginx優化(可選)

Nginx相關文件默認安裝路徑

file path
the daemon /usr/sbin/nginx
config file /etc/nginx/nginx.conf/etc/nginx/conf.d/
web dir /usr/share/nginx/html/
log file /var/log/nginx/

Shell Script

Shell腳本內容,已上傳至GitHub

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
#!/bin/bash
#Wirter: lempstacker
#Blog: https://lempstacker.com/
#Date: 2016.10.19 18:16 Wed Asia/Shanghai
# UpdateTime: 2016.12.21 15:56 Wed Asia/Shanghai
#Purpose: Auto Installing Nginx Web Server Via Package Management
#Note: Used Both RedHat/CentOS/Fedora And Debian/Ubuntu Distribution

# font color
c_red='\e[31;1m'
c_end='\e[0m'


#check nginx server has been installed or not
if [[ -f '/usr/sbin/nginx' ]]; then
# https://forum.nginx.org/read.php?2,174954,174957
printf "$c_red"Attention"$c_end: Nginx Server $c_red%s$c_end existed on this server.\n" $(nginx -v 2>&1 | sed -r -n '[email protected]*nginx\/(.*)@\[email protected]')
exit 1
fi

# Check Internet Connection
# http://stackoverflow.com/questions/929368/how-to-test-a-internet-connection-in-bash#932187
! ping -q -w 1 -c 1 `ip route | sed -r -n '/default/[email protected]*via (.*) dev.*@\[email protected]'` &> /dev/null && printf "%s\n" "Error: No Internet Connection, Please Check It!" && exit 2


# Custom function
nginx_signing_key(){
# http://nginx.org/en/linux_packages.html
# http://nginx.org/keys/nginx_signing.key
cat > /tmp/nginx_signing.key <<-'EOF'
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)

mQENBE5OMmIBCAD+FPYKGriGGf7NqwKfWC83cBV01gabgVWQmZbMcFzeW+hMsgxH
W6iimD0RsfZ9oEbfJCPG0CRSZ7ppq5pKamYs2+EJ8Q2ysOFHHwpGrA2C8zyNAs4I
QxnZZIbETgcSwFtDun0XiqPwPZgyuXVm9PAbLZRbfBzm8wR/3SWygqZBBLdQk5TE
fDR+Eny/M1RVR4xClECONF9UBB2ejFdI1LD45APbP2hsN/piFByU1t7yK2gpFyRt
97WzGHn9MV5/TL7AmRPM4pcr3JacmtCnxXeCZ8nLqedoSuHFuhwyDnlAbu8I16O5
XRrfzhrHRJFM1JnIiGmzZi6zBvH0ItfyX6ttABEBAAG0KW5naW54IHNpZ25pbmcg
a2V5IDxzaWduaW5nLWtleUBuZ2lueC5jb20+iQE+BBMBAgAoAhsDBgsJCAcDAgYV
CAIJCgsEFgIDAQIeAQIXgAUCV2K1+AUJGB4fQQAKCRCr9b2Ce9m/YloaB/9XGrol
kocm7l/tsVjaBQCteXKuwsm4XhCuAQ6YAwA1L1UheGOG/aa2xJvrXE8X32tgcTjr
KoYoXWcdxaFjlXGTt6jV85qRguUzvMOxxSEM2Dn115etN9piPl0Zz+4rkx8+2vJG
F+eMlruPXg/zd88NvyLq5gGHEsFRBMVufYmHtNfcp4okC1klWiRIRSdp4QY1wdrN
1O+/oCTl8Bzy6hcHjLIq3aoumcLxMjtBoclc/5OTioLDwSDfVx7rWyfRhcBzVbwD
oe/PD08AoAA6fxXvWjSxy+dGhEaXoTHjkCbz/l6NxrK3JFyauDgU4K4MytsZ1HDi
MgMW8hZXxszoICTTiQEcBBABAgAGBQJOTkelAAoJEKZP1bF62zmo79oH/1XDb29S
YtWp+MTJTPFEwlWRiyRuDXy3wBd/BpwBRIWfWzMs1gnCjNjk0EVBVGa2grvy9Jtx
JKMd6l/PWXVucSt+U/+GO8rBkw14SdhqxaS2l14v6gyMeUrSbY3XfToGfwHC4sa/
Thn8X4jFaQ2XN5dAIzJGU1s5JA0tjEzUwCnmrKmyMlXZaoQVrmORGjCuH0I0aAFk
RS0UtnB9HPpxhGVbs24xXZQnZDNbUQeulFxS4uP3OLDBAeCHl+v4t/uotIad8v6J
SO93vc1evIje6lguE81HHmJn9noxPItvOvSMb2yPsE8mH4cJHRTFNSEhPW6ghmlf
Wa9ZwiVX5igxcvaIRgQQEQIABgUCTk5b0gAKCRDs8OkLLBcgg1G+AKCnacLb/+W6
cflirUIExgZdUJqoogCeNPVwXiHEIVqithAM1pdY/gcaQZmIRgQQEQIABgUCTk5f
YQAKCRCpN2E5pSTFPnNWAJ9gUozyiS+9jf2rJvqmJSeWuCgVRwCcCUFhXRCpQO2Y
Va3l3WuB+rgKjsQ=
=EWWI
-----END PGP PUBLIC KEY BLOCK-----
EOF
}


#Check Bash Version
bashVersion=$(bash --version | sed -r -n '[email protected]*version (.*)\(1\)-release.*@\[email protected]')
[[ ${bashVersion%%.*} -lt 4 ]] && printf "Current bash version is $c_red%s$c_end. Strongly suggest upgrading to versin $c_red"4"$c_end.\n" "$bashVersion"


#Get Distro Info
if [[ -f '/etc/redhat-release' ]]; then
releaseFile='/etc/redhat-release'

distroFullName=$(cat "$releaseFile")
distroName=$(rpm -q --qf "%{name}" -f "$releaseFile")
distroName=${distroName%%-*}
# centos-release fedora-release redhat-release
distroVersion=$(rpm -q --qf "%{version}" -f "$releaseFile") # 7 23
distroVersion=${distroVersion//Server/} # used for redhat

elif [[ -f '/etc/os-release' ]]; then
releaseFile='/etc/os-release'

distroFullName=$(sed -r -n '[email protected]"@@g;[email protected]^PRETTY_NAME=(.*)@\[email protected]' "$releaseFile")
distroName=$(sed -r -n '[email protected]"@@g;[email protected]^ID=(.*)@\[email protected]' "$releaseFile") #debian ubuntu
distroVersion=$(sed -r -n '[email protected]"@@g;[email protected]^VERSION_ID=(.*)@\[email protected]' "$releaseFile") #8 16.10
fi

len=16
printf "%"$len"s $c_red%s$c_end\n" 'distroFullName:' "$distroFullName"
printf "%"$len"s $c_red%s$c_end\n" 'distroName:' "$distroName"
printf "%"$len"s $c_red%s$c_end\n" 'distroVersion:' "$distroVersion"


distroName=${distroName,,} #transfer to lowercase

case "$distroName" in
redhat|centos )
[[ "$distroName" = 'redhat' ]] && os='rhel' || os="$distroName"
sourceFile='/etc/yum.repos.d/nginx.repo'

echo -n -e "[nginx]\nname=nginx repo\nbaseurl=http://nginx.org/packages/$os/$distroVersion/\$basearch/\ngpgcheck=0\nenabled=1\n" > "$sourceFile"

yum -q clean all
yum install -q -y nginx &> /dev/null
;;
debian|ubuntu)
sourceFile='/etc/apt/sources.list.d/nginx.list'

declare -A codeNameArr
# http://nginx.org/en/linux_packages.html
if [[ "$distroName" = 'debian' ]]; then
codeNameArr=([7]='wheezy' [8]='jessie')
else
# codeNameArr=([12.04]='precise' [14.04]='trusty' [16.04]='xenial' [16.10]='yakkety')
codeNameArr=([12.04]='precise' [14.04]='trusty' [16.04]='xenial' [16.10]='xenial')
fi

codename=${codeNameArr[$distroVersion]}

[[ ! -f "$sourceFile" ]] && touch "$sourceFile"
echo -n -e "deb http://nginx.org/packages/$distroName/ $codename nginx\ndeb-src http://nginx.org/packages/$distroName/ $codename nginx\n" > "$sourceFile"

nginx_signing_key #custom function

nginxSigningFile='/tmp/nginx_signing.key'
[[ -f "$nginxSigningFile" ]] && apt-key add "$nginxSigningFile" && rm -f /tmp/nginx_signing.key
# apt-key add /tmp/nginx_signing.key

apt-get -qq update
apt-get -y -qq install nginx &> /dev/null

unset codeNameArr
unset codename
unset nginxSigningFile
;;
esac

[[ -f '/usr/sbin/nginx' ]] && service nginx start &> /dev/null

[[ -f '/usr/share/nginx/html/index.html' ]] && echo "$distroFullName" >> /usr/share/nginx/html/index.html && printf "Successfully Installing Nginx Server $c_red%s$c_end.\n" $(nginx -v 2>&1 | sed -r -n '[email protected]*nginx\/(.*)@\[email protected]')

#unset variables
unset sourceFile
unset releaseFile
unset distroFullName
unset distroName
unset distroVersion
unset bashVersion


#Optimization
# https://lempstacker.com/tw/LEMP-Installation-and-Nginx-Optimization/

#Script End

Implementations

CentOS

CentOS 7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[[email protected] ~]# bash /tmp/nginx.sh
distroFullName: CentOS Linux release 7.2.1511 (Core)
distroName: centos
distroVersion: 7
Successfully Installing Nginx Server 1.10.2.
[[email protected] ~]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:00:20 GMT
Content-Type: text/html
Content-Length: 650
Last-Modified: Wed, 19 Oct 2016 12:59:58 GMT
Connection: keep-alive
ETag: "58076e4e-28a"
Accept-Ranges: bytes

[[email protected] ~]# curl -s http://127.0.0.1 | tail -1
CentOS Linux release 7.2.1511 (Core)
[[email protected] ~]#

CentOS 6.8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[[email protected] ~]# bash /tmp/nginx.sh
distroFullName: CentOS release 6.8 (Final)
distroName: centos
distroVersion: 6
Successfully Installing Nginx Server 1.10.2.
[[email protected] ~]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:04:42 GMT
Content-Type: text/html
Content-Length: 639
Last-Modified: Wed, 19 Oct 2016 13:04:21 GMT
Connection: keep-alive
ETag: "58076f55-27f"
Accept-Ranges: bytes

[[email protected] ~]# curl -s http://127.0.0.1 | tail -1
CentOS release 6.8 (Final)
[[email protected] ~]#

CentOS 5.11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[[email protected] ~]# cat /etc/redhat-release
CentOS release 5.11 (Final)
[[email protected] ~]# bash /tmp/nginx.sh
Current bash version is 3.2.25. Strongly suggest upgrading to versin 4.
distroFullName: CentOS release 5.11 (Final)
distroName: centos
distroVersion: 5
/tmp/nginx.sh: line 89: ${distroName,,}: bad substitution
Successfully Installing Nginx Server 1.10.2.
[[email protected] ~]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:08:52 GMT
Content-Type: text/html
Content-Length: 640
Last-Modified: Wed, 19 Oct 2016 13:08:38 GMT
Connection: keep-alive
ETag: "58077056-280"
Accept-Ranges: bytes

[[email protected] ~]# curl -s http://127.0.0.1 | tail -1
CentOS release 5.11 (Final)
[[email protected] ~]#

Debian

Debian 8.6

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
[email protected]:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
[email protected]:~# bash /tmp/nginx.sh
distroFullName: Debian GNU/Linux 8 (jessie)
distroName: debian
distroVersion: 8
OK
Successfully Installing Nginx Server 1.10.2.
[email protected]:~# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:12:38 GMT
Content-Type: text/html
Content-Length: 640
Last-Modified: Wed, 19 Oct 2016 13:12:32 GMT
Connection: keep-alive
ETag: "58077140-280"
Accept-Ranges: bytes

[email protected]:~# curl -s http://127.0.0.1 | tail -1
Debian GNU/Linux 8 (jessie)
[email protected]:~#

Debian 7.11

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
[email protected]:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support/"
BUG_REPORT_URL="http://bugs.debian.org/"
[email protected]:~# bash /tmp/nginx.sh
distroFullName: Debian GNU/Linux 7 (wheezy)
distroName: debian
distroVersion: 7
OK
Successfully Installing Nginx Server 1.10.2.
[email protected]:~# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:15:11 GMT
Content-Type: text/html
Content-Length: 640
Last-Modified: Wed, 19 Oct 2016 13:15:08 GMT
Connection: keep-alive
ETag: "580771dc-280"
Accept-Ranges: bytes

[email protected]:~# curl -s http://127.0.0.1 | tail -1
Debian GNU/Linux 7 (wheezy)
[email protected]:~#

Ubuntu

Ubuntu 16.10

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
[email protected]:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.10 (Yakkety Yak)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.10"
VERSION_ID="16.10"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="http://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=yakkety
UBUNTU_CODENAME=yakkety
[email protected]:~# bash /tmp/nginx.sh
distroFullName: Ubuntu 16.10
distroName: ubuntu
distroVersion: 16.10
OK
Successfully Installing Nginx Server 1.10.2.
[email protected]:~# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:23:08 GMT
Content-Type: text/html
Content-Length: 625
Last-Modified: Wed, 19 Oct 2016 13:22:59 GMT
Connection: keep-alive
ETag: "580773b3-271"
Accept-Ranges: bytes

[email protected]:~# curl -s http://127.0.0.1 | tail -1
Ubuntu 16.10
[email protected]:~# bash /tmp/nginx.sh
Attention: Nginx Server 1.10.2 existed on this server.
[email protected]:~#

Ubuntu 16.04

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
[email protected]:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
UBUNTU_CODENAME=xenial
[email protected]:~# bash /tmp/nginx.sh
distroFullName: Ubuntu 16.04.1 LTS
distroName: ubuntu
distroVersion: 16.04
OK
Successfully Installing Nginx Server 1.10.2.
[email protected]:~# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:23:16 GMT
Content-Type: text/html
Content-Length: 631
Last-Modified: Wed, 19 Oct 2016 13:23:03 GMT
Connection: keep-alive
ETag: "580773b7-277"
Accept-Ranges: bytes

[email protected]:~# curl -s http://127.0.0.1 | tail -1
Ubuntu 16.04.1 LTS
[email protected]:~# bash /tmp/nginx.sh
Attention: Nginx Server 1.10.2 existed on this server.
[email protected]:~#

Ubuntu 14.04

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
[email protected]:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
[email protected]:~# bash /tmp/nginx.sh
distroFullName: Ubuntu 14.04.5 LTS
distroName: ubuntu
distroVersion: 14.04
OK
Successfully Installing Nginx Server 1.10.2.
[email protected]:~# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:23:19 GMT
Content-Type: text/html
Content-Length: 631
Last-Modified: Wed, 19 Oct 2016 13:23:12 GMT
Connection: keep-alive
ETag: "580773c0-277"
Accept-Ranges: bytes

[email protected]:~# curl -s http://127.0.0.1 | tail -1
Ubuntu 14.04.5 LTS
[email protected]:~# bash /tmp/nginx.sh
Attention: Nginx Server 1.10.2 existed on this server.
[email protected]:~#

Ubuntu 12.04

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
[email protected]:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
[email protected]:~# bash /tmp/nginx.sh
distroFullName: Ubuntu 14.04.5 LTS
distroName: ubuntu
distroVersion: 14.04
OK
Successfully Installing Nginx Server 1.10.2.
[email protected]:~# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 19 Oct 2016 13:23:19 GMT
Content-Type: text/html
Content-Length: 631
Last-Modified: Wed, 19 Oct 2016 13:23:12 GMT
Connection: keep-alive
ETag: "580773c0-277"
Accept-Ranges: bytes

[email protected]:~# curl -s http://127.0.0.1 | tail -1
Ubuntu 14.04.5 LTS
[email protected]:~# bash /tmp/nginx.sh
Attention: Nginx Server 1.10.2 existed on this server.
[email protected]:~#

References

相關參考信息詳見Shell腳本


Change Logs

  • 2016.10.19 21:36 Wed Asia/Shanghai
    • 初稿完成
  • 2016.12.21 15:59 Wed Asia/Shanghai
    • 腳本優化,添加網路狀態檢測

  • Note Time: 2016.10.19 21:36 Wed
  • Note Location: Asia/Shanghai
  • Writer: lempstacker