NGINX安装

简介:

1.获取新版本的nginx服务器.
  登录nginx官网下载页面: http://nginx.org/en/download.html
  下载nginx的stable(稳定)版本:nginx-1.6.2.tar.gz


2. 安装nginx之前在linux系统上确保安装以下软件和第三方库.
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel

注意如下:
(1)gcc是安装GUN C语言编译器,用来编译nginx源代码的。
(2)automake是安装Automake工具,Automake工具是用来完成自动创建Makefile文件的。
(3)由于nginx的一些模块需要依赖其他第三方库,通常有:pcre库(支持rewrite模块),zlib库(支持gzip模块),openssl库(支持ssl模块 ),所以需要安装pcre, zlib, openssl库。
(4)gcc-c++ 是C++编译器。

安装之前添加nginx用户来管理nginx服务器
# groupadd nginx
# useradd nginx -g nginx
#增加一个名为 nginx的用户。
-g:指定新用户所属的用户组(group)


3.编译和安装nginx服务器
#复制nginx到/usr/local/src/目录下
[root@ip40 tmp]# cp nginx-1.6.2.tar.gz /usr/local/src/
[root@ip40 tmp]# cd /usr/local/src
[root@ip40 src]# ls
nginx-1.6.2.tar.gz
#解压缩
[root@ip40 src]# tar zxf nginx-1.6.2.tar.gz
[root@ip40 src]# ls nginx-1.6.2  nginx-1.6.2.tar.gz
#进入nginx源码目录下
[root@ip40 src]# cd nginx-1.6.2
#查看解压缩出来的文件和目录
[root@ip40 nginx-1.6.2]# ls -l
总用量 620
drwxr-xr-x. 6 1001 1001   4096 10月 27 15:06 auto
-rw-r--r--. 1 1001 1001 236013 9月  16 20:23 CHANGES
-rw-r--r--. 1 1001 1001 359556 9月  16 20:23 CHANGES.ru
drwxr-xr-x. 2 1001 1001   4096 10月 27 15:06 conf
-rwxr-xr-x. 1 1001 1001   2369 9月  16 20:23 configure
drwxr-xr-x. 4 1001 1001   4096 10月 27 15:06 contrib
drwxr-xr-x. 2 1001 1001   4096 10月 27 15:06 html
-rw-r--r--. 1 1001 1001   1397 9月  16 20:23 LICENSE
drwxr-xr-x. 2 1001 1001   4096 10月 27 15:06 man
-rw-r--r--. 1 1001 1001     49 9月  16 20:23 README
drwxr-xr-x. 8 1001 1001   4096 10月 27 15:06 src
对解压缩出来的部分文件和目录做个简单的介绍:
(1) src目录中存放了nginx软件的所有源代码.
(2) man目录中存放了nginx软件的帮助文档,nginx安装后,在命令行输入man命令可查看:
[root@ip40 nginx-1.6.2]# man nginx
(3) html目录中存放了后缀是.html的静态网页文件,这是网页文件的存放目录
(4) conf目录存放的是nginx服务器的配置文件,最重要的nginx.conf配置文件。
(5)auto目录存放了大量的脚本文件,和configure脚本程序有关.
(6)configure 文件是nginx软件的自动脚本程序,运行configure脚本会
根据系统叁数及环境产生合适的Makefile文件或是C的头文件(header file),让源程序可以很方便地在这些不同的平台上被编译连接。

