在CentOS 7系統(tǒng)里搭建好LAMP環(huán)境后,就可以安裝網(wǎng)站程序了,以最流行了Wordpess為例。為了加快網(wǎng)站的訪問(wèn)速度,除了花錢(qián)買(mǎi)更好的硬件設(shè)施外。我們可以通過(guò)優(yōu)化網(wǎng)站的程序、主題。為服務(wù)器開(kāi)啟緩存功能,為網(wǎng)站提速。我們知道,互聯(lián)網(wǎng)上緩存為王。
1.安裝php加速器Xcache
XCache 是一個(gè)國(guó)人開(kāi)發(fā)的又快又穩(wěn)定的 PHP opcode 緩存器,通過(guò)共享編譯內(nèi)存從而降低服務(wù)器負(fù)載。
由于yum源倉(cāng)庫(kù)里面沒(méi)有,先下載源文件,最新版3.2.0
在tmp目錄下:cd /tmp 下載:wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
解壓縮:tar xvfz xcache-3.2.0.tar.gz
準(zhǔn)備安裝:cd xcache-3.2.0
安裝前,先要準(zhǔn)備編譯環(huán)境:yum -y install php-devel gcc
運(yùn)行phpize,非常重要:phpize
配置:./configure --enable-xcache
安裝:make && make install
復(fù)制配置文件 cp xcache.ini /etc/php.d(xcache.ini在源程序安裝目錄)
重新啟動(dòng)http服務(wù):systemctl restart httpd
2.安裝memcached
Memcached 是一個(gè)高性能的分布式內(nèi)存對(duì)象緩存系統(tǒng),用于動(dòng)態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫(kù)負(fù)載。它通過(guò)在內(nèi)存中緩存數(shù)據(jù)和對(duì)象來(lái)減少讀取數(shù)據(jù)庫(kù)的次數(shù),從而提供動(dòng)態(tài)、數(shù)據(jù)庫(kù)驅(qū)動(dòng)網(wǎng)站的速度。
a.安裝
yum -y install memcached
安裝memcache關(guān)聯(lián)php
yum -y install php-pecl-memcache
編譯安裝PHP的memcache擴(kuò)展
下載 wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar xf memcache-3.0.8.tgz
cd memcache-3.0.8
依次執(zhí)行
phpize
./configure
make && make install
b.配置
在php.ini文件中添加memcache擴(kuò)展
extension=/usr/lib64/php/modules/memcache.so (版本不同目錄可能不同)
c.運(yùn)行
memcached -d -m 128 -c 1024 -P /tmp/memcached.pid
d.測(cè)試
測(cè)試memcached是否工作正常,在網(wǎng)站目錄下編輯一個(gè) 文件如memtest.php,放入如下代碼:
$memcache = new Memcache;
$memcache->connect(localhost, 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Servers version: ".$version."
n";
$tmp_object = new stdClass;
$tmp_object->str_attr = test;
$tmp_object->int_attr = 123;
$memcache->set(key, $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)
n";
$get_result = $memcache->get(key);
echo "Data from the cache:
n";
var_dump($get_result);
?>
訪問(wèn)后如果能現(xiàn)實(shí) 版本號(hào)servers version: 1.4…… store date in the cache等信息說(shuō)明memcached運(yùn)行正常。
5.整合
Wordpress支持memcached
下載:wget https://downloads.wordpress.org/plugin/memcached.2.0.2.zip
unzip memcached.2.0.2.zip
cd memcached.2.0.2
復(fù)制 object-cache.php 到網(wǎng)站根目錄 wp-content文件夾內(nèi),wordpress會(huì)自動(dòng)調(diào)用緩存。
重啟服務(wù)器:
sytemctl restart memcached
sytemctl restart httpd
3.開(kāi)啟Gzip壓縮。
apache2.4版本默認(rèn)添加了gzip模塊,我們要同時(shí)開(kāi)啟deflate模塊,壓縮網(wǎng)頁(yè)文件,提高服務(wù)器瀏覽速度。
vim /etc/httpd/conf/httpd.conf 在最后加入如下幾行:
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE css js
重新啟動(dòng)http服務(wù):systemctl restart httpd
新聞熱點(diǎn)
疑難解答
圖片精選