阿里云ECS服務(wù)器部署django的方法
服務(wù)器安裝的是Centos 系統(tǒng)。
uwsgi是使用pip安裝的。
nginx是使用yum install nginx安裝。
python 2.7, mysql 5.5使用 yum安裝。
它們之間的邏輯關(guān)系如下:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
uswgi負(fù)責(zé)從Django拿內(nèi)容,通過(guò)socket傳給 web server如nginx, 最后顯示到 網(wǎng)頁(yè)瀏覽器。
在django的項(xiàng)目下,建文件 uswgi.ini,可以不用在uswgi后面寫一串選項(xiàng)。
# 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這個(gè)對(duì)應(yīng)你自己Django項(xiàng)目的就好。 chdir就是Django的所在目錄,和manage.py同一目錄。
其他可以默認(rèn)。
同樣建立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 相對(duì)應(yīng)。
兩頭的socket名字要一樣。uwsgi里要改sock的權(quán)限為666,默認(rèn)的664,nginx會(huì)連不上,在/var/log/nginx/error.log里可以看到connect is denied。
據(jù)說(shuō)使用socket比端口要好,注意unix://這個(gè)前綴,加上后面sock的路徑,是3個(gè)///,看起來(lái)不好看。
無(wú)論使用socket還是TCP端口,uwsgi的socket和nginx的server值要對(duì)應(yīng),否則沒(méi)法接通路徑。
server_name demo.mmm.com; 看文章時(shí),把server_name這個(gè)詞看成域名,給修改掉,結(jié)果nginx啟動(dòng)失敗??梢杂糜蛎蛘逫P。
ln -s /var/www/html/nginx.conf /etc/nginx/conf.d/
鏈接后,這樣在conf.d 配置目錄里會(huì)有Django下建立的nginx.conf,比較方便。
uwsgi_params文件在/etc/nginx下面有,老外說(shuō)是拷貝到Django目錄下,不知道直接使用會(huì)有什么區(qū)別。
最后:
使用chkconfig nginx on 把nginx設(shè)置成自啟動(dòng)服務(wù)。
在/etc/rc.local里加一行 uwsgi /var/www/html/uwsgi.ini --uid www --gid www
我沒(méi)加uid和gid,以root運(yùn)行uwsgi會(huì)被警告的。
原來(lái)是打算用apache的,所以有個(gè)/var/www/html目錄。mod-python報(bào)錯(cuò)后,不知道怎么處理。
系統(tǒng)自帶Python2.6,mod-python就是調(diào)用的2.6。
nginx不能從uwsgi獲得數(shù)據(jù)時(shí),就會(huì)輸出nginx的默認(rèn)頁(yè)面。還會(huì)輸出 Bad Gateway提示。
linux最大的麻煩是,程序和配置文件分散,裝好一個(gè)程序,都不知道它在哪里。
以上這篇阿里云ECS服務(wù)器部署django的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python算法學(xué)習(xí)雙曲嵌入論文代碼實(shí)現(xiàn)數(shù)據(jù)集介紹
由于雙曲嵌入相關(guān)的文章已經(jīng)有了一系列的代碼。本篇博客主要目的實(shí)現(xiàn)最開始的雙曲嵌入論文,將論文中有些直接寫出來(lái)的內(nèi)容進(jìn)行了細(xì)節(jié)的推導(dǎo),同時(shí)實(shí)現(xiàn)對(duì)應(yīng)的代碼2021-11-11
Python學(xué)習(xí)筆記之Zip和Enumerate用法實(shí)例分析
這篇文章主要介紹了Python學(xué)習(xí)筆記之Zip和Enumerate用法,結(jié)合實(shí)例形式分析了Zip和Enumerate的功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08
django和vue實(shí)現(xiàn)數(shù)據(jù)交互的方法
今天小編就為大家分享一篇django和vue實(shí)現(xiàn)數(shù)據(jù)交互的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實(shí)現(xiàn)
這篇文章主要介紹了Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
python正則實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了python正則實(shí)現(xiàn)計(jì)算器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
django的autoreload機(jī)制實(shí)現(xiàn)
這篇文章主要介紹了django的autoreload機(jī)制實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Python將多個(gè)excel文件合并為一個(gè)文件
這篇文章主要為大家詳細(xì)介紹了Python將多個(gè)excel文件合并為一個(gè)文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
python線程定時(shí)器Timer實(shí)現(xiàn)原理解析
這篇文章主要介紹了python線程定時(shí)器Timer實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Python Pillow庫(kù)詳細(xì)介紹與代碼示例
Python Pillow 庫(kù)是一個(gè)強(qiáng)大的圖像處理工具,是經(jīng)典 PIL(Python Imaging Library)庫(kù)的現(xiàn)代分支,以下是對(duì) Pillow 庫(kù)的全面講解和完整代碼示例,需要的朋友可以參考下2025-04-04