4.使用configure脚本自动生成Makefile文件
[root@ip40 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx
configure脚本常用选项:
--prefix=      指定nginx软件的安装路径.
 生成的nginx软件的Makefile文件就在当前目录下,自己可以ls查看一下。
5.编译和安装nginx
得到了nginx软件的Makefile文件就可以编译源码了:
[root@ip40 nginx-1.6.2]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@ip40 nginx-1.6.2]# make
[root@ip40 nginx-1.6.2]# make install
#安装完成后,到刚才--prefix指定的安装目录下看看
[root@ip40 nginx-1.6.2]# cd /usr/local/nginx
[root@ip40 nginx]# ls
conf  html  logs  sbin
niginx服务器的安装目录中包含了conf,html,logs,sbin四个目录:
(1)conf目录
[root@ip40 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
conf目录存放了nginx的所有配置文件,其中nginx.conf是Nginx服务器的主配置文件,其他配置文件是用来配置nginx相关功能的。在这个目录下所有的配置文件都有对应的以.default结尾的默认配置文件,方便我们将配置过的.conf文件回复到初始状态。
(2)html目录
[root@ip40 html]# ls
50x.html  index.html
html目录中存放了Nginx运行过程中调用的html静态网页文件,我们自定义的网页文件都要放到这里才能解析。
 (3)  logs目录
logs目录存放Nginx服务器日志文件的,目前Nginx服务器没有启动,所以目录是空的。
 (4) sbin目录
[root@ip40 sbin]# ls
nginx
sbin目录中有一个nginx文件,这是nginx服务器的主程序。


打开nginx的配置文件修改user指令(nginx.conf配置文件在conf目录下):
vim conf/nginx.conf
把user   nobody这行中的nobody改成nginx, 然后保存退出。

修改nginx安装目录(/usr/local/nginx是安装时指定的目录)的权限,让nginx能访问
 chown  -R nginx:nginx nginx
 chmod        u+w  nginx


6.Nginx服务器的启动/停止/重启

6.1 nginx服务的启动:(nginx服务器启动必须用超级用户root权限)
[root@ip40 nginx]# cd /usr/local/nginx/sbin
[root@ip40 sbin]# ./nginx -h    (-h参数是列出帮助信息)
nginx version: nginx/1.6.2
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help                      #显示该帮助信息
  -v            : show version and exit   #打印版本和退出
  -V            : show version and configure options then exit   #打印版本和配置选项然后退出
  -t            : test configuration and exit   #测试nginx配置文件是否有语法错误和退出
  -q            : suppress non-error messages during configuration testing # 在测试nginx配置文件是否有语法错误过程中不会显示非错误的消息
  -s signal     : send signal to a master process: stop, quit, reopen, reload  #发送信号到主进程
  -p prefix     : set prefix path (default: /usr/local/nginx/)  #指定nginx服务器所在的路径前缀
  -c filename   : set configuration file (default: conf/nginx.conf) #指定nginx配置文件路径
  -g directives : set global directives out of configuration file

以下是每个参数的分析:

-t检查nginx服务器配置文件是否有语法错误,可以与-c一起使用,如果检查通过列出以下信息:
[root@ip40 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
-s用于向nginx服务器的主进程发送信号来控制服务器的启动,停止,重启。
-p用来改变nginx服务器的安装路径,用在平滑升级nginx服务器场合。
-c用来指定启动nginx服务器的配置文件,用在平滑重启Nginx服务器场合。
-g用来补充nginx配置文件的,向Nginx服务器指定启动时应用于全局的配置。

分析了以上参数后,启动nginx服务器(用超级用户root启动):
[root@ip40 sbin]# ./nginx
查看nginx服务的进程:
[root@ip10 ~]# ps -ef | grep nginx
root      2584      0 20:28 ?        00:00:00 nginx: master process ./sbin/nginx
nginx    2585  2584  0 20:28 ?        00:00:00 nginx: worker process
root      3208  3197  0 20:40 pts/0    00:00:00 grep --color=auto nginx
大家可以看到 2585工作进程是2584主进程的子进程,2585子进程的所属用户我们刚才配置的nginx,由主进程管理。

[root@ip10 ~]# netstat -tnlp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2584/nginx

在浏览器地址栏中输入localhost 后会看到如下的网页内容

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.



上面的网页内容正式nginx安装目录下的html目录下的index.html网页文件内容,说明安装成功

6.2 nginx服务器的停止:

nginx服务器的停止有两种方法:一个是快速停止,一种是平滑停止。

快速停止是指立即停止当前nginx服务正在处理的所有网络请求,马上丢弃连接,停止工作。
平滑停止是指允许Nginx服务将当前正在处理的网络请求处理完成,但不再接收新的请求,之后关闭连接,停止工作。

1)查询主进程号     
ps -ef | grep nginx
注意:主进程号也能在nginx.conf配置文件中配置的pid文件中查到。
2)停止nginx
A.平滑停止
kill -QUIT  主进程号

[root@ip10 nginx]# ps -ef | grep nginx
root      5511     1  0 18:55 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    5512  5511  0 18:55 ?        00:00:00 nginx: worker process
root      5514  5356  0 18:55 pts/0    00:00:00 grep --color=auto nginx
[root@ip10 nginx]# kill -QUIT 5511


B.快速停止
kill -TERM 主进程号

[root@ip10 nginx]# ps -ef | grep nginx
root      5410     1  0 18:51 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    5411  5410  0 18:51 ?        00:00:00 nginx: worker process
root      5458  5356  0 18:52 pts/0    00:00:00 grep --color=auto nginx
[root@ip10 nginx]# kill -TERM 5410
C.强制停止(上述两种方法失效后,强制停止)
kill  -9  主进程号


6.3 nginx重启命令

nginx重启可以分成两种类型

1.先关闭nginx服务,然后修改配置文件或者用新的配置文件替换旧的配置文件,最后启动nginx服务。

[root@ip10 nginx]# ps -ef | grep nginx
root      2584     1  0 20:28 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    2585  2584  0 20:28 ?        00:00:00 nginx: worker process
root      3299  3197  0 21:03 pts/0    00:00:00 grep --color=auto nginx

先关闭nginx
[root@ip10 nginx]# kill -QUIT 2584

然后重新启动nginx

[root@ip10 nginx]# ./sbin/nginx  [-c newConfFIle]

