nginx+uwsgi 部署 django 代碼(centos 7) nignix靜態(tài)文件設(shè)置

django 項(xiàng)目的部署用好幾種部署方式,現(xiàn)在簡單說下用nginx + uwsgi 來部署django項(xiàng)目。及nginx靜態(tài)文件設(shè)置

首先,先理解下概念 nginx uwsgi 和django的關(guān)系. 這三者都是相互獨(dú)立存在的,django就是我們寫的python代碼程序,如果有django自己帶大服務(wù)器也可以運(yùn)行但是用戶訪問量有限,而且會經(jīng)常掛件,然后有uwsgi 在做橋梁,用uwsgi在啟動服務(wù)器,而uwsgi 啟動服務(wù)器 用戶通過web訪問的時候有些不用走后臺代碼如 html和css,uwsgi是都要走后臺代碼和靜態(tài)html;這時nginx 就來了,用戶通過web訪問nginx部署的代碼的時候,nginx會自行判斷,是靜態(tài)和動態(tài),如果是靜態(tài)的html等代碼,nginx 會自行處理不走django的python代碼,如果是動態(tài)再轉(zhuǎn)接個uwsgi在處理返回。

一,uwsgi 安裝部署(有些文章是先安裝nginx, 其他如果是用uwsgi 也可以發(fā)布,所以先安裝 uwsgi 之后再用nginx聯(lián)合部署)

1,安裝 uwsgi ,uwsgi 是python 中一個包所以用pip 安裝

pip install uwsgi

? ? ?注 .uwsgi 運(yùn)行python有些版本需要安裝?

? ? ? ? yum install uwsgi-plugin-python

2,創(chuàng)建一個文件來測試是否安裝成功

創(chuàng)建測試文件 test.py 測試文件,文件內(nèi)容:

defapplication(env, start_response):

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

? ? return[b"Hello World"]

2, 啟動uwsgi

uwsgi --http-socket :8000 --plugin python --wsgi-file /home/admin/temp/test.py

查看啟動日志沒有錯誤后在 瀏覽器訪問

顯示 hello world ,啟動成功

瀏覽器訪問成功

后臺有一條請求紀(jì)錄.

http 200請求成功

3,有的時候在瀏覽器訪問不到可以再起一個 命令窗口 執(zhí)行?

curl localhost:8000 --verbose

查看能否請求到,如果請求到是 防火墻沒有開端口號

4,centos 7 防火墻命令

防護(hù)墻命令

二,nginx 部署

1,nginx安裝

sudo yum install nginx

2,啟動 nginx

安裝完成,在命令行啟動nginx

systemctl start nginx

3,查看nginx是否啟動成功

systemctl start nginx

在瀏覽器直接輸入 服務(wù)器ip 看到 nginx 字樣訪問成功(我之前配的nginx項(xiàng)目有錯沒有關(guān)系,這樣就是nginx啟動起來來)

nginx啟動成功

在命令行?

ps -ef|grep nginx 看到nginx正在運(yùn)行.

nginx 正在運(yùn)行

4,配置nginx ,nginx 配置文件在 /etc/nginx 下

vim /etc/nginx/nginx.conf

-------------------------------------------------------------------------------------------------

