Laravel is one of the best free and open-source frameworks for PHP to generate web applications, and it is recognized due to its eloquent and sophisticated syntax. Laravel also provides several web development tools, especially for large and modern web applications, because it remains accessible through improved versions. Its popularity is increasing day by day, and it is becoming the first choice of developers due to the continuous growth. Additionally, the developers use it in the streamlined development process.

In this detailed and simplified guide, you will be able to install the Laravel framework on Ubuntu 20.04 server or 20.04 LTS operating system.

Prerequisites

There are a couple of prerequisites that you must have before installing the Laravel PHP framework with Nginx on Ubuntu 20.04 LTS system.

You should have:

  • PHP & its required libraries
  • Composer
  • MySQL
  • Nginx installed on your Ubuntu 20.04 LTS system.

If you have not installed any of these prerequisites before, there is no need to worry about anything. We will show you how to install and configure PHP, Composer, MySQL, and Nginx to set up the Laravel PHP framework.

Let’s begin with the installation and configuration of PHP and its modules.

Installation of PHP and Other Required Libraries for Laravel Project

Before jumping into the installation part of any package or libraries, it is always a good practice to update the system’s repository cache. So, update Ubuntu’s APT cache repository using the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image11-5.png" data-lazy- height="371" src="data:image/svg xml,” width=”620″>

Alright, now to install PHP and some other required modules of it, type the command provided below:

$ sudo apt install phpcommon phpfpm phpjson phpmbstring phpzip phpcli phpxml phptokenizer y

The above command will install PHP and all other required PHP modules on your Ubuntu 20.04 LTS system, and the “-y” flag will auto-answer “yes” to any prompt that occurred during the installation process.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image13-5.png" data-lazy- height="362" src="data:image/svg xml,” width=”617″>

Along with the PHP and its required libraries, install the other software dependencies required for the installation and configuration like curl and unzip. To install curl and unzip, type the command given below:

$ sudo apt install curl unzip y

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image12-5.png" data-lazy- height="279" src="data:image/svg xml,” width=”616″>

After running the above command, all the software dependencies will be installed.

Let’s move ahead to install the Composer.

How to Install and Configure Composer for Laravel on Ubuntu 20.04

To install Composer on Linux for installing and creating the Laravel application for managing the project’s dependencies, we will use the curl command to install it on Ubuntu 20.04 LTS system.

Installation of Composer on Ubuntu 20.04

To install Composer on Ubuntu 20.04, type the command mentioned below:

$ curl sS https://getcomposer.org/installer | php

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image15-3.png" data-lazy- height="141" src="data:image/svg xml,” width=”619″>

This command will download and install the Composer.

Configuration of Composer on Ubuntu 20.04

You need to move the “composer.phar” file to the “/usr/local/bin/composer” directory, and you can do so using the command:

$ sudo mv composer.phar /usr/local/bin/composer

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image14-4.png" data-lazy- height="37" src="data:image/svg xml,” width=”534″>

After moving the “composer.phar” file to the “/usr/local/bin/composer” directory, also change the mode of the file to executable using the command typed below:

$ sudo chmod x /usr/local/bin/composer

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image17-2.png" data-lazy- height="33" src="data:image/svg xml,” width=”476″>

Alright, at this point, the Composer is installed and ready to serve for the creating and managing Laravel application dependencies.

To check whether Composer is installed or not, you can type the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image16-2.png" data-lazy- height="305" src="data:image/svg xml,” width=”596″>

Alright, the next step is to install and configure MySQL for the Laravel application.

How to Install and Configure MySQL for Laravel on Ubuntu 20.04

Now, we need to install MySQL so that we can create a database for the Laravel application.

Installation of MySQL Server

To install MySQL, simply type the command:

$ sudo apt install mysqlclient mysqlserver y

The above command will install MySQL in a couple of minutes, and you do not have to confirm any installation process because the “-y” flag will take care of that.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image21.png" data-lazy- height="94" src="data:image/svg xml,” width=”533″>

After the successful installation of MySQL, verify the status of MySQL service using the command:

$ sudo systemctl status mysql.service

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image19-2.png" data-lazy- height="218" src="data:image/svg xml,” width=”620″>

