Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone

There are lot of resources on line that you will find explaining how to install these amigos on Ubuntu. This will be another guide explaining it again. There will be slight differences on how I do it especially where I’d like to setup my Virtual Hosts, log directories. Moreover I’m trying to explain everything from scratch on pure Ubuntu installation so that you can follow along without facing many issues. I will be using MariaDB instead of MySql as many websites, developers now moving towards it due to uncertainty of MySql and rate of its improvements bug fixes. In most on line tutorials you will find Apache instead of NGINX so I will be using NGINX as it is gaining its popularity very quickly recently. Laravel has been dominant over the past few years among PHP framework due to its simplicity and ability to attract the attention of developers who had ASP.NET MVC background but with little experience in PHP (like me :)). So in overall I think this is well selected technologies to develop really good web applications and worth understanding how to install and set up them from one single tutorial. In this first part we will look at how to install NGINX PHP and MariaDB and in the next part we will install Laravel and configure NGINX.

Assumptions

  • I believe you already have Ubuntu (preferably latest version, I will be using v14.04) installed in your machine or on VirtualBox.
  • You have selected your favourite editor (nano vim) as we may need to edit few configuration files.
  • You must know little bit about Ubuntu directory structure and basic commands.
A sip of Coffee with Laravel and Backbone - Book

1. Installing MariaDB

Please note that I assume you don’t have MySql is installed in your system. If you already have then best option is to use it and skip this section.
First install software-properties-common package using following command. If you’re not logged in as root you may need to execute it as super user.

Then we need to add following public key into trunsted key list so that the package we’re going to install next, will be authenticated properly.

Next you could either include the MariaDB repository into main repository list or the way I prefer is to create new file under /etc/apt/sources.list.d/. So let’s do that way for a change.

Create mariadb.list file under /etc/apt/sources.list.d/ using your favourite editor, then copy following content and save.

Note: Depending your location you might find different servers to get MariaDB installation. In that case best option is, first check with MariaDB download page and then selecting your OS, release and download mirror closer to you.

Now you can update the packages and then install MariaDB.
During the installation it will prompt for root password for the database server. You have to give strong password. (In this tutorial case I will just hit ENTER to set no password for root user.)
Ok now we have completed installing MariaDB and you can verify by log in using following command. You may need to give the password you set-up during the installation.

As you can see even though we installed MariaDB there will be no much difference to usage of it compared to MySql.

Note: We may need to install additional libraries when we actually use the database from Laravel or PHP. We will install them later.

2. Install NGINX

It’s time to install NGINX web server. As in previous installation we will first add new repository file. Create /etc/apt/sources.list.d/nginx.list file and add following content.

Then update and install NGINX (It’s better to run these two commands seperately so that we can add the public key to trusted list before installing NGINX)

When you get following error message at the end of update command fix it by adding the key to trusted key list as before.

Then again run update and check, there shouldn’t be any errors. After that execute sudo apt-get install nginx to install NGINX.

Note: You can now verify package manager whether it gets the installation from new repository by executing sudo apt-cache policy nginx command before installing.
It should show you the available versions and which one is the candidate.

Once the NGINX is installed you can verify it by opening http://localhost on your browser. You should see NGINX welcome page.

Testing NGINX

Testing NGINX

3. Install PHP

Finally it’s time to make our NGINX web server PHP enabled. What does this mean? Right now NGIX web server can serve normal HTTP content such as html pages, images etc. But it doesn’t have capability to process (or forward to somone knows how to process) PHP files. All web applications these days heavily use scripting languages such as PHP to generate dynamic content and/or access database to store/fetch data. Since we’re planning to install Laravel we must have support for PHP.

To understand this just create test.php file in /usr/share/ngix/html/ folder. (This is the default web root just after you install NGIX, later we will see how to change this location).

Now open the browser and access http://localhost/test.php. You will see browser prompt this file to be saved, as it doesn’t understand what else to do with it. While keeping this file in this location we will install PHP, so after that we can access same URL and see the different behaviour.

Note that in here we are installing three packages. Last one is PHP crypting library which is used by Laravel. php-fpm (FastCGI Process Manager) is the one currently used in most web servers to handle PHP. php5-cli is the command line interface package.

Now we have PHP libraries installed. Still NGIX doesn’t know anything about how to pass the PHP processing to new PHP processors we just installed. For that we need to do add few configuration changes. First open /etc/php5/fpm/php.ini file and locate the line with cgi.fix_pathinfo=1 and change it to cgi.fix_pathinfo=0 and uncomment the line. This is to inform PHP not to execute similar named file when it cannot find exact match for requested file.

Then we need to enable mcrypt extension. Just execute following command to do that.

Then restart php5-fpm service so it will know about changes made.

Now open /etc/nginx/sites-available/default file and look for commented section starts with location ~ \.php$ {. Replace that entire block (within { and }) as follows.

This is the way we inform that when the requested file is with php extension then use php5-fpm to process it. Save the file and restart NGINX again.

Now try to access previous url (http://localhost/test.php) once again on browser. This time you should see PHP information.

Testing PHP

Testing PHP

 

Note: There is one more library (php5-mysql) to be installed, and this is required when you access database from PHP. But I’d like to do it when it actually needs it, that is when we install Laravel and then accessing database. Because that’s how we learn things trial and error. That’s how you will remember why we di certain things instead of blindly install everything by looking at an online resource. I would recommend you install all these on Ubuntu minimal installation. www.tutewall.com site is installed on such VM. By doing that not only you keep the machine clean with only necessary software to run, but also learn to find solutions/ reasons behind them to most problems.

Ok that’s all for Part I – How to Install NGINX, PHP, MariaDB and Laravel on Ubuntu. We now have working environment with NGINX web server and it supports PHP, we have our database servcer MariaDB installed. In Part II we will see how to install Laravel, install any additional packages required to use Laravel, set up folder structure and configure NGINX to use Laravel.

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone