Home / How To / How to install Laravel PHP Web Framework on Ubuntu 20.04

How to install Laravel PHP Web Framework on Ubuntu 20.04

Laravel is a free, open source PHP framework that implements the MVC (Model-View-Controller) design pattern. Taylor Otwell created it in 2011 as an attempt to provide an advanced alternative to the CodeIgniter (CI) framework.

In 201

1, Laravel released version 1 and version 2. The latest version 8.x comes with more and improved features such as versioning schedule, support policy, blade component tags and enhancements, floating string operations, route model binding enhancements, etc.

This tutorial shows you how to install the latest Laravel version 8.x with the Apache web server on Ubuntu 20.04 Server. We show you two different ways to install Laravel web framework on the Linux Ubuntu system.


  • Ubuntu 20.04 system
  • 2 GB or more RAM
  • Root privileges
  • Understand the basic usage of the Debian / Ubuntu system

What should we do?

  • Install Apache Web Server
  • Install and configure PHP 7.4
  • Install Composer PHP package management
  • Install the Laravel Web Framework
  • Set up Apache Virtual Host for Laravel Project
  • Testing

Step 1 – Install the Apache Web Server

First, we install the Apache web server on the Ubuntu 20.04 system and set up the UFW firewall which only allows incoming connections for certain services including SSH, HTTP and HTTPS.

Update all available archives on your system and install the Apache web server using the apt command below.

sudo apt update
sudo apt install apache2

When all installation is complete, start the Apache service and add it to system boot.

systemctl start apache2
systemctl enable apache2

Now check the status of the Apache service with the following command.

systemctl status apache2

Below is the result you get.

Install Apache Web Server

As a result, the Apache service is up and running.

Then add the SSH, HTTP and HTTPS services to the UFW firewall with the following command.

for svc in ssh http https
ufw allow $svc

Now enable UFW Firewall Services.

sudo ufw enable

Write ‘Y‘to continue, and the UFW firewall has been activated.

Set up UFW firewall

Then open your browser and type your server’s IP address into the address bar.

And you get the default page for Apache index.html.

Apache default index.html

As a result, the installation of the Apache web server and the UFW firewall configuration have been completed.

Step 2 – Install and configure PHP 7.4

In this step, we will install and configure PHP 7.4 for our Laravel installation. To install Laravel 8.x, you must have at least PHP> = 7.2.5 on your system. And by default, the official Ubuntu 20.04 repository provides PHP 7.4 packages.

Install PHP 7.4 package using the apt command below.

sudo apt install libapache2-mod-php php php-common php-xml php-gd php-opcache php-mbstring php-tokenizer php-json php-bcmath php-zip unzip

When all the installation is complete, go to the ‘/etc/php/7.4/’ directory and edit the ‘php.ini’ configuration for the Apache2 web server.

cd /etc/php/7.4/
vim apache2/php.ini

Deselect the ‘cgi.fix_pathinfo’ option and change the value to ‘0’.Advertisement


Save and close.

Then restart the Apache service to apply the new configuration ‘php.ini’.

systemctl restart apache2

As a result, the basic configuration of PHP 7.4 has been successfully completed.

Configure PHP 7.4 on the Ubuntu system

Step 3 – Install Composer PHP Packages Management

Composer is a dependency manager for PHP. It is an application-level package manager for PHP that allows you to download additional PHP dependencies and libraries to your PHP project.

In this tutorial we will use Composer to install Laravel, and we will install Composer by downloading the composer’s binary from the official composer website.

Download the binary file to the composer and move the file to the directory ‘/ usr / local / bin’.

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Then check the composer version with the following command.

composer --version

Below is the result you get.

Install Composer on Ubuntu 20.04Advertisement

As can be seen, Composer PHP Packages Management has been installed in the Ubuntu system.

Step 4 – Install Laravel 8.x on Ubuntu 20.04

For this step, we will show you two different ways to install Laravel web framework. We install Laravel via Laravel Installer and install it via Composer.

– Install Laravel via Laravel Installer

This type of installation is best suited for Laravel Developer. Because when using Laravel Installer, you can install Laravel anywhere with a non-root user.

This section will install the Laravel web framework with the non-root user named ‘hakase’.

You can add the user and set the password with the following command.

useradd -m -s /bin/bash hakase
passwd hakase

Then log in to the “hakase” user.

su - hakase

Then install the Laravel Installer package using the composer command below.

composer global require laravel/installer

When all installation is complete, you will get the following results.

Install Laravel Installer

As can be seen, all packages have been installed in the directory ‘~ / .config / composer’.

Next, we need to add the “bin” directory to the PATH environment through the ~ / .bashrc configuration.

Edit the ~ / .bashrc configuration with vim-editor.

vim ~/.bashrc

Paste the following configuration to the end of the line.

export PATH="$HOME/.config/composer/vendor/bin:$PATH"

Save and close.

Now load the new version of the ~ / .bashrc configuration and check the $ PATH environment variable.

source ~/.bashrc
echo $PATH

Below is the result you get.

Add binary path for Laravel Installer

The ‘bin’ directory for composer packages has been added to the $ PATH environment variable. And as a result, you can use the ‘laravel’ command to start and create a new project.

As a result, you can now use the ‘laravel’ command to create a new project.

To create a new Laravel project named ‘blog’, run the laravel command below.

laravel new blog

Now you get the “blog” Laravel project catalog.

Create a new Laravel project with Laravel InstallerAdvertisement

To make it available to the Apache web server to access your Laravel project directory, change the project directory group owner to the ‘www-data’ group and make the ‘storage’ directory writable.

sudo chgrp -R www-data /home/hakase/blog
sudo chmod -R 775 /home/hakase/blog/storage

As a result, Laravel is installed with the Laravel installer, and you are ready to configure the Apache virtual host for your Laravel project.

Install the Laravel project

– Install Laravel Via Composer create project

Another way to install Laravel’s web framework is to use Composer PHP Packages management.

Go to the directory ‘/ var / www /’ and create a new Laravel project called ‘blog’ with the command ‘composer’ below.

cd /var/www/
composer create-project --prefer-dist laravel/laravel blog

Now you get the new project catalog with the name “blog”.

Create Laravel Project with Composer

Then change the ownership of the project directory ‘blog’ to the user ‘www-data’ and make the directory ‘storage’ writable.

sudo chown -R www-data:www-data /var/www/blog
sudo chmod -R 775 /var/www/blog/storage

As a result, you have installed the Laravel webframe using the Composer command.

Configure Laravel Project

Step 5 – Configure Apache for Laravel Project

In this step, we set the Apache virtual host for the Laravel project.

Now go to the directory ‘/ etc / apache / sites-available’ and create a new virtual host configuration ‘laravel.conf’ using vim-editor.

cd /etc/apache2/sites-available/
vim laravel.conf

Change the domain name and web directory with your own and paste the configuration into it.

    ServerName hakase-labs.io

    ServerAdmin [email protected]
    DocumentRoot /home/hakase/blog/public

    Options Indexes MultiViews
    AllowOverride None
    Require all granted

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close.

Then enable the Apache rewrite module and Laravel’s virtual host configuration using the following command.

sudo a2enmod rewrite
sudo a2ensite laravel.conf

Then test the Apache configuration, make sure there is no error and restart the Apache service.

apachectl configtest
systemctl restart apache2

As a result, the virtual Apache host configuration for Laravel has been completed.

Set up Apache Virtual Host for Laravel

Step 6 – Testing

Open your browser and enter the server’s IP address or domain name in the address bar.


Below is the result you get.

Laravel's standard page

As a result, the installation and configuration of Laravel with the Apache web server has been completed.


Source link