在Apache和FastCGI上使用Django,你需要安裝和配置Apache,并且安裝mod_fastcgi。 請參見Apache和mod_fastcgi文檔: http://www.djangoproject.com/r/mod_fastcgi/ 。
當完成了安裝,通過 httpd.conf (Apache的配置文件)來讓Apache和Django FastCGI互相通信。 你需要做兩件事:
- 使用 FastCGIExternalServer 指明FastCGI的位置。
- 使用 mod_rewrite 為FastCGI指定合適的URL。
指定 FastCGI Server 的位置
FastCGIExternalServer 告訴Apache如何找到FastCGI服務器。 按照FastCGIExternalServer 文檔( http://www.djangoproject.com/r/mod_fastcgi/FastCGIExternalServer/ ),你可以指明 socket 或者 host 。以下是兩個例子:
1
2
3
4
5
|
# Connect to FastCGI via a socket/named pipe: FastCGIExternalServer /home/user/public_html/mysite.fcgi -socket /home/user/mysite.sock # Connect to FastCGI via a TCP host/port: FastCGIExternalServer /home/user/public_html/mysite.fcgi -host 127.0.0.1:3033 |
在這兩個例子中, /home/user/public_html/ 目錄必須存在,而 /home/user/public_html/mysite.fcgi 文件不一定存在。 它僅僅是一個Web服務器內部使用的接口,這個URL決定了對于哪些URL的請求會被FastCGI處理(下一部分詳細討論)。 (下一章將會有更多有關于此的介紹)
使用mod_rewrite為FastCGI指定URL
第二步是告訴Apache為符合一定模式的URL使用FastCGI。 為了實現這一點,請使用mod_rewrite 模塊,并將這些URL重定向到 mysite.fcgi (或者正如在前文中描述的那樣,使用任何在 FastCGIExternalServer 指定的內容)。
在這個例子里面,我們告訴Apache使用FastCGI來處理那些在文件系統上不提供文件
1
2
3
4
5
6
7
8
9
|
<VirtualHost 12.34.56.78> ServerName example.com DocumentRoot /home/user/public_html Alias /media /home/user/python/django/contrib/admin/media RewriteEngine On RewriteRule ^/(media.*)$ /$1 [QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^/(.*)$ /mysite.fcgi/$1 [QSA,L] </VirtualHost> |