阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

109次阅读
没有评论

共计 2680 个字符,预计需要花费 7 分钟才能阅读完成。

在 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 

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计2680字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中