Mac编译安装nginx+uwsgi+Django

nginx背景

nginx(发音为“engine x”)是由俄罗斯软件工程师Igor Sysoev编写的免费开源Web服务器。自2004年公开发布以来,nginx一直专注于高性能,高并发性和低内存使用。Web服务器功能之上的其他功能,如负载平衡,缓存,访问和带宽控制,以及与各种应用程序高效集成的能力,有助于使nginx成为现代网站架构的良好选择。目前,nginx是互联网上第二大最受欢迎的开源Web服务器。

服务器的重要性

对于持久连接,处理并发性的问题更加明显,因为为了避免与建立新HTTP连接相关联的延迟,客户端将保持连接,并且对于每个连接的客户端,Web服务器分配了一定量的内存
因此,为了处理与增长的受众相关的增加的工作量以及因此更高的并发性 - 并且能够持续这样做 - 网站应该基于许多非常有效的构建块。虽然硬件(CPU,内存,磁盘),网络容量,应用程序和数据存储架构等方程式的其他部分显然很重要,但是在Web服务器软件中,客户端连接被接受和处理。因此,Web服务器应该能够随着每秒同时连接和请求数量的增加而非线性地扩展

因此,为了处理与增长的受众相关的增加的工作量以及因此更高的并发性 - 并且能够持续这样做 - 网站应该基于许多非常有效的构建块。虽然硬件(CPU,内存,磁盘),网络容量,应用程序和数据存储架构等方程式的其他部分显然很重要,但是在Web服务器软件中,客户端连接被接受和处理。因此,Web服务器应该能够随着每秒同时连接和请求数量的增加而非线性地扩展。

Apache缺点

Apache,这种网络服务器软件在很大程度上仍然主宰着互联网,它的根源在于20世纪90年代初。最初,它的架构与当时存在的操作系统和硬件相匹配,但也与互联网状态相匹配,其中网站通常是运行单个Apache实例的独立物理服务器。到了2000年代初,很明显,无法轻松复制独立的Web服务器模型以满足不断增长的Web服务的需求。尽管Apache为未来的开发提供了坚实的基础,但它的架构是为每个新连接生成自己的副本,这不适合网站的非线性可伸缩性。最终,Apache成为了一个通用的Web服务器,专注于拥有许多不同的功能,各种第三方扩展,并且几乎适用于任何类型的Web应用程序开发。然而,没有任何代价可以实现,并且在单个软件中拥有如此丰富且通用的工具组合的缺点是可扩展性较低,因为每个连接的CPU和内存使用量增加。

服务器的限制:

服务器硬件,操作系统和网络资源不再是网站增长的主要限制时

Nginx解决的问题

旨在解决10,000个同时连接的C10K问题,nginx在编写时考虑了不同的体系结构 - 一个更适合同时连接数和每秒请求数的非线性可伸缩性。nginx是基于事件的,因此它不遵循Apache为每个网页请求生成新进程或线程的风格。最终结果是,即使负载增加,内存和CPU使用率仍然可以管理。nginx现在可以在具有典型硬件的服务器上提供数万个并发连接。
当nginx的第一个版本发布时,它意味着与Apache一起部署,以便nginx处理静态内容,如HTML,CSS,JavaScript和图像,以卸载基于Apache的应用程序服务器的并发和延迟处理。在开发过程中,nginx通过使用FastCGI,uswgi或SCGI协议以及分布式内存对象缓存系统(如memcached)增加了与应用程序的集成 。还添加了其他有用的功能,例如具有负载平衡和缓存的反向代理。这些附加功能使nginx成为有效的工具组合,可构建可扩展的Web基础架构。

nginx优势

nginx非常适合这一点,因为它提供了方便卸载并发,延迟处理,SSL(安全套接字层),静态内容,压缩和缓存,连接和请求限制,甚至来自应用程序的HTTP媒体流所需的关键功能层到更有效的边缘Web服务器层。它还允许直接与memcached / Redis或其他“NoSQL”解决方案集成,以在为大量并发用户提供服务时提高性能。

