Django項目採用Nginx+uwsgi發佈在Ubuntu(採坑完畢,足夠詳細)

一.安裝Nginx

1.1打開ubuntu控制檯(ctrl+alt+t)利用Ubuntu的倉庫安裝。

sudo apt-get install nginx  #安裝

1.2啓動Nginx:

 /etc/init.d/nginx start  #啓動

 /etc/init.d/nginx stop  #關閉

 /etc/init.d/nginx restart  #重啓

1.3修改Nginx默認端口號,打開/etc/nginx/nginx.conf 文件,修改端口號。

在http標籤下添加如下代碼

server{

                 listen 8080;

                 server_name loaclhost;

                 location / {

                        root html;

                        index index.html index.htm;

                 }

 }

1.4測試是否成功

啓動nginx

 /etc/init.d/nginx start  #啓動

瀏覽器輸入ip:8080

 

2.安裝uwsgi

2.1通過pip安裝uwsgi。

python -m pip install uwsgi

2.2測試uwsgi,在/home/chengxun創建test.py文件:

def application(env, start_response):

    start_response('200 OK', [('Content-Type','text/html')])

    return [b"Hello World"]

2.3通過uwsgi運行該文件。

/home/chengxun        執行uwsgi --http :8001 --wsgi-file test.py

出現這個錯誤,網上找了好多,發現以下解決方案適合我:

找到uwsgi執行位置,建立軟鏈接

 1.查找出所有uwsgi文件的路徑

find / -name uwsgi

2.篩選有效的uwsgi,每個路徑執行 --plugins-list  看結果條數

/var/lib/dpkg/alternatives/uwsgi  --plugins-list  #看結果條數
/var/lib/nginx/uwsgi  --plugins-list  #看結果條數
/etc/alternatives/uwsgi  --plugins-list  #看結果條數
/usr/bin/python3.7/bin/uwsgi  --plugins-list  #看結果條數
/usr/bin/uwsgi  --plugins-list  #看結果條數
/usr/lib/uwsgi    --plugins-list  #看結果條數

3.有效的uwsgi如下:
*** uWSGI loaded generic plugins ***
gevent
nagios
rrdtool
carbon
corerouter
fastrouter
http
ugreen
syslog
rsyslog
logsocket
router_uwsgi
router_redirect
router_basicauth
zergpool
redislog
mongodblog
router_rewrite
router_http
logfile
router_cache
rawrouter
router_static
sslrouter
cheaper_busyness
transformation_tofile
transformation_gzip
transformation_chunked
transformation_offload
router_memcached
router_redis
router_hash
router_expires
router_metrics
transformation_template
stats_pusher_socket

*** uWSGI loaded request plugins ***
0: python
17: spooler
18: symcall
100: ping
110: signal
111: cache
173: rpc
--- end of plugins list ---

我發現/usr/bin/python3.7/bin/uwsgi下的uwsgi是有效的

4.建立軟鏈接

如果存在/usr/bin/uwsgi,那麼久刪除。接下來執行下面命令,以後輸入uwsgi就是調用/usr/bin/python3.7/bin/uwsgi

 ln -s /usr/bin/python3.7/bin/uwsgi  /usr/bin/uwsgi

 

2.4瀏覽器輸入 ip:8001

 

2.5接下來配置Django與uwsgi連接。

此處,假定的我的django項目位置爲:/home/project/hxuner

執行下面命令:

uwsgi --http :8001 --chdir /home/project/hxuner

--wsgi-file hxuner/wsgi.py

--master --processes 4 --threads 2 --stats 127.0.0.1:9191

 

常用選項:

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號。

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

 

瀏覽器輸入ip:8001,但是沒有加載靜態文件。

三、Nginx+uwsgi+Django

3.1接下來,我們要將三者結合起來。首先羅列一下項目的所需要的文件:

hxuner/

├── manage.py

├── hxuner/

│   ├── __init__.py

│   ├── settings.py

│   ├── urls.py

│   └── wsgi.py

└── hxuner_uwsgi.ini


在我們通過Django創建hxuner項目時,在子目錄hxuner下已經幫我們生成的 wsgi.py文件。所以,我們只需要再創建hxuner_uwsgi.ini配置文件即可,當然,uwsgi支持多種類型的配置文件,如xml,ini等。此處,使用ini類型的配置。

 

