前言:

对于做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会话

    B/S请求响应模式是无状态的.任意的请求间不存在任何的联系,不能将请求状态保持下去. 会话技术可以给每个浏览器分配持久数据,这些数据不会随着一次请求和相应结束而销毁. COOKIE cookie是一种 ...

  2. scrapy Pipeline使用twisted异步实现mysql数据插入

    from twisted.enterprise import adbapi class MySQLAsyncPipeline: def open_spider(self, spider): db = ...

  3. 类BigInteger

    BigInteger类 可以让超过Integer范围内的数据进行运算 构造方法 public BigIntege(String val); package com.jacky; import java ...

  4. c++调用c#代码

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h" ...

  5. python3爬虫之urllib初探

    urllib主要包含request(请求模块).error(异常处理模块).parse(工具模块).robotparser(识别网站的robots.txt文件,是否允许爬取). request(请求模 ...

  6. python+selenium链接对象操作

    对于链接对象常见的操作有:单击.获取链接文字.获取链接地址等: from selenium import webdriverfrom time import sleep driver = webdri ...

  7. JWT详解-(JSON Web Token教程)

    JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案,本文介绍它的原理和用法. 一.跨域认证的问题 互联网服务离不开用户认证.一般流程是下面这样. 1.用户向服务器发送用户名和密 ...

  8. Django中用 form 实现登录注册

    1.forms模块 将Models和Forms结合到一起使用,将Forms中的类和Models中的类关联到一起,实现属性的共享 1.在forms.py中创建class,继承自forms.ModelFo ...

  9. Gradle打包问题Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0

    前言 使用gradle打包react native的时候,出现了如下报错,下面和大家说一下解决的具体办法 Deprecated Gradle features were used in this bu ...

  10. VS中发布并调试IIS程序

    1.创建本地IIS站点 2.修改配置 .net framework 右击项目属性,服务器修改为本地IIS,并且项目URL修改为相对应的站点即可 .net core 右键属性,进入调试栏新建一个配置 选 ...