去评论
dz插件网

一个老外写的wp博客性能优化,大家看看是否可以,最近打算做一个wp的站。

xiao9469
2022/04/04 17:11:17
浣跨敤 15 缇庡厓鐨勬湇鍔″櫒浣跨敤 WordPress 姣忓ぉ鐐瑰嚮 1000 涓囨
[鏇存柊 2] 鈥 浼间箮 W3 Total Cache 鏀寔宸茬粡鎮勬棤澹版伅鍦板仠姝簡 鈥 鐜板湪宸茬粡瓒呰繃 12 涓湀娌℃湁鏇存柊浜嗭紝骞朵笖鎹О瀛樺湪涓浜涘畨鍏ㄩ棶棰樸傚洜姝わ紝鎴戝缓璁笉瑕佸湪杩欎簺璇存槑涓畨瑁 W3 Total Cache锛屽叾浠栦竴鍒囬兘搴旇杩愯鑹ソ銆俒/鏇存柊]

[鏇存柊] 鈥 鎴戝凡缁忎娇鐢ㄦ柊鐨 Ubuntu 12.04 LTS 鐗堟湰瀵规杩涜浜嗘祴璇曪紝鎵鏈夎缃技涔庨兘鍙互姝e父宸ヤ綔鑰屾棤闇淇敼銆傚鏋滄偍鍙戠幇闂锛岃鍛婅瘔鎴 [/Update]

