Install Wordpress, Nginx, Php, and Varnish On Ubuntu 12.04

Install Wordpress, Nginx, PHP, and Varnish on Ubuntu 12.04
by Janeth Kent Date: 29-05-2013 wordpress nginx php varnish ubuntu


About Varnish


Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the server itself if there is a need for an active process.

Setup


Before you start to work through this tutorial, there are a couple prerequisites. You will need a user with root privileges, the LEMP stack, and Wordpress already installed on your server.


Step One—Install Varnish


Once you have all of the prerequisites needed to configure varnish with wordpress, you should go ahead and start the process to install Varnish.

The varnish site recommends installing the varnish package through their repository.

You can start that process by grabbing the repository:
sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

The next step is to add the repository to the list of apt sources. Go ahead and open up the file.
sudo nano /etc/apt/sources.list

Once inside the file, add the varnish repository to the list of sources.
deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

Save and exit. Finally, update apt-get and install varnish.
sudo apt-get update
sudo apt-get install varnish

Step Two—Configure Varnish


Once you have both nginx and varnish installed, you can start to configure them to ease the load on your virtual private server.

Varnish will serve the content on port 80, while fetching it from nginx which will run on port 8080.

Go ahead and start setting that up by opening the /etc/default/varnish file:
sudo nano /etc/default/varnish

Find the lines under “DAEMON_OPTS”— in the Alternative 2 section, and change the port number by "-a" to 80. The configuration should match the following code:
 DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

That's the only change you need to make there. Save and exit out of that file and open up the default.vcl file:
sudo nano /etc/varnish/default.vcl

This file tells varnish where to look for the webserver content. It should already be configured to have the backend (ie. nginx) listening in on port 8080.
We need to use this file for a secondary purpose. Wordpress is chock full of various cookies that make caching it very difficult. In order to have varnish work as efficiently as possible, we need to tell it to drop all the cookies that don't relate to the admin side of the Wordpress site. Additionally, we need to tell varnish to remove the cookies that make worpdress very difficult to cache. The beginning of the default.vcl file should look like this:
[...]
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

# Drop any cookies sent to Wordpress.
sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}

# Drop any cookies Wordpress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

[...]

Step Three—Configure Nginx


Although we have already configured varnish to expect that nginx ports will be running on 8080, the default settings for nginx are still on port 80. We will correct the discrepancy now.

Open up the virtual host file with the Wordpress information. In the previous Wordpress tutorial we simply called it wordpress:
sudo nano /etc/nginx/sites-available/wordpress

The Virtual Host should also be set to port 8080 and be accessible only from the localhost. The updated line looks like this:
[...]
server {
        listen  127.0.0.1:8080; ## listen for ipv4; this line is default and implied
        [...]

We need to do one last thing prior to starting varnish running on our site, and that is to remove the default enabled virtual host.
sudo rm /etc/nginx/sites-enabled/default

The template will remain in the sites-available directory, should you need it once more.

Step Five—Restart


Once you have made all of the required changes, restart varnish and nginx.
sudo service nginx restart
sudo service varnish restart

Accessing your domain should instantly take you to the varnish cached version, and you can see the details of varnish’s workings on your VPS with this command:
varnishstat
Try this tutorial on digitalocean.com

 
by Janeth Kent Date: 29-05-2013 wordpress nginx php varnish ubuntu hits : 2947  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.

 
 
 

Related Posts

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The most popular Array Sorting Algorithms In PHP

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations,…

MySQL 8.0 is now fully supported in PHP 7.4

MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules…

A roadmap to becoming a web developer in 2019

There are plenty of tutorials online, which won't cost you a cent. If you are sufficiently self-driven and interested, you have no difficulty training yourself. The point to learn coding…

10 PHP code snippets to work with dates

Here we have some set of Useful PHP Snippets, which are useful for PHP Developers. In this tutorial we'll show you the 10 PHP date snippets you can use on…

8 Free PHP Books to Read in Summer 2018

In this article, we've listed 8 free PHP books that can help you to learn new approaches to solving problems and keep your skill up to date.   Practical PHP Testing This book…

Best Websites to Learn Coding Online

You know and we know that it’s totally possible to learn to code for free... If you can teach yourself how to write code, you gain a competitive edge over your…

PHP and XMP format: Create a Panorama 360/VR viewer in PHP and A-Frame

The Extensible Metadata Platform (XMP) is an ISO standard, originally created by Adobe Systems Inc., for the creation, processing and interchange of standardized and custom metadata for digital documents and…

PHP: Basic Introduction to Namespaces

History of PHP Namespaces In PHP prior to 5.3 (2009), any class you define lived at the same global level as other classes. Class User, class Contact, class Db they're all together…

Best Paywall Plugins for WordPress

The most effective way of monetising a blog or a website, is moving content behind a subscription fee much in the same way the traditional print press is doing. Using a…

How to write real client IP address in error Log with Varnish 4 and Apache 2.4 in Ubuntu 16.04

In order to have Varnish 4 pass on the real client IP to your Apache 2.4 error log in Ubuntu 16.04 , you'll need to edit your Varnish configuration (/etc/varnish/default.vcl…

PHP 7.2 will be the first Programming Language to add Modern Cryptography to its Standard Library

Last week, the voting phase closed on an RFC to add libsodium to PHP 7.2. The result was unanimous (37 in favor, 0 against). When version 7.2 releases at the end…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration