Deploy your Bitnami Memcached Stack on Bitnami Cloud Hosting now! Launch Now

Memcached for Bitnami Cloud Hosting

Description

Memcached is an high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

First steps with the Bitnami Memcached Stack

Welcome to your new Bitnami application running on Bitnami Cloud Hosting! Here are a few questions (and answers!) you might need when first starting with your application.

What credentials do I need?

You need two sets of credentials:

  • The application credentials, consisting of a username and password. These credentials allow you to log in to your new Bitnami application.

  • The server credentials, consisting of an SSH username and key. These credentials allow you to log in to your Bitnami Cloud Hosting server using an SSH client and execute commands on the server using the command line.

What is the administrator username set for me to log in to the application for the first time?

Username: user

What is the administrator password?

Click here for instructions.

What SSH username should I use for secure shell access to my application?

SSH username: bitnami

How do I get my SSH key or password?

You can obtain your SSH key from the Bitnami Cloud Hosting dashboard. Click here for more information.

What are the default ports?

A port is an endpoint of communication in an operating system that identifies a specific process or a type of service. Bitnami stacks include several services or servers that require a port.

Remember that if you need to open some ports you can follow the instructions given in the FAQ to learn how to open the server ports for remote access.

Port 22 is the default port for SSH connections.

The Memcached access port is 11211. This port is closed by default, you must open it to enable remote access.

How to start or stop the services?

Each Bitnami stack includes a control script that lets you easily stop, start and restart services. The script is located at /opt/bitnami/ctlscript.sh. Call it without any service name arguments to start all services:

$ sudo /opt/bitnami/ctlscript.sh start

Or use it to restart a single service, such as Apache only, by passing the service name as argument:

$ sudo /opt/bitnami/ctlscript.sh restart apache

Use this script to stop all services:

$ sudo /opt/bitnami/ctlscript.sh stop

Restart the services by running the script without any arguments:

$ sudo /opt/bitnami/ctlscript.sh restart

Obtain a list of available services and operations by running the script without any arguments:

$ sudo /opt/bitnami/ctlscript.sh

How to upload files to the server with SFTP?

NOTE: Bitnami applications can be found in /opt/bitnami/apps.

First, obtain your SSH credentials by following these steps:

  • Browse to the Bitnami Cloud Hosting dashboard and sign in if required using your Bitnami account.

  • Select the "Servers" menu item.

  • Select your cloud server from the resulting list.

  • Click the "Manage" button.

  • On the resulting page, note the server IP address.

    SSH credentials

  • Click the "Connect" button and download the SSH key for your server in .ppk format (for FileZilla or WinSCP) or in .pem format (for Cyberduck).

    SSH credentials

Although you can use any SFTP/SCP client to transfer files to your server, this guide documents FileZilla (Windows, Linux and Mac OS X), WinSCP (Windows) and Cyberduck (Mac OS X).

Using an SSH Key

Once you have your server's SSH key, choose your preferred application and follow the steps below to connect to the server using SFTP.

FileZilla
IMPORTANT: To use FileZilla, your server private key should be in PPK format.

Follow these steps:

  • Download and install FileZilla.
  • Launch FileZilla and use the "Edit -> Settings" command to bring up FileZilla's configuration settings.
  • Within the "Connection -> SFTP" section, use the "Add keyfile" command to select the private key file for the server. FileZilla will use this private key to log in to the server.

    FileZilla configuration

  • Use the "File -> Site Manager -> New Site" command to bring up the FileZilla Site Manager, where you can set up a connection to your server.
  • Enter your server host name and specify bitnami as the user name.
  • Select "SFTP" as the protocol and "Ask for password" as the logon type.

    FileZilla configuration

  • Use the "Connect" button to connect to the server and begin an SFTP session. You might need to accept the server key, by clicking "Yes" or "OK" to proceed.

You should now be logged into the /home/bitnami directory on the server. You can now transfer files by dragging and dropping them from the local server window to the remote server window.

