The Django Book

Chapter 20: Deploying Django

绗簩鍗佺珷锛氶儴缃睤jango

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 鍚э紒

Shared Nothing

鏃犲叡浜

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

  • Facebook

  • Facebook

  • Google

  • Google

  • 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!

璇峰厑璁告垜鐢 褰撳搱鍒╅亣瑙佹矙鑾 涓殑钁楀悕鍦烘櫙鏉ユ瘮鍠伙細浠栦滑鏈夌殑鎴戜滑涔熶細鏈夛紒

A Note on Personal Preferences

鍏充簬涓汉鍋忓ソ鐨勫娉

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鐢ㄦ埛鍚屾牱涔熷ぇ鑾锋垚鍔熴

Using Django with Apache and mod_python

鐢ˋpache鍜宮od_python鏉ラ儴缃睤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鐨勭浉鍏崇煡璇嗭紝鍙互鎵惧埌鐩稿綋澶氱殑缁濅匠璧勬簮銆備笅闈㈡槸鎴戜滑鎵涓剰鐨勯儴鍒嗚祫鏂欙細

Basic Configuration

鍩烘湰閰嶇疆

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')),
)

Running Multiple Django Installations on the Same Apache Instance

鍦ㄥ悓涓涓 Apache 鐨勫疄渚嬩腑杩愯澶氫釜 Django 绋嬪簭

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 鍧椾腑涓嶅悓銆

Running a Development Server with mod_python

鐢 mod_python 杩愯涓涓紑鍙戞湇鍔″櫒

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 銆傚彟涓涓夋嫨鏄湪妯℃澘椤甸潰涓姞鍏ヨ皟璇曚俊鎭

Serving Django and Media Files from the Same Apache Instance

浣跨敤鐩稿悓鐨凙pache瀹炰緥鏉ユ湇鍔jango鍜孧edia鏂囦欢

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鎵嶈兘鐭ラ亾浣犲瓨鏀鹃潤鎬佹枃浠剁殑浣嶇疆銆

Error Handling

閿欒澶勭悊

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 鐨勫畬鏁村洖婧俊鎭 閿欒鏃ュ織 鐨勫洖婧俊鎭湁澶氳銆傚綋鐒讹紝杩欎簺淇℃伅鏄毦鐪嬩笖闅句互闃呰鐨勩

Handling a Segmentation Fault

澶勭悊娈甸敊璇

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 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锛夐兘鍙互甯綘璇嗗埆鍏变韩渚濊禆鍜屽彲鑳界殑鐗堟湰鍐茬獊銆

Using Django with FastCGI

浣跨敤FastCGI閮ㄧ讲Django搴旂敤

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 Overview

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鐗堟湰銆

Running Your FastCGI Server

杩愯浣犵殑 FastCGI 鏈嶅姟鍣

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 鎴栬呭悓鏃舵寚瀹 hostport 銆傚綋浣犺鍒涘缓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
Stopping the FastCGI Daemon
鍋滄FastCGI鐨勮绋

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

Using Django with Apache and FastCGI

鍦ˋpache涓互FastCGI鐨勬柟寮忎娇鐢―jango

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銆

Specifying the Location of the FastCGI Server
鎸囧畾 FastCGI Server 鐨勪綅缃

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澶勭悊锛堜笅涓閮ㄥ垎璇︾粏璁ㄨ锛夈

Using mod_rewrite to Point URLs at FastCGI
浣跨敤mod_rewrite涓篎astCGI鎸囧畾URL

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>

FastCGI and lighttpd

FastCGI 鍜 lighttpd

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_rewritemod_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",
)
Running Multiple Django Sites on One lighttpd Instance
鍦ㄤ竴涓猯ighttpd杩涚▼涓繍琛屽涓狣jango绔欑偣

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涓绘満銆

Running Django on a Shared-Hosting Provider with Apache

鍦ㄤ娇鐢ˋpache鐨勫叡浜富鏈烘湇鍔″晢澶勮繍琛孌jango

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")
Restarting the Spawned Server
閲嶅惎鏂颁骇鐢熺殑杩涚▼鏈嶅姟鍣

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

Scaling

