Deploying a LEMP Server on Ubuntu 13

Overview

It’s one of the most high performance web servers available today, and it won’t steal all of your system resources for kicks. Although earlier version of Nginx were primarily suited to serving up static content (css, jpgs, gifs, html, js), because it could not handle processing dynamic pages well, more recent version this has all changed.  You’re likely to see an increase in web applications running off of pure Nginx deployments.

This tutorial will walk you through deploying what is called a LEMP server (Linux, Nginx, MySQL, PHP) on Ubuntu 13 based servers. I will only cover the basics for now, but it will be enough to prepare the server for WordPress, for example.

Lab Configuration

For this tutorial, I will only be using a single server with the following configuration. The sizing for a small application, so make sure you add sufficient resources for a production server that fits your expected traffic profile.

Hostname OS Processors RAM IP Address
websrv01 Ubuntu 13.10 2 1 GB 172.30.0.40

Install Packages

The first thing we need to do is install all of the required packages. For this tutorial, we will be installing MySQL, Nginx, PHP5’s FPM and MySQL packages.

  1. Install MySQL
    sudo apt-get install mysql-server mysql-client
  2. When prompted, set Mysql’s root password.
    Set MySQL's Root Password
  3. Install Nginx from repository
    sudo apt-get install nginx
  4. Install PHP
    sudo apt-get install php5-fpm php5-mysql

Configure Nginx

  1. Open the Nginx Default site configuration file into a text editor.
    sudo vi /etc/nginx/sites-enabled/default
  2. Find the following line
    index index.html index.htm;
  3. And add index.php to it.
    index index.html index.htm index.php;
  4. Enable PHP processing by finding the following lines:
    #location ~ .php$ {
            #       fastcgi_split_path_info ^(.+.php)(/.+)$;
            #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            #
            #       # With php5-cgi alone:
            #       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;
            #}

    And modify them to look like this

    location ~ .php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+.php)(/.+)$;
            #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            #
            #       # With php5-cgi alone:
            #       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;
            }
  5. Save changes and exit the text editor.
  6. Reload the default site’s configuration file to apply the changes.
    sudo service nginx reload

Configure MySQL

  1. Run the secure installation script.
    sudo mysql_secure_installation
  2. When prompted, enter the Root password you set during the installation.
  3. When prompted, type ‘N’ to not change Root’s password.
  4. Accept all defaults to remove anonymous users, disallow remote root login, and remove the test database.

Test PHP on Nginx

  1. Navigate to the default website’s root directory.
    cd /usr/share/nginx/html/
  2. Delete the index.html file.
    rm index.html
  3. Create a file called index.php.
    vi index.php
  4. Add the following to the file.
    <?php
        phpinfo();
    ?>
  5. Save the file and exit the editor.
  6. Open a web browser on your desktop and navigate to your web server.
  7. If all is well, you should see the following in your web browser.

Conclusion

The web server is now ready to serve dynamic content. Download WordPress’ latest release as a test to ensure everything works as expected.

Keep in mind security and performance optimizations weren’t done. If this server is expected to run publicly run production applications, it’s recommended you research how to harden it. I’ll cover that in a future tutorial, but for now it remains out of scope of this one.

For information on how to set firewall policies for this web server, see IPTables Policies for Ubuntu 13 Web Servers.

If you have any questions, suggestions or feel I’ve missed something important, make sure you leave a comment below.