Installing Laravel 5.2 & PHP 7 on Ubuntu 16.04 for Apache 2.4.10

Laravel 5.2  is a very popular open source PHP framework aimed at easy development of applications. If you are looking for a new PHP framework to try, you should give Laravel a try. The following guide will allow you to run Laravel on a Ubuntu 15.10 based Apache server.

Pre-Requisities:

Before proceeding with the installation, its always a good idea to make sure your sources and existing softwares are all updated.

sudo apt-get update
sudo apt-get upgrade

Installing PHP7:

We can install PHP 7 and the Apache PHP module as follows:

sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mcrypt php7.0-mbstring

Then restart Apache:

systemctl restart apache2

Installing Composer:

Composer, the now de facto dependency manager, is a must-use in the PHP ecosystem. It will manage the dependencies – pull in required libraries, dependencies and manage them all for you. Composer also has a main repository: Packagist. Combined, Composer and Packagist make the experience similar to npm or Ruby’s Bundler.

In order to use Composer, install zip/unzip:
sudo apt install zip 

Also, we will need Git installed on our system:

apt install git

Okay, now – let’s go ahead and actually install Composer:
cd ~
curl -sS https://getcomposer.org/installer | php

This will create a file called composer.phar in your home directory. This is a PHP archive, and it can be run from the command line.

We want to install it in a globally accessible location though. Also, we want to change the name to composer (without the file extension). We can do this in a couple steps.

Let’s make it an executable that we can run as composer:

sudo mv composer.phar /usr/local/bin/composer

Alright, alright, alright! We should be able to see a list of Composer commands:

composer

Saw the list? Great. Let’s move on to Laravel.

 

Install Laravel & Quickstart Tutorial:

To install Laravel, we need to install Composer first. It is a tool for dependency management in PHP that allows you to package all the required libraries associated with a package as one. To install Laravel and all its dependencies, Composer is required. It will download and install everything that is required to run Laravel framework. To install Composer, issue the following commands.

MySQL

First, let’s set up our database. It will only take a second. We’re going to use MySQL – remember that password you saw after you logged in?

Login to MySQL on the command line – using your saved password:
mysql -uroot -p

You will be prompted for your password. Once in – enter in the following commands:

Once logged in to MySQL – enter in the following commands:
mysql> CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'laravel';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'laravel'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE laravel;
Query OK, 1 row affected (0.00 sec)

You can name your user, password, and database anything you want – we are using laravel for the sake of being easy.

Laravel & Stream Example Project

To make it as easy as possible to get started, let’s just clone the completed Stream-Laravel example app.

Please note that you do not need to copy and paste any code, or create any files. Rather, this is intended to help you learn while you follow along with the process.

Follow the steps below to add Laravel, along with the Stream-Laravel example application:

First, move to /var/www/:


cd /var/www/

You may want to use sudo with the following commands to avoid any permissions errors. Check out this DigitalOcean how-to and search for “permissions”.

Next, clone the Stream-Laravel-Example project as “quickstart”:


git clone https://github.com/GetStream/Stream-Laravel-Example quickstart
cd quickstart
composer install

Okay, cool. If all went well – you just installed a bunch of dependencies inside of vendor.

Note: The Stream-Laravel example application is based off of the Laravel quickstart-intermediate project. Please take a look at the additional references below for more information.

Additional References:
GitHub Repository
Laravel Quickstart Documentation

Set Database Credentials

One thing we need to do is set our new application’s database credentials – which happens in a .env file.

Copy .env.example, which is located in the root directory, to .env:


cp .env.example .env

Next, let’s add our database credentials. For the sake of an easy example we use “laravel”:


DB_HOST=127.0.0.1
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=laravel

Now let’s run php artisan migrate.

If all goes well, you should see something like:


Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2016_02_04_041533_create_tasks_table
Migrated: 2016_07_12_182728_create_follows_table

Let’s also generate and set an encryption key:

php artisan key:generate

Create an Apache Virtual Host for your project.

cd /etc/apache2/sites-available
sudo nano myapp.conf

Have the contents of the file match what’s below.

<VirtualHost *:80>
  ServerName myapp.localhost.com
  DocumentRoot "/home/vagrant/projects/myapp/public"
  <Directory "/home/vagrant/projects/myapp/public">
    AllowOverride all
  </Directory>
</VirtualHost>

Save the file, then continue below.

cd ../sites-enabled
sudo a2ensite myapp.conf
sudo service apache2 reload

Fixing Permissions

If you’re running a virtual machine under Vagrant, you may want to change the user and group to avoid permission issues.

To do this:

cd /etc/apache2
sudo nano envvars

Change the lines below to contain the desired user and group

export APACHE_RUN_USER=vagrant
export APACHE_RUN_GROUP=vagrant

Save the file and restart apache.

sudo service apache2 restart

One thought on “Installing Laravel 5.2 & PHP 7 on Ubuntu 16.04 for Apache 2.4.10

  1. Instead of manually install the dependencies and packages, I would recommend using Cloudways Laravel web hosting for installing Laravel on a PHP 7 based server to save time and efforts that would have otherwise gone into manual installation and configuration.

Leave a comment