Ubuntu下通过Nginx部署Django(2)

  在我们通过Django创建myweb项目时,在子目录myweb下已经帮我们生成的 wsgi.py文件。所以,我们只需要再创建myweb_uwsgi.ini配置文件即可,当然,uwsgi支持多种类型的配置文件,如xml,ini等。此处,使用ini类型的配置。

# myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8000 # the base directory (full path) chdir = /home/fnngj/pydj/myweb # Django s wsgi file module = myweb.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true

  这个配置,其实就相当于在上一小节中通过wsgi命令,后面跟一堆参数的方式,给文件化了。

  socket  指定项目执行的端口号。

  chdir   指定项目的目录。

  module  myweb.wsgi ,可以这么来理解,对于myweb_uwsgi.ini文件来说,与它的平级的有一个myweb目录,这个目录下有一个wsgi.py文件。

  其它几个参数,可以参考上一小节中参数的介绍。

  接下来,切换到myweb项目目录下,通过uwsgi命令读取myweb_uwsgi.ini文件启动项目。

fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/ fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini [uWSGI] getting INI configuration from myweb_uwsgi.ini *** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2016] *** compiled with version: 4.8.4 on 26 January 2016 06:14:41 os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015 nodename: ubuntu machine: i686 clock source: unix detected number of CPU cores: 2 current working directory: /home/fnngj/pydj/myweb detected binary path: /usr/local/bin/uwsgi !!! no internal routing support, rebuild with pcre support !!! chdir() to /home/fnngj/pydj/myweb your processes number limit is 15962 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to TCP address :8000 fd 3 Python version: 3.4.3 (default, Oct 14 2015, 20:37:06) [GCC 4.8.4] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x8b52dc0 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 319920 bytes (312 KB) for 4 cores *** Operational MODE: preforking *** WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 7158) spawned uWSGI worker 1 (pid: 7160, cores: 1) spawned uWSGI worker 2 (pid: 7161, cores: 1) spawned uWSGI worker 3 (pid: 7162, cores: 1) spawned uWSGI worker 4 (pid: 7163, cores: 1)

  注意查看uwsgi的启动信息,如果有错,就要检查配置文件的参数是否设置有误。

  再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,添加如下内容。 

…… server { listen 8099; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 2; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /home/fnngj/pydj/myweb/static/; } } ……

   listen 指定的是nginx代理uwsgi对外的端口号。

  server_name  网上大多资料都是设置的一个网址(例,),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip。

  在进行配置的时候,我有个问题一直想不通。nginx到底是如何uwsgi产生关联。现在看来大概最主要的就是这两行配置。

  include uwsgi_params;

  uwsgi_pass 127.0.0.1:8000;

  include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口与myweb_uwsgi.ini配置文件中的必须一直。

  现在重新启动nginx,翻看上面重启动nginx的命令。然后,访问::8099/

  通过这个IP和端口号的指向,请求应该是先到nginx的。如果你在页面上执行一些请求,就会看到,这些请求最终会转到uwsgi来处理。

  

Ubuntu下通过Nginx部署Django

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/728ca2491871a2cd680dbf4ec4d10b9d.html