nginx+uwsgi 部署 django 代码(centos
django 项目的部署用好几种部署方式,现在简单说下用nginx + uwsgi 来部署django项目。及nginx静态文件设置
首先,先理解下概念 nginx uwsgi 和django的关系. 这三者都是相互独立存在的,django就是我们写的python代码程序,如果有django自己带大服务器也可以运行但是用户访问量有限,而且会经常挂件,然后有uwsgi 在做桥梁,用uwsgi在启动服务器,而uwsgi 启动服务器 用户通过web访问的时候有些不用走后台代码如 html和css,uwsgi是都要走后台代码和静态html;这时nginx 就来了,用户通过web访问nginx部署的代码的时候,nginx会自行判断,是静态和动态,如果是静态的html等代码,nginx 会自行处理不走django的python代码,如果是动态再转接个uwsgi在处理返回。
一,uwsgi 安装部署(有些文章是先安装nginx, 其他如果是用uwsgi 也可以发布,所以先安装 uwsgi 之后再用nginx联合部署)
1,安装 uwsgi ,uwsgi 是python 中一个包所以用pip 安装
pip install uwsgi
注 .uwsgi 运行python有些版本需要安装
yum install uwsgi-plugin-python
2,创建一个文件来测试是否安装成功
创建测试文件 test.py 测试文件,文件内容:
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 ,启动成功
浏览器访问成功后台有一条请求纪录.
http 200请求成功3,有的时候在浏览器访问不到可以再起一个 命令窗口 执行
curl localhost:8000 --verbose
查看能否请求到,如果请求到是 防火墙没有开端口号
4,centos 7 防火墙命令
防护墙命令二,nginx 部署
1,nginx安装
sudo yum install nginx
2,启动 nginx
安装完成,在命令行启动nginx
systemctl start nginx
3,查看nginx是否启动成功
systemctl start nginx
在浏览器直接输入 服务器ip 看到 nginx 字样访问成功(我之前配的nginx项目有错没有关系,这样就是nginx启动起来来)
nginx启动成功在命令行
ps -ef|grep nginx 看到nginx正在运行.
nginx 正在运行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 {
}
}
-------------------------------------------------------------------------------------------------
现在只配置两个内容 listen 接受端口和uwsgi_pass 127.0.0.1:8000 与uwsgi 的端口对接(因为 此时uwsgi 和nginx在同一台服务器,写本机127.0.0.1就可以),listen是nginx 接受的外界请求的端口号 80, (如果改成90,需要打开90的防火墙端口号,在浏览器上填写http://123.206.18.248:90也可以访问,以为我们配置的是80是 http默认端口 http3 默认443这个不解释)
三、uwsgi+nginx+django 代码部署
要先安装 yum install django
1,再部署之前修改在项目的根目录新建 .ini 文件在联系
目录结构blog_uwsgi.ini 内容
-------------------------------------------------------------------------------------------------
# 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
-------------------------------------------------------------------------------------------------
这个配置,其实就相当于在上一小节中通过wsgi命令,后面跟一堆参数的方式,给文件化了。
socket指定项目执行的端口号。
chdir指定项目的目录。
xtblog.wsgi,可以这么来理解,对于blog_uwsgi.ini文件来说,与它的平级的有一个xtblog目录,这个目录下有一个wsgi.py文件。
2,启动uwsgi
# uwsgi --plugin python --ini blog_uwsgi.ini
打开 浏览器输入请求地址,请求成功.
浏览器打开执行成功注. 1,在centos 7中执行前 要确定你的django程序可以在本机执行成功
并且在 settings.py中设置 ALLOWED_HOSTS = ['自己服务器地址']
setting.py设置2,你电脑上安装的包,同样在服务器端也需要安装.
参照 http://www.cnblogs.com/fnng/p/5268633.html
补充nginx 设置访问静态文件
上面的设置是没有问题但是并没有对静态文件设置
直接上代码
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------------------------