How to set up LEMP Server on Ubuntu 11.04

This tutorial will help you to set up a web server using Ubuntu 11.04 Natty, nginx, php-fpm and MySQL.

Why nginx

Nginx is a web server and a competitor to Apache. So why using nginx?

  • Nginx uses a lot less system resources than Apache;
  • Apache spawns a process for each static content request and it runs until the client finishes transfer which leads to resource shortage problems in case of big amounts of clients with slow connection. Nginx doesn't have this problem;
  • Nginx has an amazing flexibility, powerful cache solutions and it's even possible to change content on-the-fly with embedded Perl inside of the config files. Nginx can be configured to reflect many kinds of application-level DDoS.

Before getting started

All of these steps are completed using a fresh install of Ubuntu 11.04 Natty, after applying all the available updates. Although these steps should work exactly the same, there's no guarantee that your set up will work at once.

If you have Ubuntu 10.04, you cannot install php-fpm from aptitude, you'll have to compile from the PHP 5.3.x source.

Also, all of these commands are being run as root. But logging in as root isn't recommended, so you'd better create a user and use sudo to execute root commands.

Installing nginx

First of all, we should get the server running, to make sure we can access it through a web browser. Now, the nginx version in the Ubuntu repositories is grossly out of date. It's better to run the latest stable release, and to do that we have to add a PPA to Ubuntu. These commands are taken from the nginx wiki.

We start with installing python-software-properties. This will give us the ability to add a PPA to our repository sources.

apt-get install python-software-properties

Once we have python-software-properties installed, we can add the PPA to install the latest version of nginx. The PPA can be installed by running this:

add-apt-repository ppa:nginx/stable

That will add the PPA to our sources list, so now we have to tell apt to refresh the sources list so it knows about the new PPA we just added.

apt-get update

Now we can install nginx as we usually would.

apt-get install nginx

Start nginx by typing /etc/init.d/nginx start, and navigate your server address in your browser. If you don't know your server IP address, run the ifconfig command and it will list all of the network devices on your server. Look for inet addr: and that will be your server public IP address.

If you can see a "Welcome to nginx" page, then your server is running. If you can't see this page, make sure your server isn't being blocked by a firewall, and make sure nginx is running.

Installing PHP (php-fpm)

For PHP we're going to use php-fpm, which stands for PHP FastCGI Process Manager. On Ubuntu 11.04, it's quite easy to install php-fpm. On older releases (notably 10.04), you had to compile PHP from source, but with 11.04 all it takes is a simple apt-get command.

apt-get install php5-fpm

Once PHP is installed, we should install some common libraries to make sure most web applications work right away. You could do this later, as each library is needed, but it would be easier just to install some common ones now.

apt-get install php5-cgi php5-dev php5-curl php5-gd php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ps php5-pspell php5-recode php5-snmp php5-xmlrpc php5-xsl php5-common php5-mysql

Now we need to start php-fpm. Do this by running:

/etc/init.d/php5-fpm start

Though we have PHP installed nginx doesn't know about it yet. We need to add a section of code to our nginx VirtualHost in order for PHP to run. Open up the /etc/nginx/sites-available/default file in an editor (For example GNU nano so you will type nano /etc/nginx/sites-available/default), and delete everything inside there (hint: in nano press control + k and it will erase an entire line). Once the file is empty, put this as its contents:

server {
  listen 80;
  server_name localhost;
  root /var/www;
 
  location / {
    index index.html index.htm;
  }
}

That is a basic Virtualhost in nginx. But we're not done yet. We need to tell nginx that we want to use PHP. To do so, we need to add a section under the location block, then our file will look like this:

server {
  listen 80;
  server_name localhost;
  root /var/www;
 
  location / {
    index index.html index.php index.htm;
    location ~* \.php$ {
      include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      }
  }
}

What this does is tell nginx to pass all PHP files to php-fpm to be processed, and then php-fpm will give nginx some static HTML in return. We've also added index.php to the list of index files. It's recommended to put index.php after index.html so you can override any PHP-based app with a static holding page if necessary.

We've specified /var/www as our webroot, but that directory isn't there by default. We'll have to create it by running:

mkdir /var/www

After we make our config changes, we need to reload nginx. Because we only made some virtualhost changes, we can reload nginx instead of restart.

/etc/init.d/nginx reload

Note: because our new webroot is an empty folder, the welcome page will be replaced with a forbidden error. This is because we don't have directory listings enabled, which is more secure. If we were to place an index file in our webroot, the forbidden error would disappear.

Installing MySQL Server

Now we have to set up the MySQL database server. MySQL is required by most web applications (notably WordPress), so we almost have no choice but to install it. MySQL is the easiest of all to install, with only one command.

apt-get install mysql-server mysql-client

It will prompt you for a root password during the installation. It's recommended to use a randomly-generated string, because this user will have privileges to absolutely everything on the MySQL server, and if someone was to gain access it would be catastrophic.

Once the installation finishes, start MySQL by running:

start mysql

Testing our installed LEMP

When everything is installed and running you've got to make sure it's all working. We'll continue just using the command line, instead of going into SFTP, etc. First, let's make a PHP file in our webroot.

touch /var/www/test.php

Then open it in a command-line editor (nano, Vim, Pico, etc.)

nano /var/www/test.php

Inside the test.php file, put:

<?php phpinfo(); ?>

Now open the server's URL in your browser, and append /test.php (ex. http://123.456.7.89/test.php). You should be greeted with a page that has all the details of your PHP installation.

If we search for "mysql" on the test page, we should get several hits. If we find a heading that says "mysql", that means MySQL is installed properly and connected to PHP. You should see a section similar to the one below:

phpinfo output example

Now we have a fully-operational LEMP server running under Ubuntu.


Source: based on Alex Blackie`s tutorial





  1. sasi
    2014-10-28 08:02:23
    mangatholi

Got a comment?

captcha =

Categories

  1. System (20)
    1. FreeBSD (5)
    2. Linux (9)
  2. Email (2)
  3. DNS (2)
  4. Databases (1)
  5. WebServer (27)
 
Copyright © 2012-2015 HowToUnix - *nix Howtos and Tutorials
All Rights Reserved.