鍙墿灞曟

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銆

Running on a Single Server

杩愯鍦ㄤ竴鍙板崟鏈烘湇鍔″櫒涓

Most sites start out running on a single server, with an architecture that looks something like Figure 20-1.

澶у鏁扮殑绔欑偣涓寮濮嬮兘杩愯鍦ㄥ崟鏈烘湇鍔″櫒涓婏紝鐪嬭捣鏉ュ儚鍥20-1杩欐牱鐨勬瀯鏋躲

http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-1.png http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-1.png

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.

閫氳繃鎶婃暟鎹簱鏈嶅姟鍣ㄦ惉绉诲埌绗簩鍙颁富鏈轰笂锛屽彲浠ュ緢瀹规槗鍦拌В鍐宠繖涓棶棰樸傝繖灏嗗湪涓嬩竴閮ㄥ垎浠嬬粛銆

Separating Out the Database Server

鍒嗙鍑烘暟鎹簱鏈嶅姟鍣

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銆

http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-2.png http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-2.png

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.

鎴戜滑鍐嶆潵鐪嬶紝濡傛灉鍙戠幇闇瑕佷笉姝竴鍙扮殑鏁版嵁搴撴湇鍔″櫒锛岃冭檻浣跨敤杩炴帴姹犲拰鏁版嵁搴撳浠(鍐椾綑锛夊皢鏄竴涓ソ涓绘剰銆備笉骞哥殑鏄紝鏈功娌℃湁瓒冲鐨勬椂闂存潵璁ㄨ杩欎釜闂锛屾墍浠ヤ綘鍙傝冩暟鎹簱鏂囨。鎴栬呭悜绀惧尯姹傚姪銆

Running a Separate Media Server

杩愯涓涓嫭绔嬬殑濯掍綋鏈嶅姟鍣

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锛夈

http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-3.png http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-3.png

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‘锛屽獟浣撶殑涓婁紶灏卞彲浠ユ甯稿伐浣溿

Implementing Load Balancing and Redundancy

瀹炵幇璐熸媴鍧囪 鍜屾暟鎹啑浣欏浠

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銆

http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-4.png http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-4.png

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鏈嶅姟鍣紝鑰屼笖涓嶄細閫犳垚浠讳綍缃戠珯涓嶅彲鐢ㄧ殑鏃堕棿銆

Going Big

鎱㈡參鍙樺ぇ

At this point, the next few steps are pretty much derivatives of the last one:

涓嬮潰鐨勮繖浜涙楠ら兘鏄笂闈㈡渶鍚庝竴涓殑鍙樹綋锛

  • 褰撲綘闇瑕佹洿濂界殑鏁版嵁搴撴ц兘锛屼綘鍙兘闇瑕佸鍔犳暟鎹簱鐨勫啑浣欐湇鍔″櫒銆侻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銆

http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-5.png http://new-media.djangobook.com/content/en/1.0/chapter20/scaling-5.png

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绔欑偣锛岄偅鍊煎緱涓璇汇

Performance Tuning

鎬ц兘浼樺寲

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鐨勪紭鍖栨妧宸с

Theres No Such Thing As Too Much RAM

RAM鎬庝箞涔熶笉瀚屽

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父鐨勬祦閲忋

Turn Off Keep-Alive

绂佺敤 Keep-Alive

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鏈嶅姟鍣ㄥ拰宸ヤ綔鏃舵秷鑰椾竴鏍峰鐨勫唴瀛樸

Use memcached

浣跨敤 memcached

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銆

Use memcached Often

缁忓父浣跨敤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父宸ヤ綔鐨勫敮涓鐡堕銆

Join the Conversation

鍙傚姞璁ㄨ

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寮鍙戝洟闃熶腑鐨勪袱浣嶆垚鍛樸傛垜浠殑绀惧尯鏈夊ぇ閲忕殑缁忛獙鍙互鎻愪緵銆

Whats Next?

涓嬩竴姝ワ紵

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銆

Copyright 2006 Adrian Holovaty and Jacob Kaplan-Moss.
This work is licensed under the GNU Free Document License.
Hosting graciously provided by media temple
Chinese translate hosting by py3k.cn.