Installing Laravel 5.2 on Ubuntu 16.04 and Apache2

This post will document how I installed Laravel 5.2, on Apache2 on an Ubuntu 16.04 server. Will will also install MySQL, as we will need a database, and PHP which is required by Laravel. This will be the starting point of most of my posts, so if you’re following along from scratch…this is “scratch!”

First thing you need, of course, is the Ubuntu 16.04 server, with an SSH connection. Follow these excellent instructions and get yourself sorted out with one. Make sure you also give the server a static IP address (step 8., in the linked instructions). Come back when you’re done.

Welcome back! Lets get started.

Connect to your server now, using SSH, on your terminal.

1. Install Apache2 Web Server

The Apache server will run on Ubuntu and allow us to host websites. We will need to use “sudo” so that we have the correct permission to install it. Enter your user’s password when it asks for it. Run the following commands:

If you enabled the UFW firewall, then you must add a rule to allow traffic to the Apache2 server by running the following command:

Now use your web browser and test the Apache installation. Do this by going to the server’s IP address. In my case, this was 192.168.1.147. Insert your server IP address, of course, and not mine! 😛

url

If all is well, your browser will load the default “It Works!” page. Well done!

apache_hello

While we’re here, let’s enable mod_rewrite that Laravel will need later. Run the following commands in your terminal:

Upwards and onward!

2. Install MySQL server

Although Laravel 5.2 is supports many different types of database, I will be using MySQL for all my posts. If you are going to use a different type of database, then feel free the skip this section.

Go back to your terminal connection and run the following command:

When you are asked to set a MySQL root password, please do so. Don’t leave it blank.

3. Install PHP

Laravel 5.2 requires the following packages installed:

  • PHP >= 5.5.9
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension

Run the following command to make it all happen:

We also included php7.0-zip  as this will be required by Composer, and
php7.0-xml which is required by PHPUnit (required to create a new Laravel project).

4. Install Composer

Laravel uses Composer to handle all its dependencies, so we need to install this next, by running the following command:

Note that this is for a global install. If you want to install it locally, just follow these instructions.

5. Install Laravel

We will use Composer to get Laravel, then add it to the PATH, so we can use Laravel commands anywhere. Run this command now:

And add it to the PATH:

If for some reason your installation doesn’t have  ~/.bashrc, you can use ~/.bash_profile  instead.

Restart apache now with:

6. Create a Laravel Project and configure

Now in order to create a new Laravel project, just use the laravel new command, followed by the directory name. For example, laravel new monkey will create a new Laravel project, in the directory monkey . Let’s create a new project now at  /var/www/html/ .

If you are hit with a permissions problem, make sure that you have correct permission to use the directory ( in this case html ).

We now have to make sure that a couple of directories, /storage  and /bootstrap/cache  are accessible by Apache2. We do this by running the following command:

Make sure you change “project” to whatever you named your own project.

7. Apache Virtual Host

To get Apache to serve up the correct page to your browser, we need to create a Virtual Host for our Laravel project. First thing to do is to go to the depth of Apache and copy the default configuration file, like so:

Use your favorite text-editor, I will be using VIM, and open up the copy you just made:

Now edit your copy of project.conf as below. If you are following along with VIM, then you need to press “i” to start editing the file. Note that I have removed all the comments just to make it clearer:

Again, if you are using VIM, click “esc” (the escape key) to come out of insert mode, then click :wq (colon w and q) to save the changes to you have made and quit VIM.

A quick note regarding some of the lines above. ServerName  is what you will type into the browser to reach your Laravel project’s first page. If you are not using a domain for this, then you can put whatever you want as the ServerName. If you do have a domain, then use that instead. DocumentRoot  and Directory are set with the structure that I have been using in this post, if yours differ, then please adjust as appropriate.

Now enable this new virtual host, disable the default one and reload Apache2:

For those who are not using a registered domain name, a final step is required. We need to change our computer’s host file so that entering http://local.project.com will tell our browser to go to our server, and not look for project.com on the internet.

On my Windows 10 machine, the hosts file is at C:\Windows\System32\drivers\etc\hosts . Once there, we’re going to add our server’s IP address (set when we were installing Ubuntu at the very beginning of this post) and the value we set for ServerName in the virtual host conf file (earlier in this step) to it:

The hosts file tends to have some crafty security added to it, so make sure you running your text-editor (notepad etc.) as administrator to edit it.

Now go back to your browser, enter the address you gave your project, and view the default index page in all its glory!

2016-08-03 12_59_22-Laravel

This entry was posted in Dev, Laravel, PHP. Bookmark the permalink.

7 Responses to Installing Laravel 5.2 on Ubuntu 16.04 and Apache2

  1. Antonio says:

    Awesome, thank you for this tutorial!
    Everything worked until the last part, I had to execute the command sudo chmod -R 775 /var/www/html/ to stop showing http error 500.
    Thanks again!

  2. Clint says:

    This worked perfectly! I was having trouble finding installation and configuration for my AWS Ubuntu EC2 instance. The only thing I would add is “source .bashrc” after adding composer to PATH. Thanks for the post!

  3. Thank you! Everything just worked fine!
    I’m using PHP 7.1 on Bash on Ubuntu on Windows and with some adjustments worked perfectly!

  4. Michael says:

    Thanks!

  5. Vinny says:

    I had to set two more permissions for it to work.

    root@newcube:/var/www/html# sudo chown -R :www-data project

    root@newcube:/var/www/html/project# sudo chmod -R 775 storage

Leave a Reply

Your email address will not be published. Required fields are marked *