Nginx扩展

功能模块可分为事件模块,阶段处理程序,输出过滤器,变量处理程序,协议,上游和负载平衡器。这些模块中的大多数补充了nginx的HTTP功能,但也使用了事件模块和协议mail。事件模块提供特定的OS依赖事件通知机制,如kqueue或epoll。nginx使用的事件模块取决于操作系统功能和构建配置。协议模块允许nginx通过HTTPS,TLS / SSL,SMTP,POP3和IMAP进行通信。

Nginx架构:
http://www.aosabook.org/en/nginx.html
在这里插入图片描述
Nginx文档:

https://nginx.org/en/docs/
Nginx源码安装:
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#
nginx模块配置文档列表:
https://nginx.org/en/docs/
http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.14.1.tar.gz
nginx配置文件:
	https://nginx.org/en/docs/configure.html
	https://www.nginx.com/resources/wiki/start/topics/examples/fullexample2/#nginx-conf
nginx配置文件:
	https://nginx.org/en/docs/beginners_guide.html
nginx是如何处理一个请求的:
	https://nginx.org/en/docs/http/request_processing.html

ubuntu下安装:

sudo apt install nginx
源码安装:
下载地址:http://hg.nginx.org/nginx

sudo apt install build-essential
wget http://hg.nginx.org/nginx/archive/ac466b907fa9.tar.gz
tar zxvf nginx.tar.gz
./configure   
make
make install 
查看nginx安装状态和安装的模块
nginx -V 

解压

sudo tar zxvf nginx-1.14.1.tar.gz

包含的文件夹

➜  nginx-1.14.1 tree -L 1
.
├── CHANGES
├── CHANGES.ru
├── LICENSE
├── README
├── auto
├── conf
├── configure
├── contrib
├── html
├── man
└── src

配置相关模块
因为nginx的源码安装前提是安装了openssl\zlib\pcre,分别对nginx提供ssl安全\gzip压缩(提高静态资源访问速度从而提高网站的整体访问速度)\正则表达式支持,但是有时候在安装nginx的时候会找不到你的openssl解压目录,这个时候就需要手动指定openssl的解压缩目录。

./configure --prefix=/usr/local/nginx  --with-openssl=/usr/local/openssl-1.0.1j --with-http_ssl_module
如果pcre和zlib出现类似的问题,指定路径就可。
--with-pcre=/usr/local/pcre-7.7 --with-zlib=/usr/local/zlib-1.2.3 --with-http_stub_status_module

Ngin配置文件

./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.36 --with-openssl=/usr/include/openssl
注意:TCP_FASTOPEN 只在 3.7.1 以及更新的 Linux 内核版本才支持
--with-http_dav_module             #启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认关闭,需要编译开启
--with-http_stub_status_module     #启用支持(获取Nginx上次启动以来的工作状态)
--with-http_addition_module        #启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_sub_module             #启用支持(允许一些其他文本替换Nginx相应中的一些文本)
--with-http_flv_module             #启用支持(提供支持flv视频文件支持)
--with-http_mp4_module             #启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)
--with-pcre=/usr/local/src/pcre-8.36            #需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助
[root@linuxprobe nginx-1.10.1]# make && make install
#Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。
--prefix=PATH       #Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为PATH/sbin/nginx。
--sbin-path=PATH    #在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为PATH/conf/nginx.conf。 
--conf-path=PATH    #在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 PATH/logs/nginx.pid。
--pid-path=PATH     #nginx.lock文件的路径。
--lock-path=PATH    #在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 PATH/logs/error.log。
--error-log-path=PATH    #在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 PATH/logs/access.log。
--http-log-path=PATH     #在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--user=USER       #在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--group=GROUP     #指定编译的目录
--builddir=DIR    #启用 rtsig 模块
--with-rtsig_module    #允许或不允许开启SELECT模式,如果configure没有找到合适的模式,比如,kqueue(sun os)、epoll(linux kenel 2.6+)、rtsig(实时信号)
--with-select_module(--without-select_module)    #允许或不允许开启POLL模式,如果没有合适的,则开启该模式。
--with-poll_module(--without-poll_module)        #开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl-dev

