nginx部署django應用


  Django部署方式有很多種,之前寫過一篇部署在Apache上的博文:https://www.cnblogs.com/shenh/p/7552722.html 。下文介紹的是通過Nginx來部署。

  Nginx是一個高性能的HTTP和反向代理服務,運用非常廣泛。Django應用可以通過Nginx+uwsgi的方式進行部署。Nginx放置在服務器的最前端來接收所有web請求,統一管理,首先分離出靜態請求,自己做處理。然后,Nginx將非靜態請求通過uwsgi轉發給Django,由Django處理。

 

 一、安裝Nginx

  安裝Nginx命令

apt-get install Nginx

  Nginx的默認端口為80,很容易被其他服務占用,因此需要修改成其他端口。打開文件 vi /etc/nginx/nginx.conf ,如果能找到監聽的端口,直接修改 。找不到的也沒關系 ,找到如下配置 ,說明是引用了其他目錄的文件

...
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
...

  打開 /etc/nginx/sites-enabled/ ,找到並編輯 default ,將80修改成8088

# Default server configuration
#
server {
    listen 8088 default_server;
    listen [::]:8088 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

  Nginx啟動命令

service nginx start

service nginx restart

service nginx stop

  測試下,訪問  http://10.1.35.51:8088/

 

二、配置uwsgi

1.安裝  

  pip 安裝 uwsgi 命令:

pip install uwsgi

2.配置 uwsgi 和 Django 的連接  

  Django 項目路徑位於 /home/project/web/ ,在 Django 項目根目錄(manage.py 同級目錄)新建文件 uwsgi.ini (也支持 xml 文件格式)。在文件中添加如下內容:

# uwsgi.ini file
[uwsgi]

# Django-related settings
socket = :8000

# the base directory (full path)
chdir = /home/project/web

# Django s wsgi file
module = web.wsgi

# process-related settings
master = true

# maximum number of worker processes
processes = 5

#maximum number of worker threads
threads = 5
# try to remove all of the generated file/sockets
vacuum = true

3. 配置 uwsgi 和 Nginx 的連接

  修改Nginx配置文件:vi /etc/nginx/sites-available/default ,增加一段配置。然后重啟Nginx

server {
    listen         8099; 
    server_name    127.0.0.1 
    charset UTF-8;
    access_log      /var/log/nginx/web_access.log;
    error_log       /var/log/nginx/web_error.log;

    client_max_body_size 75M;

    location / { 
        include uwsgi_params;                   # 通過uwsgi轉發請求
        uwsgi_pass 127.0.0.1:8000;              # 和上文配置的socket端口保持一致
        uwsgi_read_timeout 15;                  # 設置請求超時時間
    }   
    location /static {                          # 訪問靜態資源
        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /var/web/;
     }
 }

4.配置 Django 靜態資源

  在setting里增加配置,靜態資源 路徑 和 Nginx 里的訪問路徑一致

STATIC_ROOT = os.path.join(BASE_DIR,'/var/web')

  執行命令

Python manage.py collectstatic

5.最后啟動項目

cd /home/project/web
uwsgi --ini uwsgi.ini

運行成功:

(env35) root@ubuntu:/home/project/ShiHangTool# uwsgi --ini ShiHangTool_uwsgi.ini
[uWSGI] getting INI configuration from ShiHangTool_uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Tue Dec 11 17:42:10 2018] ***
compiled with version: 5.4.0 20160609 on 10 December 2018 08:38:05
os: Linux-4.4.0-139-generic #165-Ubuntu SMP Wed Oct 24 10:58:50 UTC 2018
nodename: ubuntu
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /home/project/ShiHangTool
detected binary path: /root/.virtualenvs/env35/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /home/project/ShiHangTool
your processes number limit is 7764
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.5.2 (default, Nov 12 2018, 13:43:14)  [GCC 5.4.0 20160609]
Python main interpreter initialized at 0x1504fe0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 687984 bytes (671 KB) for 25 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1504fe0 pid: 5194 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 5194)
spawned uWSGI worker 1 (pid: 5196, cores: 5)
spawned uWSGI worker 2 (pid: 5197, cores: 5)
spawned uWSGI worker 3 (pid: 5202, cores: 5)
spawned uWSGI worker 4 (pid: 5207, cores: 5)
spawned uWSGI worker 5 (pid: 5208, cores: 5)

 

訪問 http://10.1.35.51:8099/Home/OrderSettle-K8S/ 

 

 


免責聲明!

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



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