Bitnami Memcached Virtual Machine

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! This guide includes some basic information you will need to get started with your application.

How to import a Bitnami Virtual Machine?

Check the following instructions to import a Bitnami Virtual Machine:

Importing a Bitnami Virtual Machine in VirtualBox
  • Select the "File -> Import Appliance" menu option and select the .ova file downloaded from the Bitnami website. Then click "Continue".
  • Once it is imported, click the "Start" button in the VirtualBox toolbar.

For a detailed walkthrough, check our Virtualbox tutorial.

Importing a Bitnami Virtual Machine in a VMware product
  • Select the "File -> Import" menu option and select the .ova file downloaded from the Bitnami website. Then click "Continue".
  • Once the import is complete, click "Finish" to start the virtual machine.

For a detailed walkthrough, check our VMware tutorial, which uses VMware Fusion as an example. To learn how to use our virtual machines with other VMware products, refer to the VMware Workstation documentation or the VMware vSphere documentation.

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 password. These credentials allow you to log in to your Virtual Machines 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?

Password: The administrator password to log in to your application is randomly generated during the first boot. Check the FAQ to learn how to retrieve it.

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

SSH username: bitnami

What is my server IP address?

The IP address is displayed on screen at the end of the boot process, but you can check it at any time by running the following command:

  $ sudo ifconfig

Check server IP address

How do I get my SSH key or password?

You can obtain the SSH password from the virtual machine console when it starts up. Click here for more information.

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

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 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 can I run a command in the Bitnami Memcached Stack?

Log in to the server console as the bitnami user and run the command as usual. The required environment is automatically loaded for the bitnami user.

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.

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;
    
  • Now, you have to connect to Memcached. 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.

How to create a full backup of Memcached?

Backup

The Bitnami Memcached Stack is self-contained and the simplest option for performing a backup is to copy or compress the Bitnami stack installation directory. To do so in a safe manner, you will need to stop all servers, so this method may not be appropriate if you have people accessing the application continuously.

Follow these steps:

  • Change to the directory in which you wish to save your backup:

      $ cd /your/directory
    
  • Stop all servers:

      $ sudo /opt/bitnami/ctlscript.sh stop
    
  • Create a compressed file with the stack contents:

      $ sudo tar -pczvf application-backup.tar.gz /opt/bitnami
    
  • Restart all servers:

      $ sudo /opt/bitnami/ctlscript.sh start
    

You should now download or transfer the application-backup.tar.gz file to a safe location.

Restore

Follow these steps:

  • Change to the directory containing your backup:

      $ cd /your/directory
    
  • Stop all servers:

      $ sudo /opt/bitnami/ctlscript.sh stop
    
  • Move the current stack to a different location:

      $ sudo mv /opt/bitnami /tmp/bitnami-backup
    
  • Uncompress the backup file to the original directoryv

      $ sudo tar -pxzvf application-backup.tar.gz -C /
    
  • Start all servers:

      $ sudo /opt/bitnami/ctlscript.sh start
    

If you want to create only a database backup, refer to these instructions for MySQL and PostgreSQL.

virtualMachine

Bitnami Documentation