返回文章列表文章索引阅读全文
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 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

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

加载评论中…