Varun Yellina

Install LEMP stack on Ubuntu 14.04

Steps to install LEMP stack.

Ubuntu 14.04 LTS is out and it’s now time to get our webserver running on it.


Nginx

(Pronounced EngineX)

With 14.04 Trusty Tahr, nginx has now finally been included in the official Ubuntu repositories. Installing it is a breeze.

$ sudo apt-get install nginx

Nginx configuration lies at /etc/nginx/nginx.conf which you’ll be needing later.

If you want to host your domain, then we need to setup a virtual hosts file. Note that this can be repeated if you want to host multiple sites.

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mydomain.com

$ sudo nano /etc/nginx/sites-available/mydomain.com

Use nano or your any of your favourite editor and edit the virtual host file to reflect the following. Of course you should modify root to the location of your website. Also, server_name should be the same as the domain with which you will be accessing the website.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
  listen            80;
  server_name       mydomain.com www.mydomain.com;

  root              /home/user/mydomain.com/public;
  index             index.php index.html index.htm;

  location / {
    try_files       $uri $uri/ =404;
  }

  location ~ \.php$ {
    fastcgi_split_path_info   ^(.+\.php)(/.+)$;
    fastcgi_pass              unix:/var/run/php5-fpm.sock;
    fastcgi_index             index.php;
    include                   fastcgi_params;
  }
}

Now we enable the domain by using a symlink of the file from sites-available to sites-enabled,

$ sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com

Restart nginx for the changes to take effect.

$ sudo service nginx restart

Note that I’ve configured my root directory as /home/user/mydomain.com/public/ . I find this easier to navigate.

Next, we set permissions on the root directory

$ sudo chown -R www-data:www-data /home/user/mydomain.com

$ sudo chmod -R 755 /home/user/mydomain.com

Also if you would like to be able to write to the root directory of your webserver without getting permissions issue - we’ll add your user account to the group www-data.

$ sudo adduser $USER www-data

PHP

$ sudo apt-get install php5-fpm

Next we have to edit /etc/php5/fpm/php.ini

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

and uncomment the following

cgi.fix_pathinfo=1

MariaDB

This is simple as this

$ sudo apt-get install mariadb-server

You will be prompted to enter a password for MariaDB root user. Remember it.

To start, stop mariadb

$ sudo service mysql start

$ sudo service mysql stop

You’re good to go!