Nginx安裝(從Linx包安裝)

目前,nginx軟體包可用於以下發行版和版本:

RHEL/CentOS:

版本 支持的平臺
5.x x86_64, i386
6.x x86_64, i386
7.x x86_64, ppc64le

Debian:

版本 代碼名稱(codename) 支持的平臺
7.x wheezy x86_64, i386
8.x jessie x86_64, i386

Ubuntu:

版本 代碼名稱(codename) 支持的平臺
12.04 precise x86_64, i386
14.04 trusty x86_64, i386, aarch64/arm64
16.04 xenial x86_64, i386, ppc64el
16.10 yakkety x86_64, i386

SLES:

版本 支持的平臺
12 x86_64

要實現Linux軟體包的自動更新,可以為RHEL/CentOS發行版設置yum存儲庫,Debian/Ubuntu發行版的apt存儲庫,以及SLES的zypper存儲庫。

在本文中,我們重點以Debian/Ubuntu發行版的apt存儲庫作為一個演示,並在Ubuntu系統上安裝Nginx。

1. 用於穩定版本的預構建軟體包

RHEL/CentOS系統平臺

要設置RHEL/CentOS的yum存儲庫,需要創建一個名稱為/etc/yum.repos.d/nginx.repo的檔,其中包含以下內容:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

根據所使用的分配,將“OS”替換為“rhel”或“centos”,對於5.x6.x7.x,請將“OSRELEASE”替換為“5”,“6”或“7” 版本。
有關於如何查看系統版本 -

[root@localhost nginx-1.10.3]# lsb_release -a
bash:lsb_release: command not found
[root@localhost nginx-1.10.3]# yum install lsb –y
[root@localhost nginx-1.10.3]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.3.1611 (Core)
Release:        7.3.1611
Codename:       Core
[root@localhost nginx-1.10.3]#

第二種方法:

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@localhost ~]#

Debian/Ubuntu系統平臺

對於Debian/Ubuntu,為了驗證nginx存儲庫簽名,並且在安裝nginx軟體包時消除關於缺少PGP密鑰的警告,必須將用於將nginx軟體包和存儲庫簽署的密鑰添加到apt程式密鑰環中。 請從Nginx官方網站下載密鑰:http://nginx.org/keys/nginx_signing.key , 並使用以下命令將其添加到apt程式密鑰環:

sudo apt-key add nginx_signing.key

Debian系統則將Debian代碼替換為Debian發行版代碼(codename),並將以下內容附加到/etc/apt/sources.list檔的末尾:

deb http://nginx.org/packages/debian/ codename nginx
deb-src http://nginx.org/packages/debian/ codename nginx

對於Ubuntu將Ubuntu發行版代號替換codename,並將以下內容附加到/etc/apt/sources.list檔的末尾:

deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx

對於Debian / Ubuntu,請運行以下命令:

apt-get update
apt-get install nginx

SLES系統平臺

對於SLES,運行以下命令:

zypper addrepo -G -t yum -c 'http://nginx.org/packages/sles/12' nginx

2. 源碼包

包源碼包可以在包源碼源碼庫中找到: http://hg.nginx.org/pkg-oss?_ga=1.85593177.1509956953.1490042234

默認分支保存當前主線版本的包源碼,而stable-*分支則用於穩定版本的最新的源碼包。 要構建二進位包,可在Debian/Ubuntu上的debian/目錄中或在RHEL/CentOS/SLES上的rpm/SPECS/中運行make命令。

包源碼分佈在與nginx相同的2BSD類許可證下。

3. Ubuntu Server安裝Nginx實例

對於Debian/Ubuntu,為了驗證nginx存儲庫簽名,並且在安裝nginx軟體包時消除關於缺少PGP密鑰的警告,必須將用於將nginx軟體包和存儲庫簽署的密鑰添加到apt程式密鑰環中。 請從Nginx官方網站下載密鑰:http://nginx.org/keys/nginx_signing.key , 並使用以下命令將其添加到apt程式密鑰環:

zaixian@ubuntu:~$ wget -c http://nginx.org/keys/nginx_signing.key
--2017-03-31 20:13:06--  http://nginx.org/keys/nginx_signing.key
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ...
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1561 (1.5K) [application/octet-stream]
Saving to: ‘nginx_signing.key’


nginx_signing.key   100%[===================>]   1.52K  --.-KB/s    in 0s

2017-03-31 20:13:06 (105 MB/s) - ‘nginx_signing.key’ saved [1561/1561]

zaixian@ubuntu:~$ ls
nginx_signing.key
zaixian@ubuntu:~$
zaixian@ubuntu:~$ sudo apt-key add nginx_signing.key

查看 Ubuntu發行版代號,使用以下命令 -

zaixian@ubuntu:~$ sudo lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial
zaixian@ubuntu:~$

Ubuntu將Ubuntu發行版代號替換codename,並將以下兩行內容附加到/etc/apt/sources.list檔的末尾:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

如下所示 -

現在,可以運行下麵命令來安裝nginx了:

zaixian@ubuntu:~$ sudo apt-get update
Get:1 http://nginx.org/packages/ubuntu xenial InRelease [3,589 B]
Hit:2 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://nginx.org/packages/ubuntu xenial/nginx Sources [4,139 B]
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:5 http://nginx.org/packages/ubuntu xenial/nginx amd64 Packages [7,012 B]
Get:6 http://nginx.org/packages/ubuntu xenial/nginx i386 Packages [6,999 B]
Get:7 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Fetched 328 kB in 5s (61.3 kB/s)
Reading package lists... Done
zaixian@ubuntu:~$
zaixian@ubuntu:~$ sudo apt-get install nginx
... ...
... ...
Selecting previously unselected package nginx.
(Reading database ... 54601 files and directories currently installed.)
Preparing to unpack .../nginx_1.10.3-1~xenial_amd64.deb ...
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
Unpacking nginx (1.10.3-1~xenial) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up nginx (1.10.3-1~xenial) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...

配置檔默認會被安裝到: /etc/nginx 目錄下 -

1. 啟動 Nginx 測試 -

zaixian@ubuntu:~$ sudo /etc/init.d/nginx
Usage: /etc/init.d/nginx {start|stop|status|restart|reload|force-reload|upgrade|configtest|check-reload}
zaixian@ubuntu:~$ sudo /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.
zaixian@ubuntu:~$

2. 查詢nginx進程

zaixian@ubuntu:~$ ps -ef | grep nginx
root       9933      1  0 20:36 ?        00:00:00 nginx: master process /usr/sbi                        n/nginx -c /etc/nginx/nginx.conf
nginx      9938   9933  0 20:36 ?        00:00:00 nginx: worker process
zaixian     9978   8461  0 20:40 pts/0    00:00:00 grep --color=auto nginx
zaixian@ubuntu:~$

上面輸出結果中,master就代表該進程是nginx的主進程,它的進程ID是:9933

3. 停止nginx

要停止Nginx,可以使用以下命令 -

kill -QUIT 主進程號

本例中就是 : kill -QUIT 9933
當然還有通用的停止進程的命令,那就是暴力停止了。
kill -9 進程號
kill一個進程,在查詢一下進程是否還存在,一個個的kill,全部kill完就可以了。

4. 測試nginx服務

打開流覽器或或在終端上使用 curl localhost訪問目標伺服器,如下 -

或在終端上使用:curl localhost -

zaixian@ubuntu:~$ curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
zaixian@ubuntu:~$

上一篇: Nginx安裝 下一篇: Nginx從源碼構建安裝配置