阿里雲ECS伺服器部署django的方法
參考
伺服器安裝的是Centos 系統。
uwsgi是使用pip安裝的。
nginx是使用yum install nginx安裝。
python 2.7, mysql 5.5使用 yum安裝。
它們之間的邏輯關係如下:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
uswgi負責從Django拿內容,通過socket傳給 web server如nginx, 最後顯示到 網頁瀏覽器。
在django的專案下,建檔案 uswgi.ini,可以不用在uswgi後面寫一串選項。
# uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /var/www/html/ # Django's wsgi file module = app.wsgi:application # 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 socket = /tmp/site.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true process = 4 threads = 2
# Django's wsgi file這個對應你自己Django專案的就好。 chdir就是Django的所在目錄,和manage.py同一目錄。
其他可以預設。
同樣建立nginx.conf
# nginx.conf # the upstream component nginx needs to connect to upstream django { server unix:///tmp/site.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 demo.mmm.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 128M; # adjust to taste # Django media location /media { alias /var/www/html/media; # your Django project's media files - amend as required } location /static { alias /var/www/html/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 /var/www/html/uwsgi_params; # the uwsgi_params file you installed } }
uwsgi_pass django; 中的django和upstream django 相對應。
兩頭的socket名字要一樣。uwsgi裡要改sock的許可權為666,預設的664,nginx會連不上,在/var/log/nginx/error.log裡可以看到connect is denied。
據說使用socket比埠要好,注意unix://這個字首,加上後面sock的路徑,是3個///,看起來不好看。
無論使用socket還是TCP埠,uwsgi的socket和nginx的server值要對應,否則沒法接通路徑。
server_name demo.mmm.com; 看文章時,把server_name這個詞看成域名,給修改掉,結果nginx啟動失敗。可以用域名或者IP。
ln -s /var/www/html/nginx.conf /etc/nginx/conf.d/
連結後,這樣在conf.d 配置目錄裡會有Django下建立的nginx.conf,比較方便。
uwsgi_params檔案在/etc/nginx下面有,老外說是拷貝到Django目錄下,不知道直接使用會有什麼區別。
最後:
使用chkconfig nginx on 把nginx設定成自啟動服務。
在/etc/rc.local里加一行 uwsgi /var/www/html/uwsgi.ini --uid www --gid www
我沒加uid和gid,以root執行uwsgi會被警告的。
原來是打算用apache的,所以有個/var/www/html目錄。mod-python報錯後,不知道怎麼處理。
系統自帶Python2.6,mod-python就是呼叫的2.6。
nginx不能從uwsgi獲得資料時,就會輸出nginx的預設頁面。還會輸出 Bad Gateway提示。
linux最大的麻煩是,程式和配置檔案分散,裝好一個程式,都不知道它在哪裡。
以上這篇阿里雲ECS伺服器部署django的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。