返回文章列表文章索引阅读全文
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全文检索实现:HayStack-+-Whoosh-+-Jieba

涉及到的工具 haystack是django的开源搜索框架,该框架支持**Solr**, Elasticsearch, Whoosh, **Xapian搜索引擎,不用更改代码,直接切换引擎,减少代码量。 搜索引擎使用Whoosh,这是一个由纯Python实现的全文搜索引擎,没有二进制文件等,比较小巧,配置比较简单,当然性能自然略低。 中文分词Jieba,由于Whoosh自带的是英文分词,对中文的分

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

轻量级消息队列Django-Q轻度体验

前言 最近做的这个项目(基于Django),需要做个功能,实现定时采集车辆定位。 这让我想起来几年前那个OneCat项目,当时我用的是Celery这个很重的组件 Celery实在是太重了,后来我做公众号采集平台的时候,又接触了Django-RQ和Django-Q这俩,前者是对RQ的封装,让RQ和Django更好的结合在一起;后者是一个全新的「多进程任务队列」组件,相比起celery很轻量,当时使用

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

讨论

共 0 条

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

加载评论中…