Step #1: Install Memcached
First, we’ll clean up our package files:
dnf clean all
As a matter of best practice we’ll now update our packages:
dnf -y update
To install Memcached and related packages, run the command:
dnf -y install memcached
Step #2: Configure Memcached
Use the following command to view information on the memcached command:
memcached -h
The default configuration file is located at /etc/sysconfig/memcached. Open that file to view the configuration:
vim /etc/sysconfig/memcached
The contents of the file should appear similar to this:
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
- PORTÂ is the port on which memcached will run. By default it is 11211.
- USERÂ is the user as which memcached runs.
- MAXCONNÂ is the maximum number of connections to memcached.
- CACHESIZEÂ is the size of the cache, in MB.
- OPTIONSÂ contains any additional specified options. In this case, there are none.
If you would like to change the port (PORT), the user Memcached runs as (USER), the maximum number of allowed connections to Memcached (MAXCONN), or the cache size in megabytes (CACHESIZE), simply change the options in the configuration file.
After making any changes, save and exit the configuration file with :wq and then restart Memcached:
systemctl restart memcached
Step #3: Configure Memcached to Start on Boot
The following code will ensure that Memcached starts at boot:
systemctl enable memcached
That will produce output similar to:
[root@host ~]# systemctl enable memcached
Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service.
To check the status of Memcached:
systemctl status memcached
To stop Memcached:
systemctl stop memcached
To start Memcached:
systemctl start memcached
Step #4: Install the Memcached PHP Extension
If needed, install the Memcached PHP extension with the following command:
dnf -y install php-pecl-memcache
Now restart memcached and Apache for the settings to take effect:
systemctl restart memcached
systemctl restart httpd
To verify that the PHP module is loaded, run the command:
php -m | grep memcache
That should produce output similar to:
[root@host ~]# php -m | grep memcache
memcache
And finally, you can check the php.ini to confirm your Memcached settings:
php -i | grep memcache
That should produce output similar to:
[root@host ~]# php -i | grep memcache
/etc/php.d/40-memcache.ini,
memcache
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 32768 => 32768
memcache.compress_threshold => 20000 => 20000
memcache.default_port => 11211 => 11211
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => consistent => consistent
memcache.lock_timeout => 15 => 15
memcache.max_failover_attempts => 20 => 20
memcache.protocol => ascii => ascii
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
Registered save handlers => files user memcache
Â