手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

LAMP平台安装Xcache和Memcached加速网站运行

[日期:2015-06-14] 来源:Linux社区  作者:Linux [字体: ]

CentOS 7系统里搭建好LAMP环境后,就可以安装网站程序了,以最流行了Wordpess为例。为了加快网站的访问速度,除了花钱买更好的硬件设施外。我们可以通过优化网站的程序、主题。为服务器开启缓存功能,为网站提速。我们知道,互联网上缓存为王。

CentOS 7下搭建LAMP平台环境  http://www.linuxidc.com/Linux/2015-06/118818.htm

1.安装php加速器Xcache

XCache 是一个国人开发的又快又稳定的 PHP opcode 缓存器,通过共享编译内存从而降低服务器负载。

由于yum源仓库里面没有,先下载源文件,最新版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

准备安装:cd xcache-3.2.0

安装前,先要准备编译环境:yum -y install php-devel gcc

运行phpize,非常重要:phpize

配置:./configure --enable-xcache

安装:make && make install

复制配置文件 cp xcache.ini /etc/php.d(xcache.ini在源程序安装目录)

重新启动http服务:systemctl restart httpd

2.安装memcached

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。

a.安装

yum -y install memcached

安装memcache关联php

yum -y install php-pecl-memcache

编译安装PHP的memcache扩展

下载 wget http://pecl.php.net/get/memcache-3.0.8.tgz

tar xf memcache-3.0.8.tgz

cd memcache-3.0.8

依次执行

phpize

./configure

make && make install

b.配置

在php.ini文件中添加memcache扩展

extension=/usr/lib64/php/modules/memcache.so (版本不同目录可能不同)

c.运行

memcached -d -m 128 -c 1024 -P /tmp/memcached.pid

d.测试

测试memcached是否工作正常,在网站目录下编辑一个 文件如memtest.php,放入如下代码:

<?php

$memcache = new Memcache;

$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();

echo "Server's version: ".$version."<br/>\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)<br/>\n";

$get_result = $memcache->get('key');

echo "Data from the cache:<br/>\n";

var_dump($get_result);

?>

访问后如果能现实 版本号server's version: 1.4…… store date in the cache等信息说明memcached运行正常。

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

复制 object-cache.php  到网站根目录 wp-content文件夹内,wordpress会自动调用缓存。

重启服务器:

sytemctl restart memcached

sytemctl restart httpd

3.开启Gzip压缩。

apache2.4版本默认添加了gzip模块,我们要同时开启deflate模块,压缩网页文件,提高服务器浏览速度。

vim /etc/httpd/conf/httpd.conf  在最后加入如下几行:

<IfModule mod_deflate.c>

DeflateCompressionLevel 9

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php

AddOutputFilter DEFLATE css js

</IfModule>

重新启动http服务:systemctl restart httpd

Memcached 安装及启动脚本 http://www.linuxidc.com/Linux/2013-07/87641.htm

PHP中使用Memcached的性能问题 http://www.linuxidc.com/Linux/2013-06/85883.htm

Ubuntu下安装Memcached及命令解释 http://www.linuxidc.com/Linux/2013-06/85832.htm

Memcached的安装和应用 http://www.linuxidc.com/Linux/2013-08/89165.htm

使用Nginx+Memcached的小图片存储方案 http://www.linuxidc.com/Linux/2013-11/92390.htm

Memcached使用入门 http://www.linuxidc.com/Linux/2011-12/49516p2.htm

Memcached 的详细介绍请点这里
Memcached 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-06/118835.htm 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款