Setup Laravel On Ubuntu with Apache

Hi Friends, in this article we are going to install Laravel Framework with Apache on Ubuntu 20.04 LTS. It will give you feelings of production environment while developing your application in the local environment.

As you all know Laravel is a powerful web application framework. At the time of recording this video, Laravel’s latest version is 8.13, and Laravel 8.13 requires PHP 7.3 or greater version.

We can segregate the entire setup with 5 major steps as follows:

  • Step 1 – Install Apache Web Server
  • Step 2 – Install and Configure PHP 7.4
  • Step 3 – Install Composer PHP Packages Management
  • Step 4 – Install Laravel 8.x on Ubuntu 20.04
  • Step 5 – Finally Configure Apache for Laravel and test it.

Step 1 – Install Apache Web Server

Let’s open up a Terminal and do first thing first update your package list using Sudo apt update command.

sudo apt update

After updating your package list install apache webserver. So, go ahead and type sudo apt install apache2 then hit the enter key. Press y key to proceed. You can also setup laravel with Nginx instead of the apache web server. Click Here

sudo apt install apache2
systemctl status apache2

laravel ubuntu apache

If the Apache server not running then use the following command to start apache serve and add to boot startup.

systemctl start apache2
systemctl enable apache2

Now open up the web browser and type localhost to see the default apache webpage is serving or not.

Step 2 – Install and Configure PHP 7.4

Get back to the terminal and it’s time to install PHP. Also, you can Checkout this Article on how to handle multiple PHP on a single server. To install Laravel 8.x, at least you must have PHP >= 7.3 on your system. And by default, the official Ubuntu 20.04 repository provides PHP 7.4 packages. Install PHP 7.4 packages 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

You can check your PHP version using it.

php -- version

php version

Now go ahead and make tweak changes in PHP ini file and set cgi.fix_pathinfo set to be 0. If this number is kept as a 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative.

cd /etc/php/7.4/apache2
sudo nano php.ini

Press ctrl+w and search for the word “cgi.fix” the uncomment the line and set it to 0.

...
cgi.fix_pathinfo=0
...

Press Ctrl + x then y to Save and Exit.

Now Restart The apache service.

systemctl restart apache2

Step 3 – Install Composer PHP Packages Management

Now it’s time to install the composer package manager go ahead and download and install Composer. and move the composer .phar file to usr/local/bin/composer directory.

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

You can check your installed composer version by typing the composer – version.

composer --version

Here you can see my version is 2.8.4

composer version

Step 4 – Install Laravel 8.x on Ubuntu 20.04

Now install Laravel Framework using composer, just type composer global require Laravel/installer It will take a while to complete download its dependencies.

composer global require laravel/installer

As you had seen above image, all packages have been installed on the ‘~/.config/composer’ directory. Next, we need to add the ‘bin’ directory to the PATH environment through the ~/.bashrc configuration. So Now Edit the ~/.bashrc configuration using nano command.

nano ~/.bashrc

And add the following line at the end of the file.

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

Now reload your bashrc configuration using the source command.

source ~/.bashrc

Now echo $PATH. It will return your “Bin” directory path for the Composer package.

echo $PATH

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

laravel new myapp1

This will take a while to download all dependencies required by Laravel.

Here you can see the installation of my new project myapp1 finished. You can also see inside my home directory a new directory has been created with my project name.

Step 5 – Finally Configure Apache for Laravel and test it.

First, add your project directory to www-data group use the following command

sudo chgrp -R www-data /home/techblog/myapp1

 -R flag is recursive, Recursive means all subdirectory and files under your project directory become changed to the “www-data” group.

Also, you need to change access permission 775 of the storage directory under your project. So, go ahead and use the following command.

sudo chmod -R 775 /home/techblog/myapp1/storage

Now create an apache vhost configuration go to the following directory and create a vhost config file using nano file editor.

cd /etc/apache2/sites-available/
sudo nano myapp1.com.conf

And paste the following line inside the file.

<VirtualHost *:80>
    ServerName myapp1.com

    ServerAdmin [email protected]
    DocumentRoot /home/techblog/myapp1/public

    <Directory /home/techblog/myapp1>
        Options Indexes MultiViews
        AllowOverride None
        Require all granted
    </Directory>

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

//All Red colored text must be changed as per your//

To learn more about Vhost configuration click here How to configure Apache Virtual Host on Ubuntu. Now enable mod rewrite for apache2 just type

sudo a2enmod rewrite.

Now enable your site, just type

sudo a2ensite myapp1.com.conf.

Finally, Restart the apache service, type

systemctl restart apache2

As you are in a local environment you need a local dns resolver for your site. Go ahead and edit /etc/hosts file, add a dns record for your site then save the file.

sudo nano /etc/hosts
...
127.0.0.1   myapp1.com
...

Now get back to the web browser and open a tab then type your project hostname.

laravel home

And here it is it’s working. Here you can see the Laravel version and PHP version.

So, from here open your project directory with the sublime editor or any PHP editor and create amazing projects for clients.

Hope this article helpful to you. Leave a comment if you have any questions. Also, click on the subscribe button to encourage us and get the latest update. Thank you.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.
subroto
  • subroto
  • I like Programming and New Technologies. And work with Linux.

One Pingback

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.