If it is inactive, start it using the command:

$ sudo systemctl start mysql.service

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image26.png" data-lazy- height="33" src="data:image/svg xml,” width=”445″>

Also, enable it for future purposes to automatically start on the boot time of the system using the command given below:

$ sudo systemctl enable mysql.service now

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image22.png" data-lazy- height="80" src="data:image/svg xml,” width=”622″>

Now, the MySQL server is installed and running fine. Let’s perform some configuration for the Laravel project.

Configuration of MySQL Server for Laravel Project

Since the MySql server is recently installed, there is no password set for any user now. Set it all up by running the command typed below and answering the questions asked appropriately:

$ sudo mysql_secure_installation

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image24.png" data-lazy- height="195" src="data:image/svg xml,” width=”544″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image27.png" data-lazy- height="331" src="data:image/svg xml,” width=”622″>

After setting all up, log in to the MySQL shell using the simple command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image29.png" data-lazy- height="219" src="data:image/svg xml,” width=”608″>

If you have set any password for root, then instead of the above command, you need to log in as a root user using the command given below and provide the password that you recently set for it:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image31.png" data-lazy- height="233" src="data:image/svg xml,” width=”607″>

After signing in to the MySQL shell, create a database first using the command:

mysql> CREATE DATABASE sample_db;

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image33.png" height="71" src="data:image/svg xml,” width=”291″>

After creating the database, create a user as well using the command:

mysql> CREATE USER ‘user_name’@‘localhost’ IDENTIFIED BY ‘user_password’;

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image35.png" data-lazy- height="72" src="data:image/svg xml,” width=”540″>

In the above query, change the user_name and user_password to the desired username and password.

Now, grant all permissions of the recently created database to the newly-created user using the GRANT query;

mysql> GRANT ALL ON ‘sample_db.*’ TO ‘username’@‘localhost’;

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image37.png" data-lazy- height="69" src="data:image/svg xml,” width=”551″>

Once the permissions are granted, Flush the privileges:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image38.png" height="67" src="data:image/svg xml,” width=”286″>

Then, exit from the MySQL shell to get back to the terminal.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image39.png" height="52" src="data:image/svg xml,” width=”194″>

Alright, at this stage, MySQL is configured to be connected to the Laravel application. Now, let’s install and configure Nginx so we can finalize our Laravel application in the end.

How to Install and Configure Nginx for Laravel on Ubuntu 20.04

Installation and configuration of Nginx for the Laravel application on Ubuntu 20.04 are really easy and simple.

Installation of Nginx

To install Nginx, simply type the command given below, and you will have the latest and stable version of Nginx installed on your Ubuntu 20.04 LTS system:

$ sudo apt install nginx y

The “-y” flag will answer yes to any question prompted during the installation process.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-33.png" data-lazy- height="311" src="data:image/svg xml,” width=”609″>

After the installation of Nginx, the Nginx service will automatically start and verify the status of the service using the command:

$ sudo systemctl status nginx

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-33.png" data-lazy- height="284" src="data:image/svg xml,” width=”622″>

If it is active and running, then it is ready to be configured. You can move to the configuration step ahead. If it is inactive in your case, then start the Nginx service using the command typed below;

$ sudo systemctl start nginx

Ok, now let’s install and configure the Laravel project.

How to Install and Configure Laravel on Ubuntu 20.04

Laravel can be installed on Ubuntu 20.04 easily using the Composer. You just need to follow a couple of steps, and you will have the Laravel installed on your Ubuntu 20.04 LTS system.

Installation of Laravel

For installing Laravel, navigate to the /var/www/html directory using the cd command as shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-33.png" data-lazy- height="37" src="data:image/svg xml,” width=”303″>

Before creating the Laravel project using the Composer, make composer global for installing Laravel using the command given below:

$ sudo composer global require laravel/installer

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-32.png" data-lazy- height="91" src="data:image/svg xml,” width=”622″>

Now, to create a Laravel project using the Composer, run the command:

$ sudo composer create-project –prefer-dist laravel/laravel example.com

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-29.png" data-lazy- height="220" src="data:image/svg xml,” width=”622″>

