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 #使用 ...
随机推荐
- JAVA利用HttpClient进行POST请求(HTTPS)
目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...
- xunsearch使用namespace后bug修复
xunsearch在使用了namespace后会出现不能正常使用 错误例如以下: Fatal error: Uncaught [vendors\xunsearch\lib\XSException] . ...
- rails generate model/resource/scaffold的区别
If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, ...
- javaSE中的输入输出流---一个读取流,相应多个输出流。并且生成的碎片文件都有有序的编号
<span style="font-size:18px;">package com.io.other.split; import java.io.File; impor ...
- EJB学习笔记之十(BMT事务和CMT事务)
1.前言 前两篇博客主要介绍了与事务相关的知识.比如事务的一些特性,以及并发产生的问题.本篇来解说一下EJB中两种处理事务的方式.一种是以生命式方式来管理事务(CMT):还有一种则是在EJB内部使用 ...
- STL源码剖析(空间配置器)
前言 在STL中,容器的定义中都带一个模板参数,如vector template <class T, class Alloc = alloc> class vector {...} 其中第 ...
- 创建支持多种屏幕尺寸的apk
文章转至:http://hell0android.iteye.com/blog/1899605 创建对两种以上屏幕尺寸的多apk支持(Creating Multiple APKs with 2+ Di ...
- Python中函数参数传递问题【转】
1. Python passes everything the same way, but calling it "by value" or "by reference& ...
- 网站相关技术探究keepalive_timeout(转)
网站相关技术探究keepalive设多少: /proc/$PID/fd/$number 0:标准输入 1:标准输出 2:标准错误 Test: [root@KTQT ~]# ll /proc/12 ...
- Service#onStartCommand返回值解析
Service#onStartCommand返回值解析 Service类有个生命周期方法叫onStartCommand,每次启动服务(startService)都会回调此方法.此方法的原型例如以下: ...