setup ubuntu_django_uwsgi_nginx

nginx 是一个免费的开源的http服务器,支持反向代理,以及支持IMAP/POP3代理服务器 下面是原理:

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

开始,首先安装uwsgi、nginx

pip install uwsgi
sudo apt-get install nginx
sudo /etc/init.d/nginx start

安装django和启动项目

pip install Django
django-admin.py startproject mysite
cd mysite

在django根目录下创建一个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 :8000 --wsgi-file test.py

http :8000:是服务器端口 wsgi-file test.py: 载入的文件

下载uwsgi_params 下载网址 uwsgi_params是nginx需要用到的文件, 创建一个website_nginx.conf文件

sudo vim nginx_zss.conf

下面是nginx_zss.conf代码

# mysite_nginx.conf

# the upstream component nginx needs to connect to
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)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

下面是我的配置

upstream zss {
    server unix:///home/zss/zss.sock; # 设置sock文件,用sock模式访问,
   # server 127.0.0.1:8002; # 用tcp/ip访问,2种模式都可以,我用的是上面的。
}
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name vanxv.vicp.net; # 域名
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/zss/media;  # 静态文件
    }

    location /static {
        alias /home/zss/static; # static文件
    }
    location / {
        uwsgi_pass  zss;
        include     /home/zss/uwsgi_params; # 指定params文件位置
    }
}

这个配置文件告诉nginx提供媒体和静态文件的文件系统,以及处理请求,要求Django的设置。对于大型部署它是一种不错的做法让一台服务器处理静态/媒体文件,另一个处理Django应用程序

sudo ln -s /home/zss/zss_nginx.conf /etc/nginx/sites-enabled/

连接 nginx/sites-enabled/ 让nginx可以看到这个文件

重启nginx

sudo nginx -s reload

创建 mysite_uwsgi.ini django 下面

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
#home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

下面是我的配置:

#ite_uwsgi.ini file
[uwsgi]
chdir           = /home/zss/
module          = zhess103.wsgi

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 6
# the socket (use the full path to be safe
socket          = /home/zss/zss.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
pidfile = /home/zss/zss.pid
disable-logging = true
daemonize = /var/log/zss_uwsgi.log

home = /path/to/virtualenv 模拟环境的网址,我的没用所以直接删了 chdir= /home/zss/ 项目网址 module= zhess103.wsgi django默认自带的wsgi配置文件 master 主线程 processes = 6 进程数, socket的地址 chmod-socket socket的权限

disable-logging = true daemonize = /var/log/zss_uwsgi.log log日志

pidfile = /home/zss/zss.pid 进程文件

测试与载入配置

sudo uwsgi --ini uwsgi_zss.ini
sudo uwsgi -d uwsgi_zss.ini

sudo uwsgi --ini uwsgi_zss.ini sudo vim uwsgi_zss.ini

关闭进程和重啓的方法 sudo uwsgi --stop zss.pid sudo uwsgi --ini uwsgi_zss.ini