国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Nginx+uWSGI或fastcgi部署Django項目

2019-11-14 17:36:28
字體:
來源:轉載
供稿:網友

nginx+uWSGI

Ubuntu下先安裝下C編譯器和Python環境:

sudo apt-get install build-essential python-dev

使用pip安裝uWSGI:

pip install uwsgi

nginx配置:

可以單獨為站點設置一個配置文件:

sudo vim /etc/nginx/sites-enabled/mysite

或者直接在nginx.conf中設置:

sudo vim /etc/nginx/nginx.conf

設置:

server {    listen   80; ## listen for ipv4; this line is default and implied    #listen   [::]:80 default ipv6only=on; ## listen for ipv6     server_name 站點域名;    location / {            uwsgi_pass 127.0.0.1:8080;            include uwsgi_params;    }    #設置該網站應用中所需要的靜態文件的根目錄,需要將admin和用到的第三方庫像restframework的靜態文件都放到此目錄中     location ~/static/ {            root   /home/user/mysite/;  #項目地址           # root  html;           # index  index.html index.htm;            break;        }        #設置媒體文件的根目錄       location ~/media/ {            root   /home/user/mysite/;           # root  html;           # index  index.html index.htm;            break;        }}

自己電腦上搭建localhost服務器時,注意別被/etc/nginx/sites-enabled/default中的配置覆蓋掉了,最好將其注釋掉。

然后設置uWSGI,建立文件myfile.ini:

[uwsgi]socket = 127.0.0.1:8080   #與nginx配置中的uwsgi_pass相同chdir = /home/user/mysite/    #項目地址wsgi-file = mysite/wsgi.pyPRocesses = 4threads = 2pidfile=/tmp/project-master.pidstats = 127.0.0.1:9191

其中wsgi-file是django(1.4版本以上)項目自動建立的文件,里面內容為:

import osos.environ.setdefault("DJANGO_SETTINGS_MODULE", "weixian.settings")from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()

如果django版本過低無此文件的話,可以自己建立,或者在myfile.ini中設置env,module,pythonpath:

[uwsgi]socket = 127.0.0.1:8080chdir = /home/user/mysite/pythonpath = ..env = DJANGO_SETTINGS_MODULE=mysite.settingsmodule = django.core.handlers.wsgi:WSGIHandler()processes = 4threads = 2stats = 127.0.0.1:9191

按配置重啟nginx:

/usr/sbin/nginx -s reload

或者:

killall -9 nginxnginx -c /etc/nginx/nginx.conf

啟動uWSGI:

uwsgi myfile.ini

重啟uWSGI:

# using kill to send the signalkill -HUP `cat /tmp/project-master.pid`# or the convenience option --reloaduwsgi --reload /tmp/project-master.pid# or if uwsgi was started with touch-reload=/tmp/somefiletouch /tmp/somefile

ini文件也可以用xml文件來設置。

添加xml支持:

sudo apt-get install libxml2-dev

配置myfile.xml

<uwsgi>    <socket>127.0.0.1:8080</socket>    <chdir>/home/user/mysite/</chdir>    <module>mysite/wsgi</module></uwsgi>

啟動:

uwsgi -x myfile.xml

nginx+fastcgi

fastcgi需要安裝flup:

wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gztar zxvf flup-1.0.2.tar.gzcd flup-1.0.2python setup.py install

或者:

sudo apt-get install  python-flup

nginx配置:

server {        listen       80;        server_name  localhost;        #設置該網站應用中所需要的靜態文件的根目錄        location ~/static/ {            root   /home/user/mysite/;           # root  html;           # index  index.html index.htm;            break;        }        #設置媒體的根目錄        location ~/media/ {            root   /home/user/mysite/;           # root  html;           # index  index.html index.htm;            break;        }        #host and port to fastcgi server         location / {        fastcgi_pass 127.0.0.1:8080;        fastcgi_param PATH_INFO $fastcgi_script_name;        fastcgi_param REQUEST_METHOD $request_method;        fastcgi_param QUERY_STRING $query_string;        fastcgi_param CONTENT_TYPE $content_type;        fastcgi_param CONTENT_LENGTH $content_length;        fastcgi_param pass_header Authorization;        fastcgi_intercept_errors off;         }        #設置瀏覽器緩存這些圖片格式文件瀏覽器緩存時間是30天,CSS/js緩存時間1小時        location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$        {            expires 30d;        }        location ~ .*/.(js|css)?$        {            expires 1h;        }}

重啟nginx,然后啟動fcgi:

python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork --settings=mysite.settings  #采用靜態進程池的話性能會比動態線程生成高2倍左右

ok。

要更新django項目的話,

ps -ef |grep fcgi 

找出主進程號kill掉,再重啟fcgi即可。

也可以寫個重啟腳本。使用pidfile選項將fcgi的pid存儲到文件中,重啟時讀取kill掉。

#!/bin/bashPROJDIR="/home/user/myproject"PIDFILE="$PROJDIR/mysite.pid"cd $PROJDIRif [ -f $PIDFILE ]; then    kill `cat -- $PIDFILE`    rm -f -- $PIDFILEfiexec python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork pidfile=$PIDFILE --settings=mysite.settings

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇平县| 苗栗县| 遂宁市| 改则县| 彭阳县| 宾川县| 朝阳市| 应用必备| 榆社县| 乐都县| 佛山市| 金门县| 边坝县| 泰安市| 武定县| 禹城市| 辛集市| 古丈县| 张北县| 莒南县| 苍南县| 赣榆县| 西华县| 齐河县| 四平市| 唐海县| 巫山县| 东阳市| 棋牌| 大方县| 获嘉县| 龙井市| 沙田区| 建宁县| 河北省| 万安县| 高密市| 来凤县| 新邵县| 武宁县| 浦东新区|