温馨提示×

CentOS LAMP如何使用缓存

小樊
64
2025-05-03 01:58:33
栏目: 智能运维

在CentOS上配置LAMP(Linux, Apache, MySQL, PHP)环境时,使用缓存可以显著提高网站性能。以下是一些常见的缓存方法:

1. Apache缓存

Apache提供了多种缓存模块,如mod_cachemod_expires

安装和启用模块

sudo yum install mod_cache mod_cache_disk mod_expires
sudo systemctl restart httpd

配置缓存

编辑Apache配置文件(通常是/etc/httpd/conf/httpd.conf/etc/httpd/conf.d/目录下的文件),添加以下内容:

<IfModule mod_cache.c>
    <IfModule mod_cache_disk.c>
        CacheRoot "/var/cache/apache2/mod_cache_disk"
        CacheEnable disk /
        CacheDirLevels 2
        CacheDirLength 1
    </IfModule>
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html "access plus 1 hour"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
</IfModule>

2. PHP缓存

PHP有多种缓存扩展,如opcacheapcumemcached

安装和启用PHP OPcache

sudo yum install php-opcache

编辑PHP配置文件(通常是/etc/php.ini),添加或修改以下内容:

[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60

安装和启用APCu

sudo yum install php-pecl-apcu

编辑PHP配置文件(通常是/etc/php.ini),添加或修改以下内容:

[apcu]
extension=apcu.so
apcu.enable_cli=1
apcu.shm_size=32M
apcu.ttl=7200
apcu.user_ttl=7200
apcu.gc_ttl=3600

安装和启用Memcached

sudo yum install memcached php-pecl-memcached

编辑PHP配置文件(通常是/etc/php.ini),添加或修改以下内容:

[memcached]
extension=memcached.so
memcached.sess_consistency=0
memcached.sess_lock_wait=1000
memcached.sess_lock_peers=1
memcached.sess_lock_peers_timeout=5000
memcached.sess_prefix=phpsess_

3. MySQL缓存

MySQL提供了查询缓存和InnoDB缓冲池等缓存机制。

启用查询缓存

编辑MySQL配置文件(通常是/etc/my.cnf/etc/mysql/my.cnf),添加或修改以下内容:

[mysqld]
query_cache_type=1
query_cache_size=64M

配置InnoDB缓冲池

编辑MySQL配置文件(通常是/etc/my.cnf/etc/mysql/my.cnf),添加或修改以下内容:

[mysqld]
innodb_buffer_pool_size=1G
innodb_buffer_pool_instances=8
innodb_log_file_size=256M
innodb_log_buffer_size=16M

4. 使用Varnish作为反向代理缓存

Varnish是一个高性能的HTTP加速器,可以作为Apache或Nginx的反向代理。

安装Varnish

sudo yum install varnish

配置Varnish

编辑Varnish配置文件(通常是/etc/varnish/default.vcl),添加以下内容:

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

sub vcl_recv {
    if (req.http.Cookie) {
        set req.http.Cookie = ";" + req.http.Cookie;
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        set req.http.Cookie = regsuball(req.http.Cookie, ";([^ ]+)=([^;]+)", "\1=\2");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
        if (req.http.Cookie == "") {
            unset req.http.Cookie;
        }
    }
}

sub vcl_backend_response {
    if (beresp.http.Set-Cookie) {
        set beresp.http.Set-Cookie = ";" + beresp.http.Set-Cookie;
        set beresp.http.Set-Cookie = regsuball(beresp.http.Set-Cookie, "; +", ";");
        set beresp.http.Set-Cookie = regsuball(beresp.http.Set-Cookie, ";([^ ]+)=([^;]+)", "\1=\2");
        set beresp.http.Set-Cookie = regsuball(beresp.http.Set-Cookie, "^[; ]+|[; ]+$", "");
        if (beresp.http.Set-Cookie == "") {
            unset beresp.http.Set-Cookie;
        }
    }
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

启动Varnish

sudo systemctl start varnish
sudo systemctl enable varnish

通过以上步骤,你可以在CentOS上配置LAMP环境并使用缓存来提高网站性能。根据具体需求选择合适的缓存方案,并进行相应的配置和优化。

0