Php session with memcached

memcached for PHP sessions tutorial

Sessions are a simple mechanism for web technologies like PHP to create a unique ID for each visitor which can then be used to persist data between page requests. Upon their first visit, a cookie is dropped in the user’s browser with the generated ID which the server then uses to store data about this session until the cookie expires or is deleted.

This post explorer the use of memcached as the PHP session handler, and explains the pro’s and con’s of its use compared to the default.

What is the memcached daemon?

Free & open source, high-performance, distributed memory object caching system.

https://memcached.org/

The salient points of this are that it is distributed which enables us to scale as required and that it is in-memory which provides for the high-performance

Comparison with ‘files’

Out of the box, most Linux distributions (and we will be using Debian for the basis of this tutorial) use files to manage sessions at the server end. This is fairly obvious as every server has a filesystem! If you inspect the path defined in php.ini by the session.save_path directive you will see the current session files.

But filesystem access is significantly slower than memory, particularly if your visitor numbers are high. But what if your site is being served from a farm of servers? Although most reverse proxies can be configured to pin each session to just one backend server, this can cause issues if that server needs to be taken offline – the user loses their session and is no longer logged in.

Читайте также:  Плавное появление изображения css

Either of these two scenarios are where memcached can have significant benefits over files.

Memcache vs memcached

There is inevitably some confusion over this; memcached is a daemon which you need to install to provide the caching. The most confusing aspect is that there are two separate PHP modules to interface – memcache and memcached. Either of these modules will work, but I shall use memcache for this tutorial.

Installation

I shall assume that you already have a single webserver serving php pages. As every with Debian, installation with apt takes care of all dependencies

apt-get install memcached php-memcache libmemcached-tools

Configuration

Memcached

The default configuration file /etc/memcached.conf provided is sufficient for a single server installation.

#memcached default config file #2003 - Jay Bonci jaybonci@debian.org #This configuration file is read by the start-memcached script provided as #part of the Debian GNU/Linux distribution. #Run memcached as a daemon. This command is implied, and is not needed for the #daemon to run. See the README.Debian that comes with this package for more #information. -d #Log memcached's output to /var/log/memcached logfile /var/log/memcached.log #Be verbose # -v #Be even more verbose (print client commands as well) # -vv #Start with a cap of 64 megs of memory. It's reasonable, and the daemon default #Note that the daemon will grow to this size, but does not start out holding this much #memory -m 64 #Default connection port is 11211 -p 11211 #Run the daemon as root. The start-memcached will default to running as root if no #-u command is present in this config file -u memcache #Specify which IP address to listen on. The default is to listen on all IP addresses #This parameter is one of the only security measures that memcached has, so make sure #it's listening on a firewalled interface. -l 127.0.0.1 #Limit the number of simultaneous incoming connections. The daemon default is 1024 -c 1024 #Lock down all paged memory. Consult with the README and homepage before you do this -k #Return error when memory is exhausted (rather than removing items) -M #Maximize core file limit -r

The listen address will only allow connections from localhost. In a later post I shall explore sharing sessions in a farm situation.

php memcache

Likewise the default memcache config file /etc/php/7.3/apache2/conf.d/20-memcache.ini )assuming you are using Apache as the front end) is sufficient

; uncomment the next line to enable the module extension=memcache.so [memcache] ; Whether to transparently failover to other servers on errors. ; ; memcache.allow_failover="1" ; Defines how many servers to try when setting and getting data. Used ; only in conjunction with memcache.allow_failover. ; ; memcache.max_failover_attempts="20" ; Data will be transferred in chunks of this size, setting the value ; lower requires more network writes. Try increasing this value to ; 32768 if noticing otherwise inexplicable slowdowns. ; ; memcache.chunk_size="8192" ; The default TCP port number to use when connecting to the memcached ; server if no other port is specified. ; ; memcache.default_port="11211" ; Controls which strategy to use when mapping keys to servers. Set ; this value to consistent to enable consistent hashing which allows ; servers to be added or removed from the pool without causing keys to ; be remapped. Setting this value to standard results in the old ; strategy being used. ; ; memcache.hash_strategy="consistent" ; Controls which hash function to apply when mapping keys to servers, ; crc32 uses the standard CRC32 hash while fnv uses FNV-1a ; ; memcache.hash_function="crc32" ; Use memcache as a session handler by setting this value to memcache. ; ; session.save_handler="memcache" ; Defines a comma separated of server urls to use for session storage, ; for example session.save_path="tcp://host1:11211,tcp://host2:11211". ; ; Each url may contain parameters which are applied to that server, ; they are the same as for the Memcache::addServer() method. For ; example: session.save_path="tcp://host1:11211?persistent=1&weight=1&timeout=1&retry_interval=15" ; ; session.save_path="tcp://localhost:11211" ; Additional parameters ; memcache.protocol=ascii ; memcache.redundancy=1 ; memcache.session_redundancy=2 ; memcache.compress_threshold=20000 ; memcache.lock_timeout=15

