Nginx的介绍以及编译安装详解

本文介绍了Nginx的编译安装过程及其配置方法,涵盖了依赖安装、源码下载、编译配置、模块选择等内容,并提供了启动脚本及开机自启设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                       Nginx的介绍以及编译安装详解

                                                  作者:尹正杰   

版权声明:原创作品,谢绝转载!否则将追究法律责任。 

 

 

一.Nginx(全称”engine x“)诞生背景
  apache主要优点是功能丰富且足够稳定,但是其并发效果并不是很理想。还记得我们上次说的MPM模型吗?其中有一种模型是“prefork”模型,它的优点就是一个进程只相应一个请求。所以当web服务器需要处理1W个并发的话就需要1W个进程,我们假设每个进程只需要2M的空间的话,那么平均下来只响应1W个请求就得20G的物理内存啊。所以,早期的web服务器如果访问并发过万的话服务就会自动崩掉。这就是传说中的“C10K”问题。后来Apache有推出了2.4版本的中的”event“模型来解决这个并发问题。与此同时,也诞生了其他类型的应用服务器来解决这个问题,比如:lighttpd(支持lua语言,属于轻量级服务,豆瓣早期就是用的就是它),Nginx(全称”engine x“)。
 
二.Nginx特性
1.基本功能
  a>..静态资源的web服务器,能缓存打开的文件描述符;
  b>.反向代理服务器,可以缓存,负载均衡,以及后端服务器的状态检测功能;
  c>.可以支持FastCGI协议,典型代表就是LNMP网络架构;
  d>.模块化非DSO机制,支持多种过滤器,比如gzip,SSI和图形大小调整等;
  e>.支持SSL等等;
2.扩展功能
  a>.基于名称和IP做虚拟主机;
  b>.支持keepalive;
  c>.支持平滑配置更新或程序版本升级;
  d>.支持定制访问日志,支持使用日志缓存以提高性能;
  e>.支持URL rewrite;
  f>.支持路径别名;
  g>.支持基于IP及用户的认证;
  h>.支持速率限制,并发限制等;
  我们可以用一句话概括Nginx特性:它是一个非阻塞模型,支持事件驱动,它是一个master进程和多个worker进程,一个worker可以响应多个用户请求。
 

 

三.Nginx的基本架构

 

  一个master进程生产多个worker进程,当然也有人说生成了多个worker线程。当然这个对于Linux来说是没有什么太大区别的,我们知道Linux原生不支持线程,在Linux中,线程也被叫做轻量级进程。有可能你会问为什么Linux不支持线程,因为Linux进程本身就已经很轻量了啊。Linux进程比起其他的操作系统来讲以及轻量很多了。但是,为了兼容更多的线程本身的特性,Linux本身也有自己的线程库用于实现轻量级进程。所以,我们可以说Linux所有进程都是以进程的方式来管理的,master可以生成多个worker进程,至于生成多少个我们可以自行配置。worker的进程个数一般取决于CPU的物理核心数。一般而言,worker进程数应该小于物理CPU的核心数(降低进程切换的可能性),因为操作系统的运行也是需要占用CPU的核心数的。

 

  Nginx是基于事件驱动机制来进行工作的。而支持事件驱动的方式有kqueue,epoll和/dev/poll;同时也支持消息通知如select,poll,rt signals;也支持sendfile和sendfile64;也支持文件的AIO机制(异步I/O);还支持mmap等功能。

 

 

四.Ngin的模块类别

 

1.核心模块

 

2.标准http模块

 

3.可选的http模块

 

4.邮件模块

 

5.第三方扩展模块(需要下载并重新编译)
 
 

 

 
五.Nginx的安装方式
1.rpm方式安装:
  下载地址:http://nginx.org/packages/(根据自己的操作系统选择适合你的RPM包程序)
2.yum方式安装
  下载地址:http://nginx.org/en/linux_packages.html#Stable(需要手动指定epel源)
3.编译方式安装
  编译安装是我们推荐的安装方式,因为每个公司的对nginx的用法也不同,所以我们需要定制化安装需要Nginx支持的功能。当然,在编译安装的试试,我们难免要解决依赖环境的问题。接下来我们一起来源码安装一下Nginx吧。
