Ubuntu LEMP NGNIX Varnish WordPress Install

Ubuntu 12.10 LEMP NGNIX Varnish Server Configuration for WordPress Install on Digital Ocean VPS

This is a tutorial for developers who are installing WordPress on a virtual private server (VPS) and would like to optimize their server configuration for WordPress and SEO. There are many more advanced configurations that can be made for this WordPress server configuration, but this is a good starting point. In this tutorial I will be using a cloud server from Digital Ocean. I’ll start with their entry level $5 per month could server, because I can always upgrade it later. If you plan on having a good amount of traffic or content, then I would recommend starting with a larger server. This tutorial is really a basic server configuration to run a WordPress site that does not have a significant amount of content or traffic, which is great for small business, blogs and other basic WordPress sites. By the end of this tutorial you will have an blazing fast WordPress site up and running on a dedicated IP address. This tutorial will be particularly useful to those who provide WordPress web design and development services and are looking to run WordPress on Nginx rather than Apache in order to increase page speed and improve SEO. We will not be covering any advanced sever optimization techniques in this tutorial.

I will be using the example username of superuser and the example password of pswd1. Also whenever I write “ipadress” in a command example, you should obviously replace it with your servers actual ip address. I’ve made the instructions fairly verbose, but many techniques in this tutorial have been sampled from other tutorials. I have included all of the original tutorials with each step for your reference.

How to Configure Your Ubuntu 12.10 VPS for WordPress

Once you have created your new droplet in Digital Ocean, or you already have a VPS set up, you will need to SSH into your server. I started with the Ubuntu 12.10 x32 Server image. I’m using Coda2, so I literally just fill in the ip address for the host name, root as the username and use the default password that was emailed to me by Digital Ocean.

If you do no have Coda or a comparable app and you would like to login using a default terminal us this command.

ssh root@ipaddress

It will then show you your ECDSA key fingerprint and prompt you for the root password.

Stage 1: Ubuntu 12.10 Initial Server Configuration

First complete you initial server configuration for Ubuntu 12.10. This tutorial is based on the Ubuntu 12.04 initial server configuration tutorial from Digital Ocean.

https://www.digitalocean.com/community/articles/initial-server-setup-with-ubuntu-12-04

Change the default password that was emailed to you using this command.
passwd

Add a user with the following command.

adduser superuser

Now we need to give this user root permissions. In order to perform a root task you will have to type “sudo” in front of the command. It’s a nice safety net built into Linux.

We need to edit the sudo configuration file using the default editor called “nano”.

visudo

Now look for this line:
# User privilege specification
root ALL=(ALL:ALL) ALL

And add this line under it to grant all permissions to the new user.

superuser ALL=(ALL:ALL) ALL

Type CRTL+X to exit the file. Then press Y followed by Enter to save the file in the proper location. If you need help with nano editor commands refer to this nano editor cheat sheet.

You initial Ubuntu 12.10 x32 server configuration is now complete.

How to install Linux Nginx MySql PHP Lemp Stack on Ubuntu 12.10

Stage 2: Installing a LEMP Stack on Ubuntu 12.10 for WordPress

https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04

Update Apt-Get

Apt-Get install is a super easy way to download and compile server programs. Here’s how you updated it from the terminal.

sudo apt-get update

Install MySQL

sudo apt-get install mysql-server

Use this command to install mysql software on your server. At the end of the install you’ll be prompted to enter a mysql root password.

mysql password: pswd1

Install Nginx

Nginx is super easy to install but can prove challenging to configure. To install simple use the following command.

sudo apt-get install nginx

Now start Nginx with this command.

sudo service nginx start

Test Nginx to make sure it installed ok with this command. It should show your IP Address if it’s all set.
ifconfig eth0 | grep inet | awk ‘{ print $2 }’

Intall PHP

You are definitely going to need php5-fpm and php5-mysql to run WordPress. These other PHP extensions will be needed for server optimization we will be making in more advanced tutorials.

sudo apt-get install php5-fpm php-pear php5-common php5-mysql php-apc php5-gd

Configure PHP

Open up the php.ini file and find the line cgi.fix_path=0. Uncomment the line (by deleting the #). If the number is 1, keep it that way. If the number is 0, change it to one.

sudo nano /etc/php5/fpm/php.ini
cgi.fix_pathinfo=0

Then go to the very end of the file and add the following code.

[apc]
apc.write_lock = 1
apc.slam_defense = 0

Now save and exit.

Restart php-fpm for the changes to take full effect.

sudo service php5-fpm restart

Configure Nginx

Start by opening up the default virtual host file.

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

Make these changes:

  • Add index.php to the end of the index line.
  • Change the server_name from localhost to your domain name or IP address.
  • Change the correct lines in “location ~ \.php$ {“ section (it should look like below).
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;

}

save and exit

Create the info.php Page

Simply use nano editor to open up a new file called info.php and paste in the php code snippet show here.

sudo nano /usr/share/nginx/www/info.php
<?php
phpinfo();
?>

Save and exit.

Restart Nginx

sudo service nginx restart

Now test your info.php file by entering ipaddress/info.php into your browser. You should get the PHP Nginx configuration details with a nice PHP logo at the top. If you get a bad gateway error you most likely didin’t do something right in your Nginx virtual hosts file. Go back and debug it. Something as minor as missing # can throw off the entire configuration.

How to Install WordPress on Nginx

https://www.digitalocean.com/community/articles/how-to-install-wordpress-with-nginx-on-ubuntu-12-04

/*** the order of this tutorial is not logical in my case, so i will be starting with step for and then going to back to step one once nginx is all set ***/

Installing Wordpress with Nginx

Let’s get started by setting up our public we directory. You can add an additional folder level to the end of the /var/www path if your like (especially if you intend on hosting multiple sites). So it could aslo be /var/www/mysite.

Run through these commands to set up the public web directory.

sudo mkdir -p /var/www
sudo chown www-data:www-data /var/www -R

Give ownership of the directory to nginx by replacing superuser with your user name.

cd /var/www/
sudo usermod -a -G www-data superuser

It’s time to set up the Nginx server blocks. Let’s create a new virtual hosts file for our WordPress site by copying the default configuration we set up earlier.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress

Now open up the file in Nano, and make the these edits. We need to set root to the correct directory path we just set up. Then we need to change the try_files lin to work with WordPress. Just past the line of code show here in place of the “try_files $uri $uri/ /index.html;” line.

sudo nano /etc/nginx/sites-available/wordpress
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;

Save and exit.

Ok time to activate the server block we just set up. Enter these sudo commands.

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress
sudo rm /etc/nginx/sites-enabled/default

Now that nginx is set up we can install WordPress.

Download the lastest version of WordPress and decompress the files.

wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

Now we need a database for it to run on. Enter the following mysql commands to set up the database. (Write the out on a text file first because you’re going to need to access all of the info in the next step).

mysql -u root -p

CREATE DATABASE wordpress;
CREATE USER wpuser@localhost;
SET PASSWORD FOR wpuser@localhost= PASSWORD("pswd1");
GRANT ALL PRIVILEGES ON wordpress.* TO wpuser@localhost IDENTIFIED BY 'pswd1';
FLUSH PRIVILEGES;
exit

The database is all set up, so let’s copy WordPress into the public web directory we set up earlier.

sudo cp -r ~/wordpress/* /var/www

Now go to your ip address or url and follow the WordPress install wizard. For security reasons, I strongly recommend changing the table prefix from wp_ to something else.

From here there’s a list of things you can do to optimize your WordPress intall and VPS. I’ll be posting them soon.