Replace the example.com with the project name of your own choice in the above command for creating a Laravel project.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-26.png" data-lazy- height="309" src="data:image/svg xml,” width=”621″>

Once you have created a Laravel Project, you need to grant some appropriate permissions of the Laravel project directory to the non-root user of the system. You can do this by typing the commands:

$ sudo chmod -R 755 /var/www/html/example.com

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image7-19.png" data-lazy- height="40" src="data:image/svg xml,” width=”605″>

$ sudo chown -R www-data:www-data /var/www/html/example.com

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image8-16.png" data-lazy- height="51" src="data:image/svg xml,” width=”621″>

After setting the permissions of the directory, navigate to the project directory using the cd command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image9-14.png" data-lazy- height="35" src="data:image/svg xml,” width=”397″>

Run the composer install command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image10-8.png" data-lazy- height="281" src="data:image/svg xml,” width=”502″>

After running the composer install command, make sure you are in the project’s directory. Then, execute the command given below with the server’s IP address and port number to verify and access the Laravel application created:

$ php artisan serve –host=192.168.18.250 –port=8000

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image18-2.png" data-lazy- height="82" src="data:image/svg xml,” width=”624″>

Now, if you want to view the application, go to the browser and type the IP address and port number that you have just given in the php artisan command.

http://192.168.18.250:8000

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image20.png" data-lazy- height="673" src="data:image/svg xml,” width=”1091″>

Alright, now let’s configure Laravel for connecting it to the database created.

Just open up the “.env” file in the Laravel project’s directory using the nano editor:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image23.png" data-lazy- height="435" src="data:image/svg xml,” width=”646″>

In the ‘.env’ file, you can have a lot of configuration variables, and from those variables, our concerned variables are:

APP_NAME=exampleApp

APP_ENV=development

APP_KEY=some_key

APP_DEBUG=true

APP_URL=http://domain_or_IP

LOG_CHANNEL=stack

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=laravel

DB_USERNAME=root

DB_PASSWORD=password

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image25.png" data-lazy- height="444" src="data:image/svg xml,” width=”659″>

Modify the variables as per your settings and save the “.env” file using the keyboard shortcut CTRL S.

Configuration of Nginx for a Laravel Project

For configuring the Nginx for a Laravel project, first, create the configuration file of Nginx in the /etc/nginx directory using the command:

$ sudo nano /etc/nginx/sites-available/example.com.conf

Change the server name, server’s IP address, and the /var/www/html/example/public address as per your demands:

server{


        server_name www.example.com;


        root        /var/www/html/example.com/public;


        index       index.php;

        charset utf-8;


        gzip on;


        gzip_types text/css application/javascript text/javascript application/x-javascript  image/svg xml text/plain text/xsd text/xsl text/xml image/x-icon;

        location / {


                try_files $uri $uri/ /index.php?$query_string;


        }

        location ~ .php {


                include fastcgi.conf;


                fastcgi_split_path_info ^(. .php)(/. )$;


                fastcgi_pass unix:/run/php/php7.4-fpm.sock;


        }


        location ~ /.ht {


                deny all;


        }

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image28.png" data-lazy- height="440" src="data:image/svg xml,” width=”657″>

After the configuration of Nginx, enable the virtual host file for configuration.

First, create a symbolic link:

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image30.png" data-lazy- height="51" src="data:image/svg xml,” width=”626″>

And, remove the old configuration file if any by using the following command:

$ sudo rm /etc/nginx/sites-enabled/default

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image32.png" data-lazy- height="43" src="data:image/svg xml,” width=”490″>

Verify if there is a syntax error or not in the configuration file, using the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image34.png" data-lazy- height="68" src="data:image/svg xml,” width=”533″>

If the output has shown that the file is ok and the test is successful, then reload the Nginx service using the command:

$ sudo systemctl reload nginx

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image36.png" data-lazy- height="41" src="data:image/svg xml,” width=”390″>

The Laravel application is now all set up and ready to use.

Conclusion

This post contains a brief method for installing a Laravel PHP framework with Nginx on Ubuntu 20.04. In this post, you have learned to install PHP and the other required modules, such as Composer, MySQL, Nginx, and configuration for Laravel project on Ubuntu 20.04 LTS operating system.