Nginx教程:从Linux包(RHEL/CentOS、Debian、Ubuntu)安装

2023年 7月 12日 13.9k 0

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

Shell

根据所使用的分配,将“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

Shell

2. 源码包

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

默认分支保存当前主线版本的包源码,而stable-*分支则用于稳定版本的最新的源码包。 要构建二进制包,可在Debian/Ubuntu上的debian/目录中或在RHEL/CentOS/SLES上的rpm/SPECS/中运行make命令。

包源码分布在与nginx相同的2条BSD类许可证下。

3. Ubuntu Server安装Nginx实例

对于Debian/Ubuntu,为了验证nginx存储库签名,并且在安装nginx软件包时消除关于缺少PGP密钥的警告,必须将用于将nginx软件包和存储库签署的密钥添加到apt程序密钥环中。 请从Nginx官方网站下载密钥:http://nginx.org/keys/nginx_signing.key , 并使用以下命令将其添加到apt程序密钥环:

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

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

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

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

Shell

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教程:从Linux包(RHEL/CentOS、Debian、Ubuntu)安装

现在,可以运行下面命令来安装nginx了:

yiibai@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
yiibai@ubuntu:~$
yiibai@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 目录下 - Nginx教程:从Linux包(RHEL/CentOS、Debian、Ubuntu)安装

1. 启动 Nginx 测试 -

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

Shell

2. 查询nginx进程

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

Shell

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

3. 停止nginx

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

kill -QUIT 主进程号

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

4. 测试nginx服务

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

Nginx教程:从Linux包(RHEL/CentOS、Debian、Ubuntu)安装

或在终端上使用:curl localhost -

yiibai@ubuntu:~$ curl localhost



Welcome to nginx!

    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.

Thank you for using nginx.

yiibai@ubuntu:~$

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论