为了试django项目部署到生产环境上,能够稳定的运行,且能够同时指出http和https的访问,对django的部署进行了一些研究,决定采用django + gunicorn + virtualenv +nginx + mysql的方式进行部署,下面是本次部署项目的目录结构:
本次部署是采用virtualenv方式的部署,考虑到可能在多台机器上进行部署(例如:在多台测试机器上部署),所以为了减少Python模块包的安装,决定将虚拟环境打成包的形式,可以考到任意一台环境上执行,避免了在每台机器上都要安装相应的模块包,一下是虚拟包的制作方法:
(myenv) [root@test_172_30_131_183 myenv]#
然后在虚拟环境进行你所需要的模块包的安装
export VIRTUAL_ENV=`pwd`
import multiprocessing # 绑定ip和端口 bind = "0.0.0.0:8088" # 用于处理工作进程的数量 workers = multiprocessing.cpu_count() * 2 + 1 # 错误日志 errorlog = "/home/kxdqa/logic_mock/logs/gunicorn.error.log" |
注:gunicorn的配置详解见:点击打开链接
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static/') |
gunicorn的启动方式有两种:
guncorn的启动后的关闭命令:ps aux | grep -Ei 'logic_mock.wsgi' | grep -v 'grep' | awk '{print $2}' | xargs kill
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的欢迎上,如果出现则安装成功
#!/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 |
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 帮助 |
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; # } #} } |
至此,django项目安装完成,一次启动nginx和gunicorn即可(如果需要使用https,则需要在目录结构的certs目录下,添加ssl证书,然后在nginx中改变证书的名字,重启nginx即可)