Nginx安装(从Linx包安装)

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程序密钥环:

zyiz@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]

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

查看 Ubuntu发行版代号,使用以下命令 -

zyiz@ubuntu:~$ sudo lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial
zyiz@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了:

zyiz@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
zyiz@ubuntu:~$
zyiz@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 测试 -

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

2. 查询nginx进程

zyiz@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
zyiz     9978   8461  0 20:40 pts/0    00:00:00 grep --color=auto nginx
zyiz@ubuntu:~$

上面输出结果中,master就代表该进程是nginx的主进程,它的进程ID是:9933

3. 停止nginx

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

kill -QUIT 主进程号

本例中就是 : kill -QUIT 9933
当然还有通用的停止进程的命令,那就是暴力停止了。
kill -9 进程号
kill一个进程,在查询一下进程是否还存在,一个个的kill,全部kill完就可以了。

4. 测试nginx服务

打开浏览器或或在终端上使用 curl localhost访问目标服务器,如下 -

或在终端上使用:curl localhost -

zyiz@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>
zyiz@ubuntu:~$