曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Django

Django Application Deployment: uwsgi+Nginx

Environment Setup As the name suggests, deploying with nginx+uwsgi requires the nginx and uwsgi packages. Needless to say, nginx is essential. This article will not cover nginx installation in detail; you can search for it yourself or refer to my previous article: Setting Up a LEMP Environment on Debian 8 Installing uwsgi pip install uwsgi Uploading Project Code & Testing # Enter the project directory; choose the specific directory yourself cd /path/to/project # Create a virtual environment v

曦远 (DealiAxy)约 9 分钟
目录6 个章节

Environment Setup

As the name suggests, deploying with nginx+uwsgi requires the nginx and uwsgi packages.

Needless to say, nginx is essential. This article will not cover nginx installation in detail; you can search for it yourself or refer to my previous article: Setting Up a LEMP Environment on Debian 8

Installing uwsgi

pip install uwsgi

Uploading Project Code & Testing

# 进入项目目录,具体目录请自行选择
cd /path/to/project

# 建立虚拟环境
virtualenv venv

# 激活虚拟环境
source venv/bin/activate

# 安装依赖
pip install -r requirement.txt

# 开启测试服务器
python manage.py runserver 0.0.0.0:8001

Configuring uwsgi

uwsgi supports configuration files in multiple formats; this article uses the ini format.

Create a uwsgi.ini file in the project directory with the following content:

[uwsgi]
# Django-related settings

socket = :8001

# Virtualenv
home            = /var/www/html/_project/ClassHelperWxApp/venv

# the base directory (full path)
chdir           = /var/www/html/_project/ClassHelperWxApp

# Django s wsgi file
module          = Config.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

Starting uwsgi for Testing

# 进入项目目录
cd /path/to/project

# 开启uwsgi服务器
uwsgi -s --ini uwsgi.ini

At this point you still cannot access it, because the server is set to socket mode. If you want to try accessing it first, you can change socket to http in the ini configuration file, and then you can visit the project website, though static files will not be accessible for now.

nginx Reverse Proxy Configuration

Create a new configuration file in the /etc/nginx/sites-available directory. For example: domain.com.conf, with the following configuration

HTTPS configuration is included as a bonus; if you don't need it, just delete the ssl-related configuration lines.

server {
    # the port your site will be served on
    listen 80;
    listen 443 ssl;

    # the domain name it will serve for
    server_name domain.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    ssl on;
    #证书文件
    ssl_certificate /path/to/keys/1_domain.com_bundle.crt;
    #私钥文件
    ssl_certificate_key /path/to/keys/2_domain.com.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers on;


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

    # Django media
    location /media  {
        alias /path/to/project/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/project/static; # your Django project's static files - amend as required
    }

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

Then use ln -s to create a symlink to sites-enabled, and reload nginx.

service nginx reload

_EOF_

Django Application Deployment: uwsgi+Nginx

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Django+Taro项目实现企业微信登录

前言 还是最近在做的一个小项目,后端用的是Django搭配RestFramework做接口,前端第一次尝试用京东开源的Taro框架来做多端(目前需要做用于企业微信的H5端和微信小程序) 本文记录一下企业微信登录的流程,上周看文档看得头晕晕的,其实很简单,封装好了就几行代码的事~ 两种方式 一种是先拼接好登录要用的地址authorize_url,回调地址设置成h5应用的网页入口,然后把authori

2022-04-08 · Django · 约 1 分钟

Django数据库性能优化之使用Python集合操作

前言 最近有个新需求: 人员基础信息(记作人员A),10w 某种类型的人员信息(记作人员B),1000 要求在后台上(Django Admin)分别展示:已录入A的人员B列表、未录入的人员B列表 团队的DBA提议使用视图可以解决这个问题(不愧是搞数据库的) PS:起先我觉得Django的Model是直接管理数据库表的,使用Model来映射数据库模型怕是有一定的麻烦,不过查了一下资料发现并不会~ 只

2022-08-26 · Django · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…