LinuxTutorialsUbuntu

How To Install Laravel on Ubuntu 20.04 LTS

Install Laravel on Ubuntu 20.04

In this tutorial, we will show you how to install Laravel on Ubuntu 20.04 LTS. For those of you who didn’t know, For those of you who didn’t know, Laravel is a free, open-source PHP web application framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. It is a pretty new framework, but with a big potential to become one of the most popular PHP frameworks.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of Laravel on Ubuntu 20.04 Focal Fossa. You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 20.04, 18.04, and any other Debian-based distribution like Linux Mint or elementary OS.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Laravel on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Install LAMP server.

A Ubuntu 20.04 LAMP server is required. If you do not have LAMP installed, you can follow our guide here.

Step 3. Installing Composer.

Composer a dependency manager tool for PHP programming language. To install Composer, type the following commands:

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

Verify the installation of the composer run the below command:

composer -V

Step 4. Installing Laravel on Ubuntu 20.04.

The latest Laravel version is available under the Github repository. Now we download the latest version, and install it with the following command:

cd /var/www
git clone https://github.com/laravel/laravel.git

Next, switch to the Laravel directory and use the composer to install all dependencies required for the Laravel framework:

cd /var/www/laravel
sudo composer install

Then, set proper permissions on files:

chown -R www-data.www-data /var/www/laravel
chmod -R 755 /var/www/laravel
chmod -R 777 /var/www/laravel/storage

Step 5. Create Environment Settings.

Now we create the Laravel environment configuration file. You can do it by renaming the .evn.example file to .env. :

mv .env.example .env

Next, generate a base64 random number encryption key, which is used by the Illuminate encrypter service:

$ php artisan key:generate

Application key set successfully.

Then, edit the .env configuration file and update the required settings. Also, make sure APP_KEY is properly set as generated:

nano .env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HFdS7c9rhDp+AeHu7kcmeilanaq2BQ/1gfFWEpoAk=
APP_DEBUG=true
APP_URL=http://localhost
...

Step 6. Configure MySQL / MariaDB Database Laravel.

Create a MySQL database for your Laravel application:

CREATE DATABASE laravel;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL ON laravel.* to 'laravel'@'localhost';
FLUSH PRIVILEGES;
quit

Next, edit the .env file and update database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=your-password

Step 7. Configuring Apache web server for Laravel.

Now we edit the Apache default virtual host configuration file (ie: 000-default.conf) and update Document Root to the Laravel public directory:

nano /etc/apache2/sites-enabled/000-default.conf

Add the following lines:

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/laravel/public

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/laravel>
                AllowOverride All
        </Directory>

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

</VirtualHost>

Now, we can restart the Apache webserver so that the changes take place:

sudo systemctl restart apache2

Step 8. Accessing Laravel Web Interface.

Laravel will be available on HTTP port 80 by default. Open your favorite browser and navigate to http://your-domain.com or http://server-ip-address and complete the required steps to finish the installation. If you are using a firewall, please open port 80 to enable access to the control panel.

Congratulations! You have successfully installed Laravel. Thanks for using this tutorial for installing Laravel in Ubuntu 20.04 LTS (Focal Fossa) systems. For additional help or useful information, we recommend you to check the official Laravel website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button