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