GOOD centos + apache + mod_wsgi + python2.6 + django web环境搭建

centos + apache + mod_wsgi + python2.6 + django web环境搭建 - 望仔的日志 - 网易博客

centos + apache + mod_wsgi + python2.6 + django web环境搭建   

2011-12-17 14:14:55|  分类: python |  标签:  |字号  订阅

一、环境,python2.6.5
    1、contos 5.5
    2、apache2.2.6
    3、Django-1.3.1
    4、mod_wsgi-3.3
二、安装django
    python setup.py install
三、Apache2.2.6安装:
    ./configure --prefix=/usr/local/apache2--enable-mods-shared=most--enable-rewrite--enable-forward--with-mpm=worker
    make
    make install
四、安装mod_wsgi:
   ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/bin/python
    make
    make install
五、创建工程
cd /usr/local/apache2/htdocs
export PATH=/usr/local/python/bin:$PATH
/usr/bin/django-admin.py startproject mysite 
 
创建一个工程,会在htdocs下创建一个mysite目录
cd mysite
python2.7 manage.py runserver 127.0.0.1:8000   
启动开发环境的web服务,可以在浏览器里访问此地址 http://127.0.0.1:8000
 
注意的是python和django的命令尽量带上全路径,以防用成了系统自带的!!!!
六、配置
我的应用在/usr/local/apache2/htdocs下,名称叫mysite,在mysite中创建apache与media目录,
然后在apache下创建两个文件apache_django_wsgi.conf 和 django.wsgi
    1、创建apache_django_wsgi.conf文件,内容如下:
Alias /site_media/ /usr/local/apache2/htdocs/mysite/media/
<Directory /usr/local/apache2/htdocs/mysite/media>
    Order allow,deny
    Options Indexes
    Allow from all
    IndexOptions FancyIndexing
</Directory>
 
Alias /media/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
<Directory /usr/lib/python2.6/site-packages/django/contrib/admin/media>
    Order allow,deny
    Options Indexes
    Allow from all
    IndexOptions FancyIndexing
</Directory>
WSGIScriptAlias / /usr/local/apache2/htdocs/mysite/apache/django.wsgi
<Directory /mnt/www/mysite>
    Order deny,allow 
    Allow from all
</Directory>
<Directory /mnt/www/mysite/apache>
    Allow from all
</Directory>
    2、创建django.wsgi文件,内容如下:
import os, sys
#Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
print >> sys.stderr, sys.path
3、设置apache的httpd.conf文件添加:
LoadModule wsgi_module modules/mod_wsgi.so
Include /usr/local/apache2/htdocs/mysite/apache/apache_django_wsgi.conf
七、效果:
执行:/usr/local/apache2/bin/apachectl start
访问http://127.0.0.1,显示如下:
GOOD centos + apache + mod_wsgi + python2.6 + django web环境搭建_第1张图片
 
八、测试

进入到mysite目录中,运行web测试服务器:

python manage.py runserver

提示在 http://127.0.0.1:8000/ 运行啦,可以访问一下看看。系统自动提示:It worked!

 

现在在mysite下创建一个文件,起名为:views.py,里面加入内容:

from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello django!")

更改urls.py的内容为:

 

from django.conf.urls.defaults import patterns, include, url

 

urlpatterns = patterns('',
   ('^hello/', 'mysite.views.hello')
)


http://127.0.0.1:8000/hello/

  评论这张
转发至微博
转发至微博

你可能感兴趣的:(apache)