Django项目部署(django+guncorn+virtualenv+ngin…

2018-12-06 07:35:53来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 

一、说明

    为了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.0.0.2 version.


# chkconfig: - 85 15


# 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=0


prog="nginx"


# Source function library.


. /etc/rc.d/init.d/functions


# Source networking configuration.


. /etc/sysconfig/network


# Check that networking is up.


[ ${NETWORKING} = "no" ] && exit 0


[ -x $nginxd ] || exit 0


# Start nginx daemons functions.


start() {


if [ -e $nginx_pid ];then


   echo "nginx already running...."


   exit 1


fi


   echo -n $"Starting $prog: "


   daemon $nginxd -c ${nginx_config}


   RETVAL=$?


   echo


   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx


   return $RETVAL


}


# Stop nginx daemons functions.


stop() {


        echo -n $"Stopping $prog: "


        killproc $nginxd


        RETVAL=$?


        echo


        [ $RETVAL = 0 ] && 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 1


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  1;
    
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    
    #pid        logs/nginx.pid;
    
    
    
    
    events {
        worker_connections  1024;
    }
    
    
    
    
    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  0;
        keepalive_timeout  65;
    
    
        #gzip  on;
    
    
        server {
            listen      80 default backlog=2048;
    listen      443 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-8;
    
    
    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.2;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    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  404              /404.html;
    
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    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       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
    
    
        # HTTPS server
        #
        #server {
        #    listen       443;
        #    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即可)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:SSL证书的生成

下一篇:python深浅拷贝