[2013 骞 9 鏈堟洿鏂癩 鈥 鎴戞渶杩戝皢鑷繁鐨勫崥瀹㈡墭绠′粠Digital Ocean杞Щ鍒版瘡鏈 10 缇庡厓鐨 VPS 锛堣閾炬帴閫氳繃闄勫睘閾炬帴涓烘偍鎻愪緵 10 缇庡厓鐨勬姌鎵o級骞朵笖瀵瑰畠浠潪甯告弧鎰忥紝鍖呮嫭鎬ц兘鍗氬銆

杩欎簺璇存槑鐩稿綋鍐楅暱锛屼絾甯屾湜寰堝鏄撻伒寰紝浣跨敤 Varnish銆丯ginx銆乄3 Total Cache 鍜 WordPress 鏋勫缓鏂 Linux 鏈嶅姟鍣ㄧ殑姝ラ锛屽湪 Amazon Micro 鏈嶅姟鍣紙鎴栧悓绛夋湇鍔″櫒锛変笂鏋勫缓 WordPress 鍗氬锛屾墍鏈夋垚鏈牴鎹産litz.io琛¢噺锛屾瘡鏈堜笉鍒 15缇庡厓锛屾瘡澶╄兘澶熺淮鎸 1000 涓囨鐐瑰嚮銆

鍦 15 缇庡厓鐨勮櫄鎷熸湇鍔″櫒涓婁娇鐢 WordPress 姣忓ぉ鐐瑰嚮 1000 涓囨

鍦ㄦ柊鐨勮櫄鎷熶笓鐢ㄦ湇鍔″櫒涓婂畨瑁 Ubuntu 11.10 (Oneiric) - 鎴戣鍒楀嚭鐨勬墍鏈夎蒋浠跺寘閮藉繀椤绘槸 11.10 鎵嶈兘寮绠卞嵆鐢紝浣嗗叾浠栧彂琛岀増閮藉彲浠ャ

鎴戜娇鐢 Amazon EC2 鏋勫缓鎴戠殑娴嬭瘯鏈嶅姟鍣紝浣 Linode 涔熼潪甯稿ソ銆鎴嚦 2013 骞 9 鏈堬紝姝ゅ崥瀹㈢幇鍦ㄦ墭绠″湪Digital Ocean 涓

鍑轰簬鏂囨。鐨勭洰鐨勶紝鎴戠殑鏈嶅姟鍣ㄨ缁嗕俊鎭涓嬶紝鎮ㄧ殑灏嗘湁鎵涓嶅悓锛

鍏叡 DNS 鍚嶇О锛歟c2-23-20-235-223.compute-1.amazonaws.com
鍏叡 IP 鍦板潃锛23.20.235.223

鐧诲綍鏈嶅姟鍣ㄥ苟鎴愪负root

閫氳繃 ssh 浠 ubuntu 韬唤鐧诲綍锛岀劧鍚庤繍琛 鈥嬧媠udo -i 鍛戒护鎴愪负 root

ssh ubuntu@ec2-23-20-235-223.compute-1.amazonaws.com
sudo -i

鍏堥厤缃槻鐏

鐢变簬鎴戜滑灏嗗畨瑁呴粯璁や睛鍚墍鏈夋帴鍙g殑鍚勭缃戠粶鏈嶅姟锛屽洜姝ら厤缃槻鐏寰堥噸瑕併

瀵逛簬 Ubuntu锛岃繖寰堢畝鍗曪紝鍙渶浣跨敤 ufw 鍖呫

鎵ц浠ヤ笅鍛戒护杩涜閰嶇疆锛

ufw 鍏佽 ssh
ufw 鍏佽 http
ufw 娉ㄩ攢
ufw 鍚敤

瀹屾垚姝ゆ搷浣滃悗锛屾偍鐨勬湇鍔″櫒灏嗘嫢鏈変竴涓浉瀵瑰畨鍏ㄧ殑闃茬伀澧欙紝灏界鍊煎緱鏌ョ湅 fail2ban 浠ラ槻姝㈡毚鍔涘瘑鐮佹敾鍑汇

濡傛灉鎮ㄤ娇鐢ㄧ殑鏄 Amazon EC2锛屾偍杩橀渶瑕佹墦寮瀹夊叏缁勪互鍏佽绔彛 80 涓婄殑娴侀噺銆傛偍鍙互浣跨敤AWS 瀹夊叏缁勬帶鍒跺彴鎵ц姝ゆ搷浣滐紝鎮ㄥ彲鑳介渶瑕佹洿鏀瑰尯鍩熴傞夋嫨鍚姩瀹炰緥鏃朵娇鐢ㄧ殑瀹夊叏缁勶紝鐒跺悗鐐瑰嚮鈥淚nbound鈥濓紝鐒跺悗浠庝笅鎷夎彍鍗曚腑閫夋嫨鈥淗TTP鈥濓紝鏈鍚庣偣鍑烩淎dd Rule鈥濄傛偍鏃犻渶閲嶆柊鍚姩瀹炰緥鍗冲彲浣垮叾鐢熸晥銆

瀹夎鍜岄厤缃 MySQL

apt-get 鏇存柊
apt-get 瀹夎 mysql-server

鍑虹幇鎻愮ず鏃讹紝璁剧疆 mysql 鈥渞oot鈥濈敤鎴峰瘑鐮

mysql -u 鏍圭洰褰 -p

鍑虹幇鎻愮ず鏃讹紝杈撳叆鏂拌缃殑 root 瀵嗙爜

鍦 mysql> 鎻愮ず绗︿笅锛岃繍琛屼互涓 4 涓懡浠わ紝灏 ENTER_A_PASSWORD 鏇挎崲涓烘偍鑷繁鐨勫瘑鐮

鍒涘缓鏁版嵁搴 wordpress锛
鎺堜簣 wordpress 涓婄殑鎵鏈夋潈闄愩* 鍒扳渨p_user鈥滰鈥渓ocalhost鈥濈敱鈥淓NTER_A_PASSWORD鈥濊瘑鍒紱
鍚岃姳椤虹壒鏉冿紱
鍑哄彛

鍗冲畨瑁呬簡 MySQL锛屼负 PHP 鍜 Web 鏈嶅姟鍣ㄥ畨瑁 (nginx) 鍋氬ソ浜嗗噯澶囥

瀹夎鍜岄厤缃 PHP

鎴戜滑涓嶄粎闇瑕佸畨瑁 PHP锛岃繕闇瑕佸畨瑁 PHP FPM 绯荤粺銆丄PC 鍜 MySQL 妯″潡

apt-get install php5-fpm php-pear php5-common php5-mysql php-apc php5-gd

缂栬緫 /etc/php5/fpm/php.ini 骞跺湪搴曢儴娣诲姞杩欎簺琛岋細

[apc]
apc.write_lock = 1
apc.slam_defense = 0

缂栬緫 /etc/php5/fpm/pool.d/www.conf

浠f浛

鍚 = 127.0.0.1:9000


鍚 = /dev/shm/php-fpm-www.sock

鍦ㄦ涔嬩笅锛屾彃鍏ヨ繖 3 琛

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

鐒跺悗锛屽湪鍚屼竴涓枃浠朵腑鍐嶅線涓嬶紝鏇挎崲杩欎袱琛

鐢ㄦ埛 = www 鏁版嵁
缁 = www 鏁版嵁


鐢ㄦ埛 = nginx
缁 = nginx

淇濆瓨鏂囦欢锛孭HP FPM鐜板湪宸茬粡瀹屾垚浜嗭紝浣嗘槸鍦ㄦ垜浠畨瑁呭ソnginx涔嬪墠瀹冧笉浼氬伐浣滐紝鎵浠ョ幇鍦ㄤ笉鐢ㄦ媴蹇冨惎鍔ㄥ畠銆

瀹夎鍜岄厤缃 Nginx

鍩轰簬Nginx 缃戠珯鐨勮鏄庛

涓嬭浇nginx瀹夊叏瀵嗛挜楠岃瘉鍖

cd /tmp/
wget http://nginx.org/keys/nginx_signing.key
apt-key add /tmp/nginx_signing.key

閫氳繃杩愯杩 2 涓懡浠ゅ皢婧愭坊鍔犲埌 APT 婧愭枃浠讹紙>> 寰堥噸瑕侊紒锛

echo 鈥渄eb http://nginx.org/packages/ubuntu/ lucid nginx鈥 >> /etc/apt/sources.list
echo 鈥渄eb-src http://nginx.org/packages/ubuntu/ lucid nginx鈥 >> /etc/apt/sources.list

閫氳繃杩愯涓嬭浇骞跺畨瑁 nginx

apt-get 鏇存柊
apt-get 瀹夎 nginx

瀹屾垚鍚庯紝灏嗗畨瑁 nginx锛屼絾闇瑕佷负 WordPress 杩涜閰嶇疆銆

nginx閰嶇疆鏂囦欢鍦/etc/nginx

棣栧厛锛岀紪杈 /etc/nginx/nginx.conf

鍦 http 閮ㄥ垎涓紝鎻掑叆浠ヤ笅琛岋紝浠ヤ究浠ュ悗鍦ㄥ墠闈㈡坊鍔 varnish 鏃讹紝浜嬫儏涓嶄細鍒板涔变竷鍏碂锛屽洜姝 varnish 浼氱紦瀛樺帇缂╂枃浠讹細

port_in_redirect 鍏抽棴锛
gzip鎵撳紑锛
gzip_types 鏂囨湰/css 鏂囨湰/xml 鏂囨湰/javascript 搴旂敤绋嬪簭/x-javascript锛
gzip_vary on锛

鎺ヤ笅鏉ワ紝cd 鍒 /etc/nginx/conf.d 骞跺垱寤轰竴涓柊鏂囦欢 /etc/nginx/conf.d/drop 锛屽叾涓鍖呭惈鏉ヨ嚜 GitHub鐨 drop 鏂囦欢鐨勫唴瀹

鐒跺悗锛屽皢 /etc/nginx/conf.d/default.conf 鏇挎崲涓github default.conf 鏂囦欢鐨勫唴瀹癸紝灏哾omainname.com 鐨勬墍鏈夋潯鐩洿鏀逛负鎮ㄨ嚜宸辩殑鍩熷悕锛堟湁 3 涓潯鐩紝鍖呮嫭搴曢儴闄勮繎鐨 1 涓級

鍒涘缓涓涓洰褰 /var/www/ 骞惰缃墍鏈夋潈锛

mkdir -p/var/www/
chown nginx锛歯ginx/var/www/
chmod 775/var/www

杩欐槸閰嶇疆濂界殑 nginx锛岄氳繃杩愯閲嶆柊鍚姩瀹冨拰 PHP FPM 鏈嶅姟锛

鏈嶅姟 nginx 閲嶅惎
鏈嶅姟 php5-fpm 閲嶅惎

鐜板湪锛屾偍瀹為檯涓婂凡鍑嗗濂藉畨瑁 WordPress锛

杩欏緢绠鍗曪紝杩愯锛

cd /tmp
wget http://wordpress.org/latest.tar.gz
tar zxvf latest.tar.gz
cd wordpress
mv * /var/www/
chown -R nginx:nginx /var/www

瑕侀厤缃 WordPress锛岃杩愯锛

cp /var/www/wp-config-sample.php /var/www/wp-config.php
chown nginx:nginx /var/www/wp-config.php

鍦ㄧ綉缁滄祻瑙堝櫒涓紝璁块棶https://api.wordpress.org/secret-key/1.1/salt/骞跺鍒剁粨鏋

缂栬緫 /var/www/wp-config.php

骞跺悜涓嬫粴鍔ㄤ互灏 AUTH_KEY 琛屽悜涓嬫粴鍔ㄥ埌 NONCE_SALT锛屽苟灏嗗畠浠浛鎹负鎮ㄤ粠 api.wordpress.org 绔欑偣澶嶅埗鐨勫

鐒跺悗锛屽皢榛樿鍊兼浛鎹负鎮ㄤ箣鍓嶉夋嫨鐨 MySQL 鍊硷紙涓嶆槸 root 鐢ㄦ埛锛夛細

瀹氫箟锛'DB_NAME'锛'database_name_here'锛夛紱
瀹氫箟锛'DB_USER'锛'username_here'锛夛紱
瀹氫箟锛'DB_PASSWORD'锛'password_here'锛夛紱

瀹屾垚鍚庯紝濡傛灉鎮ㄦ病鏈変换浣曢敊璇苟涓旀偍鐨勫煙鍚嶆寚鍚戞纭殑 IP锛堣繖寰堥噸瑕侊紒锛夛紝閭d箞鎮ㄥ皢鑳藉璁块棶鎮ㄧ殑鍩熷苟鍦 http:/ 涓婃煡鐪 WordPress 閰嶇疆椤甸潰/www.domainname.com/wp-admin/install.php

閫氳繃瀹夎闂锛岄夋嫨涓涓悎鐞嗙殑鐢ㄦ埛鍚嶅拰瀵嗙爜锛堝己鐑堝缓璁偍灏嗙鐞嗗憳鐢ㄦ埛浠庘渁dmin鈥濇洿鏀逛负鍏朵粬鐢ㄦ埛鍚嶃

杞埌璁剧疆锛岀劧鍚庨夋嫨姘镐箙閾炬帴锛岀劧鍚庨夋嫨鈥滆嚜瀹氫箟缁撴瀯鈥濓紝鐒跺悗绮樿创涓嬮潰鐨勫硷紙鍖呮嫭 % 绗﹀彿锛

/%post_id%/%postname%

鐒跺悗鐐瑰嚮鈥滀繚瀛樻洿鏀光

鏄椂鍊欏垱寤轰竴涓祴璇曞笘瀛愪簡锛屾墍浠ョ偣鍑烩滃笘瀛愨濈劧鍚庘滄坊鍔犳柊鈥

杈撳叆鏍囬鍜屾鏂囷紝鐒跺悗鐐瑰嚮鈥滃彂甯冣濓紝骞惰涓嬪畠鐨勫弸濂 URL銆

杩愯 blitz.io 娴嬭瘯锛岀湅鐪嬫垜浠殑琛ㄧ幇濡備綍

100 涓敤鎴凤紝60 绉掋傝秴鏃躲佷綆鍛戒腑鐜囥侀敊璇瓑 CPU 鑰楀敖锛屼技涔庢槸鏈鍒濈殑鐡堕銆

杩欎竴楂樺嘲鍦 1.0 鍒嗛挓鍐呬骇鐢熶簡 632 娆℃垚鍔熺偣鍑伙紝鎴戜滑灏 1.76 MB 鐨勬暟鎹紶鍏ュ拰浼犲嚭鎮ㄧ殑搴旂敤绋嬪簭銆9.81 娆/绉掔殑骞冲潎鍛戒腑鐜囩浉褰撲簬姣忓ぉ 847,776 娆$偣鍑汇

浣嗘槸浣犻亣鍒颁簡鏇村ぇ鐨勯棶棰橈細34.91% 鐨勭敤鎴峰湪杩欎釜楂樺嘲鏈熼棿閬囧埌浜嗚秴鏃舵垨閿欒锛

鎵浠ユ湇鍔″櫒鍦ㄨ繍琛岋紝浣嗘槸杩樻槸澶參浜嗭紒

鎺ヤ笅鏉ワ紝鎴戜滑灏嗗惎鐢 WordPress 缂撳瓨绯荤粺

杞埌 wordpress 绠$悊椤甸潰锛岀劧鍚庢槸鎻掍欢锛岀劧鍚庡崟鍑诲畨瑁呮柊鎻掍欢銆

鏇存柊 濡傛灉鎮ㄥ皢姝や綔涓烘暀绋嬶紝璇疯烦杩 W3 Total Cache 鎻掍欢锛屽畠宸茬粡寰堜箙娌℃湁鏇存柊浜嗐傛垜灏嗘潵浼氱敤W3 Super Cache鎸囦护鏇存柊杩欑瘒鏂囩珷锛屼絾鍏朵粬涓鍒囬兘搴旇鍍忕幇鍦ㄤ竴鏍峰伐浣
鎼滅储鈥淲3 Total Cache鈥濓紝鐒跺悗鍦ㄦ悳绱㈢粨鏋滆繑鍥炴椂鍗曞嚮鈥滅珛鍗冲畨瑁呪濄傚畨瑁呭畬鎴愬悗锛岀偣鍑烩滄縺娲绘彃浠垛濄

杞埌椤甸潰宸︿晶鑿滃崟涓殑鏂扳滄ц兘鈥濋儴鍒嗐

婊氬姩娴忚缂撳瓨閫夐」锛屽湪姣忎釜鏈轰細澶勯夋嫨鈥淧HP APC鈥濆苟鍚敤浠ヤ笅涓や釜閮ㄥ垎:

鏁版嵁搴撶紦瀛
瀵硅薄缂撳瓨

鐐瑰嚮鈥滀繚瀛樻墍鏈夎缃濓紝鐒跺悗鐐瑰嚮鈥滈儴缃测

鍐嶆杩愯blitz.io娴嬭瘯锛屾ц兘搴旇浼氭彁楂樺緢澶

杩欑鐑疆鍦 1.0 鍒嗛挓鍐呬骇鐢熶簡 2,902 娆℃垚鍔熺偣鍑伙紝鎴戜滑灏 27.59 MB 鐨勬暟鎹紶鍏ュ拰浼犲嚭鎮ㄧ殑搴旂敤绋嬪簭銆傚钩鍧 46 娆/绉掔殑鍛戒腑鐜囪浆鎹负澶х害 4,006,008 娆/澶┿

濡傛灉鎴戜滑灏 blitz.io 浠 100 涓敤鎴疯繍琛屽埌 250 涓敤鎴凤紝鎴戜滑浠嶇劧闇瑕佽В鍐充竴浜涢棶棰橈細

杩欎竴鐑疆鍦 1.0 鍒嗛挓鍐呬骇鐢熶簡 4,733 娆℃垚鍔熺偣鍑伙紝鎴戜滑灏 36.20 MB 鐨勬暟鎹紶鍏ュ拰浼犲嚭鎮ㄧ殑搴旂敤绋嬪簭銆72 娆/绉掔殑骞冲潎鍛戒腑鐜囩浉褰撲簬姣忓ぉ 6,297,653 娆$偣鍑汇

浣嗘槸浣犻亣鍒颁簡鏇村ぇ鐨勯棶棰橈細5.49% 鐨勭敤鎴峰湪杩欎釜楂樺嘲鏈熼棿閬囧埌浜嗚秴鏃舵垨閿欒锛

鎮ㄥ彲浠ュ湪姝 PDF 涓煡鐪 Nginx 鍜 APC 閰嶇疆鐨 Blitz 缁撴灉銆

鎵浠ヤ粛鐒朵笉瀹岀編鈥斺旀槸鏃跺欏畨瑁 varnish 3 浜嗭紒

apt-get 瀹夎娓呮紗

缂栬緫 /etc/varnish/default.vcl灏嗗唴瀹规浛鎹负鏉ヨ嚜 github 鐨勬枃浠 default.vcl 鏂囦欢銆

缂栬緫 /etc/default/varnish

鏇存敼閮ㄥ垎

DAEMON_OPTS=鈥-鈥嬧媋 :6081 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m鈥


DAEMON_OPTS=鈥-鈥嬧媋 :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,64m鈥

鎺ヤ笅鏉ワ紝鎴戜滑闇瑕佺紪杈 nginx 閰嶇疆浠ヤ睛鍚鍙 8080锛岃屼笉鏄鍙 80鈥斺擵arnish 灏嗗湪绔彛 80 涓婅繍琛屻

缂栬緫 /etc/nginx/conf.d/default.conf 骞舵浛鎹

鍚 80锛


鍚8080锛

淇濆瓨鏂囦欢锛岀劧鍚庤繍琛

鏈嶅姟 nginx 閲嶅惎
鏈嶅姟娓呮紗閲嶅惎

閲嶆柊杩愯 100 涓敤鎴风殑 blitz.io 娴嬭瘯

鍑犱箮娌℃湁 CPU 浣跨敤鐜囷紝搴旇浼氬緱鍒板畬缇庣殑缁撴灉

杩欑鐑疆鍦 1.0 鍒嗛挓鍐呬骇鐢熶簡 2,959 娆℃垚鍔熺偣鍑伙紝鎴戜滑灏 28.36 MB 鐨勬暟鎹紶鍏ュ拰浼犲嚭鎮ㄧ殑搴旂敤绋嬪簭銆傚钩鍧 47 娆/绉掔殑鍛戒腑鐜囪浆鎹负澶х害 4,093,000 娆/澶┿

閲嶆柊杩愯 250 鍚嶇敤鎴风殑闂數鎴

杩欑鐑疆鍦 1.0 鍒嗛挓鍐呬骇鐢熶簡 7,342 娆℃垚鍔熺偣鍑伙紝鎴戜滑灏 70.38 MB 鐨勬暟鎹紶鍏ュ拰浼犲嚭鎮ㄧ殑搴旂敤绋嬪簭銆117 娆/绉掔殑骞冲潎鍛戒腑鐜囪浆鎹负澶х害 10,134,627 娆/澶┿

鎮ㄥ彲浠ュ湪姝 PDF 涓煡鐪嬪畬鏁寸殑鏈缁堣繍琛岄棯鐢垫垬鎬ц兘璇︾粏淇℃伅銆

灏辨槸杩欐牱锛屽湪涓涓瘡鏈堣姳璐逛笉鍒 20 缇庡厓鐨勭洅瀛愪笂浣跨敤 WordPress 姣忓ぉ鏈 1000 涓囨鐐瑰嚮锛岃繖涓鍒囬兘褰掑姛浜 varnish 鍜 nginx锛屽緢绠鍗曪紒