Ubuntu下uwsgi启动django实战(python虚拟环境)

1.Uwsgi基本安装配置

   1).环境描述

      系统环境:ubuntu

      Python环境:python2.7

   2).django及uwsgi安装

  1. pip install django
  2. pip install uwsgi

   3).测试uwsgi

        编辑vi /home/test.py

# test.py

def application(env, start_response):

    start_response('200 OK', [('Content-Type','text/html')])

    #return [b"Hello World"] # python3

    return ["Hello World"] # python2

       启动命令uwsgi --http-socket :9000 --plugin python --wsgi-file test.py

       使用浏览器访问测试:http://127.0.0.1:9000/

       Ubuntu下uwsgi启动django实战(python虚拟环境)

     4).一些启动命令的报错及原因:

       使用uwsgi --http 127.0.0.1:9000 --wsgi-file test.py命令启动时,报错:

       uwsgi: option '--http' is ambiguous;

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       问题根源是没有这个uwsgi插件应该使用 --http-socket  参数

       使用命令:uwsgi --http-socket 127.0.0.1:9000 --wsgi-file test.py启动时报:

       uwsgi: unrecognized option '--wsgi-file'

       getopt_long() error

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       问题原因:uwsgi不识别后面的wsgi  python文件,无法解析

       解决方式:在--wsgi-file 参数前加上--plugin python 参数,告诉uwsgi使用python插件解析后面的python文件

       uwsgi --http-socket :9000 --plugin python --wsgi-file test.py

2.ubuntu虚拟环境中创建django项目及nginx代理配置

    1).安装配置VirtualEnv和VirtualEnvWrapper
        VirtualEnv可以管理多个开发环境,VirtualEnvWrapper使得VirtualEnv变得更好用

       sudo pip install virtualenv virtualenvwrapper

       安装完成以后,需要在环境变量中加入一些配置:

       echo "export WORKON_HOME=~/Env" >> ~/.bashrc

       echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

       可以重启系统**,也可以运行:

       source ~/.bashrc

    2).创建虚拟环境:

         mkvirtualenv first

    3).创建项目路径mkdir /root/workplace

       安装django:pip install django==1.10.1

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       创建django项目:django-admin startproject helloworld

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       新增view.py 修改urls.py

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       Ubuntu下uwsgi启动django实战(python虚拟环境)

 

       启动运行测试:

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       浏览器访问地址:http://192.168.122.128:8000/index/

       Ubuntu下uwsgi启动django实战(python虚拟环境)

    4).退出虚拟环境

        deactivate

    5).重新进入虚拟环境

        source /root/Env/first/bin/activate  

    6).使用uwsgi命令启动django项目

         uwsgi --http :8000 --home /root/Env/first --chdir /root/workplace/helloworld -w helloworld.wsgi

    7).uwsgi使用配置文件启动django项目

        编辑uwsgi启动配置文件:Vim /home/uwsgi.ini

[uwsgi]

http = :8000

#the local unix socket file than commnuincate to Nginx

socket = 127.0.0.1:8001

# the base directory (full path)

chdir = /root/workplace/helloworld

home = /root/Env/first

# Django's wsgi file

wsgi-file = helloworld/wsgi.py

# maximum number of worker processes

processes = 4

#thread numbers startched in each worker process

threads = 2

 

#monitor uwsgi status

stats = 127.0.0.1:9191

# clear environment on exit

vacuum          = true

 

       uwsgi使用配置文件启动django命令:uwsgi uswgi.ini

       Ubuntu下uwsgi启动django实战(python虚拟环境)

       浏览器访问路径:http://192.168.122.128:8000/index/

       Ubuntu下uwsgi启动django实战(python虚拟环境)

    8).配置nginx代理uwsgi服务访问django接口

        0.ubuntu下nginx的安装及启动

              apt-get install nginx

              启动nginx:service nginx start

              重启nginx: service nginx restart

              停止nginx: service nginx stop

              查看nginx运行状态: service nginx status

       1.在项目路径下添加vim uwsgi_params文件:vim /root/workplace/helloworld/uwsgi_param

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;

Ubuntu下uwsgi启动django实战(python虚拟环境)

       2.新增nginx配置文件:vim /etc/nginx/sites-available/helloword

upstream django {

    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket

    server 127.0.0.1:8001; # for a web port socket (we'll use this first)

}

server {

    listen 80;

    server_name 192.168.122.128;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {

        root /root/workplace/helloworld;

    }

    location / {

        uwsgi_pass         django;

        include      /root/workplace/helloworld/uwsgi_params;

    }

}

       3.配置文件添加软连接到开启的配置文件文件夹下

              sudo ln -s /etc/nginx/sites-available/helloworld /etc/nginx/sites-enabled/

              检查配置文件是否正确:

              Service nginx configtest

       4.重启nginx,使用uwsgi启动django,测试

              Service nginx restart

              Uwsgi /home/uwsgi.ini

              浏览器输入: http://192.168.122.128/index/

              Ubuntu下uwsgi启动django实战(python虚拟环境)

如果下一篇:ubuntu不在python虚拟环境下使用uwsgi启动django及nginx代理配置