一、基本介绍
官网地址www.nginx.org,nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始,第一次公开发布时间是2004年10月4日,版本号是0.1.0
Nginx是单进程单线程模型,即启动的工作进程只有一个进程响应客户端请求,不像apache可以在一个进程内启动多个线程响应可请求,因此在内存占用上比apache小的很多。Nginx维持一万个非活动会话只要2.5M内存。Nginx和Mysql是CPU密集型的,就是对CPU的占用比较大,默认session在本地文件保存,支持将session保存在memcache,但是memcache默认支持最大1M的课hash对象。
nginx的版本分为开发版、稳定版和过期版,nginx以功能丰富著称,它即可以作为http服务器,也可以作为反向代理服务器或者邮件服务器,能够快速的响应静态网页的请求,支持FastCGI/SSL/Virtual Host/URL Rwrite/Gzip/HTTP Basic Auth等功能,并且支持第三方的功能扩展。
nginx安装可以使用yum或源码安装,推荐使用源码,一是yum的版本比较旧,二是使用源码可以自定义功能,方便业务的上的使用,源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective-C,java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块需要依赖第三方库,比如pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)
二、安装
1、环境准备:先安装准备环境
yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他很多种语言 的编译器(最早的时候只能编译C,后来很快进化成一个编译多种语言的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。) gcc 在编译C++源代码的阶段,只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库链接(编译过程分为编译、链接两个阶段,注意不要和可执行文件这个概念搞混,相对可执行文件来说有三个重要的概念:编译(compile)、链接(link)、加载(load)。源程序文件被编译成目标文件,多个目标文件连同库被链接成一个最终的可执行文件,可执行文件被加载到内存中运行)。因此,通常使用 g++ 命令来完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。 gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。 automake是一个从Makefile.am文件自动生成Makefile.in的工具。为了生成Makefile.in,automake还需用到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。libtool是一款方便生成各种程序库的工具。 pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的Rewrite模块和HTTP 核心模块会使用到PCRE正则表达式语法。 zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持。 openssl openssl-devel:开启SSL的时候需要此模块的支持。
2、下载nginx 安装包: 官网地址:http://nginx.org/
使用wget进行下载
[root@localhost nginx]# pwd /usr/local/src/nginx [root@localhost nginx]# wget -P /usr/local/src/nginx/ http://nginx.org/download/nginx-1.18.0.tar.gz --2020-06-01 15:46:04-- http://nginx.org/download/nginx-1.18.0.tar.gz Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 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: 1039530 (1015K) [application/octet-stream] Saving to: ‘/usr/local/src/nginx/nginx-1.18.0.tar.gz’ 100%[==================================================================================================================================================>] 1,039,530 25.8KB/s in 37s 2020-06-01 15:46:42 (27.4 KB/s) - ‘/usr/local/src/nginx/nginx-1.18.0.tar.gz’ saved [1039530/1039530]
3、解压安装包
[root@localhost nginx]# tar zxf nginx-1.18.0.tar.gz [root@localhost nginx]# cd nginx-1.18.0/ [root@localhost nginx-1.18.0]# ll total 764 drwxr-xr-x. 6 1001 1001 4096 Jun 1 15:49 auto -rw-r--r--. 1 1001 1001 302863 Apr 21 22:09 CHANGES -rw-r--r--. 1 1001 1001 462213 Apr 21 22:09 CHANGES.ru drwxr-xr-x. 2 1001 1001 168 Jun 1 15:49 conf -rwxr-xr-x. 1 1001 1001 2502 Apr 21 22:09 configure drwxr-xr-x. 4 1001 1001 72 Jun 1 15:49 contrib drwxr-xr-x. 2 1001 1001 40 Jun 1 15:49 html -rw-r--r--. 1 1001 1001 1397 Apr 21 22:09 LICENSE drwxr-xr-x. 2 1001 1001 21 Jun 1 15:49 man -rw-r--r--. 1 1001 1001 49 Apr 21 22:09 README drwxr-xr-x. 9 1001 1001 91 Jun 1 15:49 src
4、编译nginx:configure
编译是为了检查系统环境是否符合编译安装的要求,比如是否有gcc编译工具,是否支持编译参数当中的模块,并根据开启的参数等生成Makefile文件为下一步做准备:
./configure --prefix=/usr/local/nginx --with-openssl=/usr/lib64/openssl
结果如下:
Configuration summary + using system PCRE library + using OpenSSL library: /usr/lib64/openssl + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
5、生成脚本及配置文件:make
编译步骤,根据Makefile文件生成相应的模块
[root@localhost nginx-1.18.0]# make make -f objs/Makefile make[1]: Entering directory `/usr/local/src/nginx/nginx-1.18.0‘ cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/nginx.o src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_log.o src/core/ngx_log.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_palloc.o src/core/ngx_palloc.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_array.o src/core/ngx_array.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_list.o src/core/ngx_list.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_hash.o src/core/ngx_hash.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_buf.o .................. objs/src/http/modules/ngx_http_upstream_keepalive_module.o objs/src/http/modules/ngx_http_upstream_zone_module.o objs/ngx_modules.o -ldl -lpthread -lcrypt -lpcre -lz -Wl,-E sed -e "s|%%PREFIX%%|/usr/local/nginx|" -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" < man/nginx.8 > objs/nginx.8 make[1]: Leaving directory `/usr/local/src/nginx/nginx-1.18.0‘
6、安装:make install
[root@localhost nginx-1.18.0]# make install make -f objs/Makefile install make[1]: Entering directory `/usr/local/src/nginx/nginx-1.18.0‘ test -d ‘/usr/local/nginx‘ || mkdir -p ‘/usr/local/nginx‘ test -d ‘/usr/local/nginx/sbin‘ || mkdir -p ‘/usr/local/nginx/sbin‘ test ! -f ‘/usr/local/nginx/sbin/nginx‘ || mv ‘/usr/local/nginx/sbin/nginx‘ ‘/usr/local/nginx/sbin/nginx.old‘ cp objs/nginx ‘/usr/local/nginx/sbin/nginx‘ test -d ‘/usr/local/nginx/conf‘ || mkdir -p ‘/usr/local/nginx/conf‘ cp conf/koi-win ‘/usr/local/nginx/conf‘ cp conf/koi-utf ‘/usr/local/nginx/conf‘ cp conf/win-utf ‘/usr/local/nginx/conf‘ test -f ‘/usr/local/nginx/conf/mime.types‘ || cp conf/mime.types ‘/usr/local/nginx/conf‘ cp conf/mime.types ‘/usr/local/nginx/conf/mime.types.default‘ test -f ‘/usr/local/nginx/conf/fastcgi_params‘ || cp conf/fastcgi_params ‘/usr/local/nginx/conf‘ cp conf/fastcgi_params ‘/usr/local/nginx/conf/fastcgi_params.default‘ test -f ‘/usr/local/nginx/conf/fastcgi.conf‘ || cp conf/fastcgi.conf ‘/usr/local/nginx/conf‘ cp conf/fastcgi.conf ‘/usr/local/nginx/conf/fastcgi.conf.default‘ test -f ‘/usr/local/nginx/conf/uwsgi_params‘ || cp conf/uwsgi_params ‘/usr/local/nginx/conf‘ cp conf/uwsgi_params ‘/usr/local/nginx/conf/uwsgi_params.default‘ test -f ‘/usr/local/nginx/conf/scgi_params‘ || cp conf/scgi_params ‘/usr/local/nginx/conf‘ cp conf/scgi_params ‘/usr/local/nginx/conf/scgi_params.default‘ test -f ‘/usr/local/nginx/conf/nginx.conf‘ || cp conf/nginx.conf ‘/usr/local/nginx/conf/nginx.conf‘ cp conf/nginx.conf ‘/usr/local/nginx/conf/nginx.conf.default‘ test -d ‘/usr/local/nginx/logs‘ || mkdir -p ‘/usr/local/nginx/logs‘ test -d ‘/usr/local/nginx/logs‘ || mkdir -p ‘/usr/local/nginx/logs‘ test -d ‘/usr/local/nginx/html‘ || cp -R html ‘/usr/local/nginx‘ test -d ‘/usr/local/nginx/logs‘ || mkdir -p ‘/usr/local/nginx/logs‘ make[1]: Leaving directory `/usr/local/src/nginx/nginx-1.18.0‘
安装完成目录下结构
drwxr-xr-x. 2 root root 19 Jun 1 15:59 sbin [root@localhost nginx]# pwd /usr/local/nginx [root@localhost nginx]# ll total 4 drwxr-xr-x. 2 root root 4096 Jun 1 15:59 conf drwxr-xr-x. 2 root root 40 Jun 1 15:59 html drwxr-xr-x. 2 root root 6 Jun 1 15:59 logs drwxr-xr-x. 2 root root 19 Jun 1 15:59 sbin
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。 html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。 logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。 sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
7、启动nginx
查看默认80端口是否占用: lsof -i:80
启动nginx:/usr/local/nginx/sbin/nginx
8、验证是否启动成功
[root@localhost nginx]# ps -ef|grep nginx root 6914 1 0 16:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 6915 6914 0 16:04 ? 00:00:00 nginx: worker process root 11071 2031 0 18:51 pts/1 00:00:00 grep --color=auto nginx
三、nginx常用命令
启动
nginx安装目录地址 -c nginx配置文件地址
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止
从容停止
查看进程号
[root@localhost nginx]# ps -ef|grep nginx root 11138 1 0 18:57 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11139 11138 0 18:57 ? 00:00:00 nginx: worker process root 11244 2031 0 19:12 pts/1 00:00:00 grep --color=auto nginx
杀死进程
[root@localhost nginx]# [root@localhost nginx]# kill -QUIT 11138 [root@localhost nginx]# [root@localhost nginx]# ps -ef|grep nginx root 11248 2031 0 19:13 pts/1 00:00:00 grep --color=auto nginx
快速停止
1、查看进程号
[root@localhost nginx]# ps -ef|grep nginx root 11273 1 0 19:15 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11274 11273 0 19:15 ? 00:00:00 nginx: worker process root 11276 2031 0 19:15 pts/1 00:00:00 grep --color=auto nginx
2、杀死进程
[root@localhost nginx]# kill -TERM 11273 [root@localhost nginx]# [root@localhost nginx]# ps -ef|grep nginx root 11280 2031 0 19:17 pts/1 00:00:00 grep --color=auto nginx 或 kill -INT 11273
强制停止
[root@localhost nginx]# pkill -9 nginx [root@localhost nginx]# [root@localhost nginx]# ps -ef|grep nginx root 11289 2031 0 19:18 pts/1 00:00:00 grep --color=auto nginx
重启
方法1、验证配置文件是否正确
[root@localhost nginx]# /usr/local/nginx/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
方法2、
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
重启Nginx服务
方法一、进入nginx可执行目录sbin下,输入命令./nginx -s reload 即可
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [root@localhost nginx]# [root@localhost nginx]# ps -ef|grep nginx root 11337 1 0 19:21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11338 11337 0 19:21 ? 00:00:00 nginx: worker process root 11340 2031 0 19:21 pts/1 00:00:00 grep --color=auto nginx [root@localhost nginx]# [root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload [root@localhost nginx]# [root@localhost nginx]# ps -ef|grep nginx root 11337 1 0 19:21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11342 11337 0 19:22 ? 00:00:00 nginx: worker process root 11344 2031 0 19:22 pts/1 00:00:00 grep --color=auto nginx
方法二:查找当前nginx进程号,然后输入命令:kill -HUP 进程号 实现重启nginx服务
[root@localhost nginx]# ps -ef|grep nginx root 11337 1 0 19:21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11342 11337 0 19:22 ? 00:00:00 nginx: worker process root 11344 2031 0 19:22 pts/1 00:00:00 grep --color=auto nginx [root@localhost nginx]# kill -HUP 11337 [root@localhost nginx]# [root@localhost nginx]# ps -ef|grep nginx root 11337 1 0 19:21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11346 11337 0 19:23 ? 00:00:00 nginx: worker process root 11348 2031 0 19:23 pts/1 00:00:00 grep --color=auto nginx
原文:https://www.cnblogs.com/xuhl95/p/13026989.html