文章目錄
- 1.看圖說話
- 2.開始部署django
- 3.遇到的坑
- 4.宣告
#前幾天按照檔案走了一道,也記錄了一下操作程序,其實挖了很多坑,沒走通,又捯飭了幾天終于在ubuntu成功運行了django+Nginx,記錄如下
1.看圖說話

- 首先使用django web開發框架(因為我不會html,css,js等,所以有個好用的開發框架將會開發簡單,部署也簡單)
- django使用python作為開發語言,開發完畢作為web應用程式運行在web服務器上,但是我們選擇的web服務器例如Nginx,uWSGI都不認識python,所以web應用程式django要和web服務器進行互動則需要通信標準wsgi
- uWSGI是一種web服務器,它實作了WSGI協議、uwsgi、http等協議,我們選擇uWSGI服務器運行django,他們之間使用wsgi協議通信,
- 同時為了提高網頁的回應效率,有人采用網頁的動靜分析措施,比如利用Nguinx服務器處理/回應靜態網頁,uWSGI服務器回應動態網頁,(我僅僅是聽說)
- 為了達到動靜分離的效果,我又安裝了nginx,nginx與uWSGI兩個web服務器協同作業則需要通信,他們的通信可以采用socket實作(這兩個運行在服務器上的web服務器其實也就是兩個運行程式嘛,他們之間相互呼叫/通信采用一種方法稱為socket)
- 客戶端,即用戶的瀏覽器方位服務器端時,通過http協議與nginx通信,nginx處理靜態請求/回應,然后動態請求/回應交給uWSGI,uWSGI呼叫python程式(django)處理請求,回傳處理結果,
2.開始部署django
step1:
#安裝django web開發框架
#安裝conda的用戶主要python和pip對應起來,文中的python和pip3皆是/usr/bin中的可執行程式
#python和pip對應錯誤可能出現呼叫包錯誤的現象
pip3 install django
#驗證是否安裝成功
django-admin.py startproject mysite #創建專案mysite
cd mysite #進入專案檔案
/usr/bin/python3 runserver 0.0.0.0:8000 #8000埠運行django的測驗檔案,瀏覽器打開回傳的地址會出現小火箭
#安裝uWSGI web服務器
pip3 install uwsgi
#驗證uwsgi是否安裝成功
vim test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
uwsgi --http :8000 --wsgi-file test.py #8000埠運行test測驗檔案,瀏覽器打開回傳的地址會出現hello world
#安裝postgresql資料庫的以及資料庫操作介面
#安裝資料庫
sudo apt-get install postgresql postgresql-client #安裝完畢后,系統會創建一個資料庫超級用戶 postgres,密碼為空
pip3 install psycopg2-binary
#驗證psycopg2是否安裝成功
/usr/bin/python3
>>>import psycopg2
>>>dir(psycopg2)
#安裝nginx web服務器
#nginx負責靜態web頁面處理,uWSGI負責動態頁面處理,實作動靜分離,提高頁面回應效率
sudo apt-get install nginx #安裝成功nginx會自動啟用
#檢查nginx
sudo service nginx status
#或者訪問瀏覽器出現歡迎界面
127.0.0.1:80
#激活或者stop、restart nginx
sudo sudo /etc/init.d/nginx start|stop|restart
#修改nginx的組態檔,具體請看《遇到的坑那一節》
#假設修改害了nginx的組態檔
cd mysite #進入mysite頂級專案檔案夾
#
uwsgi --http :8000 --module mysite.wsgi #測驗uwsgi與nginx是否可以聯用,如果可以恭喜你,你實作了上面的圖片內容
#推薦使用ini組態檔運行uwsgi
vim mysite_uwsgi.ini
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/mysite
# Django's wsgi file
module = mysite.wsgi:application
# the virtualenv (full path) (optional)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# the socket (use the full path to be safe
socket = 127.0.0.1:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# background the process
daemonize=/path/to/mysite/mysite.log #后臺運行uwsgi服務器
pidfile=/path/to/mysite/mysite.pid #儲存uwsgi運行時的pid,可以用于終止uwsgi服務器
#啟動web服務器
uwsgi --ini /path/to/mysite_uwsgi.ini
#查看服務
ps -aux |grep "uwsgi"
#訪問你自己的專案
127.0.0.1:8000 #注意我們設定nginx監聽的埠是8000,而8001埠是nginx與uWSGI通信的埠
#停止
uwsgi --stop /path/to/mysite/mysite.pid
或者
sudo pkill -f uwsgi -9
3.遇到的坑
- apache2與nginx重復安裝,重復卸載,最后兩個web服務器懂啟動不了,好吧!最后卸載apache2,apt安裝nginx
$ which nginx
/usr/sbin/nginx #我也不知道為啥到sbin里面去了
$ sudo /usr/sbin/nginx -t #測驗nginx的組態檔等是否正確,而且會告訴你組態檔位置
$ sudo /usr/sbin/nginx #啟動nginx
$ ps -aux |grep "nginx" #驗證nginx啟動成功
$ sudo /usr/sbin/nginx -s stop #顯將nginx停了,我們要去修改組態檔
- nginx組態檔的坑
$ sudo /usr/sbin/nginx -t 告訴我nginx組態檔在/etc/nginx/conf.d/*.conf ;有些人的組態檔在/etc/nginx/sites-enabled/檔案夾里
#如果組態檔在/etc/nginx/sites-enbled,操作如下
#create /etc/nginx/sites-available/ directory and put this in it
#sites-available檔案夾包括一些可組態檔
#sites-enabled檔案夾包括一些已經配置了的檔案
#最好不要修改nginx.conf檔案
#在sites-available檔案夾創建mysite_nginx.conf檔案并寫入如下內容
---------------------------------------------------------------------------------------------------------
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first) nginx與uWSGI通信使用8001埠
}
# configuration of the server
server {
# the port your site will be served on
listen 8000; #nginx監聽8000埠
# the domain name it will serve for
server_name 172.XXX.XXX.XXX; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/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 /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed #/etc/nginx/uwsgi_params;
}
}
----------------------------------------------------------------------------------------------------------------------------
#symlink to this file from /etc/nginx/site-enabled so nginx can see it
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/
#重啟nginx服務
sudo /etc/init.d/nginx restart 或者 sudo /usr/sbin/nginx
ps -aux |grep nginx
#如果組態檔在/etc/nginx/conf.d/,操作如下
-----------------------------------------------------------------------------------------------------
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first) nginx與uWSGI通信使用8001埠
}
# configuration of the server
server {
# the port your site will be served on
listen 8000; #nginx監聽8000埠
# the domain name it will serve for
server_name 172.XXX.XXX.XXX; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/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 /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed #/etc/nginx/uwsgi_params;
}
}
----------------------------------------------------------------------------------------------------------------------------
#symlink to this file from /etc/nginx/conf.d/ so nginx can see it
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/conf.d/
#重啟nginx服務
sudo /etc/init.d/nginx restart 或者 sudo /usr/sbin/nginx
ps -aux |grep nginx
- nginx歡迎頁面一直存在
#方法一
sudo rm /etc/nginx/sites-enabled/default.conf
#方法二
修改default.conf中監聽的埠,讓默認監聽的埠與剛才mysite-nginx.conf監聽的埠不一樣
- uwsgi安裝失敗
有沒有安裝python-dev包?
- uwsgi呼叫失敗
pip 是不是使用的conda安裝的?
4.宣告
純粹用于記錄,交流學習也是可行的,沒有任何教學目的,請根據自己的實際情況操作自己的系統,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/345610.html
標籤:其他
