

Nginx + Gunicorn + Django 部署小记
source link: https://www.isaced.com/post-248.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx + Gunicorn + Django 部署小记
这段时间在用Django开发一个论坛,打算做一个Cocoa开发者社区。这几天在学习如何部署,正好前段时间UCloud送了三个月主机,可以拿来练练手。VPS用的64位的Ubuntu 12.04,折腾两天总算走通了,赶忙记录下来…
apt-get
服务器拿到手后第一件事就是更新apt-get,要换源什么的自己搜一大堆。之前因为系统不小心选成了Ubuntu 10.04了,遇到一大堆问题,后来换到12.04一切顺利。
apt-get update
pip还是建议更换源,速度是飞一般的感觉。具体方法可以看看我之前介绍的那篇文章《Pythoner的福利,豆瓣的PyPI源》
sudo apt-get install python-pip
Django
Django不用说肯定是用pip装,我一般是用的最新版,这样就可以了。
sudo pip install django
Nginx
首先给默认的Nginx配置备个份,这是好习惯。目录位于/etc/nginx/sites-available/default
cp /etc/nginx/sites-available/default default.bak
然后Vim编辑
vim /etc/nginx/sites-available/default
server {
listen 80;
server_name www.isaced.com;
access_log /var/log/nginx/isaced.log;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
root /data/isaced; #Django项目所在目录
}
以上是最简单的Nginx配置,目的是为能跑起来就行,更多其他详细配置还请参照其他文章。
Gunicorn
Gunicorn是一个朋友(CloverStd)推荐的,一个开源Python WSGI UNIX的HTTP服务器,Github仓库地址在这,传说速度快(配置快、运行快)、简单,默认是同步工作,支持Gevent、Eventlet异步,支持Tornado,官方有很详细的文档可以参阅。
需要在你的Django项目的settings.py中的INSTALLED_APPS
加入:gunicorn
gunicorn --worker-class=gevent isaced.wsgi:application
- –worker-class
指定工作方式,这里我用的gevent
如果提示
You need gevent installed to use this worker
则表示你还没有安装 gevent。 - isaced.wsgi:application 这里是指你的项目名,在Django创建项目的时候会自动生成对应名字文件夹中的wsgi.py,这里就是指的它。
nohup
nohup是一个 Linux 命令,搭配 &
来不挂断地运行某条命令达到后台执行的效果,默认会在根目录生成一个 nohup.out
文件用来记录所有的 log 信息,也可以重定向到其他位置。这里我们用它来执行gunicorn,来保持 gunicorn 进程不会被挂断。
nohup gunicorn --worker-class=gevent NSLoger.wsgi:application -b 127.0.0.1:8000&
–worker-class来指定工作方式为gevent,-b指定地址和端口号。
注意:在尾部加上&
(and)字符表示后台运行
执行这条命令后可以用ps命令查看进程,就能看到gunicorn了~
Start
好了,以上就是整个服务器的配置了。然后我们重启下Nginx,刷新页面就能看到你的Django App了。
sudo service nginx restart
一篇小白的部署日记就到这里了,如有什么错误的地方还请各位大大指正!
另外再推荐一篇文章:
2017.6.5 更新: 建议看看这篇文章,讲的更好! How To Set Up Django with Postgres, Nginx, and Gunicorn on CentOS 7
Recommend
-
9
想要配置好 Apache + mod_wsgi + Flask 问题真是太多了,随便换一个 Linux 发行版,或者不同的 Python 版本就得让 Google 排很多温室气体。Nginx 可以使用 uWSGI 和 Flask 串联起来,配置起来第一直觉好像也不容易,所以打算换一种方式,直接用一个 Python 的...
-
11
How to manage logs with Django, Gunicorn and NGINX Sun 26 July 2020, by Matthew Segal Category: Django So...
-
14
Setting Up Python 3, Django & Gunicorn on Linux Mint 17.3Linux Mint 17.3 "Rosa" is December 2015 release of the polished and widely-used Linux distribution. This Mint release includes both Python 2.7 and 3.4 by default, but in this tutori...
-
15
Ubuntu released the newest Long Term Support (LTS) version of its operating system in April 2016. The update brings...
-
18
Internal Server Error – Django, Nginx, Gunicorn I am sure there are lots of ways to get “Internal Server Error” with Django, Nginx and Gunicorn. The following is the most tricky one I have encountered. No emails are sent. No...
-
9
Securely Deploy a Django App With...
-
9
-
4
Introduction In this guide, you will build a Python application using the Flask microframework on Ubuntu 18.04. The bulk of this article will be about how to set up the Gunicorn application server...
-
9
Using a Different System? S...
-
7
Deploy a Django App With Gunicorn and Nginx Taking a Django app from development to production is a demanding but rewarding process. In this video course, you’ll go through that process step b...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK