Optimizing Your Headway Powered Website – Installing LEMP Stack

Introduction

How’s everyone doing in this series so far? Let us know your experiences in the comments!!

To do a little bit of a recap of our previous tutorials, we’ve started with a clean Ubuntu VPS, installed some common requirements for WordPress on it, Apache, MySQL, PHP. We’ve set up a firewall that should protect us through most attacks, we’ve setup FTP or SFTP as well. Finally last week, we spent some time getting WordPress installed, configured, and your homework was to install your plugins and Headway.

Hopefully you did not have any trouble getting those installed, since I haven’t seen any comments on the previous post.

Today we are going to take a step back for those that wanted to see what a nginx setup looked like. If you have followed the last two tutorials and are ready to go with Apache (like most of us), then this one you can skip.

However, if you want to feel adventurous and run nginx, we’re going to start with getting a LEMP stack installed today and then go from there.

What is LEMP?

Easy as LAMP, LEMP stands for Linux, nginx (it’s pronounced like ENGINE X), MySQL, and PHP. Since I already mentioned that we’d be using Ubuntu for this series, we’ll need to create a server running Ubuntu 12.04. You should have also followed some of the basics from the first two tutorials in this series. Those things are the same.

Alright, now that we have everything out of the way, let’s go ahead and get started installing things.

Step One – Update ServerThroughout the series we’ve been using apt-get as our installer for the programs we’ve needed to install. It’s always important to update your server to keep it’s patches/fixes up to date. This helps protect our server down the road.

There’s actually two steps I’m going to recommend here. The first is to run the following:

sudo apt-get update

This will update the package repositories to see what needs to be installed. An important tip though is that this command DOES NOT update your server, simply looks to see if there’s a new version of the program available.

Since we want to keep our server protected, we need to run the next command:

sudo apt-get upgrade

This will actually upgrade any of our installed software to newer versions which keeps our server safe and protected.

Step Two – Installing MySQL

If you’ve been following along with the series this should be easy to remember. I’m including it here in case you skipped a few tutorials and just came here.

To install MySQL we need to run the following command:

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

MySQL will ask you to set a root password. Pay close attention so that you can set that now instead of setting it later.

Once we have MySQL installed, we want to run the next command:

sudo mysql_install_db

This will install MySQL’s internal tables and get everything ready for you.

Finally we can finish installation with:

sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password. Type it in.

It’s the easiest to say Yes to all the options the MySQL installation gies you. At the end, MySQL will reload and implement any of the new changes.

Step Three – Install nginx

Now that we have MySQL setup, we need to install nginx.

sudo apt-get install nginx

Since nginx does not start on it’s own, we’ll go ahead and run:

sudo service nginx start

You can confirm that nginx is installed by directing your web browser to your server’s IP address.

Step Four – Installing PHP

Alright, assuming you don’t need to go back and install nginx again, or make changes, you can move on to installing PHP.

sudo apt-get install php5-fpm

In our next step we will configure nginx and php to know each other exists.

Step Five – Configuring PHP and nginx

We need to make a small change to the default PHP.ini configuration:

sudo nano /etc/php5/fpm/php.ini

Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.

If the number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This exposes a security risk, so we want to close that up. If the number is set to 0, conversely, the interpreter will only process the exact file path – a much safer alternative.

Now save and exit.

We need to make another change, this time to the FPM configuration. Go ahead and open www.conf:

sudo nano /etc/php5/fpm/pool.d/www.conf

Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.

Save and exit.

Now restart php-fpm:

sudo service php5-fpm restart

So a quick recap is so far we’ve only configured PHP. We need to move to nginx now.

Let’s open the default virtual host file.

sudo nano /etc/nginx/sites-available/default

The configuration should include the changes below.

[...]
server {
listen 80;
root /usr/share/nginx/www;
 index index.php index.html index.htm;
server_name example.com;
location / {
 try_files $uri $uri/ /index.html;
 }
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/www;
 }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 location ~ .php$ {
 #fastcgi_pass 127.0.0.1:9000;
 # With php5-fpm:
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;

 }
}
[...]

Alright let me explain the details of the changes:

Add index.php to the index line.
Change the server_name from local host to your domain name or IP address (replace the example.com in the configuration)
Change the correct lines in “location ~ .php${” section

Save and Exit

Step Six – Create a PHP info Page

Alright so the easiest way to see all the details of your php configuration is to create a php file.

Let’s create a new file:

sudo nano /usr/share/nginx/www/info.php

Add the following lines:

<?php
phpinfo();
?>

Now save and exit.

Let’s restart nginx one more time.

sudo service nginx restart

Now we should be able to see our nginx and php-fpm configuration details by visiting the following:

http://IP-ADDRESS/info.php

If everything worked out well, you should now be configured and ready to install WordPress.

The next tutorial in this lesson will be installing WordPress on nginx. There’s a few tricky things to do, but we’ll be showing you all that in the next post.

Spread the word!

2 Responses

  1. Thanks for this. I’ve been really getting a lot out of it since I’ve been playing around with Nginx. Mostly using AWS but since you pointed out Digital Ocean I’m a now a convert.

  2. Thanks for the feedback Corey. I love Digital Ocean. Found out about them in January I think and converted all my sites over there as of June. I’m running some pretty sweet setups for a couple of clients now too there and it’s definitely been a fun learning experience.

Leave a reply

Copyright © 2016 Vesped Inc. All Rights Reserved. Proudly Powered by Headway and WordPress