How to Install Memcached on Ubuntu 18.04 | 20.04 with Apache

How to Install Memcached on Ubuntu 18.04 20.04 with Apache

This post shows users and new students that How to Install Memcached on Ubuntu 18.04 | 20.04 with Apache. If you’re going to Install Memcached on Ubuntu with Apache then this post is ideal for you.

Memcached is a free, open source, object and memory-caching System. It is also used to speed up dynamic web applications by caching data and objects in the RAM for reduce the number of times an external data source must be read. Memcached is free and open-source software and licensed under the Revised BSD license.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering…

Follow the below steps for starting to Install Memcached on Ubuntu 18.04 | 20.04 with Apache :

Step 1: Install Memcached

To install Memcached, simply run the below commands to install it with its supporting tools.

sudo apt update
sudo apt install memcached libmemcached-tools

After execute the above commands, Memcached server is installed and ready to us and for check its status, simply run the below commands :

sudo systemctl status memcached

When you run the above command it will show a result similar lines as below:

memcached.service - memcached daemon
   Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-06-06 10:36:25 CDT; 27s ago
     Docs: man:memcached(1)
 Main PID: 19852 (memcached)
    Tasks: 10 (limit: 4682)
   CGroup: /system.slice/memcached.service
           └─19852 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jun 06 10:36:25 ubuntu1804 systemd[1]: Started memcached daemon.

In the above steps, we explain that how to install Memcached on Ubuntu.

Now use the below command to stop, start and enable Memcached services.

sudo systemctl stop memcached.service
sudo systemctl start memcached.service
sudo systemctl enable memcached.service

Step 2: Configure Memcached

Memcached server is installed and now it’s time to configure it. You can be found the configuration file at /etc/memcached.conf.

The default configuration setting of Memcached is ok for most of environments and applications but if you want more advanced setting then open the config file and make changes that you want to apply for your application and environment.

Let we explain a example for advanced setting, by default Memcached is listens on the server’s local IP address (127.0.0.1) but if you want to change it for different IP then follow below steps.

Run below command to open its config file :

sudo nano /etc/memcached.conf

After open config file, simply replace the local server IP with the one you want to use.

# 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 194.178.3.1

Save the config file and exit it. Now restart the Memcached services to apply the changes.

Step 3: Setup Memcached

In the above steps, Memcached is installed and configured with advanced setting, so now you can use the below steps to includes Apache2 and PHP-7.2 support.

For do that, simply run the below commands install Apache2 and PHP 7.2.

sudo apt-get install apache2 php7.2 libapache2-mod-php7.2 php-memcached php7.2-cli

Now you need to test that Memcached is installed, for do that run the below commands to create a test php file.

sudo nano /var/www/html/phpinfo.php

After creating test file, simply copy and paste the below content into the test file and save it.

<?php
phpinfo();
?>

And save the file. Now open your browser and browse to the server hostname or IP address as below:

http://localhost/phpinfo.php

When test file is open, you see all the PHP related modules and memcached information. Look the below picture observe that Memcached is installed and enabled.

Memcached Ubuntu install Apache

If you don’t see Memcached module on the above test page , then restart Apache2 services and try again.

Remote Access

For Setup the Ubuntu firewall to block the all remote clients except for those are explicit allowed.

sudo ufw allow from 194.178.3.1 to any port 11211

That’s all

If you find any error and issue in above steps , please use comment box below to report.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.