ubuntu下发布Django Web的两种方法

来源:互联网 发布:js 获取文本框的值 编辑:程序博客网 时间:2024/06/10 19:48

一、利用django自带服务器框架发布web

 

    系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份、ftp 传输等等。通常情况下我们都是为每一个这样的任务开一个远程终端窗口,因为它们执行的时间太长了。必须等待它们执行完毕,在此期间不能关掉窗口或者断开连接,否则这个任务就会被杀掉,一切半途而废了。这时screen就可以派上用场了。

    screen进入一个新建的screen,此时关闭shell窗口,screendetached状态,exit为退出回到正常的shell窗口,此时关闭shell窗口为attached状态

当在正常窗口要连接detached窗口时,命令:screen -r 查看所有screenpid,当要连接某个的时候,命令:screen -r [pid],当要停止某个screen并且销毁时,我的作法是kill -s 9 [pid],此时对应screendead??? 状态,再执行screen -wipe清除即可。

参考:http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html

 

Django自带了发布Web的功能,如命令

python manage.py runserver 8080

其服务器日志直接在接入shell界面输出(default),通过一定设置应该也可以重定向到log文件中。

然而,当我们将shell关闭的时候,会发送关闭信号给django服务器,导致服务器关闭。有一种办法可以解决,利用screen

只需新建一个screen,再输入指令python manage.py runserver 0.0.0.0:8080 & 即可。

0.0.0.0: 或者 &都不能成功。

 

二、利用uwsgi+nginx发布django web

 

    配置之前,随便看看这两篇,开始按照第一篇完全来是成功的,第二篇就不行了,然后就各种google

http://django-china.cn/topic/101/

http://django-china.cn/topic/124/

 

    引用博主的说法,通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求。nginx把所有静态请求自己来处理(这是NGINX的强项)。然后,NGINX将所有非静态请求通过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。

本机部件为python2.7django1.7nginx1.6ubuntu12.04

先说说我的django工程文件部署情况

cimc2>

cimc2>

                  __init__.py

                  settins.py

                  urls.py

                wsgi.py

                 hello>

                               __init__.py

                               static>

                                               css

                                                js

                                                fonts

                              templates

                              views.py

                              admin.py

                              models.py

                manage.py

备注:cimc2为工程中的主文件,helloapp文件,static存放各种要用到样式文件,js或者csstemplates存放各种html的模板文件。

 

1uwsgidjango    

    在与manage.py同级的文件目录下新建django_socket.ini文件,也可以新建django_socket.xml文件,不过还要再装libxml(命令:sudo apt-get install libxml2-dev ),因为编译uwsgi前需要将libxml编译,所以要先安装libxml再,安装uwsgi(网上的说法),所以要先卸载掉,甚至把文件夹rm掉再按照libxml->uwsgi进行。

 

不过我用xml方式没有成功,不知为什么,就配置django_socket.ini文件。

 

uwsgi配置文件配置方法可以参考:

 

http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html

http://blog.csdn.net/sasoritattoo/article/details/17187949

 

我是这么搞的:

 

[uwsgi]socket=127.0.0.1:8011pidfile=/var/run/nginx.pidmaster=truechmod-socket=777enable-threads=trueworkers=4wsgi-file=django_wsgi.pylogdate=truedaemonize=/home/gugugujiawei/DjangoServer/cimc2/log/django.log

 

解释一下:

[uwsgi]

socket-------------用于uwsginginx的通信

pidfile------------当失去权限时写入信息到该处

master=true

chmod-socket=777---uwsginginx通信的socket权限,网上说644,反正777我可以

enable-threads-----允许多线程通信,感觉必须的

workers------------开启多个线程,一般4个,kill时可以一次杀掉4

wsgi-file----------uwsgidjango的配置文件

logdate------------随便加进去的

daemonize----------日志文件,不写的话就和发布python一样裸奔在shell界面

 

在与manage.py同级的文件目录下新建django_wsgi.py文件,原因在上面

#coding:utf-8import osimport sysfrom django.core.wsgi import get_wsgi_applicationreload(sys)sys.setdefaultencoding('utf8') sys.path.append(os.path.abspath(os.path.dirname(__file__)))os.environ.setdefault("DJANGO_SETTINGS_MODULE","cimc2.settings")#from django.core.handlers.wsgi import WSGIHandler#application=WSGIHandler()application=get_wsgi_application()

 

搞定这里之后,只是完成uwsgidjango的配置,接下来才是辛苦(调了好久)

 

2Nginx

 

    在捣腾nginx的时候,确实发现它的不少好处,规划性好,维护性强,各种配置用得好的话,可以优化你的系统,方便维护,也可以做出很好的负载均衡和系统资源分配。

    在/etc/ngin目录下可以看到

 

看到网上n多种方法,各种试,以下亲测可用

sites-available下有个default文件,该文件用来改变端口号即可,如

 

配置文件在这里,nginx.conf,按照之前工程文件的部署情况,这里可以这么写:

 

user root;worker_processes 4;pid /run/nginx.pid; events {worker_connections 768;# multi_accept on;} http { ### Basic Settings## sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off; # server_names_hash_bucket_size 64;# server_name_in_redirect off; include /etc/nginx/mime.types;default_type application/octet-stream; ### Logging Settings## access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log; ### Gzip Settings## gzip on;gzip_disable "msie6"; # gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ### Virtual Host Configs## include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;server{listen 8080;server_name 127.0.0.1;charset utf-8;client_max_body_size 25M;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.# try_files $uri $uri/ =404;include uwsgi_params;uwsgi_pass 127.0.0.1:8011;access_log off;}location /static{alias /home/gugugujiawei/DjangoServer/cimc2/hello/static;}} }  #mail {## See sample authentication script at:## http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript# ## auth_http localhost/auth.php;## pop3_capabilities "TOP" "USER";## imap_capabilities "IMAP4rev1" "UIDPLUS";# #server {# listen     localhost:110;# protocol   pop3;# proxy      on;#}# #server {# listen     localhost:143;# protocol   imap;# proxy      on;#}#}


user改为rootuwsgi_passinisocket配置端口对应(注意别被其他程序占用了),再补充一点:

Html里面如:

<script src="/static/js/jquery-1.11.0.js" ></script>

Settings.py里面如:

 

还有STATIC_URL=’/static/’,STATIC_ROOT我没加进去。


再进阶学习之后,才知道在nginx.conf中include一个个的conf文件就可以实现监听多个server,当然,是不同端口。

之后,由于uwsgi不能像django自带发布服务器runserver那样修改源码则自动加载,因而需要重启uwsgi程序。

重启步骤试过很多种,发现这种简单粗暴:

1、查看nginx与uwsgi通信的socket端口

netstat -anp |grep xxxx

2、杀死对应的进程

kill -s 9 ****

3、重启uwsgi

uwsgi --ini django_socket.ini

4、重启nginx

killall -s 9 nginx(直接杀死进程,因为有时候出现stop不了的情况)

/etc/init.d/nginx start

转载前请留下网址:http://blog.csdn.net/gugugujiawei

1 0
原创粉丝点击