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

Wordpress DigitalOcean Nginx

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.


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.


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;

That completes the database user creation for WordPress.

Download and Install WordPress

Grab the latest version of WordPress and extract the contents:

cd ~
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;


        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 =

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:


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.vfs_cache_pressure = 50

Save your changes. You’re all done!


Leave a Reply