# hxuner_uwsgi.ini file

[uwsgi]

# Django-related settings

socket = :8000

# the base directory (full path)

chdir           = /home/project/hxuner

# Django s wsgi file

module          = hxuner.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

 

這個配置,其實就相當於在上一小節中通過uwsgi命令,後面跟一堆參數的方式,給文件化了。

  socket  指定項目執行的端口號。

  chdir   指定項目的目錄。

  module hxuner.wsgi ,可以這麼來理解,對於hxuner_uwsgi.ini文件來說,與它的平級的有一個hxuner目錄,這個目錄下有一個wsgi.py文件。

  其它幾個參數,可以參考上一小節中參數的介紹。

 

3.2接下來,切換到hxuner項目目錄下,通過uwsgi命令讀取hxuner_uwsgi.ini文件啓動項目。

 cd /home/project/hxuner

 uwsgi --ini hxuner_uwsgi.ini

注意查看uwsgi的啓動信息,如果有錯,就要檢查配置文件的參數是否設置有誤。

 

3.3再接下來要做的就是修改nginx.conf配置文件。打開/etc/nginx/nginx.conf文件,添加如下內容。

server {

    listen         80;

    server_name    127.0.0.1

    charset UTF-8;

    access_log      /var/log/nginx/hxuner_access.log;

    error_log       /var/log/nginx/hxuner_error.log;

 

    client_max_body_size 75M;

 

    location / {

        include uwsgi_params;

        uwsgi_pass 127.0.0.1:8000;

        uwsgi_read_timeout 2;

    }  

    location /static {

        expires 30d;

        autoindex on;

        add_header Cache-Control private;

        alias /home/project/hxuner/static/;

     }

 }

       listen 指定的是nginx代理uwsgi對外的端口號。

  server_name  網上大多資料都是設置的一個網址(例,www.example.com),我這裏如果設置成網址無法訪問,所以,指定的到了本機默認ip。

  在進行配置的時候,我有個問題一直想不通。nginx到底是如何uwsgi產生關聯。現在看來大概最主要的就是這兩行配置。

  include uwsgi_params;

  uwsgi_pass 127.0.0.1:8000;

  include 必須指定爲uwsgi_params;而uwsgi_pass指的本機IP的端口與hxuner_uwsgi.ini配置文件中的必須一致。

 

3.4啓動nginx

/etc/init.d/nginx start  #啓動

3.5瀏覽器輸入ip就行,因爲80默認

發現原來80端口已經被nginx歡迎頁佔用

解決方案:

cd /etc/nginx/sites-enables/

vim defalut

將80改爲用不到的端口

 

3.6現在瀏覽器輸入ip就可以直接訪問

 

四 總結 Nginx+uwsgi+django項目部署

以後在一臺環境全部安裝完成的機器上,並且python manage.py runserver 0.0.0.0:port運行起來之後,只需4步就可以通過nginx+uwsgi部署django項目。

4.1在項目的主目錄下新建  項目名_uwsgi.ini,

內容如下:

# hxuner_uwsgi.ini file

[uwsgi]

# Django-related settings

socket = :8000

# the base directory (full path)

chdir           = /home/project/hxuner

# Django s wsgi file

module          = hxuner.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

 

2.切換到項目目錄下,通過uwsgi命令讀取

項目名_uwsgi.ini文件啓動項目。

 cd /home/project/項目名

 uwsgi --ini  項目名_uwsgi.ini

 

3.配置nginx.conf

vim  /etc/nginx/nginx.conf

 

server {

    listen         80;

    server_name    127.0.0.1

    charset UTF-8;

    access_log      /var/log/nginx/hxuner_access.log;

    error_log       /var/log/nginx/hxuner_error.log;

    client_max_body_size 75M;

    location / {

        include uwsgi_params;

        uwsgi_pass 127.0.0.1:8000;

        uwsgi_read_timeout 2;

    }  

    location /static {

        expires 30d;

        autoindex on;

        add_header Cache-Control private;

        alias /home/project/hxuner/static/;

     }

 }

 

4.啓動nginx

/etc/init.d/nginx start  #啓動

 

5.瀏覽器輸入nginx.conf配置的listen端口

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章