Setup Memcached on Ubuntu 18.04 | 16.04 with Nginx and PHP-7.2

Our previous tutorial showed you how to install Memcahced with Apache2 and PHP 7.2… This post shows you how to install Memcached on Ubuntu with Nginx and PHP 7.2-FPM support…

Memcached is a free, open source, object caching program that allows repeated PHP object / database calls to be cached in system memory to help speed up dynamic web applications…

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

This brief tutorial shows students and new users how to install and configure Memcached on Ubuntu and use with Nginx HTTP server to help speed up web applications like WordPress, Drupal, Joomla and other PHP based apps..

When you’re ready to install Memcached on Ubuntu, follow the steps below:

Step 1: Install Memcached

Memcached package is included with Ubuntu default repositories and the installation process is straightforward… Simply run the commands below to install it along with its support tools…

sudo apt update
sudo apt install memcached libmemcached-tools

Memcached tools provides several command line tools for managing the Memcached server… You’ll mostly want to install it with Memcached server…

After running the commands above, Memcached server should be installed and ready to use… To check its status run the commands below:

sudo systemctl status memcached

You should see 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 -P /var/run/memcached/

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

That’s how to install Memcached on Ubuntu… The server should be running and should respond to requests… The commands below can be used to stop, start and enable Memcached.

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

Step 2: Configure Memcached

Now that the server is installed, its configuration file can be found at /etc/memcached.conf

The default settings in the file should be enough for most environments and applications… However, for more advanced settings, open the file and make changes you want to apply to your environment…

For example, Memcached listens on the server’s local IP address (… If you want to only have it listen on a different IP, edit the lines in the file to look similar to the one below:

sudo nano /etc/memcached.conf

Then replace the local sever IP with the one you want to use… You can also change its default port number as well…

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

Save the file and exit… then restart Memcached services for the changes to apply…

Step 3: Setup Nginx to Support Memcached

Now that Memcached is installed and configured you can use the steps below to include Nginx and PHP7.2-FPM support….

First, run the commands below to install Nginx and PHP 7.2-FPM

sudo apt-get install nginx php7.2-fpm php-memcached php7.2-cli

The commands above will install Nginx HTTP server along with necessary PHP modules to enable Memcached to function and be accessible.

To enable Nginx PHP site support, open its default site configuration file and enable the PHP block…..

sudo nano /etc/nginx/sites-available/default

Then remove the comment to enable the PHP section..

# Default server configuration
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # Read up on ssl_ciphers to ensure a secure configuration.
        # See:
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

Restart Nginx Webserver

sudo systemctl reload nginx.service

This is how to get Nginx with PHP-FPM and Memcached support. Now to see if Memcached is installed, run the commands below to create a test php file.

To test if Memcached is installed, run the commands below to create a test php file.

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

Then copy and paste the content below into the file and save.


Save the file. After saving, open your browser and browse to the server hostname or IP address followed by phpinfo.php


You should see all the PHP related modules as well as memcached information. When you see the screenshot below, then you’ll know Memcached is installed and enabled…

Memcached Apache2 Install

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

Remote Access

When Memcahced server is improperly configured, it can be used to perform a distributed denial-of-service (DDoS) attack… If you’re going to be allowing remote access you need to make sure that only trusted client can access it remotely…

Setup Ubuntu firewall to block all remote clients except for those are explicit allowed…

sudo ufw allow from to any port 11211

That’s it!

Congratulations! You’ve successfully installed Memcached with Nginx and PHP-7.2 support on Ubuntu.

You may also like the post below:

1 Comment

  1. when i search for:
    my browser keeps downloading file again and again rather than displaying the content as in your case you can see in your browser. but with me the page is not loaded bt php rather it is downloading again and again.

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.