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
目录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-Admin添加验证码和多次登录尝试限制
Django自带的Admin很好用,但是放到生产环境总还差了点什么= = 看看admin的介绍: Django奉行Python的内置电池哲学。它自带了一系列在Web开发中用于解决常见问题或需求的额外的、可选工具。这些工具和插件,例如django.contrib.redirects都必须在settings中的INSTALLED_APPS处进行注册,有的还需要执行manage.py migrate命令
2021-12-18 · Django · 约 1 分钟
Django-Import-Export插件控制数据导入流程
前言 之前写过两篇跟这个插件有关的文章,可以回顾一下: Django数据导入导出神器django-import-export使用 Django-Import-Export插件关于外键的处理 最近有个朋友留言问我一个关于django-import-export插件的问题 为了形象表达这个问题,我举个书籍管理的例子来描述一下 数据库表 | id | name | price | | ---- |
2022-09-02 · Django · 约 1 分钟
使用django-treebeard实现树类型存储与编辑
无非继承MP_Node配置TreeAdmin,拖拽编辑树节点。可视化?也就django-treebeard能用,其他要么停更要么功能拉胯。初始化数据?手动导入,否则admin连按钮都没有,bug。接口输出无非path和depth,自己实现更舒服。
2024-08-11 · Django · 约 1 分钟
使用easy_thumbnails轻松为图片创建缩略图
关于Django的缩略图 我在Django的官方文档里看了一下,没有找到有关缩略图的官方库,不过在Django的Wiki里头倒是看到了一篇关于使用缩略图的介绍,请参考:https://code.djangoproject.com/wiki/ThumbNails 关于Problem的描述 The majority of applications that have images, probably
2022-08-26 · Django · 约 1 分钟
讨论
共 0 条加载评论中…