起飞前的准备

# 首先在Ubuntu的当前用户zhang下新建data文件夹,然后在data文件夹下新建你的项目目录
root@zhang-virtual-machine:/home/zhang/data# mkdir my_web_project root@zhang-virtual-machine:/home/zhang/data# ls my_web_project root@zhang-virtual-machine:/home/zhang/data# cd my_web_project/

# virtualenv --no-site-paceages env 创建虚拟环境,--no-site-packages 表示一个干净的目录,不会安装没必要的包 root@zhang-virtual-machine:/home/zhang/data/my_web_project# virtualenv --no-site-packages env Using base prefix '/usr' New python executable in /home/zhang/data/my_web_project/env/bin/python3 Also creating executable in /home/zhang/data/my_web_project/env/bin/python Installing setuptools, pip, wheel...done. root@zhang-virtual-machine:/home/zhang/data/my_web_project# ls env root@zhang-virtual-machine:/home/zhang/data/my_web_project# cd env/ root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# ls bin include lib pip-selfcheck.json

# 激活虚拟环境 root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# source bin/activate

#查看版本,查看到当前的环境下没有太多的包 (env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip -V pip 10.0.0 from /home/zhang/data/my_web_project/env/lib/python3.5/site-packages/pip (python 3.5) (env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip list Package Version ---------- ------- pip 10.0.0 setuptools 39.0.1 wheel 0.31.0

# 退出虚拟环境 (env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# deactivate root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# source bin/activate
# 安装1.10.4版本的Django
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip install django==1.10.4 Collecting django==1.10.4
# 安装uwsgi
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project/env# pip install uwsgi Collecting uwsgi

第一步:uwsgi

首先测试uwsgi,并用uwsgi启动服务

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello uwsgi!"]
刚开始按照网上说的用这个方法启动老是报错
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# python app.py 
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# uwsgi --http :8008 --wsfi-file app.py 
uwsgi: unrecognized option '--wsfi-file'
getopt_long() error

最后发现启动的方式默认是socket的方式
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# uwsgi --http-socket :8008 --wsgi-file app.py 
*** Starting uWSGI 2.0.17 (64bit) on [Tue Apr 17 19:05:22 2018] ***
compiled with version: 5.4.0 20160609 on 18 April 2018 01:30:18
os: Linux-4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017

pkill -f -9 uwsgi #停止uwsgi

第二步:nginx

# 直接在命令行下运行 apt-get install nginx
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# apt-get install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
nginx的启动与关闭
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# service nginx stop
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# ps aux | grep nginx
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# service nginx start
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# ps aux | grep nginx
root      55075  0.0  0.0 124968  1420 ?        Ss   19:24   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  55076  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
www-data  55077  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
www-data  55078  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
www-data  55079  0.0  0.1 125332  3072 ?        S    19:24   0:00 nginx: worker process
root      55082  0.0  0.0  14220   988 pts/20   S+   19:24   0:00 grep --color=auto nginx
(env) root@zhang-virtual-machine:/home/zhang/data/my_web_project# 

nginx的配置文件都在

# /etc/nginx/...
(env) root@zhang-virtual-machine:/etc/nginx# ls
conf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_params
fastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       win-utf

此时访问浏览器可以看到如下:

第三步:Django+uwsgi+nginx

nginx的配置文件所在路径:
/etc/nginx/nginx.conf
uwsgi 所在路径:项目更目录
启动项目:
uwsgi --ini demo.ini &    #利用配置文件启动,默认不在终端显示
/etc/init.d/nginx start     #启动nginx

附加:

supervisor是一个对进程管理的软件,可以帮助我们启动uwsgi并维护(uwsgi进程关闭时,自动将其启动起来)。

安装:apt-get install supervisor
配置 vim /etc/supervisor/supervisor.conf
[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
;http_port=127.0.0.1:9001  ; (alternately, ip_address:port specifies AF_INET)
;sockchmod=0700              ; AF_UNIX socketmode (AF_INET ignore, default 0700)
;sockchown=nobody.nogroup     ; AF_UNIX socket uid.gid owner (AF_INET ignores)
;umask=022                   ; (process file creation umask;default 022)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;http_username=user          ; (default is no username (open system))
;http_password=123           ; (default is no password (open system))
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;user=chrism                 ; (default is current user, required if root)
;directory=/tmp              ; (default is not to cd during start)
;environment=KEY=value       ; (key value pairs to add to environment)

[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat            ; the program (relative uses PATH, can take args)
;priority=999                ; the relative start priority (default 999)
;autostart=true              ; start at supervisord start (default: true)
;autorestart=true            ; retstart at unexpected quit (default: true)
;startsecs=10                ; number of secs prog must stay running (def. 10)
;startretries=3              ; max # of serial start failures (default 3)
;exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT             ; signal used to kill process (default TERM)
;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
;user=chrism                 ; setuid to this UNIX account to run the program
;log_stdout=true             ; if true, log program stdout (default true)
;log_stderr=true             ; if true, log program stderr (def false)
;logfile=/var/log/cat.log    ; child log path, use NONE for none; default AUTO
;logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
;logfile_backups=10          ; # of logfile backups (default 10)



[program:oldboy]
command=/usr/local/bin/uwsgi /data/oldboy/oldboy.ini ;命令
priority=999                ; 优先级(越小越优先)
autostart=true              ; supervisord启动时,该程序也启动
autorestart=true            ; 异常退出时,自动启动
startsecs=10                ; 启动后持续10s后未发生异常,才表示启动成功
startretries=3              ; 异常后,自动重启次数
exitcodes=0,2               ; exit异常抛出的是0、2时才认为是异常
stopsignal=QUIT             ; 杀进程的信号
stopwaitsecs=10             ; 向进程发出stopsignal后等待OS向supervisord返回SIGCHILD 的时间。若超时则supervisord将使用SIGKILL杀进程 
user=chrism                 ; 设置启动该程序的用户
log_stdout=true             ; 如果为True,则记录程序日志
log_stderr=false            ; 如果为True,则记录程序错误日志
logfile=/var/log/cat.log    ; 程序日志路径
logfile_maxbytes=1MB        ; 日志文件最大大小
logfile_backups=10          ; 日志文件最大数量
配置文件
启动:
supervisord /etc/supervisor.conf       #这个总是报错,不知道什么原因
或
/etc/init.d/supervisor start              #这个比较好用

# 通过配置文件启动
        uwsgi --ini uwsgi.ini           # 这样启动之后后台可以看到,不方便
     uwsgi --ini uwsgi.ini & # 建议这种,启动之后后台运行
# 会生成两个文件
            PID文件 他是标识这个程序所处的状态
            SOCK文件  他是用来和其他程序通信的
    # 停止uwsgi
        uwsgi --stop uwsgi.pid

    # 重载配置
        uwsgi --reload uwsgi.ini