前言:

对于做Django web项目的童鞋,重要性不言而喻。

参考:https://www.cnblogs.com/alwaysInMe/p/9096565.html

   https://blog.csdn.net/yjdlailin/article/details/50879449

一、几条命令

# 查看是否有 uwsgi 相关的进程
ps -aux|grep "uwsgi" 或者 ps -ef|grep uwsgi # 杀死有关 uwsgi 相关的进程
pkill -9 uwsgi

二、安装环境

安装python

安装uwsgi

用python的pip安装最简单:
apt-get install python-dev #不安装这个,下面的安装可能会失败
pip install uwsgi

安装nginx

apt-get install nginx

三、配置nginx和uwsgi配置

uwsgi配置

[uwsgi]
# socket协议,和下面http任意存在一个就可以了
# 使用http协议,监听端口,可以是 :8001(比socket多了一层封装)
http = :8001
#the local unix socket file than commnuincate to Nginx
# 这里的:8001在nginx中也有用到
socket = :8001
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
wsgi-file = /path/to/your/project/wsgi.py
# maximum number of worker processes
# 该项目使用的进程数,一般使用电脑的 核数
processes = 4
#thread numbers startched in each worker process
threads = 2
# 指定静态文件
static-map=/static=path/to/static #monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true pidfile = path/to/logs/uwsgi.pid
daemonize = path/to/logs/uwsgi.log 

nginx配置

user  nginx;
worker_processes 5; #error_log logs/error.log notice;
#error_log logs/error.log info; pid logs/nginx.pid; events {
worker_connections 1024;
} http { upstream django {
server 127.0.0.1:8001;
} include mime.types;
default_type application/octet-stream; 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 logs/access.log main;
error_log logs/error.log; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 1800; #gzip on; server {
listen 80; # 80 是http默认的端口, 443 是https默认的端口(网页一般使用这两个端口)
server_name your server name; # 你访问的路径前面的url名称 #charset koi8-r;
charset utf-8;
client_max_body_size 5000M; #access_log logs/host.access.log main; location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass django; # 如果上面写别名了,那么,这里还可以直接使用别名
} location /static {
alias path/to/static/;
} location /media {
alias path/to/media/;
}
error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

四、修改Django配置

# 将debug模式改成False
DEBUG = False # 允许访问的 host, 可以写成单独的 host, 也可以直接写 "*",代表全部
ALLOWED_HOSTS = ['*', ] STATIC_URL = '/static/' # 修改 静态文件的位置
if DEBUG:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
else:
STATIC_ROOT = 'path/to/static' MEDIA_URL = '/media/'
if DEBUG:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
else:
MEDIA_ROOT = 'path/to/media'

  

五、基于uwsgi和nginx部署Django

1.原理

the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django

2.基本测试

测试uwsgi是否正常

在django项目的根目录下创建test.py文件,添加源码如下:

# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ["Hello World"] # python2
#return [b"Hello World"] # python3

然后,Run uWSGI:

uwsgi --http :8000 --wsgi-file test.py

参数含义:

  • http :8000: 使用http协议,8000端口
  • wsgi-file test.py: 加载指定文件 test.py

打开下面url,浏览器上应该显示hello world

http://example.com:8000

如果显示正确,说明下面3个环节是通畅的:

the web client <-> uWSGI <-> Python

测试Django项目是否正常

首先确保project本身是正常的:

python manage.py runserver 0.0.0.0:8000

如果没问题,使用uWSGI把project拉起来:

uwsgi --http :8000 --module mysite.wsgi

参数含义:

  • module mysite.wsgi: 加载wsgi module

如果project能够正常被拉起,说明以下环节是通的:

the web client <-> uWSGI <-> Django

六、收集静态文件

python manage.py collectstatic

这条命令会将所有 Django 项目的静态文件搜集到上面配置中的,静态文件的位置

七、启动服务

启动nginx

sudo service nginx start

启动uwsgi

uwsgi uwsgi.ini

附:

# 启动nginx    sudo service nginx start
# 停止nginx sudo service nginx stop
# 重启nginx sudo service nginx restart

  

