本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7637729
建立时间:2006年5月29日




[Django]django with apache2
软件技术

lhwork 发表于 2007/2/3 11:46:21

One thing that I feel is harder than it should be with django is getting it working with Apache2. Sure the built in webserver is nice for development, but there comes a time when you need to step up and use a webserver for development that you can also deploy the site upon. I have used Apache a lot in the past, but recently I have decided to move over to installing everything the Debian way, and this will reflect that. I am going to use python2.3, Apache2 and the version of mod_python that is in stable. You can follow the instructions if you have installed Apache2 and mod_python in otherways after this step if you wish. To get Apache2 on Debian with mod_python, simply: apt-get install apache2apt-get install libapache2-mod-python For the rest of the tutorial, I am assuming that you have Django setup correctly, and working with your database. If not, I recommend installing the development version. It is easy to update using SVN and there are some features that are better than the stable version I find. Once you have done this you can start to build your site. From then on you can create your apache virtualhost file: I have decided to run my django site on a different port, but of course you can run it on different ipaddresses, and with different hostheaders. All you need to do is follow the apache 2 documentation for this. I feel this is already well documented so I will just show how I did it by running on different ports: Edit the ports file for apache2 to tell it to listen on more than one port: vi /etc/apache2/ports.confAdd this line to tell it to listen on port 8000: Listen 8000Create a new sites file for this new virtual host: vi /etc/apache2/sites-available/djangositeNow we need to put in the configuration details: <VirtualHost *:8000><Location "/">SetHandler mod_pythonPythonHandler django.core.handlers.modpythonPythonPath "['/home/tim/dev/projects/'] + sys.path"SetEnv DJANGO_SETTINGS_MODULE djangosite.settingsPythonDebug On</Location>Alias /media "/var/www/djangosite/media"<Location "/media/">SetHandler None</Location><LocationMatch "\.(jpg|gif|png)$">SetHandler None</LocationMatch></VirtualHost> This gives us several things: Sets up a virtualhost to listen on port 8000.For the virtual host use mod_python.PythonHandler is told to use Django.Our PythonPath is updated with our site files, in my case my django projects are located in /home/tim/dev/projects/ so this is what I use. Point this to where your django files are kept.Let django know to use the settings file for this projectThe we setup an alias to serve the media files, as they don’t need to be served through mod_python we turn the handler off for the /media/ location. I will setup my media files in /var/www/djangosite/mediaFinally tell apache2 not to use mod_python to serve image files. Now save this file, and enable the site:a2ensite djangositeNow create the webhosting directory, and the symbolic link for the admin directory, and check the permissions: mkdir /var/www/djangosite/cd /var/www/djangosite/ln -s /usr/lib/python2.3/site-packages/django/contrib/admin/media/chmod -R 755 /var/www/djangosite The stop and start apache2 on your server (this is for Debian using init.d scripts, but whatever you do stopping then starting is better than a simple reload)./etc/init.d/apache2 stop; /etc/init.d/apache2 start Once you have done that, check your site by point a webbrowser to the the machine that you have apache2 running on, but at port 8000 for instance if it is a localhost, tryhttp://127.0.0.1:8000. One thing that I haven’t talked about is where it is best to place all the files used in creating a django site, and I have yet to read any documentation about this. This is why in the above I am point it to a dev subdirectory from my home directory. I don’t think this is a good idea for a production site, and perhaps it should go in somewhere off /var. One thing is for sure, creating seperate directories for media and the actual files is a good idea, and you if you follow the django instructions you can take them up on the recommendation of using a seperate lightweight webserver to just serve these files.


阅读全文(2277) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.109 second(s), page refreshed 144792109 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号