Quantcast
Channel: python –瀚海星空
Viewing all articles
Browse latest Browse all 10

django 在ubuntu12.04上的部署

$
0
0

假设我在/path/www/ 下调django-admin.py startproject t3, 然后mv t3 web
所以我网站放在/path/www/web/下面,该目录下有manage.py,t3目录等

1.修改配置
修改settings.py文件名,并将里面debug等选项设为False
zhouhh@ubuntu:~/path/www/web/t3$ cp settings.py mysettings.py

2.创建wsgi
该文件也可以放在/path/www/web/下或别的地方
zhouhh@ubuntu:~/path/www$ cat django.wsgi

import os
import sys
path='/path/www/web'
os.environ['DJANGO_SETTINGS_MODULE'] = 't3.mysettings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
#path = os.path.dirname(__file__)
if path not in sys.path:
    sys.path.append(path)

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

此处注意path设绝对路径.

3.修改apache配置
root@ubuntu:~# cat /etc/apache2/sites-enabled/000-default

<virtualHost *:80>
        ServerAdmin webmaster@localhost
#zhh
    WSGIScriptAlias / /path/www/django.wsgi
    Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin
    Alias /static /path/www/web/static

    DocumentRoot /path/www/web
    <directory "/path/www/web">
        Order Deny,Allow
        Allow from all
    </directory>
    <location "/static/">
        SetHandler None
        Order allow,deny
        Allow from all
    </location>
    <location "/static/admin">
        SetHandler None
        Order allow,deny
        Allow from all
    </location>
#zhh end
...

此处注意
1.WSGIScriptAlias 指向的路径和创建的wsgi文件一致。
2./static/admin 静态文件别称在/static之前,否则会出警告,并找不到相应资源。
3.部署时不要在urls.py里面设置(r’^static/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: settings.MEDIA_ROOT}),这仅能用于测试,部署会性能很低并且导致安全问题。而是直接交给web服务器去处理。
所以配置了Location “/static/admin” 和Location “/static”

4.重启apache2
root@ubuntu:~# service apache2 restart

此时,即可通过缺省80端口访问django网页。

5.修改mysetting.py
STATIC_ROOT指向静态文件指向的绝对路径
zhouhh@ubuntu:~/path/www/web$ vi t3/settings.py
STATIC_ROOT = ‘/path/www/web/static/’
部署settings文件如下,
zhouhh@ubuntu:~/www/web$ cat t3/mysettings.py
from settings import *
DEBUG = False

6.收集静态文件
zhouhh@ubuntu:~/path/www/web$ ./manage.py collectstatic
该命令将各应用的静态文件复制到STATIC_ROOT指定的目录。 否则,django应用的静态资源会没法访问,找不到相关静态文件。

7.相关环境

root@ubuntu:~# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.1 LTS"
root@ubuntu:~# uname -a
Linux ubuntu 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
root@ubuntu:~# apachectl -v
Server version: Apache/2.2.22 (Ubuntu)
Server built:   Nov  8 2012 21:37:30
root@ubuntu:~# apachectl -l
Compiled in modules:
  core.c
  mod_log_config.c
  mod_logio.c
  prefork.c
  http_core.c
  mod_so.c
root@ubuntu:~# apachectl -M
Loaded Modules:
 core_module (static)
 log_config_module (static)
 logio_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 alias_module (shared)
 auth_basic_module (shared)
 authn_file_module (shared)
 authz_default_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
 wsgi_module (shared)
Syntax OK
root@ubuntu:~#

8.参考
https://docs.djangoproject.com/en/dev/howto/static-files/


Viewing all articles
Browse latest Browse all 10

Trending Articles