Installing Laravel PHP framework on Ubuntu 20.04 for Apache

Updated July 28, 2020・3 minute read Post By lord-spown#laravel #ubuntu 20.04
Installing Laravel PHP framework on Ubuntu 20.04 for Apache

In recent years, Laravel has become an important open source framework for PHP.

In this tutorial I will explain exemplary how to set up Laravel on your Ubuntu 20.04 vps.

-Prerequisites (system updates, upgrades)

-Installation of Apache and PHP 7.4

-Laravel installation

-Apache Configuration

-Open browser for viewing

Requirements

All system installations begin with system updates and upgrades. It is recommended to update the system with the latest security patches.

sudo apt-get update 
sudo apt-get upgrade

For the purposes of this guide, we assume that you are running a base server based on Ubuntu. Before Laravel, we need to install other components that are essential.

Checkout our latest product - the ultimate tailwindcss page creator 🚀
Apache and PHP 7.4

The next step is to install PHP along with some additional packages that would prove useful if you want to work with Laravel.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install apache2 libapache2-mod-php7.4 php7.4 php7.4-xml php7.4-gd php7.4-opcache php7.4-mbstring

Even though Ubuntu's own repository has PHP, it is better to add a third-party repository here as it is updated more frequently. You can skip this step and stick with the Ubuntu version if you prefer.

Laravel installation

Before we finally get to that, we also need to install Git version control. Once you have installed it, you can skip the next step. If you do not have it, you can follow our instructions to set it up first. To install Laravel, we need to install Composer first. It is a dependency management tool in PHP that allows you to combine all the required libraries associated with a package into one package. To install Laravel and all its dependencies, Composer is required. He will download and install everything needed to run the Laravel framework. To install Composer, type the following commands.

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

The curl command downloads the composer.phar package into your /tmp directory. But since we want composer to run globally, we need to move it to the /usr/local/bin/ directory under the name 'composer'. Now we can run composer from anywhere. To install Laravel, move it to the public html directory on your system. Since we are working under Ubuntu and using Apache, we will install it in the /var/www/html directory.

cd /var/www/html
sudo composer create-project laravel/laravel demo-project --prefer-dist

The above command creates a "Your Project" directory with the Laravel installation in it. Composer uses git to download and install all packages and modules that Laravel needs to work.

Apache Configuration

Now that we have installed Laravel, we will proceed to the step of configuring the Apache web server. The next step is to set the correct permissions for the project directory. To do this we need to allow access to it from the www-data group and give it write permissions to the storage directory.

sudo chgrp -R www-data /var/www/html/demo-project
sudo chmod -R 775 /var/www/html/demo-project/storage

Now go to the directory /etc/apache2/sites-available and use the following command to create a configuration file for our Laravel installation.

cd /etc/apache2/sites-available
sudo nano demo-project.conf

Now add the following content to the file and close it after saving Replace yourdomain.tld in the file with the domain name of your website.

<VirtualHost *:80>
#    ServerName yourdomain.name

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/demo-project/public

    <Directory /var/www/html/demo-project>
        AllowOverride All
    </Directory>

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

Now we need to activate this newly created .conf file and disable the default .conf file that is installed with the standard Apache installation. Also, we need to enable mod_rewrite for permalinks to work properly.

sudo a2dissite 000-default.conf
sudo a2ensite demo-project.conf
sudo a2enmod rewrite
sudo service apache2 restart

Your Laravel installation is now complete. Visit the IP address or the domain name of your server with a web browser (in my case http://serverip). You will see the Laravel default page.

Laravel Framework successfully installed on Ubuntu 20.04 LTS

Still have a question?

Get help or discuss anything development/design related.

Start a discussion