server {

listen? ? ? 80 default_server;

listen? ? ? [::]:80 default_server;

server_name? _;

root? ? ? ? /usr/share/nginx/html;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

location / {

root? ? ? ? /var/www/localhost/htdocs/mysite/polls;

include uwsgi_params;

uwsgi_pass 127.0.0.1:8000;

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

-------------------------------------------------------------------------------------------------

現(xiàn)在只配置兩個內(nèi)容 listen 接受端口和uwsgi_pass 127.0.0.1:8000? 與uwsgi 的端口對接(因?yàn)?此時uwsgi 和nginx在同一臺服務(wù)器,寫本機(jī)127.0.0.1就可以),listen是nginx 接受的外界請求的端口號 80, (如果改成90,需要打開90的防火墻端口號,在瀏覽器上填寫http://123.206.18.248:90也可以訪問,以為我們配置的是80是 http默認(rèn)端口 http3 默認(rèn)443這個不解釋)

三、uwsgi+nginx+django 代碼部署

要先安裝 yum install django

1,再部署之前修改在項(xiàng)目的根目錄新建 .ini 文件在聯(lián)系?

目錄結(jié)構(gòu)

blog_uwsgi.ini ?內(nèi)容

-------------------------------------------------------------------------------------------------

# blog_uwsgi.ini file

[uwsgi]

# Django-related settings

socket=:8000

# the base directory (full path)

chdir=/home/admin/temp/blog

# Django s wsgi file

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

buffer-size=32768

-------------------------------------------------------------------------------------------------

這個配置,其實(shí)就相當(dāng)于在上一小節(jié)中通過wsgi命令,后面跟一堆參數(shù)的方式,給文件化了。

socket指定項(xiàng)目執(zhí)行的端口號。

chdir指定項(xiàng)目的目錄。

xtblog.wsgi,可以這么來理解,對于blog_uwsgi.ini文件來說,與它的平級的有一個xtblog目錄,這個目錄下有一個wsgi.py文件。

2,啟動uwsgi?

# uwsgi --plugin python --ini blog_uwsgi.ini

執(zhí)行成功

打開 瀏覽器輸入請求地址,請求成功.

瀏覽器打開執(zhí)行成功

注. 1,在centos 7中執(zhí)行前 要確定你的django程序可以在本機(jī)執(zhí)行成功

并且在 settings.py中設(shè)置 ALLOWED_HOSTS = ['自己服務(wù)器地址']

setting.py設(shè)置

2,你電腦上安裝的包,同樣在服務(wù)器端也需要安裝.

參照 http://www.cnblogs.com/fnng/p/5268633.html

補(bǔ)充nginx 設(shè)置訪問靜態(tài)文件

上面的設(shè)置是沒有問題但是并沒有對靜態(tài)文件設(shè)置?

直接上代碼


01


02


04


05


06

---------------------------------------------start-------------------------------------------

# For more information on configuration, see:

#? * Official English Documentation: http://nginx.org/en/docs/

#? * Official Russian Documentation: http://nginx.org/ru/docs/

user root;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {

worker_connections 1024;

}

http {

log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log? /var/log/nginx/access.log? main;

sendfile? ? ? ? ? ? on;

tcp_nopush? ? ? ? ? on;

tcp_nodelay? ? ? ? on;

keepalive_timeout? 65;

types_hash_max_size 2048;

#? ? include? ? ? ? ? ? /etc/nginx/mime.types;

include? ? ? ? ? ? mime.types;

default_type? ? ? ? application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

upstream django {

server 127.0.0.1:8000;

}

server {

listen? ? ? 80 default_server;

listen? ? ? [::]:80 default_server;

server_name? sdzebai.com;

#? ? ? ? server_name? _;

#? ? ? ? root? ? ? ? /usr/share/nginx/html;

#? ? ? ? root /home/admin/temp/blog;

# Load configuration files for the default server block.

#? ? ? include /etc/nginx/default.d/*.conf;

location / {

#? ? ? ? ? ? root? ? ? ? /var/www/localhost/htdocs/mysite/polls;

root? ? ? ? /home/admin/temp/blog;

include uwsgi_params;

uwsgi_pass 127.0.0.1:8000;

proxy_redirect off;

#? ? ? ? ? ? proxy_set_header X-Real-IP $remote_addr;

#? ? ? ? ? proxy_set_header Host $host;

}

location /media/ {

alias /home/admin/temp/blog/media;

}

location /static/ {

root /home/admin/temp/blog/static;

#? ? ? ? ? ? alias /home/admin/temp/blog/static;

#? ? ? ? ? ? log_not_found off;

autoindex on;

}

location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css){

expires max;

#? ? ? ? ? ? proxy_pass http://sdzebai.com;

log_not_found off;

root /home/admin/temp/blog/;

proxy_store on;

proxy_store_access user:rw group:rw all:rw;

proxy_temp_path? ? ? ? /home/admin/temp/blog/;

proxy_redirect? ? ? ? ? off;

proxy_set_header? ? ? ? Host 127.0.0.1:8000;

client_max_body_size? ? 10m;

client_body_buffer_size 1280k;

proxy_connect_timeout? 900;

proxy_send_timeout? ? ? 900;

proxy_read_timeout? ? ? 900;

proxy_buffer_size? ? ? 40k;

proxy_buffers? ? ? ? ? 40 320k;

proxy_busy_buffers_size 640k;

proxy_temp_file_write_size 640k;

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

# Settings for a TLS enabled server.

#

server {

listen? ? ? 443 ssl http2 default_server;

listen? ? ? [::]:443 ssl http2 default_server;

server_name? badidu.com;# 自己的域名

root? ? ? ? /usr/share/nginx/html;

ssl_certificate "/home/admin/sdzebai/Nginx/1_sdzebai.com_bundle.crt";

ssl_certificate_key "/home/admin/sdzebai/Nginx/2_sdzebai.com.key";

ssl_session_cache shared:SSL:1m;

ssl_session_timeout? 10m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

#? ? ? ? location / {

#? ? ? ? }

location / {

#? ? ? ? ? ? root? ? ? ? /var/www/localhost/htdocs/mysite/polls;

root? ? ? ? /home/admin/temp/blog;

include uwsgi_params;

uwsgi_pass 127.0.0.1:8000;

proxy_redirect off;

}

location /media/ {

alias /home/admin/temp/blog/media;

}

location /static/ {

root /home/admin/temp/blog/static;

#? ? ? ? ? ? alias /home/admin/temp/blog/static;

#? ? ? ? ? ? log_not_found off;

autoindex on;

}

location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css){

expires max;

#? ? ? ? ? ? proxy_pass http://sdzebai.com;

log_not_found off;

root /home/admin/temp/blog/;

proxy_store on;

proxy_store_access user:rw group:rw all:rw;

proxy_temp_path? ? ? ? /home/admin/temp/blog/;

proxy_redirect? ? ? ? ? off;

proxy_set_header? ? ? ? Host 127.0.0.1:8000;

client_max_body_size? ? 10m;

client_body_buffer_size 1280k;

proxy_connect_timeout? 900;

proxy_send_timeout? ? ? 900;

proxy_read_timeout? ? ? 900;

proxy_buffer_size? ? ? 40k;

proxy_buffers? ? ? ? ? 40 320k;

proxy_busy_buffers_size 640k;

proxy_temp_file_write_size 640k;

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

}

-------------------------end------------------------

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容