nginx+uwsgi+django的搭建筆記


之前搭建過 Apache下的Django項目,存在的問題是admin讀寫數據庫無權限,一直沒解決?有了解的指導一下!!

 

今天測試了一下nginx下的django安裝

 

總的說來是比Apache下的配置簡單,是真簡單(Apache下的我忙活了一個星期,但是還是有個尾巴沒搞定。這個一個下午,幾乎參考了蟲師的就能配置起來)

(我的用的阿里雲,直接root安裝的,所以沒有sudo)

首先 安裝nginx:

apt-get install nginx

..................

遇到個問題,是沒有安裝成功,源找不到,於是 apt-get update后即可

修改Nginx默認端口號,打開/etc/nginx/nginx.conf 文件(此為主配置文件),修改/etc/nginx/sites-enabled/default 里的端口號(原來默認80,如果沒有被占用也沒關系)

而且 default -> /etc/nginx/sites-available/default (是個鏈接,所以如果想要自己創建個配置文件,就鏈接一下)

以下是我修改的default內容,把80改為8080

 

server {
   
 listen 8080 default_server;
listen [::]:8080 default_server;

}

 

然后啟動nginx后訪問  ip:8080,就能訪問nginx歡迎頁面了。

 

第二步 安裝uwsgi

 通過pip安裝uwsgi

root@ubuntu:/etc# python -m pip install uwsgi   #如果是py3,用 python3 。。。。。。。。。

 

編寫個測試腳本

# cat test.py 
def application(environ, start_response):  
    status = '200 OK'   
    output = 'Hello World! powerde by wsgi'  
    response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]  
    start_response(status, response_headers)
    return [output]

運行

uwsgi --http :8001 --wsgi-file test.py
然后瀏覽器訪問 http://127.0.0.1:8001
就可以訪問這個測試頁 顯示 Hello World! powerde by wsgi

測試訪問你的項目wsgi.py
 uwsgi --http :8001 --chdir /app/mysit/ --wsgi-file mysit/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

此處蟲師的寫法有問題,chdir 為project目錄    --wsgi-file 對應project下的同名目錄,再下面是wsgi.py,否則系統無法import,如下

*** Operational MODE: single process ***
failed to open python file mysit/wsgi.py
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***

 

常用選項:(照搬的)

常用選項:

http : 協議類型和端口號

processes : 開啟的進程數量

workers : 開啟的進程數量,等同於processes(官網的說法是spawn the specified number ofworkers / processes)

chdir : 指定運行目錄(chdir to specified directory before apps loading)

wsgi-file : 載入wsgi-file(load .wsgi file)

stats : 在指定的地址上,開啟狀態服務(enable the stats server on the specified address)

threads : 運行線程。由於GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)

master : 允許主進程存在(enable master process)

daemonize : 使進程在后台運行,並將日志打到指定的日志文件或者udp服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。(肯定要啟用,要不刷屏!!)

pidfile : 指定pid文件的位置,記錄主進程的pid號。   (生成pid文件,以便stop uwsgi)

vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)

 



第三步 配置Django

在django項目project目錄下(manage.py下)創建配置文件分兩種 ini 與xml
ini格式
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8081

# the
base directory (project full path) chdir = /app/mysit # Django s wsgi file module = mysit.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true # pidfile for record run pid pidfile =pid.uwsgi # run process background and save log to daemonize daemonize = UWSGI.log

使用命令  uwsgi --ini myweb  myweb_uwsgi.ini     ###在目錄下會生成pid及log文件

xml格式(搬的)(再加入pid)

 

#mytest_scoket.xml
<uwsgi> <socket>127.0.0.1:8099</socket> <chdir>/app/test_project</chdir> <module>test_project.wsgi</module> <processes>4</processes> <daemonize>uwsgi.log</daemonize> </uwsgi>

 

執行:uwsgi -x mytest_scoket.xml

 

以上格式2選1。

 

配置nginx配置文件,我還是修改的default文件!!! 如下

 

server {
    listen 8080;
    server_name www.wenxi.xin     #這個是什么都無所謂,只是一個名稱標記,蟲師說他的要寫成ip,這個應該不用,因為這個就相當於server ID,寫入log
    charset UTF-8;
    client_max_body_size 75M;
    location / {

        include uwsgi_params;      #加載uwsgi
        uwsgi_pass 127.0.0.1:8081;     #是uwsgi 啟動的socket端口, 即 myweb_uwsgi.ini 中的socket,應該也可以生成個socket文件,然后訪問這個文件!
        uwsgi_read_timeout 5;
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;        #這個要屏蔽,要不會報502錯誤,此uri是什么,還沒找到

    }
    location /static {          #指定靜態文件的別名,這個和Apache差不多

        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /app/mysit/static/;

        }
#如果有media,再加一項別名就洗

    # location /media/ {
      #       alias  /home/yixiang/Practice/Python/nginx_django/test_project/media/;
      #    }

}

 

 

 

 

配置完成后重啟服務,步驟包括

進入 myweb_uwsgi.ini 目錄

執行 uwsgi --ini myweb_uwsgi.ini

然后 service nginx restart 或者 start

 

然后訪問你的nginx對外服務的ip:8080 就到了你看到了你的django項目了!!!

 

原理,uwsgi 提供django 服務, nginx訪問uwsgi提供的服務,並將靜態文件路徑轉換,提供給客戶端,

當然了,這也是很淺顯的東西,uwsgi官方文檔很全面,如果精學需要看看

參考文檔:

官方文檔 http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#

參考了蟲師http://www.cnblogs.com/fnng/p/5268633.html

http://www.linuxidc.com/Linux/2016-07/133490.htm

歡迎訪問我的django(nginx)項目:http://60.205.221.253:8080/

----django(Apache)項目:http://60.205.221.253

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM