时间:2023-06-02 16:16:41 来源: 人气:
在CentOS 7系统里搭建好LAMP环境后,就可以安装网站程序了,以最流行了Wordpess为例。为了加快网站的访问速度,除了花钱买更好的硬件设施外。我们可以通过优化网站的程序、主题。为服务器开启缓存功能,为网站提速。我们知道,互联网上缓存为王。, 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,放入如下代码:, $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);, ?>, 访问后如果能现实 版本号servers 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 在最后加入如下几行:, DeflateCompressionLevel 9, AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php, AddOutputFilter DEFLATE css js, 重新启动http服务:systemctl restart httpd,