在Ubuntu LAMP环境中设置缓存主要涉及Apache、MySQL和PHP的优化。以下是一些具体的步骤和建议:
/etc/apache2/apache2.conf
或/etc/httpd/conf/httpd.conf
)中,找到并启用mod_cache
模块。<IfModule mod_cache.c>
CacheEnable disk /var/cache/apache2
</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>
/etc/mysql/my.cnf
或/etc/my.cnf
)中,找到并启用查询缓存。[mysqld]
query_cache_size = 64M
query_cache_type = 1
[mysqld]
innodb_buffer_pool_size = 1G
key_buffer_size = 256M
sudo apt install php-opcache
sudo phpenmod opcache
php.ini
文件中配置缓存目录,例如:zend_extension = /usr/lib/php/20190902/opcache.so
opcache.memory_consumption = 64
opcache.max_accelerated_files = 4000
opcache.validate_timestamps = On
opcache.fast_shutdown = On
sudo apt install memcached redis-server
$memcached = new Memcached();
$memcached->addServer('localhost', 11211);
在进行任何缓存配置之前,建议备份所有重要数据和配置文件,以防万一。此外,缓存配置是一个持续的过程,需要根据服务器的实际运行情况进行调整。