--with-http_ssl_module         #启用ngx_http_ssl_module
--with-http_realip_module      #启用 ngx_http_realip_module
--with-http_addition_module    #启用 ngx_http_addition_module
--with-http_sub_module    #启用 ngx_http_sub_module
--with-http_dav_module    #启用 ngx_http_dav_module
--with-http_flv_module    #启用 ngx_http_flv_module
--with-http_stub_status_module    #启用 "server status" 页
--without-http_charset_module     #禁用 ngx_http_charset_module
--without-http_gzip_module        #禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。
--without-http_ssi_module         #禁用 ngx_http_ssi_module
--without-http_userid_module      #禁用 ngx_http_userid_module
--without-http_access_module      #禁用 ngx_http_access_module
--without-http_auth_basic_module  #禁用 ngx_http_auth_basic_module
--without-http_autoindex_module   #禁用 ngx_http_autoindex_module
--without-http_geo_module         #禁用 ngx_http_geo_module
--without-http_map_module         #禁用 ngx_http_map_module
--without-http_referer_module     #禁用 ngx_http_referer_module
--without-http_rewrite_module     #禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
--without-http_proxy_module       #禁用 ngx_http_proxy_module
--without-http_fastcgi_module     #禁用 ngx_http_fastcgi_module
--without-http_memcached_module   #禁用 ngx_http_memcached_module
--without-http_limit_zone_module  #禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module   #禁用 ngx_http_empty_gif_module
--without-http_browser_module     #禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module   #禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module -         #启用 ngx_http_perl_module
--with-perl_modules_path=PATH     #指定 perl 模块的路径
--with-perl=PATH        #指定 perl 执行文件的路径
--http-log-path=PATH    #Set path to the http access log
--http-client-body-temp-path=PATH    #Set path to the http client request body temporary files
--http-proxy-temp-path=PATH          #Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH        #Set path to the http fastcgi temporary files
--without-http                 #禁用 HTTP server
--with-mail                    #启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module         #启用 ngx_mail_ssl_module
--with-cc=PATH                 #指定 C 编译器的路径
--with-cpp=PATH                #指定 C 预处理器的路径
--with-cc-opt=OPTIONS #
--with-ld-opt=OPTIONS #Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".
--with-cpu-opt=CPU        #为特定的CPU编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre            #禁止PCRE库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。
--with-pcre=DIR           #指定 PCRE 库的源代码的路径。
--with-pcre-opt=OPTIONS   #设置PCRE的额外编译选项。
--with-md5=DIR            #使用MD5汇编源码。
--with-md5-opt=OPTIONS    #Set additional options for md5 building.
--with-md5-asm            #Use md5 assembler sources.
--with-sha1=DIR           #Set path to sha1 library sources.
--with-sha1-opt=OPTIONS   #Set additional options for sha1 building.
--with-sha1-asm           #Use sha1 assembler sources.
--with-zlib=DIR           #Set path to zlib library sources.
--with-zlib-opt=OPTIONS   #Set additional options for zlib building.
--with-zlib-asm=CPU       #Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro
--with-openssl=DIR        #Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS #Set additional options for OpenSSL building
--with-debug              #启用调试日志
--add-module=PATH         #Add in a third-party module found in directory PATH

指定配置模块:

➜  nginx-1.14.1 sudo ./configure
.....
配置文件汇总:
Configuration summary
  + using system PCRE library:PCRE - Perl兼容的正则表达式
  + OpenSSL library is not used
  + using system zlib library
  nginx安装的文件夹路径:
  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"

查看是否有相关的模块库