If you have problems accessing your server, get extra information by use the "Edit -> Settings -> Debug" menu to activate FileZilla's debug log.

FileZilla debug log

WinSCP
IMPORTANT: To use WinSCP, your server private key should be in PPK format.

Follow these steps:

  • Download and install WinSCP.
  • Launch WinSCP and in the "Session" panel, select "SFTP" as the file protocol.
  • Enter your server host name and specify bitnami as the user name.

    WinSCP configuration

  • Click the "Advanced…" button and within the "SSH -> Authentication -> Authentication parameters" section, select the private key file for the server. WinSCP will use this private key to log in to the server.

    WinSCP configuration

  • From the "Session" panel, use the "Login" button to connect to the server and begin an SCP session.

You should now be logged into the /home/bitnami directory on the server. You can now transfer files by dragging and dropping them from the local server window to the remote server window.

If you need to upload files to a location where the bitnami user doesn't have write permissions, you have two options:

  • Once you have configured WinSCP as described above, click the "Advanced…" button and within the "Environment -> Shell" panel, select sudo su - as your shell. This will allow you to upload files using the administrator account.

    WinSCP configuration

  • Upload the files to the /home/bitnami directory as usual. Then, connect via SSH and move the files to the desired location with the sudo command, as shown below:

     $ sudo mv /home/bitnami/uploaded-file /path/to/desired/location/
    
Cyberduck
IMPORTANT: To use Cyberduck, your server private key should be in PEM format.

Follow these steps:

  • Select the "Open Connection" command and specify "SFTP" as the connection protocol.

    Cyberduck configuration

  • In the connection details panel, under the "More Options" section, enable the "Use Public Key Authentication" option and specify the path to the private key file for the server.

    Cyberduck configuration

  • Use the "Connect" button to connect to the server and begin an SFTP session.

You should now be logged into the /home/bitnami directory on the server. You can now transfer files by dragging and dropping them from the local server window to the remote server window.

How to connect to Memcached from a different machine?

For security reasons, the Memcached port in this solution cannot be accessed over a public IP address. To connect to Memcached from a different machine, you must open port 11211 for remote access. Refer to the FAQ for more information on this.

IMPORTANT: Making this application's network ports public is a significant security risk. You are strongly advised to only allow access to those ports from trusted networks. If, for development purposes, you need to access from outside of a trusted network, please do not allow access to those ports via a public IP address. Instead, use a secure channel such as a VPN or an SSH tunnel. Follow these instructions to remotely connect safely and reliably.

How to start memcached using another port?

The default port for accessing memcached is 11211 and is automatically configured as an allowed port for remote connections. You can change it using these commands:

$ cd /opt/bitnami/memcached/scripts/
$ sudo sed -i 's/11211/11212/g' ctl.sh

In this example, 11211 is your original Memcached port and 11212 (example) is the new port to use. Once done, restart the memcached server for the change to take effect.

$ sudo /opt/bitnami/ctlscript.sh restart

How to add another user or modify a user password?

By default, a user is automatically created during the installation process. To obtain the username and password, refer to the server dashboard. Check our FAQ for more information.

To add another user, execute the following command:

$ sudo /opt/bitnami/common/sbin/saslpasswd2 -a memcached -c USERNAME

Enter a password (twice) for the new user and the new user account will be created.

How to list created users with SASL?

To list users created with SASL, execute the following command:

$ sudo /opt/bitnami/common/sbin/sasldblistusers2

This will display a list of created users. It will only list users if there is a sasldb2 file.

NOTE: The password will appear as userPassword, but this is only a placeholder string and not the current password for the user.

How to start memcached without SASL authentication?

By default, memcached is configured to run with SASL authentication. This is highly recommended to secure your memcached installation.

To disable this authentication:

$ cd /opt/bitnami/memcached/scripts/
$ sudo sed -i 's/memcached -d -S/memcached -d/g' ctl.sh

Once done, restart the memcached server for the change to take effect.

$ sudo /opt/bitnami/ctlscript.sh restart

