Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署最简记录(精简自uwsgi官网教程)

Ubuntu 14.04 Python 2.7.6 Django 1.7.1 Virtualenv name:test Nginx uwsgi
假设 项目文件夹位于 /data/www/ts 设置保存在 ./conf
virtualenv name = test
domain name = example.com

django+uwsgi的部署实在是太蛋疼了..网上已有的教程似乎有新版本的兼容问题。最后跑到uwsgi官网上找的教程终于跑通了..
不过官网的教程似乎有引导教学性质,部署的时候就显得很绕弯路,在这里记录下来精简内容

http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

~~首先,需要一个uwsgi_params文件,放在项目的conf文件夹里面。之后需要指向它。~~
官网给的自己写一个params的文件是没有必要的,其他例子中都用这句代替: include uwsgi_params

创建一个叫做ts_nginx.conf 的文件,内容如下

设置中没有使用sockets作内部传输,因为测试没成功..可能是权限问题,日后搞明白了可以改一下.
location里面官方给的例子是没有root选项的,但是这会导致把端口改到80后无法替换掉nginx的默认首页(这破事儿折腾我好几个小时..)加上就好了.

# ts_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 80;
    # 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 /data/www/ts/media; # your Django project's media files - amend as required
    }

    location /static {
        alias /data/www/ts/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        root /data/www/ts
        uwsgi_pass django;
        include uwsgi_params; # the uwsgi_params file you installed
    }
}

把这个conf文件连接到nginx的搜索目录里面。

sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-enabled/
sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-available/

第二行是我自己觉得应该加的..

先决条件:这里要设置好django项目的settings里面static files

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

and then run

python manage.py collectstatic

之后:

service nginx restart

应该就可以看到
http://example.cn:8000/media/1.gif
了(事先放进去一个静态文件)

之后的blabla步骤都是废话(因为不用sockets),跳到这里:

Configuring uWSGI to run with a .ini file

ts_uwsgi.ini 在项目根目录

[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /data/www/ts
# Django's wsgi file
module = ts.wsgi
# the virtualenv (full path)
home = /root/.envs/test
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# set an environment variable
env = DJANGO_SETTINGS_MODULE=conf.settings 
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

这里环境变量设置env需要conf文件夹有init.py,否则conf不会被认为是module

项目目录下的同名app目录下的wsgi.py可以指定项目用uwsgi执行的settings文件.(注意!)

    原文作者:walterrwu
    原文地址: https://segmentfault.com/a/1190000001753213
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