前言:

对于做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. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第6节 static静态_15_静态代码块

    static的特殊用法, 静态代码块 加上构造方法,做测试 又创建一个对象 静态代码块 只执行一次 后续在学习jdbc的时候,静态代码块很有用途.

  2. sklearn版本

    10.19.0以前的sklearn版本才有cross_validation包,这个时候不要用model_selection导入StratifiedKFold,要用cross_validation,0. ...

  3. 字符串 字符数组, pcha string 之间的相互转化, 很重要。 很蛋疼

    http://www.cnblogs.com/del88/p/5448981.html Delphi字符串.PChar与字符数组之间的转换 来自:http://my.oschina.net/kaven ...

  4. 类Runtime

    Runtime类的概述和使用 Runtime类概述 每个Java应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接.可以通过getRuntime方法获取当前运行时. 应用程序不能 ...

  5. 在使用spring中的ContextConfiguration、test注解时出现的错误

    错误: 在使用测试注解时出现ContextConfiguration注解和test注解无法正常导包使用的编译异常,如图: 解决办法: 将pom.xml文件中以下依赖管理 中的<scope> ...

  6. Spring 容器的基本用法

    容器的基本用法 bean 是 Spring 中最核心的东西,因为 Spring 就像是个大水桶,而 bean 就像是容器中的水,水桶脱离了水也没什么用处了,来看看 bean 的定义. public c ...

  7. Nacos1.1.3小试牛刀

    什么是 Nacos(摘自https://nacos.io/zh-cn/docs/quick-start.html) Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性 ...

  8. hdu-1045.fire net(缩点 + 二分匹配)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. java_第一年_JDBC(5)

    事务概念:事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功: 开始事务:start transaction 提交事务:commit 回滚事务:rollback 事务的四大特 ...

  10. 循环结构 :while

    循环结构 :while 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: 1.初始化条件 while(2.循环条件){ 3.循环体 4.迭代条件 } public clas ...