NOTE: If you see the following error it might be because you are using a Memcached client that does not support SASL authentication. You can either disable authentication as explained in this section or use a different Memcached client like the one used in the how to test Memcached with a client section

PHP Notice:  memcache_get(): Server 127.0.0.1 (tcp 11211, udp 0) failed with: Read failed (socket was unexpectedly closed) (0) in /home/bitnami/example.php on line 10
array(0) {
}

How to enable SASL authentication on memcached?

By default, memcached is configured to run with SASL authentication. This is highly recommended to secure your memcached installation.

If you have previously disabled this authentication and now want to re-enable it:

$ cd /opt/bitnami/memcached/scripts/
$ sudo sed -i 's/memcached -d/memcached -d -S/g' ctl.sh

Then, create a user:

$ sudo /opt/bitnami/common/sbin/saslpasswd2 -a memcached -c USERNAME
$ sudo chown memcached:memcached /opt/bitnami/common/etc/sasldb2

Restart the memcached server for the change to take effect.

$ sudo /opt/bitnami/ctlscript.sh restart

How to test Memcached with a client?

You can test your Memcached installation with a client like php-memcached-sasl, a simple Memcached client written in PHP. Find a complete list of Memcached clients here (although not all of them have SASL authentication support).

NOTE: The Bitnami Memcached Stack does not include PHP. To use the example script below, you must have PHP installed on your machine. You can install PHP in your machine (refer to PHP Official Documentation for more information regarding how to install it), or you can launch a new server with PHP using the Bitnami LAMP Stack.

Download the php-memcached-sasl client from this link and begin by extracting the contents of the downloaded archive. The archive includes a simple script named example.php.

$ curl -o php-memcache-sasl-master.zip 'https://codeload.github.com/hull-graveyard/php-memcache-sasl/zip/master'
$ unzip php-memcache-sasl-master.zip
$ cd php-memcache-sasl-master
$ nano example.php

The script begins by including the necessary PHP class and instantiating an object of the class:

include('MemcacheSASL.php');
$m = new MemcacheSASL;

In order to connect to the Memcached, you have two options:

  • (Recommended): Access Memcached through an SSH tunnel (refer to the FAQ for more information about SSH tunnels).
  • Open the port 11211 for remote access (refer to the FAQ for more information about opening ports).

IMPORTANT: Making this application's network ports public is a significant security risk. You are strongly advised to only allow access to those ports from trusted networks. If, for development purposes, you need to access from outside of a trusted network, please do not allow access to those ports via a public IP address. Instead, use a secure channel such as a VPN or an SSH tunnel. Follow these instructions to remotely connect safely and reliably.

Once you have an active SSH tunnel or you opened the port for remote access, configure the client object by defining the Memcached server host and port, and SASL configuration. Replace these values in the script with actual values for your server:

$m->addServer('host', 'port');
$m->setSaslAuthData('user', 'password');

Use the values below for Server host and port when:

  • Accessing through an SSH tunnel:

    $m->addServer('127.0.0.1', 'SOURCE-PORT');
    
  • Accessing opening ports:

    $m->addServer('SERVER-IP', '11211');
    

Use the SASL credentials below:

NOTE: If your Memcached server doesn't use SASL authentication, simply omit the call to setSaslAuthData().

You can now use the object's add() and get() methods to add or remove values from the cache. In this example, the add() method stores the value '12345' using the key 'test'. The key can then be used with the get() method to retrieve the original value whenever needed.

var_dump($m->add('test', '12345'));
$var = $m->get('test');

Here's the complete code for the modified example.php script:

<?php
include('MemcacheSASL.php');
$m = new MemcacheSASL;
$m->addServer('host', 'port'); // replace these values
$m->setSaslAuthData('user', 'password');  // replace these values
var_dump($m->add('test', '12345'));
$var = $m->get('test');
print "$var";
?>

Save the file and run it:

$ php example.php

The script will connect to your Memcached server, save the value to the key 'test', then retrieve and display it.

bch