搜尋
首頁運維Nginx基於ubuntu怎麼透過Nginx部署Django

一、安裝nginx                                            

nginx是一款輕量的web 伺服器/反向代理伺服器及電子郵件(imap/pop3)代理伺服器,並在一個bsd-like 協議下發行。其特點是佔有記憶體少,並發能力強,事實上nginx的並發能力確實在同類型的網頁伺服器中表現較好。

nginx同樣為目前非常流行的網頁伺服器。利用其部署django,我們在這裡也做簡單的介紹。

nginx官網:

開啟ubuntu控制台(ctrl alt t)利用ubuntu的倉庫安裝。

fnngj@ubuntu:~$ sudo apt-get install nginx #安装

啟動nginx:

fnngj@ubuntu:~$ /etc/init.d/nginx start #启动
fnngj@ubuntu:~$ /etc/init.d/nginx stop #关闭
fnngj@ubuntu:~$ /etc/init.d/nginx restart #重启

修改nginx預設連接埠號,開啟/etc/nginx/nginx.conf 文件,修改埠號。

 server {
  listen    8088;  # 修改端口号
  server_name localhost;

  #charset koi8-r; 

  #access_log logs/host.access.log main;

  location / {
    root  html;
    index index.html index.htm;
  }

大概在檔案36行的位置,將預設的80埠號改成其它埠號,如 8088。因為預設的80埠號很容易被其它應用程式佔用。

然後,透過上面指令重啟nginx。造訪:http://127.0.0.1:8088/

基於ubuntu怎麼透過Nginx部署Django  

如果出現如上圖,說明nginx啟動成功。我           

 透過pip安裝uwsgi。

root@ubuntu:/etc# python3 -m pip install uwsgi
測試uwsgi,建立test.py檔案:
def application(env, start_response):
  start_response('200 ok', [('content-type','text/html')])
  return [b"hello world"]
透過uwsgi運行該檔案。

fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py

接下來設定django與uwsgi連接。此處,假定的我的django專案位置為:/home/fnngj/pydj/myweb

複製程式碼 程式碼如下:

基於ubuntu怎麼透過Nginx部署Django

uwsgi --http :8001 --chdir / home/fnngj/pydj/myweb/ --wsgi-file myweb/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)

#三、nginx uwsgi django

                                 且

#N對三者結合。首先羅列一下專案的所需的檔案:

myweb/

├── manage.py

├── myweb/

│  ├── __init__.py

│  ├── settings.py

│  ├── urls.py

│  └── wsgi.py

└── myweb_uwsgi.ini

在我們透過django建立myweb專案時,在子目錄myweb下已經幫我們產生的 wsgi.py檔案。所以,我們只需要再建立myweb_uwsgi.ini設定檔即可,當然,uwsgi支援多種類型的設定文件,如xml,ini等。此處,使用ini類型的配置。

# myweb_uwsgi.ini file
[uwsgi]

# django-related settings

socket = :8000

# the base directory (full path)
chdir      = /home/fnngj/pydj/myweb

# django s wsgi file
module     = myweb.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
這個配置,其實就等於在上一小節透過wsgi指令,後面跟著一堆參數的方式,給文件化了。

socket  指定專案執行的連接埠號碼。

chdir   指定項目的目錄。

module  myweb.wsgi ,可以這麼來理解,對於myweb_uwsgi.ini檔案來說,與它的平級的有一個myweb目錄,這個目錄下有一個wsgi.py檔案。

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

接下来,切换到myweb项目目录下,通过uwsgi命令读取myweb_uwsgi.ini文件启动项目。

fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/
fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini 
[uwsgi] getting ini configuration from myweb_uwsgi.ini
*** starting uwsgi 2.0.12 (32bit) on [sat mar 12 13:05:06 2016] ***
compiled with version: 4.8.4 on 26 january 2016 06:14:41
os: linux-3.19.0-25-generic #26~14.04.1-ubuntu smp fri jul 24 21:18:00 utc 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of cpu cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
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
python version: 3.4.3 (default, oct 14 2015, 20:37:06) [gcc 4.8.4]
*** python threads support is disabled. you can enable it with --enable-threads ***
python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 kb) for 4 cores
*** operational mode: preforking ***
wsgi app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uwsgi is running in multiple interpreter mode ***
spawned uwsgi master process (pid: 7158)
spawned uwsgi worker 1 (pid: 7160, cores: 1)
spawned uwsgi worker 2 (pid: 7161, cores: 1)
spawned uwsgi worker 3 (pid: 7162, cores: 1)
spawned uwsgi worker 4 (pid: 7163, cores: 1)

注意查看uwsgi的启动信息,如果有错,就要检查配置文件的参数是否设置有误。

再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,添加如下内容。

……
server {
  listen     8099; 
  server_name  127.0.0.1 
  charset utf-8;
  access_log   /var/log/nginx/myweb_access.log;
  error_log    /var/log/nginx/myweb_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/fnngj/pydj/myweb/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的端口与myweb_uwsgi.ini配置文件中的必须一直。

现在重新启动nginx,翻看上面重启动nginx的命令。然后,访问:http://127.0.0.1:8099/

通过这个ip和端口号的指向,请求应该是先到nginx的。如果你在页面上执行一些请求,就会看到,这些请求最终会转到uwsgi来处理。

基於ubuntu怎麼透過Nginx部署Django

以上是基於ubuntu怎麼透過Nginx部署Django的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
nginx怎麼啟動nginx怎麼啟動Apr 14, 2025 pm 01:06 PM

問題:如何啟動 Nginx?答案:安裝 Nginx啟動 Nginx驗證 Nginx 是否已啟動探索其他啟動選項自動啟動 Nginx

怎麼查看nginx是否啟動怎麼查看nginx是否啟動Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼關閉nginx怎麼關閉Apr 14, 2025 pm 01:00 PM

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎麼配置nginx在windows中怎麼配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

nginx403錯誤怎麼解決nginx403錯誤怎麼解決Apr 14, 2025 pm 12:54 PM

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

linux怎麼啟動nginxlinux怎麼啟動nginxApr 14, 2025 pm 12:51 PM

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

linux怎麼查看nginx是否啟動linux怎麼查看nginx是否啟動Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令檢查 Nginx 是否已啟動:systemctl status nginx根據命令輸出進行判斷:如果顯示 "Active: active (running)",則 Nginx 已啟動。如果顯示 "Active: inactive (dead)",則 Nginx 已停止。

nginx304錯誤怎麼解決nginx304錯誤怎麼解決Apr 14, 2025 pm 12:45 PM

問題的答案:304 Not Modified 錯誤表示瀏覽器已緩存客戶端請求的最新資源版本。解決方案:1. 清除瀏覽器緩存;2. 禁用瀏覽器緩存;3. 配置 Nginx 允許客戶端緩存;4. 檢查文件權限;5. 檢查文件哈希;6. 禁用 CDN 或反向代理緩存;7. 重啟 Nginx。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具