uwsgi+nginx在ubuntu14.04上部署django项目

来自:网络
时间:2019-02-25
阅读:

注意:这里默认已经有完成的django项目,本文不做相关django项目介绍

1.通过python pip安装uwsgi:

sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install --upgrade pip 
sudo pip install uwsgi

2.安装成功后查看uwsgi版本:

uwsgi --version

3.运行django服务(python manage.py runserver 8000),测试uwsgi,这里要通过django的wsgi启动,wsgi.py 在项目目录下(如:项目名为hello,则wsgi.py应该在hello/hello下):

大致内容如下:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

然后终端里运行:

uwsgi --http :8000 --module hello.wsgi

显示出是否正常工作

4.在django项目下创建uwsgi.ini文件和log文件夹:

touch uwsgi.ini

内容如下:

[uwsgi]
socket=127.0.0.1:9000  # uwsgi运行端口
chdir=/home/ubuntu/my_site/  # 项目位置
module=my_site.wsgi:application
vacuum=True
master=True
workers=4
enable-threads=True
threads=10
pidfile=/home/ubuntu/my_site/log/uwsgi.pid
daemonize=/home/ubuntu/my_site/log/uwsgi-production.log
memory-report=True

5.安装Nginx服务器

sudo apt-get install nginx-full

6.配置nginx文件:

cd /etc/nginx/conf.d/
touch default.conf

将以下内容放入default.conf中:

server{
     listen   80;
     charset  utf-8;
     location ~ ^/static {
         root   /home/cloud/my_site;   # 项目static文件夹所在目录
     }
     location / {
         uwsgi_pass    127.0.0.1:9000;   # uwsgi运行端口
         include    /etc/nginx/uwsgi_params;  
     }
}

确保/etc/nginx下有uwsgi_params文件:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;
 
uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;
 
uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param SERVER_NAME $server_name;

然后在nginx目录下创建default的软连接:

ln -s /etc/nginx/conf.d/default.conf /etc/nginx/default.conf

7.如果静态文件目录用户权限是root,则需要更改/etc/nginx.conf文件(一般都在第一行的位置):

将 user  www-data 改为 user  root

8.重启nginx服务并启动uwsgi服务(在django项目下):

service nginx restart
uwsgi -i uwsgi.ini

正常输出如下:

[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Sat Jan 31 19:27:23 2015] ***
compiled with version: 4.8.2 on 31 January 2015 19:21:57
os: Linux-2.6.32-042stab092.2 #1 SMP Tue Jul 8 10:35:55 MSK 2014
nodename: localhost.localdomAIn
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /root/project/hello
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /root/project/hello
your processes number limit is 175957
your memory page size is 4096 bytes
detected max file descriptor number: 4096
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:3400 fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1dc6ef0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds

注: 如果执行完上述步骤后报类似No Python application found的错误,则先执行

aptitude install uwsgi-plugin-python
然后重启uwsgi即可

经过以上步骤,django项目基本上就部署完成了

返回顶部
顶部