How to Install Memcached on Windows & Linux Servers
This article will teach you how to install and set up Memcached in both a Windows and Linux environment.
If you've already completed the installation process, you can also learn how to access and manage data in Memcached with PHP.
What is Memcached?
Memcached is a free, open-source memory caching system, often used for enhancing the speed of dynamic database-driven websites.
The cached data gets stored in a short-term, dynamic memory location that can be easily accessed within your applications by a key name.
Install Memcached on Windows
Memcached installation in a Windows environment is pretty straightforward. There are just a few steps to follow.
There are two locations that you need to download from. The executable and supporting files GitHub repository, and the PHP extension from PHP's official website.
First, we'll start with the executable file and supporting files. You can download them from GitHub. They keep the repository updated regularly with the latest versions.
Make sure to download the correct files that your system will support, x64 vs. x86, etc., and place them in a directory on your local drive. My files are located on the C: drive at:
C:\memcached
Now, we need to head on over to the PHP PECL Downloads Page where you'll see a long list of extension releases for PHP.
This list only contains a handful of Memcached Windows download links. The latest Windows download, at the time of this article, allows support for PHP versions 7.2 through 7.4. Download the file that fits your PHP environment description, x64 or x86, or thread-safe vs. non-thread-safe.
Move the newly downloaded DLL file to the location of your PHP installation and into the ext
folder. Mine is located here:
C:\php\ext
In my case, I've downloaded the installation file for PHP 7.4, so my php.ini
configuration will look like this:
[Memcache]
extension=php-7.4.x_memcache.dll
memcache.allow_failover = 0
memcache.chunk_size = 32768
memcache.default_port = 11211
memcache.hash_strategy = standard
memcache.hash_function = crc32
memcache.protocol = ascii
memcache.compress_threshold = 20000
memcache.lock_timeout = 1
Make sure you've used the correct file name when updating your php.ini
file. Yours could be different than what's shown in this example and adding an incorrect file name will cause issues with the installation.
After you've downloaded and moved the DLL file to the correct location and updated your php.ini
file, restart PHP by opening the Services app, or services.msc, and restarting the World Wide Web Publishing Service
service.
[Ctrl+R]
services.msc
The final step is starting Memcached in your Windows environment by running the following command:
C:\memcached\memcached.exe -d start
Make sure you're in the directory where the memcached.exe file is located before starting it, otherwise you'll get an error. Alternatively, you could set up the path in your system's environment variables to point to the correct directory so you can start it from anywhere in the command-line tool.
Install Memcached on Linux
There are two options for the first step of an installation in a Linux environment. You can either download the file directly from the downloads page or you can run the wget
command below, which will grab and download the latest version automatically to your current active directory:
wget http://memcached.org/latest
Next, you'll need to unzip the downloaded tar.gz
file:
tar -zxvf memcached-1.x.x.tar.gz
You'll need to replace "1.x.x" with the version number that was downloaded. At the time of this writing, the latest version was 1.6.9, so the command would be tar -zxvf memcached-1.6.9.tar.gz
Now that the file contents are extracted, move over to the newly created directory, again using the correct version number in place of "1.x.x":
cd memcached-1.x.x
Finally, complete the installation from the new directory:
./configure && make && make test && sudo make install
That's pretty much it. With my Linux/Apache environment, I had to restart Apache for the new installation to take effect within my PHP applications, but all you need to do is run this command to restart Apache:
sudo systemctl restart apache2
Conclusion
Memcached is a great solution for memory caching, in either a Windows or Linux environment. Especially when querying heavy loads of data, or data that typically doesn't change often.
Now that everything is setup in your environment, head on over to the next article to leverage the power of Memcached with PHP.
Comments
Marco Lungo
check the Windows version instruction: you have reported the memcache parameter, that do not works.
Thanks for the instructions
Marco
Josh Rowe