Setup WordPress With Memcached And Nginx


Setup WordPress With Memcached and Nginx

Installing Memcached

Memcached is a distributed in-memory caching system used to speed up web applications such as WordPress. It stores data in memory and improves the performance of web applications by reducing the amount of data that needs to be read from the database.

To install Memcached on Ubuntu and Debian, you need to use the apt-get command. Open up a terminal window and enter the following command:


$ sudo apt-get install memcached

Once the installation is finished, start the memcached service with the following command:


$ sudo systemctl start memcached

You can check if the Memcached service is running with the following command:


$ sudo systemctl status memcached

The output should look something like this:


Active: active (running) since Mon 2018-08-20 20:14:20 UTC
Main PID: 24461 (memcached)
CGroup: /system.slice/memcached.service

Aug 20 20:14:20 ubuntu-18 memcached[24461]: memcached.service: Succeeded.

Now that Memcached is installed and running, you can move on to the next step which is to install Nginx.

Installing Nginx

Nginx is a web server that can be used to serve static files and reverse proxy requests from applications such as WordPress. To install Nginx on Ubuntu and Debian, you will need to use the apt-get command. Open up a terminal window and enter the following command:


$ sudo apt-get install nginx

Once the installation is finished, you can start the Nginx service with the following command:


$ sudo systemctl start nginx

You can check if Nginx is running with the following command:


$ sudo systemctl status nginx

The output should look something like this:


Active: active (running) since Mon 2018-08-20 18:48:56 UTC
Main PID: 9151 (nginx)
CGroup: /system.slice/nginx.service
└─9151 nginx: master process /usr/sbin/nginx

Now that Nginx is installed and running, you can move on to the final step which is to install WordPress.

Installing WordPress

WordPress is a content management system that can be used to create and manage websites. To install WordPress on Ubuntu and Debian, you will need to use the apt-get command. Open up a terminal window and enter the following command:


$ sudo apt-get install wordpress

Once the installation is finished, you can start the WordPress service with the following command:


$ sudo systemctl start wordpress

You can check if WordPress is running with the following command:


$ sudo systemctl status wordpress

The output should look something like this:


Active: active (running) since Sun 2018-08-19 20:27:44 UTC
Main PID: 24461 (wordpress)
CGroup: /system.slice/wordpress.service
└─24461 /usr/sbin/wordpress --daemon

Configuring Nginx and Memcached for WordPress

Once the applications have been installed, it’s time to configure Nginx and Memcached for WordPress. To do this, you will need to edit the Nginx configuration file. The configuration file is located at /etc/nginx/nginx.conf. Open up the file in your text editor and add the following lines at the end of the file:


proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";

server {
listen 80;
server_name yourdomain.com;
root /var/www/wordpress;

location / {
proxy_pass http://127.0.0.1:80;
proxy_cache my_cache;
proxy_cache_valid 1m;
proxy_cache_methods GET;
proxy_cache_valid any 10m;
proxy_cache_use_stale error timeout invalid_header updating;
}
}

The above configuration will enable Nginx to cache static files such as images and CSS files. You can also enable Memcached for WordPress by adding the following lines to the Nginx config file:


upstream memcached {
server 127.0.0.1:11211;
}

server {
listen 80;
server_name yourdomain.com;
root /var/www/wordpress;

# enable memcached
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 10m;
fastcgi_cache_methods GET POST;
fastcgi_cache_lock on;
fastcgi_cache_min_uses 3;
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_cache_background_update on;
fastcgi_pass_header Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_param WORDPRESS_EARLY_LOAD true;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
}
}

The above configuration will enable Memcached to store WordPress data in memory. Finally, you will need to restart Nginx and Memcached for the changes to take effect. You can do this with the following commands:


$ sudo systemctl restart nginx
$ sudo systemctl restart memcached

Testing WordPress Performance

To test WordPress performance, you can use a tool such as Apache Bench (ab) or WPsite Speed. Apache Bench will test the load speed of web pages and can be used to get an idea of how well WordPress is performing. To use Apache Bench, open up a terminal window and enter the following command:


$ ab -n 1000 -c 10 http://yourdomain.com/

The output should look something like this:

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
....
....
....
Server Software: nginx/1.14.0
Server Hostname: test-domain.com
Server Port: 80

Document Path: /
Document Length: 233 bytes

Concurrency Level: 10
Time taken for tests: 0.322 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 191000 bytes
HTML transferred: 233000 bytes
Requests per second: 3117.76 [#/sec] (mean)
Time

Leave a Reply

Your email address will not be published. Required fields are marked *