Linux 下部署Django项目
 

说明:本文所使用的环境为CentOS 6+Python2.7+Django1.11

安装Django、Nginx和uWSGI

1.确定已经安装了2.7版本的Python; 
2.安装python-devel 
yum install python-devel 
3.安装uwsgi 
pip install uwsgi

测试uwsgi是否能正常工作

1.新建一个index.py;

# index.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
  • 1
  • 2
  • 3
  • 4

2.uwsgi –http :8000 –wsgi-file index.py 
浏览器访问8000端口看是否有hello world输出 
注意:确保8000端口能被外网访问

测试Django能否正常工作

$ cd /var/www/ 
$ django-admin startproject mysite 
$ cd mysite 
$ python manage.py runserver 0.0.0.0:8000 
浏览器访问8000端口看是否有hello world输出

测试uwsgi是否能和django集成

uwsgi --http :8000 --chdir=/var/www/mysite --module mysite.wsgi 
or 
uwsgi --http :8008 --chdir /var/www/mysite --wsgi-file weixin/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192

在浏览器中访问8000端口,看能否正常访问django网站。

参数说明:

# http : 协议类型和端口号
# processes : 开启的进程数量
# workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
# chdir : 指定运行目录(chdir to specified directory before apps loading)
# wsgi-file : 载入wsgi-file(load .wsgi file)
# stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
# threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
# master : 允许主进程存在(enable master process)
# daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常
用的,还是把运行记录输出到一个本地文件上。
# daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常
用的,还是把运行记录输出到一个本地文件上。
# vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

配置Nginx,使Nginx能为Django提供服务

在/etc/nginx/conf.d/下创建一个针对mysite项目的配置文件,详细如下:

# /etc/nginx/conf.d/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8000; # for a web port socket
} # configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8; # max upload size
client_max_body_size 75M; # adjust to taste # Django 的static和 media目录
# 如果没有static或media目录,你需要先创建
location /media {
alias /var/www/mysite/media;
} location /static {
alias /var/www/mysite/static;
} # 将所有非静态文件的请求转给django server处理,这里的django server用的是uwsgi。
location / {
uwsgi_pass django;
include /var/www/mysite/uwsgi_params;
}
}
#你可以从/etc/nginx/uwsgi_params复制一个拷贝到/var/www/mysite/uwsgi_params。
$ cp /etc/nginx/uwsgi_params /var/www/mysite/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

