Django项目部署(django+guncorn+virtualenv+nginx)
一、说明
为了django项目部署到生产环境上,能够稳定的运行,且能够同时指出http和https的访问,对django的部署进行了一些研究,决定采用django + gunicorn + virtualenv +nginx + mysql的方式进行部署,下面是本次部署项目的目录结构:
二、虚拟包的制作
本次部署是采用virtualenv方式的部署,考虑到可能在多台机器上进行部署(例如:在多台测试机器上部署),所以为了减少Python模块包的安装,决定将虚拟环境打成包的形式,可以考到任意一台环境上执行,避免了在每台机器上都要安装相应的模块包,一下是虚拟包的制作方法:
- 首先在你的linux的服务器上安装python2.7, ./configure --prefix=/home/kxdqa/mock-server/py,然后执行make && make install 进行python的安装
- 安装完python后,进行virtualenv的安装
- 然后创建你所需要的python的虚拟env环境,virtualenv -p /home/kxdqa/mock-server/py/bin/python2.7 /home/kxdqa/mock-server/myenv,myenv里面会产生一个bin目录和一个lib目录,bin里面有python,activate,easy_install,而且以后你要装django,django-admin.py也会放到这里,lib目录下有site-package,装的lib会放到这里来.
- 然后进入到虚拟环境myenv中,. bin/activate,进入虚拟环境展示如下:
(myenv) [root@test_172_30_131_183 myenv]#
然后在虚拟环境进行你所需要的模块包的安装
- 为了虚拟包可以在别的机器上依然可以运行,则需要修改bin/activate这个脚本,找到设置VIRTUAL_ENV变亮的地方,改成如下:
export VIRTUAL_ENV=`pwd`
- 本次的服务器部署包,需要再安装gunicorn,安装方式可以采用pip或者源码安装都行
- 然后将myenv打包即可
三、django项目的配置
1.gunicorn的配置
- 在当前的django项目下,添加gunicorn的 配置文件gunicorn.conf.py,内容如下:
import multiprocessing # 绑定ip和端口 bind = "0.0.0.0:8088" # 用于处理工作进程的数量 workers = multiprocessing.cpu_count() * 2 + 1 # 错误日志 errorlog = "/home/kxdqa/logic_mock/logs/gunicorn.error.log"
2.django静态文件的配置注:gunicorn的配置详解见:点击打开链接
- 在django项目的settings.py配置文件中 ,添加STATIC_ROOT的配置,配置如下:
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static/')
- 执行python manage.py collectstatic命令,就会将所有的静态文件复制一份到collect_static的文件夹下
3.尝试启动
gunicorn的启动方式有两种:
- 使用配置文件的方式:gunicorn logic_mock.wsgi:application -c /home/kxdqa/logic_mock/gunicorn.conf.py
- 不使用配置文件的方式:gunicorn logic_mock.wsgi:application -b 0.0.0.0:8088
guncorn的启动后的关闭命令:ps aux | grep -Ei 'logic_mock.wsgi' | grep -v 'grep' | awk '{print $2}' | xargs kill
四、Nginx的安装
1.安装
- 安装gcc编译模块:yum install gcc
- 安装g++编译模块:yum install gcc-c++ libstdc++-devel
- 安装nginx的依赖包:
yum -y install pcre-devel
yum install -y zlib-devel yum install -y openssl(安装nginx报错的,把这个装上)
下载nginx的安装包,进行安装:
./configure--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 编译安装:make && make install
注:后面的 --with必须要有,不然nginx进行ssl设置的话,会出错
验证是否安装成功:cd /usr/local/nginx/sbin/ . nginx 进行启动(如果启动失败的话,查看80端口是否被占用) 然后到浏览器上输入ip地址,看是否出现nginx的欢迎上,如果出现则安装成功
2.nginx配置
- 编写nginx的启动脚本,将脚本放到/etc/init.d下,脚本如下:
#!/bin/bash
#nginx Startup script for the Nginx HTTP Server
# it is v. version.
# chkconfig: -
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} =
[ -x $nginxd ] || exit
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit
esac
exit $RETVAL
nginx启动脚本
- 执行如下命令,添加到服务中:
chkconfig --add /etc/init.d/nginx chmod 755 /etc/init.d/nginx chkconfig --add nginx 至此,nginx安装成功 nginx启动、停止、无间断服务重启,可选 start | stop | restart | reload | status | help service nginx start 启动 servie nginx stop 停止 service nginx help 帮助
- 对nginx配置django项目,配置文件nginx.conf如下:
user root; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections ; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout ; keepalive_timeout ; #gzip on; server { listen default backlog=; listen ssl; server_name localhost; access_log /home/kxdqa/logic_mock/logs/nginx_access.log; error_log /home/kxdqa/logic_mock/logs/nginx_error.log; charset utf-; ssl_certificate /home/kxdqa/logic_mock/cert/logic_server.crt; ssl_certificate_key /home/kxdqa/logic_mock/cert/logic_server.key; #ssl_protocols SSLv2 SSLv3 TLSv1.; ssl_protocols TLSv1 TLSv1. TLSv1.; ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; #ssl_session_timeout 5m; #index index.html index.htm #charset koi8-r; #access_log logs/host.access.log main; # location / { #proxy_redirect off; proxy_pass http://127.0.0.1:8088; 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 ~ ^/(media|static) { root /home/kxdqa/logic_mock; expires 30d; } #error_page /.html; # redirect server error pages to the static page /50x.html # error_page /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on # #location ~ \.php$ { # root html; # fastcgi_pass ; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen ; # listen somename:; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen ; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }nginx.conf的配置
至此,django项目安装完成,一次启动nginx和gunicorn即可(如果需要使用https,则需要在目录结构的certs目录下,添加ssl证书,然后在nginx中改变证书的名字,重启nginx即可)
Django项目部署(django+guncorn+virtualenv+nginx)的更多相关文章
- 使用gunicorn将django项目部署到生产环境的子目录下,在nginx后端获取客户真实IP地址
生产环境有时,并不是为了一个项目而存在的.毕竟,域名是比较稀有的. 今天遇到这个问题,解决了.作个记录. 并且,如果将django项目部署在Nginx后面,那如何获取用户真实的IP地址呢? 下面就来解 ...
- django项目部署过程
django项目部署过程 1.上传代码 用git或者其他工具,如scp 代码上传后保证每个应用下的migrations文件夹里只有一个__init__.py文件,自己的迁移文件不要上传上来,具体的gi ...
- 五步教你实现使用Nginx+uWSGI+Django方法部署Django程序
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...
- Linux Django项目部署
步骤 .数据库的处理 1.1 上传bbs.sql 1.2 在mysql中创建bbs库,并导入数据库SQL脚本 mysql> create database bbs charset utf8mb4 ...
- 使用Nginx+uWSGI+Django方法部署Django程序(上)
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...
- Windows下django项目部署 通过Apache2.4+mod_wsgi
经过几天踩坑,记录在Windows10下通过Apache2.4部署Django项目的过程 运行环境: 先说下环境,怎么安装倒是其次的,版本很重要,我是根据mod_wsgi的版本要求下载的各个版本(py ...
- Django项目部署在Linux下以进程方式启动
Django项目部署在Linux下以进程方式启动 这是一篇关于如何在linux下,以后台进程的方式运行服务,命令改改基本上就通用了. 开发完Django项目后,需要把项目部署到linux环境下.当然, ...
- 使用Nginx+uWSGI+Django方法部署Django程序(下)
在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的, ...
- Django项目和Django初体验和创建、目录结构认识
.MVC的设计方式(跟Flask一样,都是MVC的设计模式) .开发效率高 .功能强大(丰富的第三方组件) .安全性高(帮助开发者规避安全漏洞) 目前市面上使用:Django>Flask #使用 ...
随机推荐
- Django——如何处理请求(URL配置和视图)
URLconfig—— 为了绑定视图函数和URL,我们使用URLconf. URLconf 就像是 Django 所支撑网站的目录. 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间 ...
- Js中数组的追加
Concat arrayObject.concat(arrayX,arrayX,......,arrayX) 常用于 加载更多 ,数组的追加.
- JavaScript 创建对象的几种模式
在JavaScript中虽说可以用Object的构造函数或者字面量创建单个对象,但是用这些方式来创建多个对象时就有一个明显的缺点,产生了大量的重复代码.为解决这些问题,许多模式就应运而生. 1. 工厂 ...
- 改动Androidproject的名称(非Eclipse重命名)
问题背景 在Eclipse,Import新的Android源代码project时.假设Eclipse的workspace已经存在同样名称project,是无法导入的. 网上有非常多改动工程名的方法.是 ...
- 安装Drupal7.12+Postgresql9.1(Ubuntu Server 12.04)
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ OS环境准备 这次是从OS开始安装的.最开始装Ubuntu12.04这里就不再赘述, 唯 ...
- xcode7.1.1不能真机调试ios9.2系统设备的解决方法
转载自:http://www.cocoachina.com/bbs/read.php?tid-331335.html 前些天手机升级到iOS9.2版本号 xcode7.1还能真机測试. 昨晚更新xc ...
- org.apache.jasper.compiler.TldLocationsCache tldScanJar
我在页面上一点击查询.console以下就有例如以下的红色文字: 2014-8-19 15:09:27 org.apache.jasper.compiler.TldLocationsCache tld ...
- 信号处理的好书Digital Signal Processing - A Practical Guide for Engineers and Scientists
诚心给大家推荐一本讲信号处理的好书<Digital Signal Processing - A Practical Guide for Engineers and Scientists>[ ...
- Eclipse maven 项目红叉 编译不报错问题处理
项目右键-> Maven -> Update Maven Project 选中 :Force update 复选框
- vs2015创建webService