How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 14.04 on DigitalOcean

In this guide, we will quickly provide instructions on how to install a LEMP stack on an Ubuntu 14.04 server on Digital Ocean. The Ubuntu operating system takes care of the first requirement.

You can signup for Digital Ocean here and deploy a SSD server in less than a minute.

Installing nginx

sudo apt-get update
sudo apt-get install nginx

Next, you’ll want to check the web server to see if nginx is up and running. In your Digital Ocean panel grab the IP address of the droplet and enter it in to your browser. If everything went well you’ll see the nginx welcome screen.

Install MySQL

Now that we have the web server up, we need to install the MySQL database to store all the information for WordPress. To install MySQL type the command:

sudo apt-get install mysql-server

It’s going to ask for you to create an admin password. Type in a strong password and keep it somewhere safe.

Next type in:

sudo mysql_install_db
sudo mysql_secure_installation

These two commands will create the directory structure for MySQL and run a security script to remove some of the defaults and fake user data. You will be prompted for the password that you had created in the previous step. It will also ask you if you want to change your password. You can skip this since you just created a new password.

Installing PHP

Ok, we’re just about halfway done. We’ve installed the webserver, the database, and now all we’ve got left is to install PHP and WordPress. I promise, this rest will go about as quickly and simply as the first half.

Type this command to install PHP and php5-FPM:

sudo apt-get install php5-fpm php5-mysql

Next, you’ll need to edit the config files:

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

What you’re going to edit is the link that contains ‘cgi.fix_pathinfo’. You can scroll through the file or search for the line. You need to uncomment the line by removing the # and the setting the value to 0.

cgi.fix_pathinfo=0

Save and close the file. To make the changes take effect, restart the services:

sudo service php5-fpm restart

Configuring nginx to use the PHP processor

We’ve got to edit the server block to use the PHP processor. To do this type this into the command line:

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

Edit the file by making the following changes:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name server_domain_name_or_IP;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

To apply the changes restart nginx:

sudo service nginx restart

Creating the MySQL Database the WordPress User

From the console enter:

mysql -u root -p

Enter a password for the root MySQL user when prompted.

Next create a database, you can use any name of your choice, and it’s advisable that you use something other than ‘wordpress‘, but for this example we’ll use it for simplicity.

CREATE DATABASE wordpress;

Now we’ve got to create a user for the, again for the sake of simplicity we are going to use ‘wordpressuser’ and ‘password’, but you should absolutely use other values to decrease the chance someone can guess the username and password.

CREATE USER wordpressuser@localhost IDENTIFIED BY 'password';

To grant access for this user to the database type in the following command:

GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost;
FLUSH PRIVILEGES;
exit

That completes the database user creation for WordPress.

Download and Install WordPress

Grab the latest version of WordPress and extract the contents:

cd ~
wget http://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz

Install some other components we’ll need:

sudo apt-get update
sudo apt-get install php5-gd libssh2-php

Configure WordPress

Head over the directory we extracted WordPress to:

cd ~/wordpress

Copy over the sample config file and rename it.

cp wp-config-sample.php wp-config.php

Edit the new config file:

nano wp-config.php

Replace the text in red with your unique values:

. . .
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpressuser');
/** MySQL database password */
define('DB_PASSWORD', 'password');
. . .

Copy the files over to the document root

We’re going to change the default location of where nginx stores it’s document root from /usr/share/nginx/html/ to /var/www/html/

Type the following in:

sudo mkdir -p /var/www/html
sudo rsync -avP ~/wordpress/ /var/www/html/
cd /var/www/html/

Grant nginx permissions to the directory.

