Throughout this book, weve mentioned a number of goals that drive the development of Django. Ease of use, friendliness to new programmers, abstraction of repetitive tasks these all drive Djangos developers.
鍦ㄨ繖鏈功涓紝鎴戜滑鎻愬埌浜嗛┍浣緿jango鍙戝睍鐨勫緢澶氱洰鏍囥傛槗鐢紝瀵瑰垵瀛﹁呭弸濂斤紝閲嶅浠诲姟鐨勬娊璞★紝 杩欎簺閮介┍浣跨潃Django缁х画鍙戝睍銆
However, since Djangos inception, theres always been another important goal: Django should be easy to deploy, and it should make serving large amounts of traffic possible with limited resources.
鐒惰岋紝浠嶥jango涓寮濮嬶紝灏辨湁鍙︿竴涓噸瑕佺殑鐩爣锛欴jango搴旇瀹规槗琚儴缃诧紝骞朵笖瀹冨簲璇ヨ兘澶熺敤 鏈夐檺鐨勮祫婧愭彁渚涘ぇ閲忕殑鏈嶅姟銆
The motivations for this goal are apparent when you look at Djangos background: a small, family-owned newspaper in Kansas can hardly afford top-of-the-line server hardware, so Djangos original developers were concerned with squeezing the best possible performance out of limited resources. Indeed, for years Djangos developers acted as their own system administrators there simply wasnt enough hardware to need dedicated sysadmins even as their sites handled tens of millions of hits a day.
杩欐牱鐨勫姩鏈烘槸寰堟槑鏄剧殑锛屽綋浣犵湅鍒 Django 鐨勮儗鏅細鍫惃鏂窞涓涓皬宸х殑銆佸鏃忓紡鎶ョ焊浼佷笟 璐熸媴涓嶈捣楂樺搧璐ㄧ殑鏈嶅姟鍣ㄧ‖浠讹紝鎵浠 Django 鐨勬渶鍒濆紑鍙戣呬滑閮介潪甯哥殑鍏冲績濡備綍鎵嶈兘浠庢湁闄 鐨勮祫婧愪腑鎸ゅ帇鍑烘渶濂界殑鎬ц兘銆傜‘瀹烇紝杩欎簺骞存潵 Django 鐨勫紑鍙戣呬滑鍏呭綋浜嗕粬浠嚜宸辩殑绯荤粺绠 鐞嗗憳銆傝櫧鐒朵粬浠殑绔欑偣姣忓ぉ澶勭悊涓婂崈涓囩殑鐐瑰嚮閲忥紝浣嗕粬浠‘瀹炴病鏈夐偅涔堝鏁伴噺鐨勭‖浠朵互鑷充簬*闇瑕*涓撻棬鐨勭郴缁熺鐞嗗憳銆
As Django became an open source project, this focus on performance and ease of deployment became important for a different reason: hobbyist developers have the same requirements. Individuals who want to use Django are pleased to learn they can host a small- to medium-traffic site for as little as $10 a month.
褰 Django 鎴愪负涓涓紑婧愰」鐩悗锛屽叧娉ㄥ湪鍏舵ц兘鍜屽紑鍙戠殑绠鏄撴у洜涓烘煇浜涘師鍥犲彉寰楃壒鍒噸瑕侊細 涓氫綑鐖卞ソ鑰呬篃鏈夊悓鏍风殑闇姹傘傛湁涓浜涗汉鎯宠鑺辫垂浠呬粎 10 缇庡厓骞朵娇鐢 Django 鏉ヤ綋楠屽埗浣滀竴涓 涓皬瑙勬ā娴侀噺鐨勭珯鐐广
But being able to scale down is only half the battle. Django also needs to be capable of scaling up to meet the needs of large companies and corporations. Here, Django adopts a philosophy common among LAMP-like Web stacks often called shared nothing .
浣嗘槸灏忚妯$殑搴旂敤鍙槸鐩爣鐨勪竴鍗婅屽凡銆侱jango 涔熼渶瑕佽兘澶熷鍔犲叾瑙勬ā鏉ユ弧瓒冲ぇ鍨嬪叕鍙稿拰闆嗗洟 鐨勯渶姹傘傝繖閲岋紝Django 閲囧彇浜嗙被浼间簬 LAMP 褰㈠紡鐨 Web 闆嗙殑鍝插鐞嗚锛岄氬父绉颁负 鏃犲叡浜(shared nothing) 銆
Whats LAMP?
浠涔堟槸 LAMP锛
The acronym LAMP was originally coined to describe a popular set of open source software used to drive many Web sites:
LAMP 杩欎釜缂╁啓鏈鍒濇槸鍒涢犲畠鏉ユ弿杩颁竴绯诲垪椹卞姩 Web 绔欑偣鐨勬祦琛岀殑寮婧愯蒋浠讹細
Linux (operating system)
Linux锛堟搷浣滅郴缁燂級
Apache (Web server)
Apache锛圵eb 鏈嶅姟鍣級
MySQL (database)
MySQL锛堟暟鎹簱锛
PHP (programming language)
PHP锛堢紪绋嬭瑷锛
Over time, though, the acronym has come to refer more to the philosophy of these types of open source software stacks than to any one particular stack. So while Django uses Python and is database-agnostic, the philosophies proven by the LAMP stack permeate Djangos deployment mentality.
闅忕潃鏃堕棿鐨勬帹绉伙紝杩欎釜缂╁啓宸茬粡鍙樺緱娑夊強浜嗘洿澶氬紑婧愯蒋浠舵爤鐨勫摬瀛︽濇兂锛岃屼笉浠呬粎鍐嶆槸灞闄愪簬鐗瑰畾鐨勬煇涓绉嶄簡銆傛墍浠ュ綋 Django 浣跨敤 Python 骞跺彲浠ヤ娇鐢ㄥ绉嶆暟鎹簱浜у搧鏃讹紝LAMP 杞欢鏍堣瘉瀹炵殑涓浜涚悊璁哄氨娓楅忓埌 Django 涓簡銆
There have been a few (mostly humorous) attempts at coining a similar acronym to describe Djangos technology stack. The authors of this book are fond of LAPD (Linux, Apache, PostgreSQL, and Django) or PAID (PostgreSQL, Apache, Internet, and Django). Use Django and get PAID!
杩欓噷灏濊瘯寤虹珛涓涓被浼肩殑缂╁啓鏉ユ弿杩 Django 鐨勬妧鏈爤銆傛湰涔︾殑浣滆呭枩娆㈢敤 LAPD锛圠inux, Apache, PostgreSQL, and Django锛夋垨PAID锛圥ostgreSQL, Apache, Internet, and Django锛夈 浣跨敤 Django 骞堕噰鐢 PAID 鍚э紒
At its core, the philosophy of shared nothing is really just the application of loose coupling to the entire software stack. This architecture arose in direct response to what was at the time the prevailing architecture: a monolithic Web application server that encapsulates the language, database, and Web server even parts of the operating system into a single process (e.g., Java).
鏃犲叡浜摬瀛︾殑鏍稿績瀹為檯涓婂氨鏄簲鐢ㄧ▼搴忓湪鏁翠釜杞欢鏍堜笂鐨勬澗鑰﹀悎鎬濇兂銆傝繖涓灦鏋勭殑浜х敓鐩存帴鍝嶅簲浜嗗綋鏃剁殑涓绘祦鏋舵瀯鑳屾櫙锛氬皢web搴旂敤鏈嶅姟鍣ㄤ綔涓轰笉鍙垎鐨勬暣浣擄紝瀹冨皢璇█銆佹暟鎹簱銆亀eb鏈嶅姟鍣ㄧ敋鑷虫搷浣滅郴缁熺殑涓閮ㄥ垎灏佽鍒板崟涓繘绋嬩腑(濡俲ava)銆
When it comes time to scale, this can be a major problem; its nearly impossible to split the work of a monolithic process across many different physical machines, so monolithic applications require enormously powerful servers. These servers, of course, cost tens or even hundreds of thousands of dollars, putting large-scale Web sites out of the reach of cash-hungry individuals and small companies.
褰撻渶瑕佷几缂╂ф椂锛屽氨浼氱鍒颁笅闈㈣繖涓富瑕侀棶棰橈細鍑犱箮涓嶅彲鑳芥妸娣蜂负涓浣撶殑杩涚▼鎵骞茬殑浜嬫儏鍒嗚В鍒拌澶氫笉鍚岀殑鐗╃悊鏈哄櫒涓婏紝鍥犳杩欑被搴旂敤灏卞繀椤昏鏈夋瀬涓哄己澶х殑鏈嶅姟鍣ㄦ潵鏀拺銆傝繖浜涙湇鍔″櫒锛岄渶瑕佽姳璐规暟涓囩敋鑷虫暟鍗佷竾缇庨噾锛屼粠鑰屼娇杩欑被澶ц妯eb缃戠珯杩滅浜嗚储鏀夸笉瀹借鐨勪釜浜哄拰灏忓叕鍙搞
What the LAMP community noticed, however, was that if you broke each piece of the Web stack up into individual components, you could easily start with an inexpensive server and simply add more inexpensive servers as you grew. If your $3,000 database server couldnt handle the load, youd simply buy a second (or third, or fourth) until it could. If you needed more storage capacity, youd add an NFS server.
LAMP绀惧尯娉ㄦ剰鍒帮紝濡傛灉灏哤eb鏍堝垎瑙d负澶氫釜鐙珛鐨勭粍浠讹紝浜轰滑灏辫兘浠庡鐨勪粠寤変环鐨勬湇鍔″櫒寮濮嬭嚜宸辩殑浜嬩笟锛岃屽湪鍙戝睍澹ぇ鏃跺彧闇瑕佹坊鍔犳洿澶氱殑寤変环鏈嶅姟鍣ㄣ傚鏋3000缇庡厓鐨勬暟鎹簱鏈嶅姟鍣ㄤ笉瓒充互澶勭悊璐熻浇锛屽彧闇瑕佺畝鍗曠殑璐拱绗簩涓(鎴栫涓夈佺鍥涗釜)鐩村埌瓒冲銆傚鏋滈渶瑕佹洿澶氱殑瀛樺偍绌洪棿锛屽彧闇澧炲姞鏂扮殑NFS鏈嶅姟鍣ㄣ
For this to work, though, Web applications had to stop assuming that the same server would handle each request or even each part of a single request. In a large-scale LAMP (and Django) deployment, as many as half a dozen servers might be involved in handling a single page! The repercussions of this are numerous, but they boil down to these points:
浣嗘槸锛屼负浜嗕娇杩欎釜鎴愪负鍙兘锛學eb搴旂敤蹇呴』涓嶅啀鍋囪鐢卞悓涓涓湇鍔″櫒澶勭悊鎵鏈夌殑璇锋眰锛岀敋鑷冲鐞嗗崟涓姹傜殑鎵鏈夐儴鍒嗐傚湪澶ц妯AMP(浠ュ強Django)鐨勯儴缃茬幆澧冧腑锛屽鐞嗕竴涓〉闈㈢敋鑷充細娑夊強鍒板杈惧崐鎵撶殑鏈嶅姟鍣紒杩欎竴鏉′欢浼氬鍚勬柟闈骇鐢熻澶氬奖鍝嶏紝浣嗗彲浠ュ綊缁撳埌浠ヤ笅鍑犵偣锛
State cannot be saved locally . In other words, any data that must be available between multiple requests must be stored in some sort of persistent storage like the database or a centralized cache.
涓嶈兘鍦ㄦ湰鍦颁繚瀛樼姸鎬 銆備篃灏辨槸璇达紝浠讳綍闇瑕佸湪澶氫釜璇锋眰闂村叡浜殑鏁版嵁閮藉繀椤讳繚瀛樺湪鏌愮褰㈠紡鐨勬寔涔呮у瓨鍌(濡傛暟鎹簱)鎴栭泦涓寲缂撳瓨涓
Software cannot assume that resources are local . For example, the Web platform cannot assume that the database runs on the same server; it must be capable of connecting to a remote database server.
杞欢涓嶈兘鍋囪璧勬簮鏄湰鍦扮殑 銆備緥濡傦紝Web骞冲彴涓嶈兘鍋囪鏁版嵁搴撹繍琛屼簬鍚屼竴涓湇鍔″櫒涓婏紱鍥犳锛屽畠蹇呴』鑳藉杩炴帴鍒拌繙绋嬫暟鎹簱鏈嶅姟鍣ㄣ
Each piece of the stack must be easily moved or replicated . If Apache for some reason doesnt work for a given deployment, you should be able to swap it out for another server with a minimum of fuss. Or, on a hardware level, if a Web server fails, you should be able to replace it with another physical box with minimum downtime. Remember, this whole philosophy is based around deployment on cheap, commodity hardware. Failure of individual machines is to be expected.
Web鏍堜腑鐨勬瘡涓儴鍒嗛兘蹇呴』鏄撲簬绉诲姩鎴栧鍒 銆傚鏋滃湪閮ㄧ讲鏃跺洜涓烘煇绉嶅師鍥燗pache涓嶈兘宸ヤ綔锛屽繀椤昏兘澶熷湪鏈灏忕殑浠d环涓嬪垏鎹㈠埌鍏跺畠鏈嶅姟鍣ㄣ傛垨鑰咃紝鍦ㄧ‖浠跺眰娆★紝濡傛灉Web鏈嶅姟鍣ㄥ穿婧冿紝蹇呴』鑳藉鍦ㄦ渶灏忕殑瀹曟満鏃堕棿鍐呮浛鎹㈠埌鍙︿竴鍙版満鍣ㄣ傝璁颁綇锛屾暣涓摬瀛﹀熀浜庡皢搴旂敤閮ㄧ讲鍦ㄥ粔浠风殑銆佸晢鍝佸寲鐨勭‖浠朵笂銆傚洜姝わ紝鏈氨搴斿綋棰勮鍒板崟涓満鍣ㄧ殑澶辨晥銆
As youve probably come to expect, Django handles this more or less transparently no part of Django violates these principles but knowing the philosophy helps when it comes time to scale up.
灏辨墍鏈熸湜鐨勯偅鏍凤紝Django鎴栧鎴栧皯閫忔槑鐨勫鐞嗗ソ浜嗚繖浠朵簨鎯咃紝娌℃湁涓涓儴鍒嗚繚鍙嶈繖浜涘師鍒欍備絾鏄紝浜嗚В鏋舵瀯璁捐鑳屽悗鐨勫摬瀛︼紝鍦ㄦ垜浠鐞嗕几缂╂ф椂灏嗕細鏈夋墍瑁ㄧ泭銆
But Does It Work?
浣嗚繖鏋滅湡瑙e喅闂锛
This philosophy might sound good on paper (or on your screen), but does it actually work?
杩欎竴鍝插鍙兘鍦ㄧ悊璁轰笂(鎴栬呭湪灞忓箷涓)鐪嬭捣鏉ヤ笉閿欙紝浣嗘槸鍚︾湡鐨勮兘瑙e喅闂锛
Well, instead of answering directly, lets instead look at a by-no-means-complete list of a few companies that have based their business on this architecture. You might recognize some of these names:
濂戒簡锛屾垜浠笉鐩存帴鍥炵瓟杩欎釜闂锛屽厛璇风湅涓涓嬪皢涓氬姟寤虹珛鍦ㄤ笂杩版灦鏋勪笂鐨勫叕鍙哥殑涓嶅畬鍏ㄥ垪琛ㄣ傚ぇ瀹跺彲鑳藉凡缁忕啛鎮夊叾涓殑涓浜涘悕瀛楋細
Amazon
Amazon
Blogger
Blogger
Craigslist
Craigslist
LiveJournal
LiveJournal
Slashdot
Slashdot
Wikipedia
Wikipedia
Yahoo
Yahoo
YouTube
YouTube
To paraphrase the famous scene from When Harry Met Sally : Well have what theyre having!
璇峰厑璁告垜鐢 褰撳搱鍒╅亣瑙佹矙鑾 涓殑钁楀悕鍦烘櫙鏉ユ瘮鍠伙細浠栦滑鏈夌殑鎴戜滑涔熶細鏈夛紒
Before we get into the details, a quick aside.
鍦ㄨ繘鍏ョ粏鑺備箣鍓嶏紝鐭殏璺戦涓涓嬨
Open source is famous for its so-called religious wars; much (digital) ink has been spilled arguing over text editors (emacs vs. vi ), operating systems (Linux vs. Windows vs. Mac OS), database engines (MySQL vs. PostgreSQL), and of course programming languages.
寮婧愯繍鍔ㄥ洜鍏舵墍璋撶殑瀹楁暀鎴樹簤鑰岄椈鍚嶏紱璁稿澧ㄦ按(浠ュ強澧ㄧ洅銆佺榧撶瓑)琚尌娲掑湪鍚勭被浜夎涓婏細鏂囨湰缂栬緫鍣(emacs vs. vi ),鎿嶄綔绯荤粺(Linux vs.Windows vs. Mac OS),鏁版嵁搴撳紩鎿(MySQL vs. PostgreSQL), 褰撶劧杩樺寘鎷紪绋嬭瑷銆
We try to stay away from these battles. There just isnt enough time.
鎴戜滑甯屾湜杩滅杩欎簺鎴樹簤銆備粎浠呭洜涓烘病鏈夎冻澶熺殑鏃堕棿銆
However, there are a number of choices when it comes to deploying Django, and were constantly asked for our preferences. Since stating these preferences comes dangerously close to firing a salvo in one of the aforementioned battles, weve mostly refrained. However, for the sake of completeness and full disclosure, well state them here. We prefer the following:
浣嗘槸锛屽湪閮ㄧ讲Django纭疄鏈夊緢澶氶夋嫨锛岃屼笖鎴戜滑涔熺粡甯歌闂強涓汉鍋忓ソ銆傝〃杩拌繖浜涗細浣跨ぞ鍖哄浜庡紩鍙戜笂绫绘垬浜夌殑鍗遍櫓杈圭紭锛屾墍浠ユ垜浠湪涓鐩翠互鏉ラ潪甯稿厠鍒躲備絾鏄紝鍑轰簬瀹屾暣鎬у拰鍏ㄦ棤淇濈暀鐨勭洰鐨勶紝鎴戜滑灏嗗湪杩欓噷琛ㄨ堪鑷繁鐨勫亸濂姐備互涓嬫槸鎴戜滑鐨勪紭鍏堥夋嫨锛
Linux (Ubuntu, specifically) as our operating system
鎿嶄綔绯荤粺: Linux(鍏蜂綋鑰岃█鏄疷buntu)
Apache and mod_python for the Web server
Web鏈嶅姟鍣: Apache鍜宮od_python
PostgreSQL as a database server
鏁版嵁搴撴湇鍔″櫒锛歅ostgreSQL
Of course, we can point to many Django users who have made other choices with great success.
褰撶劧锛屾垜浠篃鐪嬪埌锛岃澶氬仛浜嗕笉鍚岄夋嫨鐨凞jango鐢ㄦ埛鍚屾牱涔熷ぇ鑾锋垚鍔熴
Apache with mod_python currently is the most robust setup for using Django on a production server.
鐩墠锛孉pache鍜宮od_python鏄湪鐢熶骇鏈嶅姟鍣ㄤ笂閮ㄧ讲Django鐨勬渶鍋ュ.鎼厤銆
mod_python (http://www.djangoproject.com/r/mod_python/) is an Apache plug-in that embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements.
mod_python (http://www.djangoproject.com/r/mod_python/)鏄竴涓湪Apache涓祵鍏ython鐨凙pache鎻掍欢锛屽畠鍦ㄦ湇鍔″櫒鍚姩鏃跺皢Python浠g爜鍔犺浇鍒板唴瀛樹腑銆(璇戞敞锛氳繖閲岀殑鍐呭瓨鏄寚铏氭嫙鍐呭瓨) 浠g爜鍦ˋpache杩涚▼鐨勬暣涓敓鍛藉懆鏈熶腑閮介┗鐣欏湪鍐呭瓨涓紝涓庡叾瀹冩湇鍔″櫒鐨勫仛娉曠浉姣旓紝杩欏皢甯︽潵閲嶈鐨勬ц兘鎻愬崌銆
Django requires Apache 2.x and mod_python 3.x, and we prefer Apaches prefork MPM, as opposed to the worker MPM.
Django瑕佹眰Apache2.x鍜宮od_python3.x锛屽苟涓旀垜浠紭鍏堣冭檻Apache鐨刾refork MPM妯″紡锛岃屼笉鏄痺orker MPM銆
Note
澶囨敞
Configuring Apache is well beyond the scope of this book, so well simply mention details as needed. Luckily, a number of great resources are available if you need to learn more about Apache. A few of them we like are as follows:
濡備綍閰嶇疆Apache瓒呭嚭浜嗘湰涔︾殑鑼冨洿锛屽洜姝や笅闈㈠皢鍙畝鍗曚粙缁嶅繀瑕佺殑缁嗚妭銆傚垢杩愮殑鏄紝濡傛灉闇瑕佽繘涓姝ュ涔燗pache鐨勭浉鍏崇煡璇嗭紝鍙互鎵惧埌鐩稿綋澶氱殑缁濅匠璧勬簮銆備笅闈㈡槸鎴戜滑鎵涓剰鐨勯儴鍒嗚祫鏂欙細
The free online Apache documentation, available via http://www.djangoproject.com/r/apache/docs/
寮婧愮殑Apache鍦ㄧ嚎鏂囨。锛屼綅浜 http://www.djangoproject.com/r/apache/docs/
Pro Apache, Third Edition (Apress, 2004) by Peter Wainwright, available via http://www.djangoproject.com/r/books/pro-apache/
Pro Apache锛岀涓夌増 (Apress, 2004),浣滆匬eter Wainwright, 浣嶄簬 http://www.djangoproject.com/r/books/pro-apache/
Apache: The Definitive Guide, Third Edition (OReilly, 2002) by Ben Laurie and Peter Laurie, available via http://www.djangoproject.com/r/books/apache-pra/
Apache: The Definitive Guide, 绗笁鐗 (OReilly, 2002),浣滆匓en Laurie鍜孭eter Laurie, 浣嶄簬 http://www.djangoproject.com/r/books/apache-pra/
To configure Django with mod_python, first make sure you have Apache installed with the mod_python module activated. This usually means having a LoadModule directive in your Apache configuration file. It will look something like this:
涓轰簡閰嶇疆鍩轰簬 mod_python 鐨 Django锛岄鍏堣瀹夎鏈夊彲鐢ㄧ殑 mod_python 妯″潡鐨 Apache銆傝繖閫氬父鎰忓懗鐫搴旇鏈変竴涓 LoadModule 鎸囦护鍦 Apache 閰嶇疆鏂囦欢涓傚畠鐪嬭捣鏉ュ氨鍍忔槸杩欐牱锛
LoadModule python_module /usr/lib/apache2/modules/mod_python.so
Then, edit your Apache configuration file and add the following:
鐒跺悗锛岀紪杈戜綘鐨凙pache鐨勯厤缃枃浠讹紝娣诲姞濡備笅鍐呭锛
<Location "/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE mysite.settings PythonDebug On </Location>
Make sure to replace mysite.settings with the appropriate DJANGO_SETTINGS_MODULE for your site.
瑕佺‘淇濇妸 DJANGO_SETTINGS_MODULE 涓殑 mysite.settings 椤圭洰鎹㈡垚涓庝綘鐨勭珯鐐圭浉搴旂殑鍐呭銆
This tells Apache, Use mod_python for any URL at or under /, using the Django mod_python handler. It passes the value of DJANGO_SETTINGS_MODULE so mod_python knows which settings to use.
瀹冨憡璇 Apache锛屼换浣曞湪 / 杩欎釜璺緞涔嬪悗鐨 URL 閮戒娇鐢 Django 鐨 mod_python 鏉ュ鐞嗐傚畠 灏 DJANGO_SETTINGS_MODULE 鐨勫间紶閫掕繃鍘伙紝浣垮緱 mod_python 鐭ラ亾杩欐椂搴旇浣跨敤鍝釜閰嶇疆銆
Note that were using the <Location> directive, not the <Directory> directive. The latter is used for pointing at places on your filesystem, whereas <Location> points at places in the URL structure of a Web site. <Directory> would be meaningless here.
娉ㄦ剰杩欓噷浣跨敤 <Location> 鎸囦护鑰屼笉鏄 <Directory> 銆傚悗鑰呯敤浜庢寚鍚戜綘鐨勬枃浠剁郴缁熶腑鐨勪竴涓綅缃紝鐒惰 <Location> 鎸囧悜涓涓 Web 绔欑偣鐨 URL 浣嶇疆銆
Apache likely runs as a different user than your normal login and may have a different path and sys.path. You may need to tell mod_python how to find your project and Django itself.
Apache 鍙兘涓嶄絾浼氳繍琛屽湪浣犳甯哥櫥褰曠殑鐜涓紝涔熶細杩愯鍦ㄥ叾瀹冧笉鍚岀殑鐢ㄦ埛鐜涓紱涔熷彲鑳戒細鏈変笉鍚岀殑鏂囦欢璺緞鎴 sys.path銆備綘闇瑕佸憡璇 mod_python 濡備綍鍘诲鎵句綘鐨勯」鐩強 Django 鐨勪綅缃
PythonPath "['/path/to/project', '/path/to/django'] + sys.path"
You can also add directives such as PythonAutoReload Off for performance. See the mod_python documentation for a full list of options.
浣犱篃鍙互鍔犲叆涓浜涘叾瀹冩寚浠わ紝姣斿 PythonAutoReload Off 浠ユ彁鍗囨ц兘銆傛煡鐪 mod_python 鏂囨。鑾峰緱璇︾粏鐨勬寚浠ゅ垪琛ㄣ
Note that you should set PythonDebug Off on a production server. If you leave PythonDebug On , your users will see ugly (and revealing) Python tracebacks if something goes wrong within mod_python.
娉ㄦ剰锛屼綘搴旇鍦ㄦ垚鍝佹湇鍔″櫒涓婅缃 PythonDebug Off 銆傚鏋滀綘浣跨敤 PythonDebug On 鐨勮瘽锛屽湪绋嬪簭浜х敓閿欒鏃讹紝浣犵殑鐢ㄦ埛浼氱湅鍒伴毦鐪嬬殑锛堝苟涓旀槸鏆撮湶鐨勶級 Python 鍥炴函淇℃伅銆
Restart Apache, and any request to your site (or virtual host if youve put this directive inside a <VirtualHost> block) will be served by Django.
閲嶅惎 Apache 涔嬪悗鎵鏈夊浣犵殑绔欑偣鐨勮姹傦紙鎴栬呮槸褰撲綘鐢ㄤ簡 <VirtualHost> 鎸囦护鍚庡垯鏄櫄鎷熶富鏈猴級閮戒細鐢 Django 鏉ュ鐞嗐
Note
娉ㄦ剰
If you deploy Django at a subdirectory that is, somewhere deeper than / Django wont trim the URL prefix off of your URLpatterns. So if your Apache config looks like this:
濡傛灉浣犲湪涓涓瘮 / 浣嶇疆鏇存繁鐨勫瓙鐩綍涓儴缃 Django锛屽畠 涓嶄細 瀵逛綘鐨 URL 杩涜淇暣銆傛墍浠ュ鏋滀綘鐨 Apache 閰嶇疆鏄儚杩欐牱鐨勶細
<Location "/mysite/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE mysite.settings PythonDebug On </Location>
then all your URL patterns will need to start with "/mysite/" . For this reason we usually recommend deploying Django at the root of your domain or virtual host. Alternatively, you can simply shift your URL configuration down one level by using a shim URLconf:
鍒欎綘鐨 鎵鏈 URL閮借浠 "/mysite/" 璧峰銆傚熀浜庤繖绉嶅師鍥犳垜浠氬父寤鸿灏 Django锛岄儴缃 鍦ㄤ富鏈烘垨铏氭嫙涓绘満鐨勬牴鐩綍銆傚彟澶栬繕鏈変竴涓夋嫨灏辨槸锛屼綘鍙互绠鍗曠殑灏嗕綘鐨 URL 閰嶇疆杞崲鎴愬儚涓嬮潰杩欐牱锛
urlpatterns = patterns('', (r'^mysite/', include('normal.root.urls')), )
Its entirely possible to run multiple Django installations on the same Apache instance. You might want to do this if youre an independent Web developer with multiple clients but only a single server.
鍦ㄥ悓涓涓 Apache 瀹炰緥涓繍琛屽涓 Django 绋嬪簭鏄畬鍏ㄥ彲鑳界殑銆傚綋浣犳槸涓涓嫭绔嬬殑 Web 寮鍙戜汉鍛樺苟鏈夊涓笉鍚岀殑瀹㈡埛鏃讹紝浣犲彲鑳戒細鎯宠繖涔堝仛銆
To accomplish this, just use VirtualHost like so:
鍙鍍忎笅闈㈣繖鏍蜂娇鐢 VirtualHost 浣犲彲浠ュ疄鐜帮細
NameVirtualHost * <VirtualHost *> ServerName www.example.com # ... SetEnv DJANGO_SETTINGS_MODULE mysite.settings </VirtualHost> <VirtualHost *> ServerName www2.example.com # ... SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings </VirtualHost>
If you need to put two Django installations within the same VirtualHost , youll need to take a special precaution to ensure mod_pythons code cache doesnt mess things up. Use the PythonInterpreter directive to give different <Location> directives separate interpreters:
濡傛灉浣犻渶瑕佸湪鍚屼竴涓 VirtualHost 涓繍琛屼袱涓 Django 绋嬪簭锛屼綘闇瑕佺壒鍒暀鎰忎竴涓嬩互 纭繚 mod_python 鐨勪唬鐮佺紦瀛樹笉琚紕寰椾贡涓冨叓绯熴備娇鐢 PythonInterpreter 鎸囦护鏉ュ皢涓 鍚岀殑 <Location> 鎸囦护鍒嗗埆瑙i噴锛
<VirtualHost *> ServerName www.example.com # ... <Location "/something"> SetEnv DJANGO_SETTINGS_MODULE mysite.settings PythonInterpreter mysite </Location> <Location "/otherthing"> SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings PythonInterpreter mysite_other </Location> </VirtualHost>
The values of PythonInterpreter dont really matter, as long as theyre different between the two Location blocks.
杩欎釜 PythonInterpreter 涓殑鍊间笉閲嶈锛屽彧瑕佸畠浠湪涓や釜 Location 鍧椾腑涓嶅悓銆
Because mod_python caches loaded Python code, when deploying Django sites on mod_python youll need to restart Apache each time you make changes to your code. This can be a hassle, so heres a quick trick to avoid it: just add MaxRequestsPerChild 1 to your config file to force Apache to reload everything for each request. But dont do that on a production server, or well revoke your Django privileges.
鍥犱负 mod_python 缂撳瓨棰勮浇鍏ヤ簡 Python 鐨勪唬鐮侊紝褰撳湪 mod_python 涓婂彂甯 Django 绔欑偣鏃讹紝浣犳瘡 鏀瑰姩浜嗕竴娆′唬鐮侀兘闇瑕侀噸鍚 Apache 涓娆°傝繖杩樼湡鏄欢楹荤儲浜嬶紝鎵浠ヨ繖鏈変釜鍔炴硶鏉ラ伩鍏嶅畠锛氬彧瑕 鍔犲叆 MaxRequestsPerChild 1 鍒伴厤缃枃浠朵腑寮哄埗 Apache 鍦ㄦ瘡涓姹傛椂閮介噸鏂拌浇鍏ユ墍鏈夌殑 浠g爜銆備絾鏄笉瑕佸湪浜у搧鏈嶅姟鍣ㄤ笂浣跨敤杩欎釜鎸囦护锛岃繖浼氭挙閿 Django 鐨勭壒鏉冦
If youre the type of programmer who debugs using scattered print statements (we are), note that print statements have no effect in mod_python; they dont appear in the Apache log, as you might expect. If you have the need to print debugging information in a mod_python setup, youll probably want to use Pythons standard logging package. More information is available at http://docs.python.org/lib/module-logging.html. Alternatively, you can or add the debugging information to the template of your page.
濡傛灉浣犳槸涓涓敤鍒嗘暎鐨 print 璇彞锛堟垜浠氨鏄繖鏍凤級鏉ヨ皟璇曠殑绋嬪簭鍛橈紝娉ㄦ剰杩 print 璇 鍙ュ湪 mod_python 涓槸鏃犳晥鐨勶紱瀹冧笉浼氬儚浣犲笇鏈涚殑閭f牱浜х敓涓涓 Apache 鏃ュ織銆傚鏋滀綘闇瑕佸湪 mod_python 涓墦鍗拌皟璇曚俊鎭紝鍙兘闇瑕佺敤鍒 Python 鏍囧噯鏃ュ織鍖咃紙Pythons standard logging package锛夈 鏇村鐨勪俊鎭鍙傝 http://docs.python.org/lib/module-logging.html 銆傚彟涓涓夋嫨鏄湪妯℃澘椤甸潰涓姞鍏ヨ皟璇曚俊鎭
Django should not be used to serve media files itself; leave that job to whichever Web server you choose. We recommend using a separate Web server (i.e., one thats not also running Django) for serving media. For more information, see the Scaling section.
Django鏈韩涓嶇敤鏉ユ湇鍔edia鏂囦欢锛涘簲璇ユ妸杩欓」宸ヤ綔鐣欑粰浣犻夋嫨鐨勭綉缁滄湇鍔″櫒銆傛垜浠帹鑽愪娇鐢ㄤ竴涓崟鐙殑缃戠粶鏈嶅姟鍣紙鍗虫病鏈夎繍琛孌jango鐨勪竴涓級鏉ユ湇鍔edia銆傛兂浜嗚В鏇村淇℃伅锛岀湅涓嬮潰鐨勭珷鑺傘
If, however, you have no option but to serve media files on the same Apache VirtualHost as Django, heres how you can turn off mod_python for a particular part of the site:
涓嶈繃锛屽鏋滀綘娌℃湁鍏朵粬閫夋嫨锛屾墍浠ュ彧鑳藉湪鍚孌jango涓鏍风殑Apache VirtualHost 涓婃湇鍔edia鏂囦欢锛岃繖閲屼綘鍙互閽堝杩欎釜绔欑偣鐨勭壒瀹氶儴鍒嗗叧闂璵od_python锛
<Location "/media/"> SetHandler None </Location>
Change Location to the root URL of your media files.
灏 Location 鏀规垚浣犵殑media鏂囦欢鎵澶勭殑鏍圭洰褰曘
You can also use <LocationMatch> to match a regular expression. For example, this sets up Django at the site root but explicitly disables Django for the media subdirectory and any URL that ends with .jpg , .gif , or .png :
浣犱篃鍙互浣跨敤 <LocationMatch> 鏉ュ尮閰嶆鍒欒〃杈惧紡銆傛瘮濡傦紝涓嬮潰鐨勫啓娉曞皢Django瀹氫箟鍒扮綉绔欑殑鏍圭洰褰曪紝骞朵笖鏄惧紡鍦板皢 media 瀛愮洰褰曚互鍙婁换浣曚互 .jpg 锛 .gif 锛 鎴栬 .png 缁撳熬鐨刄RL灞忚斀鎺:
<Location "/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE mysite.settings </Location> <Location "/media/"> SetHandler None </Location> <LocationMatch "\.(jpg|gif|png)$"> SetHandler None </LocationMatch>
In all of these cases, youll need to set the DocumentRoot directive so Apache knows where to find your static files.
鍦ㄦ墍鏈夎繖浜涗緥瀛愪腑锛屼綘蹇呴』璁剧疆 DocumentRoot 锛岃繖鏍穉pache鎵嶈兘鐭ラ亾浣犲瓨鏀鹃潤鎬佹枃浠剁殑浣嶇疆銆
When you use Apache/mod_python, errors will be caught by Django in other words, they wont propagate to the Apache level and wont appear in the Apache error_log .
褰撲綘浣跨敤 Apache/mod_python 鏃讹紝閿欒浼氳 Django 鎹曟崏锛屽畠浠笉浼氫紶鎾埌 Apache 閭i噷锛屼篃涓嶄細鍑虹幇鍦 Apache 鐨 閿欒鏃ュ織 涓
The exception to this is if something is really messed up in your Django setup. In that case, youll see an Internal Server Error page in your browser and the full Python traceback in your Apache error_log file. The error_log traceback is spread over multiple lines. (Yes, this is ugly and rather hard to read, but its how mod_python does things.)
鏈変竴涓緥澶栧氨鏄綋纭疄浣犵殑 Django 璁剧疆娣蜂贡浜嗘椂銆傚湪杩欑鎯呭喌涓嬶紝浣犱細鍦ㄦ祻瑙堝櫒涓婄湅鍒颁竴涓 鍐呴儴鏈嶅姟鍣ㄩ敊璇殑椤甸潰锛屽苟鍦 Apache 鐨 閿欒鏃ュ織 涓湅鍒 Python 鐨勫畬鏁村洖婧俊鎭 閿欒鏃ュ織 鐨勫洖婧俊鎭湁澶氳銆傚綋鐒讹紝杩欎簺淇℃伅鏄毦鐪嬩笖闅句互闃呰鐨勩
Sometimes, Apache segfaults when you install Django. When this happens, its almost always one of two causes mostly unrelated to Django itself:
鏈夋椂鍊欙紝Apache浼氬湪浣犲畨瑁匘jango鐨勬椂鍊欏彂鐢熸閿欒銆傝繖鏃讹紝鍩烘湰涓 鎬绘槸 鏈変互涓嬩袱涓笌Django鏈韩鏃犲叧鐨勫師鍥犲叾涓箣涓鎵閫犳垚锛
It may be that your Python code is importing the pyexpat module (used for XML parsing), which may conflict with the version embedded in Apache. For full information, see Expat Causing Apache Crash at http://www.djangoproject.com/r/articles/expat-apache-crash/.
鏈夊彲鑳芥槸鍥犱负锛屼綘浣跨敤浜 pyexpat 妯″潡锛堣繘琛孹ML瑙f瀽锛夊苟涓斾笌Apache鍐呯疆鐨勭増鏈浉鍐茬獊銆傝鎯呰瑙 http://www.djangoproject.com/r/articles/expat-apache-crash/.
It may be because youre running mod_python and mod_php in the same Apache instance, with MySQL as your database back-end. In some cases, this causes a known mod_python issue due to version conflicts in PHP and the Python MySQL back-end. Theres full information in a mod_python FAQ entry, accessible via http://www.djangoproject.com/r/articles/php-modpython-faq/.
涔熸湁鍙兘鏄湪鍚屼竴涓狝pache杩涚▼涓紝鍚屾椂浣跨敤浜唌od_python 鍜 mod_php锛岃屼笖閮戒娇鐢∕ySQL浣滀负鏁版嵁搴撳悗绔傚湪鏈変簺鎯呭喌涓嬶紝杩欎細閫犳垚PHP鍜孭ython鐨凪ySQL妯″潡鐨勭増鏈啿绐併傚湪mod_python鐨凢AQ涓湁鏇磋缁嗙殑瑙i噴銆 http://www.djangoproject.com/r/articles/php-modpython-faq/.
If you continue to have problems setting up mod_python, a good thing to do is get a bare-bones mod_python site working, without the Django framework. This is an easy way to isolate mod_python-specific problems. The article Getting mod_python Working details this procedure: http://www.djangoproject.com/r/articles/getting-modpython-working/.
濡傛灉杩樻湁瀹夎mod_python鐨勯棶棰橈紝鏈変竴涓ソ鐨勫缓璁紝灏辨槸鍏堝彧杩愯mod_python绔欑偣锛岃屼笉浣跨敤Django妗嗘灦銆傝繖鏄尯鍒唌od_python鐗瑰畾闂鐨勫ソ鏂规硶銆備笅闈㈢殑杩欑瘒鏂囩珷缁欏嚭浜嗘洿璇︾粏鐨勮В閲娿 http://www.djangoproject.com/r/articles/getting-modpython-working/.
The next step should be to edit your test code and add an import of any Django-specific code youre using your views, your models, your URLconf, your RSS configuration, and so forth. Put these imports in your test handler function and access your test URL in a browser. If this causes a crash, youve confirmed its the importing of Django code that causes the problem. Gradually reduce the set of imports until it stops crashing, so as to find the specific module that causes the problem. Drop down further into modules and look into their imports as necessary. For more help, system tools like ldconfig on Linux, otool on Mac OS, and ListDLLs (from SysInternals) on Windows can help you identify shared dependencies and possible version conflicts.
涓嬩竴涓楠ゅ簲璇ユ槸缂栬緫涓娈垫祴璇曚唬鐮侊紝鎶婁綘鎵鏈塪jango鐩稿叧浠g爜import杩涘幓锛屼綘鐨剉iews,models,URLconf,RSS閰嶇疆锛岀瓑绛夈傛妸杩欎簺imports鏀捐繘浣犵殑handler鍑芥暟涓紝鐒跺悗浠庢祻瑙堝櫒杩涘叆浣犵殑URL銆傚鏋滆繖浜涘鑷翠簡crash锛屼綘灏卞彲浠ョ‘瀹氭槸import鐨刣jango浠g爜寮曡捣浜嗛棶棰樸傞愪釜鍘绘帀杩欎簺imports锛岀洿鍒颁笉鍐嶅啿绐侊紝杩欐牱灏辫兘鎵惧埌寮曡捣闂鐨勯偅涓ā鍧椼 娣卞叆浜嗚В鍚勬ā鍧楋紝鐪嬬湅瀹冧滑鐨刬mports銆傝鎯宠幏寰楁洿澶氬府鍔╋紝鍍弆inux鐨刲dconfig锛孧ac OS鐨刼tool鍜寃indows鐨凩istDLLs锛坒orm sysInternals锛夐兘鍙互甯綘璇嗗埆鍏变韩渚濊禆鍜屽彲鑳界殑鐗堟湰鍐茬獊銆
Although Django under Apache and mod_python is the most robust deployment setup, many people use shared hosting, on which FastCGI is the only available deployment option.
灏界灏嗕娇鐢ˋpache鍜宮od_python鎼缓Django鐜鏄渶鍏烽瞾妫掓х殑锛屼絾鍦ㄥ緢澶氳櫄鎷熶富鏈哄钩鍙颁笂锛屽線寰鍙兘浣跨敤FastCGI
Additionally, in some situations, FastCGI allows better security and possibly better performance than mod_python. For small sites, FastCGI can also be more lightweight than Apache.
姝ゅ锛屽湪寰堝鎯呭喌涓嬶紝FastCGI鑳藉鎻愪緵姣攎od_python鏇翠负浼樿秺鐨勫畨鍏ㄦу拰鏁堣兘銆傞拡瀵瑰皬鍨嬬珯鐐癸紝鐩稿浜嶢pache鏉ヨFastCGI鏇翠负杞婚噺绾с
FastCGI 绠浠
FastCGI is an efficient way of letting an external application serve pages to a Web server. The Web server delegates the incoming Web requests (via a socket) to FastCGI, which executes the code and passes the response back to the Web server, which, in turn, passes it back to the clients Web browser.
濡備綍鑳藉鐢变竴涓閮ㄧ殑搴旂敤绋嬪簭寰堟湁鏁堣В閲奧EB 鏈嶅姟鍣ㄤ笂鐨勫姩鎬侀〉闈㈣姹傚憿锛熺瓟妗堝氨鏄娇鐢‵astCGI! 瀹冪殑宸ヤ綔姝ラ绠鍗曠殑鎻忚堪璧锋潵鏄繖鏍风殑锛 1銆乄EB鏈嶅姟鍣ㄦ敹鍒板鎴风鐨勯〉闈㈣姹 2銆乄EB鏈嶅姟鍣ㄥ皢杩欎釜椤甸潰璇锋眰濮旀淳缁欎竴涓狥astCGI 澶栭儴杩涚▼锛圵EB鏈嶅姟鍣ㄤ簬FastCGI涔嬮棿鏄氳繃socket鏉ヨ繛鎺ラ氳鐨勶級 3銆丗astCGI澶栭儴杩涚▼寰楀埌WEB鏈嶅姟鍣ㄥ娲捐繃鏉ョ殑椤甸潰璇锋眰淇℃伅鍚庤繘琛屽鐞嗭紝骞朵笖灏嗗鐞嗙粨鏋滐紙鍔ㄦ侀〉闈㈠唴瀹癸級杩斿洖缁橶EB鏈嶅姟鍣 4銆乄eb鏈嶅姟鍣ㄥ皢FastCGI杩斿洖鍥炴潵鐨勭粨鏋滃啀杞佺粰瀹㈡埛绔祻瑙堝櫒銆
Like mod_python, FastCGI allows code to stay in memory, allowing requests to be served with no startup time. Unlike mod_python, a FastCGI process doesnt run inside the Web server process, but in a separate, persistent process.
鍜宮od_python涓鏍凤紝FastCGI涔熸槸椹荤暀鍦ㄥ唴瀛橀噷涓哄鎴疯姹傝繑鍥炲姩鎬佷俊鎭,鑰屼笖涔熷厤鎺変簡鍍忎紶缁熺殑CGI涓鏍峰惎鍔ㄨ繘绋嬫椂鍊欑殑鏃堕棿鑺遍攢銆備絾浜巑od_python涓嶅悓涔嬪鏄畠骞朵笉鏄綔涓烘ā鍧楄繍琛屽湪web鏈嶅姟鍣ㄥ悓涓杩涚▼鍐呯殑锛岃屾槸鏈夎嚜宸辩殑鐙珛杩涚▼銆
Why Run Code in a Separate Process?
涓轰粈涔堣鍦ㄤ竴涓嫭绔嬬殑杩涚▼涓繍琛屼唬鐮侊紵
The traditional mod_* arrangements in Apache embed various scripting languages (most notably PHP, Python/mod_python, and Perl/mod_perl) inside the process space of your Web server. Although this lowers startup time (because code doesnt have to be read off disk for every request), it comes at the cost of memory use.
鍦ㄤ互浼犵粺鐨勬柟寮忕殑鍑犵浠od_*鏂瑰紡宓屽叆鍒癆pache鐨勮剼鏈瑷涓紙甯歌鐨勪緥濡傦細PHP锛孭ython/mod_python鍜孭erl/mod_perl锛夛紝浠栦滑閮芥槸浠pache鎵╁睍妯″潡鐨勬柟寮忓皢鑷韩宓屽叆鍒癆pache杩涚▼涓殑銆傚敖绠¤繖绉嶆柟寮忓彲浠ュ噺浣庡惎鍔ㄦ椂鍊欑殑鏃堕棿鑺遍攢锛堝洜涓轰唬鐮佷笉鐢ㄥ湪姣忔鏀跺埌璁块棶璇锋眰鐨勬椂鍊欓兘璇诲幓纭洏鏁版嵁锛夛紝浣嗚繖鏄互澧炲ぇ鍐呭瓨鐨勫紑閿鏉ヤ綔涓轰唬浠风殑銆
Each Apache process gets a copy of the Apache engine, complete with all the features of Apache that Django simply doesnt take advantage of. FastCGI processes, on the other hand, only have the memory overhead of Python and Django.
姣忎竴涓狝pache杩涚▼閮芥槸涓涓狝pache寮曟搸鐨勫壇鏈紝瀹冨畬鍏ㄥ寘鎷簡鎵鏈堿pache鎵鍏锋湁鐨勪竴鍒囧姛鑳界壒鎬э紙鍝曟槸瀵笵jango姣棤濂藉鐨勪笢瑗夸篃涓骞跺姞杞借繘鏉ワ級銆傝孎astCGI灏变笉涓鏍蜂簡锛屽畠浠呬粎鎶奝ython鍜孌jango绛夊繀澶囩殑涓滀笢寮勫埌鍐呭瓨涓
Due to the nature of FastCGI, its also possible to have processes that run under a different user account than the Web server process. Thats a nice security benefit on shared systems, because it means you can secure your code from other users.
渚濇嵁FastCGI鑷韩鐨勭壒鐐瑰彲浠ョ湅鍒帮紝FastCGI杩涚▼鍙互涓嶹eb鏈嶅姟鍣ㄧ殑杩涚▼鍒嗗埆杩愯鍦ㄤ笉鍚岀殑鐢ㄦ埛鏉冮檺涓嬨傚浜庝竴涓浜哄叡鐢ㄧ殑绯荤粺鏉ヨ锛岃繖涓壒鎬у浜庡畨鍏ㄦф槸闈炲父鏈夊ソ澶勭殑锛屽洜涓轰綘鍙互瀹夊叏鐨勪簬鍒汉鍒嗕韩鍜岄噸鐢ㄤ唬鐮佷簡銆
Before you can start using FastCGI with Django, youll need to install flup , a Python library for dealing with FastCGI. Some users have reported stalled pages with older flup versions, so you may want to use the latest SVN version. Get flup at http://www.djangoproject.com/r/flup/.
濡傛灉浣犲笇鏈涗綘鐨凞jango浠astCGI鐨勬柟寮忚繍琛岋紝閭d箞浣犺繕蹇呴』瀹夎 flup 杩欎釜Python搴擄紝杩欎釜搴撳氨鏄敤浜庡鐞咶astCGI鐨勩傚緢澶氱敤鎴烽兘鎶辨 flup 鐨勫彂甯冪増澶箙浜嗭紝鑰佹槸涓嶆洿鏂般傚叾瀹炰笉鏄殑锛屼粬浠竴鐩村湪鍔姏鐨勫伐浣滅潃锛岃繖鏄病鏈夋斁鍑烘潵鑰屽凡銆備絾浣犲彲浠ラ氳繃 http://www.djangoproject.com/r/flup/ 鑾峰彇浠栦滑鐨勬渶鏂扮殑SVN鐗堟湰銆
FastCGI operates on a client/server model, and in most cases youll be starting the FastCGI server process on your own. Your Web server (be it Apache, lighttpd, or otherwise) contacts your Django-FastCGI process only when the server needs a dynamic page to be loaded. Because the daemon is already running with the code in memory, its able to serve the response very quickly.
FastCGI鏄互瀹㈡埛鏈/鏈嶅姟鍣ㄦ柟寮忚繍琛岀殑锛屽苟涓斿湪寰堝鎯呭喌涓嬶紝浣犲緱鑷繁鍘诲惎鍔‵astCGI鐨勬湇鍔¤繘绋嬨 Web鏈嶅姟鍣紙渚嬪Apache,lighttpd绛夌瓑锛変粎浠呭湪鏈夊姩鎬侀〉闈㈣闂姹傜殑鏃跺欐墠浼氬幓涓庝綘鐨凞jango-FastCGI杩涚▼浜や簰銆傚洜涓篎ast-CGI宸茬粡涓鐩撮┗鐣欏湪鍐呭瓨閲岄潰浜嗙殑锛屾墍浠ュ畠鍝嶅簲璧锋潵涔熸槸寰堝揩鐨勩
Note
娉ㄦ剰
If youre on a shared hosting system, youll probably be forced to use Web server-managed FastCGI processes. If youre in this situation, you should read the section titled Running Django on a Shared-Hosting Provider with Apache, below.
鍦ㄨ櫄鎷熶富鏈轰笂浣跨敤鐨勮瘽锛屼綘鍙兘浼氳寮哄埗鐨勪娇鐢╓eb server-managed FastCGI杩涚▼銆傚湪杩欐牱鐨勬儏鍐典笅锛岃鍙傞槄涓嬮潰鐨勨滃湪Apache鍏变韩涓绘満閲岃繍琛孌jango鈥濊繖涓灏忚妭銆
A Web server can connect to a FastCGI server in one of two ways: it can use either a Unix domain socket (a named pipe on Win32 systems) or a TCP socket. What you choose is a manner of preference; a TCP socket is usually easier due to permissions issues.
web鏈嶅姟鍣ㄦ湁涓ょ鏂瑰紡浜嶧astCGI杩涚▼浜や簰锛氫娇鐢║nix domain socket(鍦╳in32閲岄潰鏄 鍛藉悕绠¢亾 )鎴栬呬娇鐢═CP socket.鍏蜂綋浣跨敤鍝竴涓紝閭e氨鏍规嵁浣犵殑鍋忓ソ鑰屽畾浜嗭紝浣嗘槸TCP socket寮勪笉濂界殑璇濆線寰浼氬彂鐢熶竴浜涙潈闄愪笂鐨勯棶棰樸
To start your server, first change into the directory of your project (wherever your manage.py is), and then run manage.py with the runfcgi command:
寮濮嬩綘鐨勬湇鍔″櫒椤圭洰锛岄鍏堣繘鍏ヤ綘鐨勯」鐩洰褰曚笅锛堜綘鐨 manage.py 鏂囦欢鎵鍦ㄤ箣澶勶級锛岀劧鍚庝娇鐢 manage.py runfcgi 鍛戒护锛
./manage.py runfcgi [options]
If you specify help as the only option after runfcgi , a list of all the available options will display.
鎯充簡瑙e浣曚娇鐢 runfcgi 锛岃緭鍏 manage.py runfcgi help 鍛戒护銆
Youll need to specify either a socket or both host and port . Then, when you set up your Web server, youll just need to point it at the socket or host/port you specified when starting the FastCGI server.
浣犲彲浠ユ寚瀹 socket 鎴栬呭悓鏃舵寚瀹 host 鍜 port 銆傚綋浣犺鍒涘缓Web鏈嶅姟鍣ㄦ椂锛屼綘鍙渶瑕佸皢鏈嶅姟鍣ㄦ寚鍚戝綋浣犲湪鍚姩FastCGI鏈嶅姟鍣ㄦ椂纭畾鐨剆ocket鎴栬卙ost/port銆
A few examples should help explain this:
鑼冧緥锛
Running a threaded server on a TCP port:
鍦═CP绔彛涓婅繍琛屼竴涓嚎绋嬫湇鍔″櫒锛
./manage.py runfcgi method=threaded host=127.0.0.1 port=3033
Running a preforked server on a Unix domain socket:
鍦║nix socket涓婅繍琛宲refork鏈嶅姟鍣細
./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid
Run without daemonizing (backgrounding) the process (good for debugging):
鍚姩锛屼絾涓嶄綔涓哄悗鍙拌繘绋嬶紙鍦ㄨ皟璇曟椂姣旇緝鏂逛究锛夛細
./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock
If you have the process running in the foreground, its easy enough to stop it: simply press Ctrl+C to stop and quit the FastCGI server. However, when youre dealing with background processes, youll need to resort to the Unix kill command.
濡傛灉浣犵殑FastCGI鏄湪鍓嶅彴杩愯鐨勶紝閭d箞鍙渶鎸塁trl+C灏卞彲浠ュ緢鏂逛究鐨勫仠姝㈣繖涓繘绋嬩簡銆備絾濡傛灉鏄湪鍚庡彴杩愯鐨勮瘽锛屼綘灏辫浣跨敤Unix鐨 kill 鍛戒护鏉ユ潃鎺夊畠銆
If you specify the pidfile option to your manage.py runfcgi , you can kill the running FastCGI daemon like this:
濡傛灉浣犲湪 manage.py runfcgi 涓寚瀹氫簡 pidfile 杩欎釜閫夐」锛岄偅涔堜綘鍙互杩欐牱鏉ユ潃姝昏繖涓狥astCGI鍚庡彴杩涚▼锛
kill `cat $PIDFILE`
where $PIDFILE is the pidfile you specified.
$PIDFILE 灏辨槸浣犲湪 pidfile 鎸囧畾鐨勯偅涓
To easily restart your FastCGI daemon on Unix, you can use this small shell script:
浣犲彲浠ヤ娇鐢ㄤ笅闈㈣繖涓剼鏈柟渚垮湴閲嶅惎Unix閲岀殑FastCGI瀹堟姢杩涚▼锛
#!/bin/bash # Replace these three settings. PROJDIR="/home/user/myproject" PIDFILE="$PROJDIR/mysite.pid" SOCKET="$PROJDIR/mysite.sock" cd $PROJDIR if [ -f $PIDFILE ]; then kill `cat -- $PIDFILE` rm -f -- $PIDFILE fi exec /usr/bin/env - \ PYTHONPATH="../python:.." \ ./manage.py runfcgi socket=$SOCKET pidfile=$PIDFILE
To use Django with Apache and FastCGI, youll need Apache installed and configured, with mod_fastcgi installed and enabled. Consult the Apache and mod_fastcgi documentation for instructions: http://www.djangoproject.com/r/mod_fastcgi/.
鍦ˋpache鍜孎astCGI涓婁娇鐢―jango锛屼綘闇瑕佸畨瑁呭拰閰嶇疆Apache锛屽苟涓斿畨瑁卪od_fastcgi銆傝鍙傝Apache鍜宮od_fastcgi鏂囨。锛 http://www.djangoproject.com/r/mod_fastcgi/ 銆
Once youve completed the setup, point Apache at your Django FastCGI instance by editing the httpd.conf (Apache configuration) file. Youll need to do two things:
褰撳畬鎴愪簡瀹夎锛岄氳繃 httpd.conf 锛圓pache鐨勯厤缃枃浠讹級鏉ヨApache鍜孌jango FastCGI浜掔浉閫氫俊銆備綘闇瑕佸仛涓や欢浜嬶細
Use the FastCGIExternalServer directive to specify the location of your FastCGI server.
浣跨敤 FastCGIExternalServer 鎸囨槑FastCGI鐨勪綅缃
Use mod_rewrite to point URLs at FastCGI as appropriate.
浣跨敤 mod_rewrite 涓篎astCGI鎸囧畾鍚堥傜殑URL銆
The FastCGIExternalServer directive tells Apache how to find your FastCGI server. As the FastCGIExternalServer docs (http://www.djangoproject.com/r/mod_fastcgi/FastCGIExternalServer/) explain, you can specify either a socket or a host . Here are examples of both:
FastCGIExternalServer 鍛婅瘔Apache濡備綍鎵惧埌FastCGI鏈嶅姟鍣ㄣ 鎸夌収FastCGIExternalServer 鏂囨。锛 http://www.djangoproject.com/r/mod_fastcgi/FastCGIExternalServer/ 锛夛紝浣犲彲浠ユ寚鏄 socket 鎴栬 host 銆備互涓嬫槸涓や釜渚嬪瓙锛
# 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
In either case, the the directory /home/user/public_html/ should exist, though the file /home/user/public_html/mysite.fcgi doesnt actually have to exist. Its just a URL used by the Web server internally a hook for signifying which requests at a URL should be handled by FastCGI. (More on this in the next section.)
鍦ㄨ繖涓や釜渚嬪瓙涓紝 /home/user/public_html/ 鐩綍蹇呴』瀛樺湪锛岃 /home/user/public_html/mysite.fcgi 鏂囦欢涓嶄竴瀹氬瓨鍦ㄣ傚畠浠呬粎鏄竴涓猈eb鏈嶅姟鍣ㄥ唴閮ㄤ娇鐢ㄧ殑鎺ュ彛锛岃繖涓猆RL鍐冲畾浜嗗浜庡摢浜沀RL鐨勮姹備細琚獸astCGI澶勭悊锛堜笅涓閮ㄥ垎璇︾粏璁ㄨ锛夈
The second step is telling Apache to use FastCGI for URLs that match a certain pattern. To do this, use the mod_rewrite module and rewrite URLs to mysite.fcgi (or whatever you specified in the FastCGIExternalServer directive, as explained in the previous section).
绗簩姝ユ槸鍛婅瘔Apache涓虹鍚堜竴瀹氭ā寮忕殑URL浣跨敤FastCGI銆備负浜嗗疄鐜拌繖涓鐐癸紝璇蜂娇鐢╩od_rewrite 妯″潡锛屽苟灏嗚繖浜沀RL閲嶅畾鍚戝埌 mysite.fcgi 锛堟垨鑰呮濡傚湪鍓嶆枃涓弿杩扮殑閭f牱锛屼娇鐢ㄤ换浣曞湪 FastCGIExternalServer 鎸囧畾鐨勫唴瀹癸級銆
In this example, we tell Apache to use FastCGI to handle any request that doesnt represent a file on the filesystem and doesnt start with /media/ . This is probably the most common case, if youre using Djangos admin site:
鍦ㄨ繖涓緥瀛愰噷闈紝鎴戜滑鍛婅瘔Apache浣跨敤FastCGI鏉ュ鐞嗛偅浜涘湪鏂囦欢绯荤粺涓婁笉鎻愪緵鏂囦欢(璇戣呮敞锛氫篃灏辨槸鎸囧悜铏氭嫙鏂囦欢)鍜屾病鏈変粠 /media/ 寮濮嬬殑浠讳綍璇锋眰銆傚鏋滀綘浣跨敤Django鐨刟dmin绔欑偣锛屼笅闈㈠彲鑳芥槸涓涓渶鏅氱殑渚嬪瓙锛
<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>
lighttpd (http://www.djangoproject.com/r/lighttpd/) is a lightweight Web server commonly used for serving static files. It supports FastCGI natively and thus is also an ideal choice for serving both static and dynamic pages, if your site doesnt have any Apache-specific needs.
lighttpd (http://www.djangoproject.com/r/lighttpd/) 鏄竴涓交閲忕骇鐨刉eb鏈嶅姟鍣紝閫氬父琚敤鏉ユ彁渚涢潤鎬侀〉闈㈢殑璁块棶銆傚畠澶╃敓鏀寔FastCGI锛屽洜姝ら櫎闈炰綘鐨勭珯鐐归渶瑕佷竴浜汚pache鐗规湁鐨勭壒鎬э紝鍚﹀垯锛宭ighttpd瀵逛簬闈欐佸拰鍔ㄦ侀〉闈㈡潵璇撮兘鏄悊鎯崇殑閫夋嫨銆
Make sure mod_fastcgi is in your modules list, somewhere after mod_rewrite and mod_access , but not after mod_accesslog . Youll probably want mod_alias as well, for serving admin media.
纭繚 mod_fastcgi 鍦ㄦā鍧楀垪琛ㄤ腑锛屽畠闇瑕佸嚭鐜板湪 mod_rewrite 鍜 mod_access 锛屼絾鏄鍦 mod_accesslog 涔嬪墠銆備綘涔熷彲鑳介渶瑕 mod_alias 妯″潡锛屾潵鎻愪緵绠$悊鎵鐢ㄧ殑濯掍綋璧勬簮銆
Add the following to your lighttpd config file:
灏嗕笅闈㈢殑鍐呭娣诲姞鍒颁綘鐨刲ighttpd鐨勯厤缃枃浠朵腑锛
server.document-root = "/home/user/public_html" fastcgi.server = ( "/mysite.fcgi" => ( "main" => ( # Use host / port instead of socket for TCP fastcgi # "host" => "127.0.0.1", # "port" => 3033, "socket" => "/home/user/mysite.sock", "check-local" => "disable", ) ), ) alias.url = ( "/media/" => "/home/user/django/contrib/admin/media/", ) url.rewrite-once = ( "^(/media.*)$" => "$1", "^/favicon\.ico$" => "/media/favicon.ico", "^(/.*)$" => "/mysite.fcgi$1", )
lighttpd lets you use conditional configuration to allow configuration to be customized per host. To specify multiple FastCGI sites, just add a conditional block around your FastCGI config for each site:
lighttpd鍏佽浣犱娇鐢ㄦ潯浠堕厤缃潵涓烘瘡涓珯鐐瑰垎鍒彁渚涜缃備负浜嗘敮鎸丗astCGI鐨勫绔欑偣锛屽彧闇瑕佸湪FastCGI鐨勯厤缃枃浠朵腑锛屼负姣忎釜绔欑偣鍒嗗埆寤虹珛鏉′欢閰嶇疆椤癸細
# If the hostname is 'www.example1.com'... $HTTP["host"] == "www.example1.com" { server.document-root = "/foo/site1" fastcgi.server = ( ... ) ... } # If the hostname is 'www.example2.com'... $HTTP["host"] == "www.example2.com" { server.document-root = "/foo/site2" fastcgi.server = ( ... ) ... }
You can also run multiple Django installations on the same site simply by specifying multiple entries in the fastcgi.server directive. Add one FastCGI host for each.
浣犱篃鍙互閫氳繃 fastcgi.server 涓寚瀹氬涓叆鍙o紝鍦ㄥ悓涓涓珯鐐逛笂瀹炵幇澶氫釜Django瀹夎銆傝涓烘瘡涓涓畨瑁呮寚瀹氫竴涓狥astCGI涓绘満銆
Many shared-hosting providers dont allow you to run your own server daemons or edit the httpd.conf file. In these cases, its still possible to run Django using Web server-spawned processes.
璁稿鍏变韩涓绘満鐨勬湇鍔℃彁渚涘晢涓嶅厑璁歌繍琛屼綘鑷繁鐨勬湇鍔¤繘绋嬶紝涔熶笉鍏佽淇敼 httpd.conf 鏂囦欢銆傚敖绠″姝わ紝浠嶇劧鏈夊彲鑳介氳繃Web鏈嶅姟鍣ㄤ骇鐢熺殑瀛愯繘绋嬫潵杩愯Django銆
Note
澶囨敞
If youre using Web server-spawned processes, as explained in this section, theres no need for you to start the FastCGI server on your own. Apache will spawn a number of processes, scaling as it needs to.
濡傛灉浣犺浣跨敤鏈嶅姟鍣ㄧ殑瀛愯繘绋嬶紝浣犳病鏈夊繀瑕佽嚜宸卞幓鍚姩FastCGI鏈嶅姟鍣ㄣ侫pache浼氳嚜鍔ㄤ骇鐢熶竴浜涘瓙杩涚▼锛屼骇鐢熺殑鏁伴噺鎸夌収闇姹傚拰閰嶇疆浼氭湁鎵涓嶅悓銆
In your Web root directory, add this to a file named .htaccess
鍦ㄤ綘鐨刉eb鏍圭洰褰曚笅锛屽皢涓嬮潰鐨勫唴瀹瑰鍔犲埌 .htaccess 鏂囦欢涓細
AddHandler fastcgi-script .fcgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]
Then, create a small script that tells Apache how to spawn your FastCGI program. Create a file, mysite.fcgi , and place it in your Web directory, and be sure to make it executable
鎺ョ潃锛屽垱寤轰竴涓剼鏈紝鍛婄煡Apache濡備綍杩愯浣犵殑FastCGI绋嬪簭銆傚垱寤轰竴涓 mysite.fcgi 鏂囦欢锛屽苟鎶婂畠鏀惧湪浣犵殑Web鐩綍涓紝鎵撳紑鍙墽琛屾潈闄愩
#!/usr/bin/python import sys, os # Add a custom Python path. sys.path.insert(0, "/home/user/python") # Switch to the directory of your project. (Optional.) # os.chdir("/home/user/myproject") # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings" from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded", daemonize="false")
If you change any Python code on your site, youll need to tell FastCGI the code has changed. But theres no need to restart Apache in this case. Rather, just reupload mysite.fcgi or edit the file so that the timestamp on the file changes. When Apache sees the file has been updated, it will restart your Django application for you.
濡傛灉浣犳敼鍙樹簡绔欑偣涓婁换浣曠殑python浠g爜锛屼綘闇瑕佸憡鐭astCGI銆備絾鏄紝杩欎笉闇瑕侀噸鍚疉pache锛岃屽彧闇瑕侀噸鏂颁笂浼 mysite.fcgi 鎴栬呯紪杈戞敼鏂囦欢锛屼娇寰椾慨鏀规椂闂村彂鐢熶簡鍙樺寲锛屽畠浼氳嚜鍔ㄥ府浣犻噸鍚疍jango搴旂敤銆
If you have access to a command shell on a Unix system, you can accomplish this easily by using the touch command:
濡傛灉浣犳嫢鏈塙nix绯荤粺鍛戒护琛岀殑鍙墽琛屾潈闄愶紝鍙渶瑕佺畝鍗曞湴浣跨敤 touch 鍛戒护锛
touch mysite.fcgi
Now that you know how to get Django running on a single server, lets look at how you can scale out a Django installation. This section walks through how a site might scale from a single server to a large-scale cluster that could serve millions of hits an hour.
鏃㈢劧浣犲凡缁忕煡閬撳浣曞湪涓鍙版湇鍔″櫒涓婅繍琛孌jango锛岃鎴戜滑鏉ョ爺绌朵竴涓嬶紝濡備綍鎵╁睍鎴戜滑鐨凞jango瀹夎銆傝繖涓閮ㄥ垎鎴戜滑灏嗚璁猴紝濡備綍鎶婁竴鍙版湇鍔″櫒鎵╁睍涓轰竴涓ぇ瑙勬ā鐨勬湇鍔″櫒闆嗙兢锛岃繖鏍峰氨鑳芥弧瓒虫瘡灏忔椂涓婄櫨涓囩殑鐐瑰嚮鐜囥
Its important to note, however, that nearly every large site is large in different ways, so scaling is anything but a one-size-fits-all operation. The following coverage should suffice to show the general principle, and whenever possible well try to point out where different choices could be made.
鏈変竴鐐瑰緢閲嶈锛屾瘡涓涓ぇ鍨嬬殑绔欑偣澶х殑褰㈠紡鍜岃妯′笉鍚岋紝鍥犳鍙墿灞曟у叾瀹炲苟涓嶆槸涓绉嶅崈绡囦竴寰嬬殑琛屼负銆備互涓嬮儴鍒嗕細娑夊強鍒颁竴浜涢氱敤鐨勫師鍒欙紝骞朵笖浼氭寚鍑轰竴浜涗笉鍚岄夋嫨銆
First off, well make a pretty big assumption and exclusively talk about scaling under Apache and mod_python. Though we know of a number of successful medium- to large-scale FastCGI deployments, were much more familiar with Apache.
棣栧厛锛屾垜浠潵鍋氫竴涓ぇ鐨勫亣璁撅紝鍙泦涓湴璁ㄨ鍦ˋpache鍜宮od_python涓嬬殑鍙墿灞曟ч棶棰樸傚敖绠℃垜浠篃鐭ラ亾涓浜涙垚鍔熺殑涓瀷鍜屽ぇ鍨嬬殑FastCGI绛栫暐锛屼絾鏄垜浠洿鍔犵啛鎮堿pache銆
Most sites start out running on a single server, with an architecture that looks something like Figure 20-1.
澶у鏁扮殑绔欑偣涓寮濮嬮兘杩愯鍦ㄥ崟鏈烘湇鍔″櫒涓婏紝鐪嬭捣鏉ュ儚鍥20-1杩欐牱鐨勬瀯鏋躲
Figure 20-1: a single server Django setup.
鍥 20-1锛氫竴涓崟鏈嶅姟鍣ㄧ殑Django瀹夎銆
This works just fine for small- to medium-sized sites, and its relatively cheap you can put together a single-server site designed for Django for well under $3,000.
杩欏浜庡皬鍨嬪拰涓瀷鐨勭珯鐐规潵璇磋繕涓嶉敊锛屽苟涓斾篃寰堜究瀹滐紝涓鑸潵璇达紝浣犲彲浠ュ湪3000缇庡厓浠ヤ笅灏辨悶瀹氫竴鍒囥
However, as traffic increases youll quickly run into resource contention between the different pieces of software. Database servers and Web servers love to have the entire server to themselves, so when run on the same server they often end up fighting over the same resources (RAM, CPU) that theyd prefer to monopolize.
鐒惰岋紝褰撴祦閲忓鍔犵殑鏃跺欙紝浣犱細杩呴熼櫡鍏ヤ笉鍚岃蒋浠剁殑 璧勬簮浜夊ず 涔嬩腑銆傛暟鎹簱鏈嶅姟鍣ㄥ拰Web鏈嶅姟鍣ㄩ兘 鍠滄 鑷繁鎷ユ湁鏁翠釜鏈嶅姟鍣ㄨ祫婧愶紝鍥犳褰撹瀹夎鍦ㄥ崟鏈轰笂鏃讹紝瀹冧滑鎬讳細浜夊ず鐩稿悓鐨勮祫婧愶紙RAM, CPU锛夛紝瀹冧滑鏇存効鎰忕嫭浜祫婧愩
This is solved easily by moving the database server to a second machine, as explained in the following section.
閫氳繃鎶婃暟鎹簱鏈嶅姟鍣ㄦ惉绉诲埌绗簩鍙颁富鏈轰笂锛屽彲浠ュ緢瀹规槗鍦拌В鍐宠繖涓棶棰樸傝繖灏嗗湪涓嬩竴閮ㄥ垎浠嬬粛銆
As far as Django is concerned, the process of separating out the database server is extremely easy: youll simply need to change the DATABASE_HOST setting to the IP or DNS name of your database server. Its probably a good idea to use the IP if at all possible, as relying on DNS for the connection between your Web server and database server isnt recommended.
瀵逛簬Django鏉ヨ锛屾妸鏁版嵁搴撴湇鍔″櫒鍒嗙寮鏉ュ緢瀹规槗锛氬彧闇瑕佺畝鍗曞湴淇敼 DATABASE_HOST 锛岃缃负鏂扮殑鏁版嵁搴撴湇鍔″櫒鐨処P鍦板潃鎴栬匘NS鍩熷悕銆傝缃负IP鍦板潃鎬绘槸涓涓ソ涓绘剰锛屽洜涓轰娇鐢―NS鍩熷悕锛岃繕瑕佺壍娑夊埌DNS鏈嶅姟鍣ㄧ殑鍙潬鎬ц繛鎺ラ棶棰樸
With a separate database server, our architecture now looks like Figure 20-2.
浣跨敤浜嗕竴涓嫭绔嬬殑鏁版嵁搴撴湇鍔″櫒浠ュ悗锛屾垜浠殑鏋勬灦鍙樻垚浜嗗浘20-2銆
Figure 20-2: Moving the database onto a dedicated server.
鍥 20-2锛氬皢鏁版嵁搴撶Щ鍒板崟鐙殑鏈嶅姟鍣ㄤ笂銆
Here were starting to move into whats usually called n-tier architecture. Dont be scared by the buzzword it just refers to the fact that different tiers of the Web stack get separated out onto different physical machines.
杩欓噷锛屾垜浠紑濮嬫鍏 n-tier 鏋勬灦銆備笉瑕佽杩欎釜璇嶆墍鍚撳潖锛屽畠鍙槸璇存槑浜哤eb鏍堢殑涓嶅悓閮ㄥ垎锛岃鍒嗙鍒颁簡涓嶅悓鐨勭墿鐞嗘満鍣ㄤ笂銆
At this point, if you anticipate ever needing to grow beyond a single database server, its probably a good idea to start thinking about connection pooling and/or database replication. Unfortunately, theres not nearly enough space to do those topics justice in this book, so youll need to consult your databases documentation and/or community for more information.
鎴戜滑鍐嶆潵鐪嬶紝濡傛灉鍙戠幇闇瑕佷笉姝竴鍙扮殑鏁版嵁搴撴湇鍔″櫒锛岃冭檻浣跨敤杩炴帴姹犲拰鏁版嵁搴撳浠(鍐椾綑锛夊皢鏄竴涓ソ涓绘剰銆備笉骞哥殑鏄紝鏈功娌℃湁瓒冲鐨勬椂闂存潵璁ㄨ杩欎釜闂锛屾墍浠ヤ綘鍙傝冩暟鎹簱鏂囨。鎴栬呭悜绀惧尯姹傚姪銆
We still have a big problem left over from the single-server setup: the serving of media from the same box that handles dynamic content.
浣跨敤鍗曟満鏈嶅姟鍣ㄤ粛鐒剁暀涓嬩簡涓涓ぇ闂锛氬鐞嗗姩鎬佸唴瀹圭殑濯掍綋璧勬簮锛屼篃鏄湪鍚屼竴鍙版満鍣ㄤ笂瀹屾垚鐨勩
Those two activities perform best under different circumstances, and by smashing them together on the same box you end up with neither performing particularly well. So the next step is to separate out the media that is, anything not generated by a Django view onto a dedicated server (see Figure 20-3).
杩欎袱涓椿鍔ㄦ槸鍦ㄤ笉鍚岀殑鏉′欢涓嬭繘琛岀殑锛屽洜姝ゆ妸瀹冧滑寮鸿鍑戝拰鍦ㄥ悓涓鍙版満鍣ㄤ笂锛屼綘涓嶅彲鑳借幏寰楀緢濂界殑鎬ц兘銆備笅涓姝ワ紝鎴戜滑瑕佹妸濯掍綋璧勬簮锛堜换浣 涓嶆槸 鐢盌jango瑙嗗浘浜х敓鐨勪笢瑗匡級鍒嗙鍒板埆鐨勬湇鍔″櫒涓婏紙璇风湅鍥20-3锛夈
Figure 20-3: Separating out the media server.
鍥 20-3锛氬垎绂诲嚭濯掍綋鏈嶅姟鍣ㄣ
Ideally, this media server should run a stripped-down Web server optimized for static media delivery. lighttpd and tux (http://www.djangoproject.com/r/tux/) are both excellent choices here, but a heavily stripped down Apache could work, too.
鐞嗘兂鐨勬儏鍐垫槸锛岃繖涓獟浣撴湇鍔″櫒鏄竴涓畾鍒剁殑Web鏈嶅姟鍣紝涓轰紶閫侀潤鎬佸獟浣撹祫婧愬仛浜嗕紭鍖栥俵ighttpd鍜宼ux (http://www.djangoproject.com/r/tux/) 閮芥槸鏋佷匠鐨勯夋嫨锛屽綋鐒剁槮韬殑Apache鏈嶅姟鍣ㄤ篃鍙互宸ヤ綔鐨勫緢濂姐
For sites heavy in static content (photos, videos, etc.), moving to a separate media server is doubly important and should likely be the first step in scaling up.
瀵逛簬鎷ユ湁澶ч噺闈欐佸唴瀹癸紙鐓х墖銆佽棰戠瓑锛夌殑绔欑偣鏉ヨ锛屽皢濯掍綋鏈嶅姟鍣ㄥ垎绂诲嚭鍘绘樉鐒舵湁鐫鏇村姞閲嶈鐨勬剰涔夛紝鑰屼笖搴旇鏄墿澶ц妯$殑鏃跺欐墍瑕侀噰鍙栫殑 绗竴姝ユ帾鏂 銆
This step can be slightly tricky, however. Djangos admin needs to be able to write uploaded media to the media server (the MEDIA_ROOT setting controls where this media is written). If media lives on another server, however, youll need to arrange a way for that write to happen across the network.
杩欎竴姝ラ渶瑕佷竴鐐圭偣鎶宸э紝Django鐨刟dmin绠$悊鎺ュ彛闇瑕佽兘澶熻幏寰楄冻澶熺殑鏉冮檺鏉ュ鐞嗕笂浼犵殑濯掍綋锛堥氳繃璁剧疆 MEDIA_ROOT 锛夈傚鏋滃獟浣撹祫婧愬湪鍙﹀鐨勪竴鍙版湇鍔″櫒涓婏紝浣犻渶瑕佽幏寰楅氳繃缃戠粶鍐欐搷浣滅殑鏉冮檺銆
The easiest way to do this is to use NFS to mount the media servers media directories onto the Web server(s). If you mount them in the same location pointed to by MEDIA_ROOT , media uploading will Just Work.
鏈绠鍗曠殑鏂规鏄娇鐢∟FS锛堢綉缁滄枃浠剁郴缁燂級鎶婂獟浣撴湇鍔″櫒鐨勭洰褰曟寕杞藉埌Web鏈嶅姟鍣ㄤ笂鏉ャ傚彧瑕 MEDIA_ROOT 璁剧疆姝g‘锛屽獟浣撶殑涓婁紶灏卞彲浠ユ甯稿伐浣溿
At this point, weve broken things down as much as possible. This three-server setup should handle a very large amount of traffic we served around 10 million hits a day from an architecture of this sort so if you grow further, youll need to start adding redundancy.
鐜板湪锛屾垜浠凡缁忓敖鍙兘鍦拌繘琛屼簡鍒嗚В銆傝繖绉嶄笁鍙版湇鍔″櫒鐨勬瀯鏋跺彲浠ユ壙鍙楀緢澶х殑娴侀噺锛屾瘮濡傛瘡澶1000涓囩殑鐐瑰嚮鐜囥傚鏋滆繕闇瑕佽繘涓姝ュ湴澧炲姞锛屼綘灏遍渶瑕佸紑濮嬪鍔犲啑浣欏浠戒簡銆
This is a good thing, actually. One glance at Figure 20-3 shows you that if even a single one of your three servers fails, youll bring down your entire site. So as you add redundant servers, not only do you increase capacity, but you also increase reliability.
杩欐槸涓ソ涓绘剰銆傝鐪嬪浘 20-3锛屼竴鏃︿笁涓湇鍔″櫒涓殑浠讳綍涓涓彂鐢熶簡鏁呴殰锛屼綘灏卞緱鍏抽棴鏁翠釜绔欑偣銆傚洜姝ゅ湪寮曞叆鍐椾綑澶囦唤鐨勬椂鍊欙紝浣犲苟涓嶅彧鏄鍔犱簡瀹归噺锛屽悓鏃朵篃澧炲姞浜嗗彲闈犳с
For the sake of this example, lets assume that the Web server hits capacity first. Its easy to get multiple copies of a Django site running on different hardware just copy all the code onto multiple machines, and start Apache on both of them.
鎴戜滑棣栧厛鏉ヨ冭檻Web鏈嶅姟鍣ㄧ殑鐐瑰嚮閲忋傛妸鍚屼竴涓狣jango鐨勭珯鐐瑰鍒跺浠斤紝鍦ㄥ鍙版満鍣ㄤ笂鍚屾椂杩愯寰堝鏄擄紝鎴戜滑涔熷彧闇瑕佸悓鏃惰繍琛屽鍙版満鍣ㄤ笂鐨凙pache鏈嶅姟鍣ㄣ
However, youll need another piece of software to distribute traffic over your multiple servers: a load balancer . You can buy expensive and proprietary hardware load balancers, but there are a few high-quality open source software load balancers out there.
浣犺繕闇瑕佸彟涓涓蒋浠舵潵甯姪浣犲湪澶氬彴鏈嶅姟鍣ㄤ箣闂村潎琛$綉缁滄祦閲忥細 娴侀噺鍧囪 鍣紙load balancer锛 銆備綘鍙互璐拱鏄傝吹鐨勪笓鏈夌殑纭欢鍧囪 鍣紝褰撶劧涔熸湁涓浜涢珮璐ㄩ噺鐨勫紑婧愮殑杞欢鍧囪 鍣ㄥ彲渚涢夋嫨銆
Apaches mod_proxy is one option, but weve found Perlbal (http://www.djangoproject.com/r/perlbal/) to be simply fantastic. Its a load balancer and reverse proxy written by the same folks who wrote memcached (see Chapter 13).
Apaches 鐨 mod_proxy 鏄竴涓彲浠ヨ冭檻鐨勯夋嫨锛屼絾鍙︿竴涓厤缃洿妫掔殑閫夋嫨鏄細Perlbal (http://www.djangoproject.com/r/perlbal/) 銆侾erlbal鏄竴涓潎琛″櫒锛屽悓鏃朵篃鏄竴涓弽鍚戜唬鐞嗭紝瀹冪殑寮鍙戣呭拰memcached鐨勫紑鍙戣呮槸鍚屼竴鎷ㄤ汉锛堣瑙13绔狅級銆
Note
澶囨敞
If youre using FastCGI, you can accomplish this same distribution/load balancing step by separating your front-end Web servers and back-end FastCGI processes onto different machines. The front-end server essentially becomes the load balancer, and the back-end FastCGI processes replace the Apache/mod_python/Django servers.
濡傛灉浣犱娇鐢‵astCGI锛屼綘鍚屾牱鍙互鍒嗙鍓嶅彴鐨剋eb鏈嶅姟鍣紝骞跺湪澶氬彴鍏朵粬鏈哄櫒涓婅繍琛孎astCGI鏈嶅姟鍣ㄦ潵瀹炵幇鐩稿悓鐨勮礋杞藉潎琛$殑鍔熻兘銆傚墠鍙扮殑鏈嶅姟鍣ㄥ氨鐩稿綋浜庢槸涓涓潎琛″櫒锛岃屽悗鍙扮殑FastCGI鏈嶅姟杩涚▼浠f浛浜咥pache/mod_python/Django鏈嶅姟鍣ㄣ
With the Web servers now clustered, our evolving architecture starts to look more complex, as shown in Figure 20-4.
鐜板湪鎴戜滑鎷ユ湁浜嗘湇鍔″櫒闆嗙兢锛屾垜浠殑鏋勬灦鎱㈡參婕斿寲锛岃秺鏉ヨ秺澶嶆潅锛屽鍥20-4銆
Figure 20-4: A load-balanced, redundant server setup.
鍥 20-4锛氳礋杞藉潎琛$殑鏈嶅姟鍣ㄨ缃
Notice that in the diagram the Web servers are referred to as a cluster to indicate that the number of servers is basically variable. Once you have a load balancer out front, you can easily add and remove back-end Web servers without a second of downtime.
鍊煎緱涓鎻愮殑鏄紝鍦ㄥ浘涓紝Web鏈嶅姟鍣ㄦ寚鐨勬槸涓涓泦缇わ紝鏉ヨ〃绀鸿澶氭暟閲忕殑鏈嶅姟鍣ㄣ備竴鏃︿綘鎷ユ湁浜嗕竴涓墠鍙扮殑鍧囪 鍣紝浣犲氨鍙互寰堟柟渚垮湴澧炲姞鍜屽垹闄ゅ悗鍙扮殑Web鏈嶅姟鍣紝鑰屼笖涓嶄細閫犳垚浠讳綍缃戠珯涓嶅彲鐢ㄧ殑鏃堕棿銆
At this point, the next few steps are pretty much derivatives of the last one:
涓嬮潰鐨勮繖浜涙楠ら兘鏄笂闈㈡渶鍚庝竴涓殑鍙樹綋锛
As you need more database performance, youll need to add replicated database servers. MySQL includes built-in replication; PostgreSQL users should look into Slony (http://www.djangoproject.com/r/slony/) and pgpool (http://www.djangoproject.com/r/pgpool/) for replication and connection pooling, respectively.
褰撲綘闇瑕佹洿濂界殑鏁版嵁搴撴ц兘锛屼綘鍙兘闇瑕佸鍔犳暟鎹簱鐨勫啑浣欐湇鍔″櫒銆侻ySQL鍐呯疆浜嗗浠藉姛鑳斤紱PostgreSQL搴旇鐪嬩竴涓婼lony (http://www.djangoproject.com/r/slony/) 鍜 pgpool (http://www.djangoproject.com/r/pgpool/) 锛岃繖涓や釜鍒嗗埆鏄暟鎹簱澶囦唤鍜岃繛鎺ユ睜鐨勫伐鍏枫
If the single load balancer isnt enough, you can add more load balancer machines out front and distribute among them using round-robin DNS.
濡傛灉鍗曚釜鍧囪 鍣ㄤ笉鑳借揪鍒拌姹傦紝浣犲彲浠ュ鍔犳洿澶氱殑鍧囪 鍣紝骞朵笖浣跨敤杞锛坮ound-robin锛塂NS鏉ュ疄鐜板垎甯冭闂
If a single media server doesnt suffice, you can add more media servers and distribute the load with your load-balancing cluster.
濡傛灉鍗曞彴濯掍綋鏈嶅姟鍣ㄤ笉澶熺敤锛屼綘鍙互澧炲姞鏇村鐨勫獟浣撴湇鍔″櫒锛屽苟閫氳繃闆嗙兢鏉ュ垎甯冩祦閲忋
If you need more cache storage, you can add dedicated cache servers.
濡傛灉浣犻渶瑕佹洿澶氱殑楂橀熺紦瀛橈紙cache锛夛紝浣犲彲浠ュ鍔燾ache鏈嶅姟鍣ㄣ
At any stage, if a cluster isnt performing well, you can add more servers to the cluster.
鍦ㄤ换浣曟儏鍐典笅锛屽彧瑕侀泦缇ゅ伐浣滄ц兘涓嶅ソ锛屼綘閮藉彲浠ュ線涓婂鍔犳湇鍔″櫒銆
After a few of these iterations, a large-scale architecture might look like Figure 20-5.
閲嶅浜嗗嚑娆′互鍚庯紝涓涓ぇ瑙勬ā鐨勬瀯鏋朵細鍍忓浘20-5銆
Figure 20-5. An example large-scale Django setup.
鍥 20-5銆傚ぇ瑙勬ā鐨凞jango瀹夎銆
Though weve shown only two or three servers at each level, theres no fundamental limit to how many you can add.
灏界鎴戜滑鍙槸鍦ㄦ瘡涓灞備笂灞曠ず浜嗕袱鍒颁笁鍙版湇鍔″櫒锛屼綘鍙互鍦ㄤ笂闈㈤殢鎰忓湴澧炲姞鏇村銆
Once you get up to this level, youve got quite a few options. Appendix A has some information from a few developers responsible for some large-scale Django installations. If youre planning a high-traffic Django site, its worth a read.
褰撲綘鍒颁簡杩欎竴涓樁娈碉紝浣犳湁涓浜涢夋嫨銆傞檮褰旳鏈変竴浜涘紑鍙戣呭叧浜庡ぇ鍨嬬郴缁熺殑淇℃伅銆傚鏋滀綘鎯宠鏋勫缓涓涓珮娴侀噺鐨凞jango绔欑偣锛岄偅鍊煎緱涓璇汇
If you have huge amount of money, you can just keep throwing hardware at scaling problems. For the rest of us, though, performance tuning is a must.
濡傛灉浣犳湁澶х瑪澶х瑪鐨勯挶锛岄亣鍒版墿灞曟ч棶棰樻椂锛屼綘鍙互绠鍗曞湴鎶曡祫纭欢銆傚浜庡墿涓嬬殑浜烘潵璇达紝鎬ц兘浼樺寲灏辨槸蹇呴』瑕佸仛鐨勪竴浠朵簨銆
Note
澶囨敞
Incidentally, if anyone with monstrous gobs of cash is actually reading this book, please consider a substantial donation to the Django project. We accept uncut diamonds and gold ingots, too.
椤轰究鎻愪竴鍙ワ紝璋佽鏄湁澶х瑪澶х瑪鐨勯挒绁紝璇锋崘鍔╀竴鐐笵jango椤圭洰銆傛垜浠篃鎺ュ彈鏈垏鍓茬殑閽荤煶鍜岄噾甯併
Unfortunately, performance tuning is much more of an art than a science, and it is even more difficult to write about than scaling. If youre serious about deploying a large-scale Django application, you should spend a great deal of time learning how to tune each piece of your stack.
涓嶅垢鐨勬槸锛屾ц兘浼樺寲姣旇捣绉戝鏉ヨ鏇村儚鏄竴绉嶈壓鏈紝骞朵笖杩欐瘮鎵╁睍鎬ф洿闅炬弿杩般傚鏋滀綘鐪熸兂瑕佹瀯寤轰竴涓ぇ瑙勬ā鐨凞jango搴旂敤锛屼綘闇瑕佽姳澶ч噺鐨勬椂闂村拰绮惧姏瀛︿範濡備綍浼樺寲鏋勬灦涓殑姣忎竴閮ㄥ垎銆
The following sections, though, present a few Django-specific tuning tips weve discovered over the years.
浠ヤ笅閮ㄥ垎鎬荤粨浜嗗骞翠互鏉ョ殑缁忛獙锛屾槸涓浜涗笓灞炰簬Django鐨勪紭鍖栨妧宸с
As of this writing, the really expensive RAM costs only about $200 per gigabyte pennies compared to the time spent tuning elsewhere. Buy as much RAM as you can possibly afford, and then buy a little bit more.
鍐欒繖绡囨枃绔犵殑鏃跺欙紝RAM鐨勪环鏍煎凡缁忛檷鍒颁簡姣廏澶х害200缇庡厓銆傝喘涔板敖鍙兘澶氱殑RAM锛屽啀鍦ㄥ埆鐨勪笂闈㈡姇璧勪竴鐐圭偣銆
Faster processors wont improve performance all that much; most Web servers spend up to 90% of their time waiting on disk I/O. As soon as you start swapping, performance will just die. Faster disks might help slightly, but theyre much more expensive than RAM, such that it doesnt really matter.
楂橀熺殑澶勭悊鍣ㄥ苟涓嶄細澶у箙搴﹀湴鎻愰珮鎬ц兘锛涘ぇ澶氭暟鐨刉eb鏈嶅姟鍣90%鐨勬椂闂撮兘娴垂鍦ㄤ簡纭洏IO涓娿傚綋纭洏涓婄殑鏁版嵁寮濮嬩氦鎹紝鎬ц兘灏辨ュ墽涓嬮檷銆傛洿蹇熺殑纭洏鍙互鏀瑰杽杩欎釜闂锛屼絾鏄瘮璧稲AM鏉ヨ锛岄偅澶吹浜嗐
If you have multiple servers, the first place to put your RAM is in the database server. If you can afford it, get enough RAM to get fit your entire database into memory. This shouldnt be too hard. LJWorld.coms database including over half a million newspaper articles dating back to 1989 is under 2GB.
濡傛灉浣犳嫢鏈夊鍙版湇鍔″櫒锛岄瑕佺殑鏄鍦ㄦ暟鎹簱鏈嶅姟鍣ㄤ笂澧炲姞鍐呭瓨銆傚鏋滀綘鑳借礋鎷呭緱璧凤紝鎶婁綘鏁翠釜鏁版嵁搴撻兘鏀惧叆鍒板唴瀛樹腑銆傝繖涓嶄細寰堥毦銆侺JWorld.com绛夌綉绔欑殑鏁版嵁搴撲繚瀛樹簡澶ч噺浠1989骞磋捣鑷充粖鐨勬姤绾稿拰鏂囩珷锛屽唴瀛樼殑娑堣椾篃涓嶅埌2G銆
Next, max out the RAM on your Web server. The ideal situation is one where neither server swaps ever. If you get to that point, you should be able to withstand most normal traffic.
涓嬩竴姝ワ紝鏈澶у寲Web鏈嶅姟鍣ㄤ笂鐨勫唴瀛樸傛渶鐞嗘兂鐨勬儏鍐垫槸锛屾病鏈変竴鍙版湇鍔″櫒杩涜纾佺洏浜ゆ崲銆傚鏋滀綘杈惧埌浜嗚繖涓按骞筹紝浣犲氨鑳藉簲浠樺ぇ澶氭暟姝e父鐨勬祦閲忋
Keep-Alive is a feature of HTTP that allows multiple HTTP requests to be served over a single TCP connection, avoiding the TCP setup/teardown overhead.
Keep-Alive 鏄疕TTP鎻愪緵鐨勫姛鑳戒箣涓锛屽畠鐨勭洰鐨勬槸鍏佽澶氫釜HTTP璇锋眰澶嶇敤涓涓猅CP杩炴帴锛屼篃灏辨槸鍏佽鍦ㄥ悓涓涓猅CP杩炴帴涓婂彂璧峰涓狧TTP璇锋眰锛岃繖鏍锋湁鏁堢殑閬垮厤浜嗘瘡涓狧TTP璇锋眰閮介噸鏂板缓绔嬭嚜宸辩殑TCP杩炴帴鐨勫紑閿銆
This looks good at first glance, but it can kill the performance of a Django site. If youre properly serving media from a separate server, each user browsing your site will only request a page from your Django server every ten seconds or so. This leaves HTTP servers waiting around for the next keep-alive request, and an idle HTTP server just consumes RAM that an active one should be using.
杩欎竴鐪肩湅涓婂幓鏄ソ浜嬶紝浣嗗畠瓒充互鏉姝籇jango绔欑偣鐨勬ц兘銆傚鏋滀綘浠庡崟鐙殑濯掍綋鏈嶅姟鍣ㄤ笂鍚戠敤鎴锋彁渚涙湇鍔★紝姣忎釜鍏夐【浣犵珯鐐圭殑鐢ㄦ埛閮藉ぇ绾10绉掗挓宸﹀彸鍙戝嚭涓娆¤姹傘傝繖灏变娇寰桯TTP鏈嶅姟鍣ㄤ竴鐩村湪绛夊緟涓嬩竴娆eep-alive 鐨勮姹傦紝绌洪棽鐨凥TTP鏈嶅姟鍣ㄥ拰宸ヤ綔鏃舵秷鑰椾竴鏍峰鐨勫唴瀛樸
Although Django supports a number of different cache back-ends, none of them even come close to being as fast as memcached. If you have a high-traffic site, dont even bother with the other back-ends go straight to memcached.
灏界Django鏀寔澶氱涓嶅悓鐨刢ache鍚庡彴鏈哄埗锛屾病鏈変竴绉嶇殑鎬ц兘鍙互 鎺ヨ繎 memcached銆傚鏋滀綘鏈変竴涓珮娴侀噺鐨勭珯鐐癸紝涓嶈鐘硅鲍锛岀洿鎺ラ夋嫨memcached銆
Of course, selecting memcached does you no good if you dont actually use it. Chapter 13 is your best friend here: learn how to use Djangos cache framework, and use it everywhere possible. Aggressive, preemptive caching is usually the only thing that will keep a site up under major traffic.
褰撶劧锛岄夋嫨浜唌emcached鑰屼笉鍘讳娇鐢ㄥ畠锛屼綘涓嶄細浠庝腑鑾峰緱浠讳綍鎬ц兘涓婄殑鎻愬崌銆傜13绔犲皢涓轰綘鎻愪緵鏈夌敤鐨勪俊鎭細瀛︿範濡備綍浣跨敤Django鐨刢ache妗嗘灦锛屽苟涓斿敖鍙兘鍦颁娇鐢ㄥ畠銆傚ぇ閲忕殑鍙姠鍗犲紡鐨勯珮閫熺紦瀛橀氬父鏄竴涓珯鐐瑰湪澶ф祦閲忎笅姝e父宸ヤ綔鐨勫敮涓鐡堕銆
Each piece of the Django stack from Linux to Apache to PostgreSQL or MySQL has an awesome community behind it. If you really want to get that last 1% out of your servers, join the open source communities behind your software and ask for help. Most free-software community members will be happy to help.
Django鐩稿叧鐨勬瘡涓涓儴鍒嗭紝浠嶭inux鍒癆pache鍒癙ostgreSQL鎴栬匨ySQL鑳屽悗锛岄兘鏈変竴涓潪甯告鐨勭ぞ鍖烘敮鎸併傚鏋滀綘鐪熸兂浠庝綘鐨勬湇鍔″櫒涓婃Θ骞叉渶鍚1%鐨勬ц兘锛屽姞鍏ュ紑婧愮ぞ鍖哄姹傚府鍔┿傚鏁扮殑鑷敱杞欢绀惧尯鎴愬憳閮戒細寰堜箰鎰忓湴鎻愪緵甯姪銆
And also be sure to join the Django community. Your humble authors are only two members of an incredibly active, growing group of Django developers. Our community has a huge amount of collective experience to offer.
鍒繕浜咲jango绀惧尯銆傝繖鏈功璋﹂婄殑浣滆呭彧鏄疍jango寮鍙戝洟闃熶腑鐨勪袱浣嶆垚鍛樸傛垜浠殑绀惧尯鏈夊ぇ閲忕殑缁忛獙鍙互鎻愪緵銆
Youve reached the end of our regularly scheduled program. The following appendixes all contain reference material that you might need as you work on your Django projects.
浣犲凡缁忕湅鍒颁簡姝f枃鐨勭粨鏉熼儴鍒嗕簡銆備笅闈㈢殑杩欎簺闄勫綍閮藉寘鍚簡璁稿鍙傝冭祫鏂欙紝褰撲綘鏋勫缓浣犵殑Django椤圭洰鏃讹紝鏈夊彲鑳戒細鐢ㄥ埌銆
We wish you the best of luck in running your Django site, whether its a little toy for you and a few friends, or the next Google.
鎴戜滑甯屾湜浣犵殑Django绔欑偣杩愯鑹ソ锛屾棤璁轰綘鐨勭珯鐐规槸浣犲拰浣犳湅鍙嬩箣闂寸殑涓涓皬鐜╁叿锛岃繕鏄笅涓涓狦oogle銆
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful
YAHOO.ext
library. Thanks also to Yahoo for YUI itself.