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

GNU/Linux發行版包含RHELCentOSDebianUbuntu

Preparation

依據Nginx官方文檔 * nginx: Linux packages * Install | 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

#!/bin/bash
#Wirter: lempstacker
#Blog: https://lempstacker.github.io/
#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 's@.*nginx\/(.*)@\1@p')
    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/s@.*via (.*) dev.*@\1@p'` &> /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.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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 's@.*version (.*)\(1\)-release.*@\1@p')
[[ ${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 's@"@@g;s@^PRETTY_NAME=(.*)@\1@p' "$releaseFile")
    distroName=$(sed -r -n 's@"@@g;s@^ID=(.*)@\1@p' "$releaseFile")     #debian ubuntu
    distroVersion=$(sed -r -n 's@"@@g;s@^VERSION_ID=(.*)@\1@p' "$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 's@.*nginx\/(.*)@\1@p')

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


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

#Script End

Implementations

CentOS

CentOS 7

[root@CentOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@CentOS7 ~]# bash /tmp/nginx.sh
 distroFullName: CentOS Linux release 7.2.1511 (Core)
     distroName: centos
  distroVersion: 7
Successfully Installing Nginx Server 1.10.2.
[root@CentOS7 ~]# 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

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

CentOS 6.8

[root@CentOS68 ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@CentOS68 ~]# bash /tmp/nginx.sh
 distroFullName: CentOS release 6.8 (Final)
     distroName: centos
  distroVersion: 6
Successfully Installing Nginx Server 1.10.2.
[root@CentOS68 ~]# 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

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

CentOS 5.11

[root@CentOS511 ~]# cat /etc/redhat-release
CentOS release 5.11 (Final)
[root@CentOS511 ~]# 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.
[root@CentOS511 ~]# 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

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

Debian

Debian 8.6

root@Debian86:~# 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/"
root@Debian86:~# bash /tmp/nginx.sh
 distroFullName: Debian GNU/Linux 8 (jessie)
     distroName: debian
  distroVersion: 8
OK
Successfully Installing Nginx Server 1.10.2.
root@Debian86:~# 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

root@Debian86:~# curl -s http://127.0.0.1 | tail -1
Debian GNU/Linux 8 (jessie)
root@Debian86:~#

Debian 7.11

root@Debian711:~# 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/"
root@Debian711:~# bash /tmp/nginx.sh
 distroFullName: Debian GNU/Linux 7 (wheezy)
     distroName: debian
  distroVersion: 7
OK
Successfully Installing Nginx Server 1.10.2.
root@Debian711:~# 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

root@Debian711:~# curl -s http://127.0.0.1 | tail -1
Debian GNU/Linux 7 (wheezy)
root@Debian711:~#

Ubuntu

Ubuntu 16.10

root@Ubuntu1610:~# 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
root@Ubuntu1610:~# bash /tmp/nginx.sh
 distroFullName: Ubuntu 16.10
     distroName: ubuntu
  distroVersion: 16.10
OK
Successfully Installing Nginx Server 1.10.2.
root@Ubuntu1610:~# 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

root@Ubuntu1610:~# curl -s http://127.0.0.1 | tail -1
Ubuntu 16.10
root@Ubuntu1610:~# bash /tmp/nginx.sh
Attention: Nginx Server 1.10.2 existed on this server.
root@Ubuntu1610:~#

Ubuntu 16.04

root@Ubuntu1604:~# 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
root@Ubuntu1604:~# bash /tmp/nginx.sh
 distroFullName: Ubuntu 16.04.1 LTS
     distroName: ubuntu
  distroVersion: 16.04
OK
Successfully Installing Nginx Server 1.10.2.
root@Ubuntu1604:~# 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

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

Ubuntu 14.04

root@Ubuntu1404:~# 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/"
root@Ubuntu1404:~# bash /tmp/nginx.sh
 distroFullName: Ubuntu 14.04.5 LTS
     distroName: ubuntu
  distroVersion: 14.04
OK
Successfully Installing Nginx Server 1.10.2.
root@Ubuntu1404:~# 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

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

Ubuntu 12.04

root@Ubuntu1404:~# 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/"
root@Ubuntu1404:~# bash /tmp/nginx.sh
 distroFullName: Ubuntu 14.04.5 LTS
     distroName: ubuntu
  distroVersion: 14.04
OK
Successfully Installing Nginx Server 1.10.2.
root@Ubuntu1404:~# 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

root@Ubuntu1404:~# curl -s http://127.0.0.1 | tail -1
Ubuntu 14.04.5 LTS
root@Ubuntu1204:~# bash /tmp/nginx.sh
Attention: Nginx Server 1.10.2 existed on this server.
root@Ubuntu1204:~#

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