➜  nginx-1.14.1 ls -al /usr/local/lib | grep openssl
lrwxr-xr-x    1 cangck  admin       57 10 26 23:57 libevent_openssl-2.1.6.dylib -> ../Cellar/libevent/2.1.8/lib/libevent_openssl-2.1.6.dylib
lrwxr-xr-x    1 cangck  admin       47 10 26 23:57 libevent_openssl.a -> ../Cellar/libevent/2.1.8/lib/libevent_openssl.a
lrwxr-xr-x    1 cangck  admin       51 10 26 23:57 libevent_openssl.dylib -> ../Cellar/libevent/2.1.8/lib/libevent_openssl.dylib

confiugre 配置完成之后生成的配置文件Makefile

➜  nginx-1.14.1 cat Makefile

default:	build

clean:
	rm -rf Makefile objs

build:
	$(MAKE) -f objs/Makefile

install:
	$(MAKE) -f objs/Makefile install

modules:
	$(MAKE) -f objs/Makefile modules

upgrade:
	/usr/local/nginx/sbin/nginx -t

	kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
	sleep 1
	test -f /usr/local/nginx/logs/nginx.pid.oldbin

	kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

nginx-1.14.1/objs/Makefile 该文件夹下是对生成的课编译文件的文档
使用sudo make install 安排编译好的文件到系统中,Unix、linux系统中的安装文件其实就是对相关软件的文件进行拷贝,拷贝到相关的文件夹下,在$PATH中配置文件的路径

启动nginx:

/usr/local/nginx

查看nginx进程相关的信息

➜  nginx ps aux | grep nginx
nobody           48600   0.0  0.0  2489468   1452   ??  S    六09下午   0:00.01 nginx: worker process
root             48599   0.0  0.0  2461592    492   ??  Ss   六09下午   0:00.00 nginx: master process /usr/local/nginx/sbin/nginx
cangck           93465   0.0  0.0  2432804    796 s002  S+   10:19下午   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx

在浏览器中输入地址就可以看到nginx首页面

nginx配置文件
/usr/local/nginx/conf

➜  conf tree -L 2
.
├── fastcgi.conf :fastcgi相关的配置文件
├── fastcgi.conf.default
├── fastcgi_params:fastcgi配置文件参数
├── fastcgi_params.default
├── koi-utf:charset_map  koi8-r < -- > windows-1251
├── koi-win:charset_map  koi8-r < -- > utf-8
├── mime.types:MIME服务器支持文件类型
├── mime.types.default
├── nginx.conf:nginx配置文件
├── nginx.conf.default
├── scgi_params:scgi参数配置文件
├── scgi_params.default
├── uwsgi_params:uwsgi配置文件
├── uwsgi_params.default
└── win-utf:charset_map  windows-1251 < -- > utf-8
0 directories, 15 files
koi8-r是斯拉夫文字8位元编码,供俄语及保加利亚语使用。在Unicode未流行之前,KOI8-R 是最为广泛使用的俄语编码,使用率甚至起ISO/IEC 8859-5还高。这3个文件存在是因为作者是俄国人的原因。

nginx相关的配置文件

nginx命令行
1.nginx命令
ubuntu@ip-172-31-83-111:~$ nginx -h
nginx version: nginx/1.10.3 (Ubuntu)
Usage: nginx [-?hvVtTq] [-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
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/share/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

2.开启、停止、重新加载配置文件
nginx -s signal
     Where signal may be one of the following:

    stop — fast shutdown

    quit — graceful shutdown

    reload — reloading the configuration file

    reopen — reopening the log files

3.查看进程号
         /usr/local/nginx/logs or /var/run
     ps -ax | grep nginx
     kill -s QUIT 1628
4.使用信号量来控制nginx服务器:  
    强制退出Nginx:
kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid )
        TERM, INT	fast shutdown
        QUIT	graceful shutdown
         HUP	changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes
        USR1	re-opening log files
         USR2	upgrading an executable file
        WINCH	
graceful shutdown of worker processes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值