a>.安装依赖环境
[root@yinzhengjie nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel zlib-devel
b>.下载Nginx
[root@yinzhengjie ~]# mkdir -p /yinzhengjie/application/nginx && cd /yinzhengjie/application/nginx/
[root@yinzhengjie nginx]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@yinzhengjie nginx]# ll
总用量 964
-rw-r--r--+ 1 root root 981687 10月 17 21:20 nginx-1.12.2.tar.gz
[root@yinzhengjie nginx]# tar -xf nginx-1.12.2.tar.gz
[root@yinzhengjie nginx]# cd nginx-1.12.2
[root@yinzhengjie nginx-1.12.2]#
c>.查看configure帮助信息
[root@yinzhengjie nginx-1.12.2]# ./configure --help | less
--prefix=PATH #指定Nginx的安装路径
--sbin-path=PATH #指定Nginx核心程序的安装路径
--conf-path=PATH #指定Nginx主配置文件nginx.conf的路径
--error-log-path=PATH #指定错误日志文件路径
--pid-path=PATH #指定pid文件路径
--lock-path=PATH #指定nginx.lock文件的路径
--user=USER #指定用户运行worker进程
--group=GROUP #指定组运行worker进程
--http-log-path=PATH #指定Nginx访问日志的路径
--with-http_ssl_module #加载ssl模块(用于支持https)
--with-http_flv_module #加载流媒体(flash)
--with-http_stub_status_module #加载网页状态的模块
--with-http_gzip_static_module #加载压缩模块,能够实现页面压缩功能
--http-client-body-temp-path=PATH #处理http请求是,如果请求包体需要临时暂存在磁盘上时,我们需要给其指定一个暂存路径
--http-proxy-temp-path=PATH #这个参数是跟缓存相关的,Nginx作为反向代理服务器时,上游服务器(Real Server,简称RIP)产生的http包体需要临时存放的临时目录
--http-fastcgi-temp-path=PATH #fastcgi是一种对CGI的扩展,指定转发FCGI服务器时临时使用文件的存放目录
--http-uwsgi-temp-path=PATH #uwsgi是反向代理用户对于python开发的web服务器协议,这个参数也是指定临时存放文件的目录,uwsgi也是一种对于CGI的扩展
--http-scgi-temp-path=PATH #scgi也是对于CGI的扩展,相比上面两种扩展,只是扩展方式不同而已,该参数也是指定存放临时文件的目录
--without-pcre #
。。。。(and so on)
[root@yinzhengjie nginx-1.12.2]#
 
d>.配置Nginx需要安装的模块
1 [root@yinzhengjie nginx-1.12.2]# groupadd  -r nginx
2 [root@yinzhengjie nginx-1.12.2]# useradd -r -g nginx nginx
3 [root@yinzhengjie nginx-1.12.2]# mkdir -p /yinzhengjie/application/Nginx/tmp
4 [root@yinzhengjie nginx-1.12.2]# ./configure --prefix=/yinzhengjie/application/Nginx  --sbin-path=/yinzhengjie/application/Nginx/sbin/nginx --conf-path=/yinzhengjie/application/Nginx/conf/nginx.conf --error-log-path=/yinzhengjie/application/Nginx/log/error-log --http-log-path=/yinzhengjie/application/Nginx/log/access.log --pid-path=/yinzhengjie/application/Nginx/nginx.pid --lock-path=/yinzhengjie/application/Nginx/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/yinzhengjie/application/Nginx/tmp/client --http-proxy-temp-path=/yinzhengjie/application/Nginx/tmp/proxy --http-fastcgi-temp-path=/yinzhengjie/application/Nginx/tmp/fcgi --http-uwsgi-temp-path=/yinzhengjie/application/Nginx/tmp/uwsgi --http-scgi-temp-path=/yinzhengjie/application/Nginx/tmp/scgi --with-pcre | tee /yinzhengjie/application/nginx/nginx.out
5 [root@yinzhengjie nginx-1.12.2]# 
d>.编译
[root@yinzhengjie nginx-1.12.2]# make -j 4
e>.安装
[root@yinzhengjie nginx-1.12.2]# make install
f>.配置软连接
 1 [root@yinzhengjie ~]# ln -s  /yinzhengjie/application/Nginx/sbin/nginx /usr/sbin/nginx
 2 [root@yinzhengjie ~]# 
 3 [root@yinzhengjie ~]# which nginx
 4 /usr/sbin/nginx
 5 [root@yinzhengjie ~]# 
 6 [root@yinzhengjie ~]# nginx -h
 7 nginx version: nginx/1.12.2
 8 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
 9 
