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.comhtml

django+uwsgi的部署實在是太蛋疼了..網上已有的教程彷佛有新版本的兼容問題。最後跑到uwsgi官網上找的教程終於跑通了..
不過官網的教程彷佛有引導教學性質,部署的時候就顯得很繞彎路,在這裏記錄下來精簡內容python

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

~~首先,須要一個uwsgi_params文件,放在項目的conf文件夾裏面。以後須要指向它。~~
官網給的本身寫一個params的文件是沒有必要的,其餘例子中都用這句代替: include uwsgi_paramsweb

建立一個叫作ts_nginx.conf 的文件,內容以下django

設置中沒有使用sockets做內部傳輸,由於測試沒成功..多是權限問題,往後搞明白了能夠改一下.
location裏面官方給的例子是沒有root選項的,可是這會致使把端口改到80後沒法替換掉nginx的默認首頁(這破事兒折騰我好幾個小時..)加上就行了.app

# 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的搜索目錄裏面。dom

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/

第二行是我本身以爲應該加的..socket

先決條件:這裏要設置好django項目的settings裏面static files測試

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

and then runui

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文件.(注意!)

相關文章
相關標籤/搜索