阿里云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)容,通過socket傳給 web server如nginx, 最后顯示到 網(wǎng)頁瀏覽器。
在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è)對應(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 相對應(yīng)。
兩頭的socket名字要一樣。uwsgi里要改sock的權(quán)限為666,默認(rèn)的664,nginx會(huì)連不上,在/var/log/nginx/error.log里可以看到connect is denied。
據(jù)說使用socket比端口要好,注意unix://這個(gè)前綴,加上后面sock的路徑,是3個(gè)///,看起來不好看。
無論使用socket還是TCP端口,uwsgi的socket和nginx的server值要對應(yīng),否則沒法接通路徑。
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下面有,老外說是拷貝到Django目錄下,不知道直接使用會(huì)有什么區(qū)別。
最后:
使用chkconfig nginx on 把nginx設(shè)置成自啟動(dòng)服務(wù)。
在/etc/rc.local里加一行 uwsgi /var/www/html/uwsgi.ini --uid www --gid www
我沒加uid和gid,以root運(yùn)行uwsgi會(huì)被警告的。
原來是打算用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)頁面。還會(huì)輸出 Bad Gateway提示。
linux最大的麻煩是,程序和配置文件分散,裝好一個(gè)程序,都不知道它在哪里。
以上這篇阿里云ECS服務(wù)器部署django的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一個(gè)計(jì)算身份證號碼校驗(yàn)位的Python小程序
閑著無事,便想寫個(gè)實(shí)用點(diǎn)的Python小程序,如何計(jì)算機(jī)身份證號碼的校驗(yàn)位,這樣的文章在網(wǎng)上一抓一大把,這里僅簡單介紹下吧2014-08-08
Django中的DateTimeField和DateField實(shí)現(xiàn)
這篇文章主要介紹了Django中的DateTimeField和DateField實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Windows下的Jupyter Notebook 安裝與自定義啟動(dòng)(圖文詳解)
這篇文章主要介紹了Windows下的Jupyter Notebook 安裝與自定義啟動(dòng)(圖文詳解),需要的朋友可以參考下2018-02-02
Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫連接池
這篇文章主要介紹了Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫連接池,Python 連接 PostgreSQL 是主要有兩個(gè)包, py-postgresql 和 psycopg2 , 而本文的實(shí)例將使用后者,感興趣的小伙伴可以參考一下2021-10-10
Python自動(dòng)化之?dāng)?shù)據(jù)驅(qū)動(dòng)讓你的腳本簡潔10倍【推薦】
數(shù)據(jù)驅(qū)動(dòng)是一種思想,讓數(shù)據(jù)和代碼進(jìn)行分離。這篇文章主要介紹了Python自動(dòng)化之?dāng)?shù)據(jù)驅(qū)動(dòng),讓你的腳本簡潔10倍,需要的朋友可以參考下2019-06-06
python對象轉(zhuǎn)字典的兩種實(shí)現(xiàn)方式示例
這篇文章主要介紹了python對象轉(zhuǎn)字典的兩種實(shí)現(xiàn)方式,結(jié)合實(shí)例形式分析了Python字典與對象數(shù)據(jù)類型轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
詳解Python如何在多層循環(huán)中使用break/continue
關(guān)于break/continue這兩個(gè)關(guān)鍵字在平常的使用過程中一直比較迷糊。所以本文將詳細(xì)講講Python如何在多層循環(huán)中使用break/continue,需要的可以參考一下2022-05-05

