Nginx+uwsgi+django生产服务器配置

24 篇文章 0 订阅
10 篇文章 0 订阅

django

django只有自带的调试服务器,使用方法:

python manage.py runserver

调试服务器只能在本机运行,不能放在外网上供别的机器访问,所以今天就配置一下生产服务器.

nginx

安装

sudo apt-get install nginx

启动暂停服务

/etc/init.d/nginx start/stop/restart

启动后输入

http://127.0.0.1:80

显示welcome to nginx 表示安装成功

修改默认端口号(可选)

如果安装时nginx无法启动,出现

See "systemctl status nginx.service" and "journalctl -xe" for details

经查看日志,发现该80端口已被占用.要么关掉占用端口的进程,要么就修改默认端口.天下大路条条通罗马,我们可以采用后者.

sudo gedit /etc/nginx/nginx.conf

可见如下

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*; 

第二行目录下 /etc/nginx/sites-enabled/default为默认的nginx配置,其中端口号为80.
将该句注释掉,加入下面内容

    server {
        listen  8088;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
        }   
    }

重启nginx服务,在网站上输入”127.0.0.1.8088”,发现成功
http://blog.csdn.net/leo_li_3046/article/details/51823222

uwsgi

pip install uwsgi

problem1

anaconda 虚拟环境安装uwsgi出现错误。 lto1: fatal error: bytecode stream generated with LTO version 6.0 instead of the expected 4.1
本机gcc版本5.4,改用4.7版本进行编译,完美通过。
https://www.cnblogs.com/liuzhen1995/p/8893962.html

problem2

uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz 
./configure
sudo make && sudo make install
pcre-config --version #显示成功
sudo ln -s /usr/local/lib/libpcre.so.1 /lib64

经过在另一台机器安装,发现上面不一定是移到/lib64,这里要在python里找到uwsgi,通过 ldd uwsgi查看其依赖的动态库都放在哪里,然后把软链接连到对应目录下,可以解决。

problem3

invalid request block size: 21573 (max 4096)...skip

使用nginx来把请求发送给uwsgi。所以uwsgi被配置成使用socket方式(为tcp协议)进行通信。如果打开浏览器访问uwsgi指定的端口,那么浏览器请求uwsgi的方式为http协议,而不是socket方式。所以就导致uwsgi的log文件中打出上面的错误信息。
如果是使用uwsgi.ini配置文件,那么修改里面内容把socket=:8000替换成http=:8000。

另外:
我用的anaconda管理python环境,本机ubuntu16.04默认python是2.7.13版本,而我的django项目是3.6版本下完成的,所以需要在python3.6版本下安装uwsgi.
如果有多个环境下都需要uwsgi的话,在uwsgi的配置文件中指定路径与python环境.
ps:我指定了但没有生效,所以把2.7.13版本的uwsgi卸掉了= =.

测试uwsgi

创建test.py文件

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

运行文件

uwsgi --http :8001 --wsgi-file test.py

进入127.0.0.1:8001出现hello,world表示成功,如果出现 Internal Server Error,查看terminal中的报错.

连接uwsgi与django

此处,假定的我的django项目位置为:/home/fnngj/pydj/myweb

uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master 
--processes 4 --threads 2 --stats 127.0.0.1:9191
常用选项:

http : 协议类型和端口号

processes : 开启的进程数量

workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)

chdir : 指定运行目录(chdir to specified directory before apps loading)

wsgi-file : 载入wsgi-fileload .wsgi file)

stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address

threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with 
the specified number of threads)

master : 允许主进程存在(enable master process)

daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,
还是把运行记录输出到一个本地文件上。

pidfile : 指定pid文件的位置,记录主进程的pid号。

vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件
(try to remove all of the generated file/sockets)

连接nginx+uwsgi+django

目录结构如下:

myweb/

├── manage.py

├── myweb/

│ ├── init.py

│ ├── settings.py

│ ├── urls.py

│ └── wsgi.py

└── myweb_uwsgi.ini

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

# myweb_uwsgi.ini file
[uwsgi]

# Django-related settings

socket = :8000

# if you have many envs of python 
#pythonpath=/home/tang/anaconda2/envs/python36/lib/python3.6/site-packages
#virtualenv=/home/tang/anaconda2/envs/python36

# 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文件。

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

problem

ps: 此处可能报错:invalid request block size: 21573 (max 4096)…skip
解决方法: 将ini文件中的socket:=8000改成http:=8000

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)

如果出现,Python version指示2.7.13,提示某些python3.6环境下的库找不到的话,尝试在配置文件中加入virtualenv.
再不行就在python2.7环境下暂时卸掉uwsgi.

修改nginx.conf

/etc/nginx/nginx.conf文件
server部分改成如下内容

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 网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip。

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

  include uwsgi_params;

  uwsgi_pass 127.0.0.1:8000;

  include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口号与myweb_uwsgi.ini配置中的文件中的必须一致。
 现在重新启动nginx,翻看上面重启动nginx的命令。然后,访问:http://127.0.0.1:8099/

虫师大佬原博

访问服务器

经过服务器配置后,在另一台主机上访问,会出现未允许主机错误

Invalid HTTP_HOST header: '192.168.1.168:8099'.You may need to add '192.168.1.127' to ALLOWED_HOSTS

需要在django配置中更改
在其setting.py中

ALLOWED_HOSTS = []# 默认只有127.0.0.1 localhost能够访问

改为

ALLOWED_HOSTS = ['*']# 所有主机都可访问

启动方法

sudo service nginx restart//启动nginx
uwsgi --ini yourini.ini//初始化uwsgi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值