sudo chown -R www-data:www-data /var/www/html/*

Create the uploads directory and grant access to nginx.

mkdir wp-content/uploads
sudo chown -R www-data:www-data /var/www/html/wp-content/uploads

Modify Nginx Server Blocks

Copy the default block and create one for WordPress:

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

Open the new file and make the following changes:

sudo nano /etc/nginx/sites-available/wordpress
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
server_name your_domain.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

Copy the file over to the sites-enabled directory:

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

Remove the old file:

sudo rm /etc/nginx/sites-enabled/default

Restart the services:

sudo service nginx restart
sudo service php5-fpm restart

Congrats! That’s it’s all you’ve got to do is type in your domain name into a browser and configure the rest of your site.

A few notes, if you’re having issues with updating or installing plugins, you can read up on how to fix it here: How To Configure Secure Updates and Installations in WordPress on Ubuntu

PHP-FPM: Socket vs TCP/IP and sysctl tweaking

Depending on if your having issues with your WordPress install resulting in the error screen when you visit the site and your getting errors like:

connect() to unix:/var/run/php5-fpm.sock failed

You may want to give this a try. What are are about to do is use sockets instead of TCP/IP.

Open PHP-FPM pool config file:

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

Uncomment the following line by removing the semicolon:

;listen.backlog = 65536

Restart the service:

service php5-fpm restart

Using TCP/IP for FPM

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

Replace the following line:

listen = /var/run/php5-fpm.sock

So it reads:

listen = 127.0.0.1:9000

Uncomment line:

;listen.mode = 0660

Find and uncomment:

;pm.max_requests = 500

Save the changes and open the nginx virtual-host config file(s):

sudo nano /etc/nginx/sites-enabled/wordpress

Find the line:

fastcgi_pass unix:/var/run/php5-fpm.sock;

Replace with:

fastcgi_pass 127.0.0.1:9000;

Save the changes and restart the services:

service php5-fpm restart && service nginx restart

Adding Swap The Fast Way

If your on the smallest droplet size running WordPress, your likely running fairly low on RAM. To help with this we can create a swap file that should help keep things running fairly smoothly until your ready to upgrade to a larger droplet size.

Creating a 4GB Swap File

Create the file:

sudo fallocate -l 4G /swapfile

Enable the swap file and set the correct permissions:

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make the the Swap File Permanent

We created the file, enabled it, and told the system to use it, however if the system is restarted it will not use the swap by default to make the changes permanent, we’ve got to edit the following file:

sudo nano /etc/fstab

Go to the bottom of the file and add the following line:

/swapfile   none    swap    sw    0   0

Tweak the Swappiness

Let’s tweak how much of the swap the system uses:

sudo nano /etc/sysctl.conf

Insert the following lines at the bottom of the file:

vm.swappiness=10
vm.vfs_cache_pressure = 50

Save your changes. You’re all done!

 

On Page SEO – A Quick Guide For Businesses

When setting up a new website for a business, it can often be intimidating to deal with all the information out there with how to optimize your site to rank higher on Google and increase traffic to your site. So today, we’re going to review some easy steps you can make to get your business website’s SEO started off on the right foot.

Setup URL friendly links

You want to avoid using links like “http://www.eckmandesign.com/blog/?p=123”, but rather use keyword rich urls like “http://www.eckmandesign.com/blog/on-page-seo-for-business/”. If you are using WordPress, it’s dead simple to fix. Just click Settings –> Permalinks and change the default settings to post name.

Post name setting in WordPress

Start with your keyword first in the title

The title tag on pages of your website tells search engines what the page is about. It should be 70 characters or less and include your business or brand name and keywords that relate to that specific page only. Most importantly, lead with the keyword first in your title tag. This gives the page higher weight to the search engines.

Make sure you have a meta description on your pages.

Meta descriptions give the search engines and humans a little bit of information about what your site is about. There is a little bit of debate about how much weight, if any, Google gives to the keywords using in the meta description, but searchers without a doubt will see it as they search. If it’s blank or poorly written it’ll affect the number of visits your site will get. The meta description should be about 150-160 characters in length. If it is too long, Google will shorten it.

Don’t forget header tags

The header tags (<h1>, <h2>, <h3>, etc.) break up the content of each page into sections and lets Google know what each page is about. Your page should only have one <H1> tag. This <H1> tag should be the main topic of your page or post. If you need another heading with the same sized font, use CSS to change the size of the font, don’t keep using <H1> tags throughout the page. The <h2> tags are the sub-headings, and the <H3> tags are the sub-sub headings.

Double check your images for blank title and alt tags

When you add your images don’t skip including some good keywords to to your images titles and and alt tags. These tags help Google find images that are related to your keywords.

Use Out-bound links

When appropriate use out-bound links to link to high quality content. This may seem counter intuitive, but Google looks at this positive factor that demonstrates that your site is a good information resource for its readers as well as associating it with similar sites with related information.

Slow Page Loading Times

No one likes a slow loading page, but did you know that the slowness could be affecting your ranking on Google? It’s true. So go check your PageSpeed using Google’s PageSpeed tool or using GTMetrix. Both will give you a score with what you need to do it fix it. We’ll be covering how to fix many of these issues in the next few posts. So, subscribe, and stay tuned for more info on how to speed up your site.

 

So there it is, a few quick tips on how to boost your site’s ranking on Google and how to optimize your SEO. We hope these helped! Subscribe below and you’ll get an email when we post new guides.