Installing LEMP on Ubuntu 14.04

LEMP is the stack combination for Linux (Ubuntu in this case), nginx, MySQL, PHP. This is a very efficient combination for modern web servers. More and more web applications require or are optimized for LEMP.

Keep in mind, LEMP is a replacement stack for LAMP (Linux, Apache, MySQL and PHP). There are advantages to both. We assume you have a need or a want to install LEMP, and have a clean server image to do it on (one without Apache running on it).

The instructions were verified on InterServer`s OpenVZ VPS Hosting service, utilizing Ubuntu 14.04 64-bit minimal version.

Requirements:

  • Tested on InterServer’s OpenVZ VPS Hosting with Ubuntu 14.04 64-bit (instructions are for minimal distribution, but should work for regular distribution as well).
  • Putty or similar SSH client
  • root login and password or an account capable of sudo
  • nano or similar text editor installed on the server.
  • Make sure Apache is not installed (they can co-exist, with Apache behind nginx, however, that is out of scope with these instructions).

Installation


First, will run an update:

  • sudo apt-get update

Install nginx:

  • sudo apt-get install nginx

Verify the server installed

Using your web browser, type the URL or IP Address of your server. If your site does not come up with the name, this could indicate a DNS issue, so try the IP address that was listed in your welcome email from your host, instead.

You should see this screen:

Install MySQL

  • sudo apt-get install mysql-server

Note: The mysql-server install caused us some problems, which were resolved by running sudo apt-get install bsdutils, then sudo apt-get --reinstall install mysql-server. If you are curious, it was the "logger". I left this in the video, since others may experience the same error.

Check Mysql's status:

  • sudo service mysql status

Now, will allow mysql to create the database structure:

  • sudo mysql_install_db

Secure your mysql installation by removing unnecessary elements:

  • sudo mysql_secure_installation

Install PHP

Will install php5-fpm (FastCGI Process Manager) and php5-mysql (Bridge between PHP and MySQL):

  • sudo apt-get install php5-fpm php5-mysql

We need to configure a few things for everything to work correctly:

First, will configure fpm:

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

There is a potential security problem with one of the settings. If a script is not found, a similarly named script may be executed. This requirement is also documented in /etc/nginx/sites-available/default, which will work on in the next step. Let's make the change:

We are trying to find a line that says: ;cgi.fix_pathinfo=1. We can find it by hitting Ctrl-W and searching for cgi.fix.

We will remove the semicolon (if present), and change it to:

  • cgi.fix_pathinfo=0
  • ctrl-x and y will save the file.

Restart PHP FPM:

  • sudo service php5-fpm restart

At the moment, we have nginx installed, php-fpm and Mysql are configured. We just need to finish nginx so everything is connected.

Let's open the default sites available file:

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

Make the following changes:

Find the line that says: index index.html index.htm; and change it to:

  • index index.php index.html index.htm;

Find the line that says: server_name localhost; and change it to:

  • server_name yourdomain.com_or_IP;

There is also some functionality that is currently disabled with a # and we want to change that. Let's enable them by erasing the #:

#error_page 404 /404.html;

#error_page 500 502 503 504 /50x.html;

#location = /50x.html {

# root /usr/share/nginx/html;

#}

A few more:

#location ~ .php$ {

# fastcgisplitpath_info ^(.+.php)(/.+)$;

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

# fastcgi_index index.php;

# include fastcgi_params;

#}

ctrl-x and y will save the file.

Restart nginx, so the changes take effect:

sudo service nginx restart

Let’s test, to make sure everything is working:

sudo nano /usr/share/nginx/html/test.php

Will use phpinfo() to show our configuration. Write the following line:

  • <?php phpinfo(); ?>
  • Close out and save Ctrl-X and y

Use your browser to view test.php (yourdomain.com/test.php, or yourip/test.php)

  • If you received the same page as the picture above, your LEMP server is ready.

Let’s clean up the file we created for testing:

  • sudo rm /usr/share/nginx/html/test.php

Your LEMP installation is finished. You may need to install additional modules in the future, according to your needs (check requirements for any php scripts you may be installing).


This website is supported by our affiliation with web hosting companies. We encourage you to visit our friends at Interserver. They really do offer $6 per month VPS Hosting. Linux, windows and cpanel available, have super fast service, and they care about their customers!

See more articles in: Instructions, Videos, Ubuntu, LEMP, nginx

comments powered by Disqus