Again, these defaults only need changing if sessions are being shared between servers, or if you have changed the memcached defaults.

php

All that remains is to update the session directives in /etc/php/7.3/apache2/php.ini as follows.

session.save_handler = memcache session.save_path="tcp://localhost:11211"

remember to restart Apache after the changes

Testing

The libmemcached-tools package we installed has a number of useful tools to inspect the memcached server. You can use the following command to dump the data currently stored.

Tags

About This Site

Need tech advice you can trust? Brabeum specialises in PHP and Java development.

Find Us

Address
71-75 Shelton Street, Covent Garden, London, England, WC2H 9JQ

Hours
Monday—Friday: 9:00AM–5:00PM
Saturday & Sunday: 11:00AM–3:00PM

Meta

This website uses cookies to improve your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

Хранение PHP-сессий в Memcached на сервере CentOS

Memcached – это программа, хранящая в оперативной памяти небольшие отрывки произвольных данных (строк, объектов) из результатов обращений к базе данных, вызовов API или визуализации страниц в формате «ключ-значение».

Зачем хранить сессии в Memcached?

Вместо файлов Memcached будет хранить в памяти сессии. Поскольку запуск из памяти занимает меньше времени, чем чтение файла, сайт будет работать лучше, а время загрузки сократится.

Требования

Чтобы следовать данному руководству, на сервере должен быть установлен и запущен PHP 5. Для этого читайте руководство «Установка LAMP stack (Linux, Apache, MySQL, PHP) на CentOS 7».

Кроме того, нужно установить репозиторий EPEL, поскольку программа Memcached не доступна в стандартных репозиториях.

rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

Также рекомендуется обновить пакеты системы до последней доступной версии (но это не обязательно):

Установка Memcached

Итак, выполнив все предварительные требования, можно приступать к установке Memcached. Это очень просто сделать при помощи менеджера пакетов yum:

Завершив установку Memcached, откройте конфигурационный файл Memcached в текстовом редакторе vi:

PORT=»11211″
USER=»memcached»
MAXCONN=»1024″
CACHESIZE=»64″
OPTIONS=»»

Программа Memcached не защищена паролем или именем пользователя, потому любой пользователь может получить доступ к ней через порт 11211. Чтобы предотвратить это, нужно разрешить доступ к программе только данному VPS; для этого внесите некоторые опции в раздел OPTIONS=:

PORT=»11211″
USER=»memcached»
MAXCONN=»1024″
CACHESIZE=»64″
OPTIONS=»-l 127.0.0.1″

Размер кэша (раздел cachesize) по умолчанию равен 64MB. Как только Memcached достигнет этого предела, программа удалит более старые данные, чтобы освободить место для новых. В целом, 64MBбудет вполне достаточно (если только сайт не очень объемен).

Теперь запустите Memcached:

По умолчанию Memcached не запускается при загрузке сервера, потому нужно автоматизировать запуск программы:

chkconfig —levels 235 memcached on

Установка расширения PHP Memcached

На данном этапе нужно установить несколько дополнительных программ. Для начала установите development tools – инструменты разработки, которые понадобятся для компилирования исходного кода:

yum groupinstall «Development Tools»

Затем нужно установить еще несколько программ; первые две понадобятся для сборки расширения, а последние две – для запуска/установки PHP-расширения.

yum install zlib-devel libmemcached-devel php-pear php-pecl-memcached

Теперь нужно установить расширение PHP Memcached при помощи только что установленной библиотеки PECL (PHP Extension Community Library).

pecl install -f memcached-1.0.0

Редактирование файла php.ini

Последнее, что нужно сделать, – это настроить PHP для использования Memcached в качестве обработчика сессии. Для этого откройте /etc/php.ini в текстовом редакторе vi:

Найдите раздел [Session], который выглядит так:

[Session] ; Handler used to store/retrieve data.
; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler
session.save_handler = files

И измените его следующим образом:

[Session] ; Handler used to store/retrieve data.
; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler
session.save_handler = memcached
session.save_path = «127.0.0.1:11211»

Как видите, значение session_handler было изменено на memcached; также был указан путь к локальному хосту (порт 11211, на котором работает Memcached). Теперь нужно перезапустить Apache, чтобы активировать изменения, внесенные в файл php.ini.

Теперь все сессии буду храниться в Memcached, а не в файлах.

Внимание! На данном этапе может появиться следующая ошибка:

Starting httpd: httpd: apr_sockaddr_info_get() failed for memcached
httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName

Ее можно исправить путем редактирования конфигураций apache:

Здесь нужно раскомментировать строку ServerName:

Итоги

Memcached идеально справляется с хранением интенсивных запросов, которые не должны выполняться для каждой отображаемой страницы не в реальном времени, а в указанных приращениях времени (т.е. каждые 10 минут). Чтобы получить больше информации о работе Memcached, читайте руководства:

Источник

Оцените статью