需要补充说明的是,在/etc/nginx/nginx.conf文件中,在最后一行的配置是include /etc/nginx/conf.d/*.conf,也就是说,/etc/nginx/conf.d/mysite_nginx.conf是会被包含在/etc/nginx/nginx.conf中的。

重启nginx服务器,验证访问结果

/etc/init.d/nginx restart 
通过浏览器访问80端口,你发现了什么?502 Bad Gateway?是不是?想一想,这是为什么呢?原因是你访问80端口时,请求的资源不是static,也不是media,这个时候Nginx就把请求转给upstream django,upstream的网关配置的127.0.0.1:8000,而127.0.0.1:8000是要靠uwsgi启动的,所以报了一个502 Bad Gateway。你,明白了吗?

注:CentOS 7启动服务的命令是systemctl restart nginx.service

启动uwsgi,再次验证结果

执行下面一个命令,启动uwsgi。 
uwsgi --socket :8000 --chdir=/var/www/mysite --module mysite.wsgi 
重启Nginx服务/etc/init.d/nginx restart,再次通过浏览器访问80端口试试看。是不是成功了?

注:CentOS 7启动服务的命令是systemctl restart nginx.service

如何使uwsgi以配置文件运行?Configuring uWSGI to run with a .ini file

创建一个mysite_uwsgi.ini文件,内容如下:

[uwsgi]
socket=:8000
chdir = /var/www/mysite
#wsgi-file = mysite/wsgi.py
module=mysite.wsgi:application
processes = 10
threads = 2
#django<1.4,必须指定env和module
env = DJANGO_SETTINGS_MODULE=mysite.settings # clear environment on exit
vacuum = true
safe-pidfile = /tmp/project-master.pid # create a pidfile
harakiri = 20 # respawn processes taking more than 20 seconds
limit-as = 128 # limit the project to 128 MB
max-requests = 5000 # respawn processes after serving 5000 requests
daemonize = /var/log/uwsgi/mysite.log # background the process & log
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

执行命令uwsgi --ini mysite_uwsgi.ini即可运行

如何以Emperor模式运行?

什么是Emperor模式?,官网说的很清楚,如下:

uWSGI can run in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.

Whenever a config file is amended, the emperor will automatically restart the vassal.

按下面的步骤操作,即可以Emperor模式运行uwsgi: 
1. create a directory for the vassals 
sudo mkdir /etc/uwsgi 
sudo mkdir /etc/uwsgi/vassals 
2. symlink from the default config directory to your config file 
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/ 
3. run the emperor 
uwsgi --emperor /etc/uwsgi/vassals --uid nginx --gid nginx

如何创建uwsgi服务?

在Linux中,一个服务其实就是一个shell脚本。在CenOS6中,服务脚本一般都在/etc/init.d/目录下。 
首先我们在/etc/initd/目录下创建一个uwsgi文件,文件内容如下:

#!/bin/sh
#
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $syslog $remote_fs
# Should-Start: $time ypbind smtp
# Required-Stop: $syslog $remote_fs
# Should-Stop: ypbind smtp
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Check for missing binaries (stale symlinks should not happen)
UWSGI_BIN="/usr/local/bin/uwsgi"
UWSGI_EMPEROR_MODE=true
UWSGI_VASSALS="/etc/uwsgi/vassals/"
UWSGI_OPTIONS="--uid nginx --gid nginx --logto /var/log/uwsgi/uwsgi.log"
lockfile=/var/lock/subsys/uwsgi
UWSGI_OPTIONS="$UWSGI_OPTIONS --autoload"
if [ "$UWSGI_EMPEROR_MODE" = "true" ] ; then
UWSGI_OPTIONS="$UWSGI_OPTIONS --emperor $UWSGI_VASSALS"
fi
case "$1" in
start)
echo "Starting uWSGI ... "
daemon $UWSGI_BIN $UWSGI_OPTIONS &
;;
stop)
echo "Shutting down uWSGI ... "
killproc $UWSGI_BIN
;;
restart)
$0 stop
$0 start
;;
status)
echo -n "Checking for service uWSGI "
status $UWSGI_BIN
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

然后,我们可以使用此脚本来管理uwsgi,如下:

/etc/init.d/uwsgi start 
/etc/init.d/uwsgi stop 
/etc/init.d/uwsgi restart 
/etc/init.d/uwsgi status

需要注意的是,日志文件夹的所属权应该归配置文件中指定的用户nginx 
$ chown nginx.nginx /var/log/uwsgi -R

如何设置开机起动uwsgi?

把启动uwsgi的命令添加到“/etc/rc.local”文件中即可。

多站点部署问题

#Simple HTTP server
server {
listen 80;
root /usr/share/nginx/www;
server_name host1.example.com;
} #Django server
server {
listen 80;
server_name host2.example.com; #...upstream config...
}

Linux 下部署Django项目的更多相关文章

  1. Linux下部署Django项目

    目录 安装python3.X环境 安装部署开启django 由于Linux系统默认自带的是2.X环境,所以我们需要去安装3.X环境的python. 安装python3.X环境 1.使用下面的命令下载P ...

  2. linux下部署php项目-Apache、php、mysql关联

    linux下部署php项目环境可以分为两种,一种使用Apache,php,mysql的压缩包安装,一种用yum命令进行安装. 使用三种软件的压缩包进行安装,需要手动配置三者之间的关系.apache和p ...

  3. 6.linux下部署 web 项目

    安装java 1.下载 linux 环境的jdk 2.上传该压缩包到 linux 系统中并且解压 tar -zxvf 压缩包名 3.配置环境变量并且刷新配置 export JAVA_HOME=/ali ...

  4. linux中部署django项目

    通过Nginx部署Django(基于ubuntu) Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器 ...

  5. CentOS7下部署Django项目详细操作步骤

    严格按下面步骤 一.更新系统软件包 yum update -y 二.安装软件管理包和可能使用的依赖 yum -y groupinstall "Development tools" ...

  6. 03 Linux下运行Django项目

    1.安装windows和linux传输文件的工具 pip install lrzsz 提供两个命令 一个是上传一个是下载 rz 接收 直接rz sz 上传 直接sz 或者直接拖拽 2.在线下载资源的命 ...

  7. Linux下的django项目01

    1.初始化项目结构 └─shiyanlou_project # 项目根路径 │ .gitignore     # 提交git仓库时,不提交的文件必须要在这里进行标注 │ README.en.md # ...

  8. 关于linux下部署JavaWeb项目,nginx负责静态资源访问,tomcat负责处理动态请求的nginx配置

    1.项目的运行环境 linux版本 [root@localhost ~]# cat /proc/version Linux version -.el6.x86_64 (mockbuild@x86-.b ...

  9. 转载:CentOS7下部署Django项目详细操作步骤

    部署是基于:centos7+nginx+uwsgi+python3+django 之上做的 文章转自:Django中文网        https://www.django.cn/article/sh ...

随机推荐

  1. Abp Uow 设计

    初始化入口 在AbpKernelModule类中,通过UnitOfWorkRegistrar.Initialize(IocManager) 方法去初始化 /// <summary> /// ...

  2. SmokePing安装手册

    SmokePing安装部署 SmokePing简介 Smokeping是一款用于网络性能监测的开源监控软件,主要用于对IDC的网络状况,网络质量,稳定性等做检测,通过rrdtool制图方式,图形化地展 ...

  3. Buns---cf 106C(多重背包)

    题目链接:http://codeforces.com/problemset/problem/106/C 题意:有n克面粉,m种馅料,然后每种馅料有ai克,bi克馅料和ci克面粉做的面包可以买di元,也 ...

  4. Python并行编程(五):线程同步之信号量

    1.基本概念 信号量是由操作系统管理的一种抽象数据类型,用于在多线程中同步对共享资源的使用.本质上说,信号量是一个内部数据,用于标明当前的共享资源可以有多少并发读取. 同样在threading中,信号 ...

  5. ubuntu配置Python-Django Nginx+uwsgi 安装配置

    安装Nginx sudo apt-get install nginx ubantu安装完Nginx后,文件结构大致为: 所有的配置文件都在 /etc/nginx下: 启动程序文件在 /usr/sbin ...

  6. 大量高清文字版PDF编程书籍下载

    下载地址 :http://zhaojucai.com/download.html 文件夹: 数学之美(第二版) Java编程思想(第4版) 图灵程序设计丛书:Python基础教程(第2版)(修订版) ...

  7. Spring MVC学习(五)---ModelAndView没有明显申明name

    看图不解释: 对于这种写法: new ModelAndView().addObject(XXX)  

  8. Laravel 5.3 使用内置的 Auth 组件实现多用户认证功能

    https://blog.csdn.net/kevinbai_cn/article/details/54341779 概述 在开发中,我们经常会遇到多种类型的用户的认证问题,比如后台的管理员和前台的普 ...

  9. module_init module_exit

    像你写C程序需要包含C库的头文件那样,Linux内核编程也需要包含Kernel头文件,大多的Linux驱动程序需要包含下面三个头文件:#include <linux/init.h>#inc ...

  10. PAT 1091 Acute Stroke [难][bfs]

    1091 Acute Stroke (30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the ...