10 Options:
11   -?,-h         : this help
12   -v            : show version and exit
13   -V            : show version and configure options then exit
14   -t            : 测试主配置文件是否有语法错误的。
15   -T            : test configuration, dump it and exit
16   -q            : suppress non-error messages during configuration testing
17   -s signal         #向主进程发信号的一个参数,可以平滑升级或是停止服务等操作。
18   -p prefix     : set prefix path (default: /yinzhengjie/application/Nginx/)
19   -c filename   : 指定配置文件,默认配置文件是: /yinzhengjie/application/Nginx/conf/nginx.conf
20   -g directives : set global directives out of configuration file
21 
22 [root@yinzhengjie ~]# 
g>.配置Nginx启动脚本
 1 [root@yinzhengjie ~]# more /etc/init.d/nginx 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:y1053419035@qq.com
 6 #chkconfig: 2345 89 89
 7 
 8 #Description:This is Nginx web script"
 9 
10 PID="/yinzhengjie/application/Nginx/nginx.pid"
11 
12 start(){
13         /yinzhengjie/application/Nginx/sbin/nginx
14         if [ $? -eq 0 ];then
15                 echo -en "Starting Nginx...\t\t\t["
16                 echo -en "\033[32;34mOK\033[0m"
17                 echo "]"
18         else
19                 echo "Starting Nginx Error"
20         fi
21 }
22 
23 stop(){
24         /yinzhengjie/application/Nginx/sbin/nginx -s stop
25         if [ $? -eq 0 ];then
26                 echo -en "Stop Nginx...\t\t\t["
27                 echo -en "\033[32;34mOK\033[0m"
28                 echo "]"
29         else
30                 echo "Stop Nginx Error"
31         fi
32 }
33 
34 status(){
35         if [ -f $PID ];then
36                 ID=$(cat $PID)
37                 echo "Ngix($ID) is running..."
38         else
39                 echo "Nginx is stop"
40         fi
41 }
42 case $1 in
43     start)
44         start
45         ;;
46     stop)
47         stop
48         ;;
49     restart)
50         stop
51         start
52         ;;
53     status)
54         status
55         ;;
56     *)
57         echo "Usage:$0 {start|stop|restart|status}"
58 esac
59 [root@yinzhengjie ~]# chmod +x /etc/rc.d/init.d/nginx
h>.配置Nginx的开机自启
[root@yinzhengjie ~]# chkconfig --add nginx
[root@yinzhengjie ~]# chkconfig nginx on
[root@yinzhengjie ~]# chkconfig nginx --list
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@yinzhengjie ~]#
i>.检查Nginx是否启动
[root@yinzhengjie ~]# service nginx start
Starting Nginx... [OK]
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# ps -ef | grep nginx
root 6253 1 0 13:45 ? 00:00:00 nginx: master process /yinzhengjie/application/Nginx/sbin/nginx
nginx 6254 6253 0 13:45 ? 00:00:00 nginx: worker process
root 6262 6047 0 13:48 pts/0 00:00:00 grep nginx
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6253 root 6u IPv4 1730240 0t0 TCP *:http (LISTEN)
nginx 6254 nginx 6u IPv4 1730240 0t0 TCP *:http (LISTEN)
[root@yinzhengjie ~]#
 
4.配置Nginx的语法高亮
[root@yinzhengjie ~]# mkdir -p .vim/syntax
[root@yinzhengjie ~]# cd .vim/syntax/
[root@yinzhengjie syntax]# ll
总用量 0
[root@yinzhengjie syntax]# rz
rz waiting to receive.
zmodem trl+C ȡ
 
100% 28 KB 28 KB/s 00:00:01 0 Errors
 
[root@yinzhengjie syntax]# ll
总用量 32
-rw-r--r--. 1 root root 29542 11月 5 13:56 nginx.vim
[root@yinzhengjie syntax]# cd ..
[root@yinzhengjie .vim]#
[root@yinzhengjie .vim]# echo "au BufRead,BufNewFile /yinzhengjie/application/Nginx/conf/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif " > filetype.vim
[root@yinzhengjie .vim]# ll
总用量 12
drwxr-xr-x. 4 root root 4096 9月 16 10:16 bundle
-rw-r--r--. 1 root root 127 11月 5 14:04 filetype.vim
drwxr-xr-x. 2 root root 4096 11月 5 14:00 syntax
[root@yinzhengjie .vim]#
 
 
 
 
 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/yinzhengjie/p/7787940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值