newConfFile:是可选项,新的配置文件的位置(一般只是对的nginx.conf配置文件内容进行修改,修改完毕后重启nginx就可以,或者用新的配置文件替换旧的配置文件,不会变动配置文件的默认位置的,也就是修改内容,不变动位置,所以无需指定配置文件位置,用默认位置

 2.平滑重启.

平滑启动是这样一个过程,当 nginx主进程收到 HUP 信号后,它会首先读取新的配置文件进行测试(如果指定配置文件,就使用指定的,否则使用默认的),如果配置语法正确,nginx 主进程启动新的工作进程并从容关闭旧的工作进程。通知工作进程关闭监听套接字但是工作进程继续为当前连接的客户提供服务。所有客户端的服务完成后,旧的工作进程被关闭。 如果新的配置文件应用失败,nginx 将继续使用旧的配置进行工作。

第一步:或者修改nginx.conf配置文件,或者直接用新的nginx.conf配置文件替换旧nginx.conf配置文件(直接复制到旧的nginx.conf所在位置替换就好了)。

[root@ip10 nginx]# vim ./conf/nginx.conf

[root@ip10 nginx]# ps -ef | grep nginx
root      2584     1  0 20:28 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    2585  2584  0 20:28 ?        00:00:00 nginx: worker process
root      3299  3197  0 21:03 pts/0    00:00:00 grep --color=auto nginx

第二步:使用 kill -HUP 主进程号 平滑重启就可以了。
[root@ip10 nginx]# kill -HUP 2584

6.4 Nginx服务器的平滑升级

 如果需要对当前的nginx服务器进行版本升级,最简单的办法就是停止当前的nginx服务,

然后开启新的nginx服务,但这样会导致在一段时间内,用户无法访问服务器,为了解决这个问题,nginx服务器提供了平滑升级的功能:

平滑升级

你可以在不中断服务的情况下 - 新的请求也不会丢失,使用新的 nginx 可执行程序替换旧的(当升级新版本或添加/删除服务器模块时)。

首先,使用新的可执行程序替换旧的(最好做好备份),然后,发送 USR2 (kill -USR2 pid)信号给主进程。主进程将重命名它的 .pid 文件为 .oldbin (比如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新的可执行程序,依次启动新的主进程和新的工作进程:

 [root@ip10 nginx]# kill -USR2 33126

[root@ip10 nginx]# ps -ef | grep nginx

PID PPID USER %CPU VSZ WCHAN COMMAND
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 
33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 
33135 33126 nobody 0.0 1380 kqread nginx: worker process (nginx) 
33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 
36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

在这时,两个 nginx 实例会同时运行,一起处理输入的请求。要逐步停止旧的实例,你必须发送 WINCH 信号给旧的主进程,然后,它的工作进程就将开始从容关闭:

[root@ip10 nginx]# kill -WINCH 33126

[root@ip10 nginx]# ps -ef | grep nginx

PID PPID USER %CPU VSZ WCHAN COMMAND 
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 
33135 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx) 
36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

一段时间后,旧的工作进程处理了所有已连接的请求后退出,就仅由新的工作进程来处理输入的请求了:

 [root@ip10 nginx]# ps -ef | grep nginx

PID PPID USER %CPU VSZ WCHAN COMMAND 
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 
36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

这时,因为旧的服务器还尚未关闭它监听的套接字,所以,通过下面的几步,你仍可以恢复旧的服务器:

  • 发送 HUP 信号给旧的主进程 - 它将在不重载配置文件的情况下启动它的工作进程

  • 发送 QUIT 信号给新的主进程,要求其从容关闭其工作进程

  • 发送 TERM 信号给新的主进程,迫使其退出

  • 如果因为某些原因新的工作进程不能退出,向其发送 KILL 信号

新的主进程退出后,旧的主进程会由移除 .oldbin 前缀,恢复为它的 .pid 文件,这样,一切就都恢复到升级之前了。

如果尝试升级成功,而你也希望保留新的服务器时,发送 QUIT 信号给旧的主进程使其退出而只留下新的服务器运行:

 [root@ip10 nginx]# kill -QUIT 33126

[root@ip10 nginx]# ps -ef | grep nginx

PID PPID USER %CPU VSZ WCHAN COMMAND 
36264 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 
36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 
36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx


本文转自aaa超超aaa 51CTO博客,原文链接:http://blog.51cto.com/10983441/1759196
相关文章
|
1月前
|
应用服务中间件 nginx
Nginx安装nginx-rtmp-module模块
【2月更文挑战第4天】 nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展。 nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最终生成一个名为nginx的可执行文件。
71 6
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
|
3月前
|
应用服务中间件 Linux 网络安全
centos7 下离线安装gcc g++ nginx,并配置nginx进行网络流转发
centos7 下离线安装gcc g++ nginx,并配置nginx进行网络流转发
104 0
|
3月前
|
Unix 应用服务中间件 Linux
1-Nginx介绍及安装(源码安装)
1-Nginx介绍及安装(源码安装)
44 4
|
2月前
|
缓存 负载均衡 应用服务中间件
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
67 1
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
|
21天前
|
网络协议 应用服务中间件 网络安全
linxu安装nginx
linxu安装nginx
46 0
|
29天前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
179 0
|
1月前
|
负载均衡 应用服务中间件 nginx
|
1月前
|
应用服务中间件 nginx Windows
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
|
1月前
|
Kubernetes 负载均衡 应用服务中间件
Ingress Nginx 安装【亲测可用】
Ingress Nginx 安装【亲测可用】
99 2