Install Laravel on Ubuntu 16.04 with Apache

In this tutorial, we will explain how to install and run the latest stable version of the Laravel 6.0 PHP Framework on Ubuntu 16.04 LTS (Long Term Support) with Apache2 and PHP 7.4 support.

Laravel is a lightweight and flexible open-source PHP framework solution with an MVC (Model-View-Controller) design structure. Laravel certainly focuses on building scalable and flexible web applications with ease. In addition, it has in-built features like caching, authentication, session management, routing, and unit testing. As a result, it becomes the first choice of many PHP developers.

Prerequisites

Above all, An installed instance of Ubuntu 16.04 LTS is required with a privileged user for installation.

After that, Update your system sources and existing software packages using the following commands.

sudo apt-get update
sudo apt-get upgrade

On the other hand, the following components are needed for Laravel 6.0 LTS.

  • PHP >= 7.2.0
  • BCMath PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Install PHP 7.4 with required Extensions

PHP 7.4 is the latest stable version of PHP released on 28 November 2019. But the default repository of Ubuntu 16 is not providing packages for PHP7.4. So, Add the Ondrej PHP repository to install PHP 7.4 in Ubuntu by using the following command.

sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update && apt-get upgrade

After that, install PHP 7.4 using the below command.

sudo apt-get -y install php7.4

Now, install the additional required PHP Extensions by using the following command.

sudo apt-get -y install libapache2-mod-php7.4 php7.4-bcmath php7.4-json php7.4-mbstring php7.4-xml zip

Meanwhile, when you install PHP packages the Apache webserver packages are installed with them automatically. You can verify that PHP and Apache are installed by using the following command.

sudo php -v
sudo service apache2 status

Install Composer

Composer a dependency manager tool for PHP like node’s npm and ruby’s bundler. We will use it to download and install the Laravel Core and all the required Laravel components.

For download and install the latest version of Composer in Ubuntu 16.04 LTS run the following command.

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer

To verify the install of the composer run the below command.

composer -V
Output:
 Composer version 1.9.2 2020-01-14 16:30:31

Now we have composer installed. So let’s move to install Laravel 6.0 LTS on our Ubuntu 16.04 LTS Server.

Install Laravel 6.0 LTS

Now navigate to /var/www/html directory, which is the public HTML directory for Apache in Ubuntu.

cd /var/www/html

To install Laravel 6.0 LTS using Composer run the create-project command described below.

composer create-project laravel/laravel="6.0" my_webapp
Install Laravel on Ubuntu 16

For testing, you can run it in development mode by navigating to the project directory and then run the below command.

cd my_webapp
php artisan serve

You can see output like the below image.

start-laravel-in-development-mode
Starting Laravel Development Server

When you navigate to the URL you can see the below Laravel welcome page.

Laravel Welcome Page

Configure Apache for Laravel

After installing Laravel, now we configure apache for serving Laravel.

Above all, give the appropriate permissions to the project folder by using the below commands.

sudo chown -R www-data:www-data /var/www/html/my_webapp
sudo chmod -R 755 /var/www/html/my_webapp
sudo chmod -R 775 /var/www/html/my_webapp/storage

In the above commands, replace my_webapp with the name of your project directory.

After that, edit apache’s default site configuration to server our project directory. First of all, open the default site configuration of apache server.

vim /etc/apache2/sites-available/000-default.conf

And then, replace /var/www/html with /var/www/html/my_webapp/public and then save the config file. After that reload the apache server by using the below command to apply the changes.

Laravel-6-Apache-site-config-example
sudo service apache2 reload

Visit http://my_server_ip/ for verifying changes. Replace the my_server_ip with your Ubuntu machine’s IP address.

Laravel 6 on Apache

Conclusion

As a result, we have successfully installed and configure Laravel 6.0 LTS on Ubuntu 16.04 LTS with PHP 7.4 and Apache.

You can leave a comment below if have any questions.

Leave a Comment