First you need to install memcached if it isn't already installed. On CentOS memcached is easy to install via yum.

yum install memcached

Once memcached is installed you can view the main configuration file under the /etc directory. -m allows you to chance the amount of memory that memcached will use. Typically 64MB or 128MB is a good start for smaller sites, for larger sites it's not uncommon to see values of 64GB. -p allows you to specify which port memcached will listen on. You can change this to whatever you want, just keep in mind that most applications and CMS like WordPress assume memcached listens on port 11211, so you will need to update any applications that rely on memcached if you change the port. -l is the listening address for memcached. If you are using a single memcached server for multiple web servers you will more than likely want to have memcached listen on a private IP. If you only have a single server than 127.0.0.1 is fine.

Once you have finalized the memcached configuration file, go ahead and start up memcached and use chkconfig to make sure that memcached starts up on reboot. This should happen once you install memcached via yum, but it never hurts to double check that it's set to start up by default, otherwise your application may very well be broken if your server is ever rebooted.

If you are using Vim, like a true boss, you already know you can press "/" and type in "session.save_handler", hit enter and then press "i" to change this value from files to memcached. If you aren't that cool then just find the line below, add a "#" in front of it and paste in the other stuff.

You can echo stats into netcat to get a quick idea of memcached usage.

echo stats | nc 127.0.0.1 11211

STAT curr_items will tell you how many items memcached currently has.

STAT evictions will tell you how many items have been removed from memcached to make way for new or more frequently used items. If this value is very high then you will want to consider allowing memcached to use more RAM. Small numbers are ok since often times memcached will remove items that are almost never accessed to make way for more frequently used items, in this case adding more RAM will not necessarily help performance, so keep track of this stat over time.

STAT get_misses will tell you how many times an item was not in memcache's cache. Often times this value will be high after a restart since it takes a while to warm up caches. If this value continues to rise after memcached has been active for a while you may want to look into why this is happening. It could be due to lack of RAM or lots of resources that are un cache-able for whatever reason.

STAT bytes_read This value is the amount of bytes memcached has read from the network.

STAT bytes_written This value is the amount of bytes that memcached has sent to the network.

Similar to the memcached stats command, this command gives you detailed slab information. memcached will display per slab stats as well as total active slabs and total malloced amounts for all the slabs. The values are in bytes.

echo stats slabs | nc 127.0.0.1 11211

At the bottom of the stats slabs command output you will find the total amount of active_slabs as well as the total amount of malloced usage for memcached. The total_malloced value is in bytes, so below you can see that I am using about 64MB of memory.

This is the location of the main Memcached configuration file on Ubuntu. If you want to adjust settings like where the logs for memcached are stored, or the amount of memory that memcached is allowed to use, you will want to modify this file. The file shown below is what memcached uses by default.

vim /etc/memcached.conf
# memcached default config file
# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d
# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log
# Be verbose
# -v
# Be even more verbose (print client commands as well)
# -vv
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64
# 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 127.0.0.1
# Limit the number of simultaneous incoming connections. The daemon default is 1024
# -c 1024
# Lock down all paged memory. Consult with the README and homepage before you do this
# -k
# Return error when memory is exhausted (rather than removing items)
# -M
# Maximize core file limit
# -r

This memcached file controls whether or not memcached should be disabled. By default, at least on Ubuntu 14.10 memcached is enabled once you install it. It's possible that someone might mess around with this file and disable memcached for whatever reason, so if you cannot figure out why memcached isn't working, you may want to check /etc/default/memcached and make sure memcached is actually enabled.

/etc/default/memcached
# Set this to no to disable memcached.
ENABLE_MEMCACHED=yes

This is where the memcached binary is located on Ubuntu. This is not a configuration file, but I am including it here just in case anyone is wondering where the memcached binary is located on Ubuntu 14.10.

If you notice that memcached is using too much memory, or unable to store enough information you can modify memcache's configuration file at the location shown below.

vim /etc/memcached.conf

These are the default settings (with comments removed). There are not a ton of "optimization" like tweaks for memcached, but the main way to improve performance is allow it to cache more things in RAM. I suggest giving memcached 64MB for memory for 1GB VPS servers, 128MB for 2GB VPS servers, and 256MB for 4GB VPS servers. If you are only running memcached on the vps then you can give it as much RAM as you want, just be sure to save about 20% - 30% of the VPS memory for the operating system and various other services otherwise you may cause the vps to swap and slow down.

These 4GB server settings assume you are running other services like Apache, MySQL and PHP-FPM. You should review those optimization wikis first and then allocate any left over memory to Memcached. 256MB of RAM should be enough for memcached to speed up your website, if you have lots of RAM to spare even after you tune other services then I would suggest raising -m higher if memcached fills up quickly.