Django项目部署

一、Nginx+uWSGI+Django部署生产环境

原理

为什么还要Nginx

Django有个runserver直接起了一个WebServer,为什么还要Nginx起一个WebServer呢?

  • Nginx的性能比Django自带的WebServer要好

大家是否想过,我们用Python写的程序(Django是Python写的),想要被WEB访问是不是还要有一个Python写的WEB Server?
就拿Nginx举例他是什么写的?C写的,代码上不同!!!

常见的Python HTTP服务器

NameVersionhttp1.1FlavourRepoCommunity
uWSGITrunk(253)Yesprocessor/threadrepoMailing List
Gunicorn0.6.4Noprocessor/threadGIT#gunicorn
wsgirefPy 2.6.4Noprocessor/threadSVNMailing List
Twisted10.0.0Yesprocessor/threadSVNCommunity
Tornado0.9.6Yeslightweight threadsMercurialMailinglist

什么是WEB 应用以及WEB 发展史

img_d03721960bf61e105d82fb71a0520f23.png
选区_063
img_8a4b69bc2d51f82fb7d2dddf87819b11.png
选区_064
img_790df433469654e1f035060041d60e83.png
选区_065

什么是WSGI?

WSGI,全程 Web Server Gateway Interface,或者Python Web Server Gateway Interface,是为Python语言定义的Web服务器和Web 应用程序或者框架之间的一种简单接口.自从WSGI被开发出来以后,许多其他语言中也出现类似接口

WSGI就是一种标准.譬如,一个德国人和一个法国人聊天,他们要想聊天可以通过过一个标准的国际语言:英语~

哪些框架自带了WSGI Server

很多框架都自带了WSGI Server,比如Flask,webpy,Django,CherryPy等等.当然性能都不好,都不行,自带的web server 更多的时候是我们测试的时候使用,发布时则使用生产环境的WSGI server或者联合nginx 做 uwsgi

概念总结

WSGI是一种Web服务器网关接口.它是一个Web服务器(如Nginx)与应用服务器(uWSGI服务器)通信的一种规范.

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换

特别注意WSGI/uwsgi/uWSGI这三个概念的区别

  1. WSGI我们已经清楚是一种通信协议
  2. uwsgi同WSGI一样是一种通信协议
  3. 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器

为什么有了uWSGI还需要Nginx?因为Nginx具备优秀的静态内容处理能力,然后将动态内容转发给uWSGI服务器,这样可以达到很好的客户端响应

img_ff65bf230180afa61000afb314474209.png
选区_067
img_cee62f196aa879aed513cab81c4b5e29.png
选区_068

二、部署

1. 通过ssh连接你的远程服务器

ssh 用户名@ip地址

2. 检查和更新系统软件

sudo apt update && upgrade 

3. 安装pip3

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

4. 创建虚拟环境(安装相关的python包)

5. 安装Nginx

sudo apt-get install nginx
img_1d06becfa3d322593e15ac7388e31ddd.png
选区_055

6.上传项目

7. 安装 uwsgi

pip install uwsgi

8. 测试uwsgi

uwsgi --http :8000 --module test1.wsgi
img_65a5e94cca38e4a32a660542767f01e5.png
选区_056

9. 静态文件配置(可以先跳过,进行下一步配置nginx)

10. 配置nginx

10.1 在项目目录下面新建一个文件夹conf,用于存放一些我们的配置文件

[站外图片上传中...(image-cb4a1d-1540135016670)]

10.2 创建一个配置文件test1_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name 你的外网ip ; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
    alias /home/ubuntu/test1/static/media;  # 指向django的media目录
}

location /static {
    alias /home/ubuntu/test1/static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}

10.3 将配置文件加入到nginx的启动配置文件中(当然你也可以把文件直接拷贝进去)

sudo ln -s /home/ubuntu/test1/conf/test1_nginx.conf /etc/nginx/conf.d
img_12e1e730a393da756871d0c862284078.png
选区_058
img_24e8565114e88db16c563c1526ecb7f4.png
选区_059

10.4 重启nginx

img_9710eb7fcb405114d253cc0ab1f9a877.png
选区_060

10.5 拉取所有需要的static file到同一个目录

在django的settings文件中加一句

STATIC_ROOT = os.path.join(BASE_DIR,'static/')

运行命令

python manage.py collectstatic
img_0ec9bc5ee27833e28b811ad516b9cc23.png
选区_061
img_f53c59d5270f1caa423810e0d78a40e5.png
选区_062

11. 配置uwsgi

11.1 新建uwsgi配置文件叫uwsgi.ini文件(我们依旧保存到conf目录下面)

  • 新建uwsgi.ini 配置文件, 内容如下:


    # mysite_uwsgi.ini file
    [uwsgi]

    # Django-related settings
    # the base directory (full path)
    chdir           = /home/ubuntu/test1
    # Django's wsgi file
    module          = test1.wsgi
    # the virtualenv (full path)

    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 10
    # the socket (use the full path to be safe
    socket          = 127.0.0.1:8000
    # ... with appropriate permissions - may be needed
    # chmod-socket    = 664
    # clear environment on exit
    vacuum          = true
    virtualenv = /home/ubuntu/.virtualenvs/h1_django

    #logto = /tmp/mylog.log

注:
chdir: 表示需要操作的目录,也就是项目的目录
module: wsgi文件的路径
processes: 进程数
virtualenv:虚拟环境的目录

uwsgi -i 你的目录/home/ubuntu/test1/conf/uwsgi.ini 

如果想后台启动加一个&

uwsgi -i 你的目录/home/ubuntu/test1/conf/uwsgi.ini &

12. 拉起项目

img_0d84ddde3f042b5e51b2735848c40ce6.png
选区_063
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于部署Django项目,你可以按照以下步骤进行操作: 1. 确保你的服务器上已经安装了Python和pip,并且pip已经升级到最新版本。 2. 创建一个虚拟环境来隔离项目所需的依赖项。可以使用`python3 -m venv myenv`命令创建一个名为myenv的虚拟环境,然后通过`source myenv/bin/activate`命令激活虚拟环境。 3. 使用pip安装Django和其他项目所需的依赖项。可以使用`pip install -r requirements.txt`命令来安装requirements.txt文件中列出的依赖项。 4. 在服务器上设置数据库。可以使用SQLite作为开发环境中的默认数据库,但在生产环境中,建议使用更强大的数据库,如MySQL或PostgreSQL。 5. 配置Django项目的设置。确保在settings.py文件中配置了正确的数据库连接和其他项目设置。 6. 运行数据库迁移。使用`python manage.py migrate`命令来应用任何未应用的数据库迁移。 7. 收集静态文件。使用`python manage.py collectstatic`命令将静态文件(如CSS和JavaScript文件)收集到一个单独的目录中,以便Web服务器可以提供这些文件。 8. 配置Web服务器。根据你选择的Web服务器(如Nginx或Apache),配置服务器以将请求转发到Django应用程序。 9. 启动Web服务器并测试部署。启动Web服务器,并访问你的应用程序的URL以确保一切正常。 这些是部署Django项目的一般步骤。根据你的具体部署环境和需求,可能还需要进行其他配置和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值