基于Nginx和uWSGI在Ubuntu上部署Django项目的更多相关文章

  1. 基于nginx和uWSGI在Ubuntu上部署Django

    转自: http://www.jianshu.com/p/e6ff4a28ab5a

  2. 基于nginx和uWSGI在Ubuntu上部署Djan

    http://www.jianshu.com/p/e6ff4a28ab5a 文/Gevin(简书作者)原文链接:http://www.jianshu.com/p/e6ff4a28ab5a著作权归作者所 ...

  3. 基于centos7+nginx+uwsgi+python3+django2.0部署Django项目

    0.序言 本文讲解如何基于centos7+nginx+uwsgi+python3+django2.0把windows上的本地项目部署到云服务器上. 本文服务器上的django项目和虚拟环境的路径将建立 ...

  4. 在nginx上部署django项目--------Gunicorn+Django+nginx+mysql

    一.安装nginx 以前的博客我有写,这里就不写了 http://www.cnblogs.com/wt11/p/6420442.html 二.安装mysql 我用的mysql5.7  64位的二进制包 ...

  5. 服务器上部署django项目流程?

    1. 简单粗暴 项目开发完毕,在部署之前需要再配置文件中将 ALLOWED_HOSTS配置设置为:当前服务器IP或*,如: ALLOWED_HOSTS = ["*",] 然后将源码 ...

  6. 在PythonAnyWhere上部署Django项目

    http://www.jianshu.com/p/91047e3a4ee9 将项目放到git上,然后将pathonanywhere上的ssh传到git上,没有的话先创建,然后从git上把项目拷贝到pa ...

  7. Ubuntu中部署Django项目的配置与链接MySQL

    Django的简介 MVT模式的介绍创建项目的虚拟环境 本次使用的是pip安装 一.更新 sudo apt update 二.安装pip sudo apt install python3-pip 三. ...

  8. 关于Nginx部署Django项目的资料收集

    参考:https://www.cnblogs.com/chenice/p/6921727.html 参考:https://blog.csdn.net/fengzq15/article/details/ ...

  9. nginx+uwsgi部署Django项目到Ubuntu服务器全过程,以及那些坑!!!

    前言:自己在windows上用PyCharm编写的Django项目,编写完后在windows上运行一点问题都没有,但是部署到服务器上时却Bug百出.百度,CSDN,sf,各种搜索寻求解决方案在历时3天 ...

随机推荐

  1. PHP文件和目录操作

    目录操作 创建目录:mkdir(目录地址, 权限, 是否递归创建=false); 删除目录:rmdir(目录地址);(仅仅可以删除空目录,不支持递归删除) 移动(改名):rename(旧地址, 新地址 ...

  2. Understanding ECMAScript 6 阅读问题小记

    拖了一年说要看这本书,一直都没坚持下来,开个 bo 记录下觉得疑惑的问题,也算鞭策一下自己. 第一章 块级绑定 1. 第一章“块级绑定”下,说 const 变量如果绑定的是对象 Object,那么修改 ...

  3. Docker image 和 volume 的关系

    image :镜像 虚拟机容器需要加载image才能运行,镜像中打包了构建好服务的运行环境. Docker images are the basis of containers. An Image i ...

  4. c# Thread——1.为什么Abort中断线程是不可靠的

    Thread.Abort 方法在c#中用作强制中断线程的执行,大多用于线程内部满足某个特定条件而自己调用关闭自身,比如下面的代码在i自增到3的时候就会停止打印. class Program { sta ...

  5. js变量和数据类型

  6. 20191105 《Spring5高级编程》笔记-第10章

    第10章 使用类型转换和格式化进行验证 在应用程序开发中,数据验证通常与转换和格式化一起被提及.因为数据源的格式很可能与应用程序中所使用的格式不同. 名词缩写: SPI(Service Provide ...

  7. opencv中画圆circle函数和椭圆ellipse函数

    1.      void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, ...

  8. 洛谷 P1073 最优贸易 & [NOIP2009提高组](反向最短路)

    传送门 解题思路 很长的题,实际上在一个有向图(点有点权)中求一个从起点1到终点n的路径,使得这条路径上点权最大的点与点权最小的点的差值最大(要求必须从点权较小的点能够走到点权较大的点). ——最短路 ...

  9. day16 django 笔记

    一 jQuery是什么? [1]   jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2]   jQuery是继prototy ...

  10. Java8 HashMap详解(转)

    Java8 对 HashMap 进行了一些修改,最大的不同就是利用了红黑树,所以其由 数组+链表+红黑树 组成. 根据 Java7 HashMap 的介绍,我们知道,查找的时候,根据 hash 值我们 ...