How to Install Laravel PHP framework with Nginx on Ubuntu 20.04

Laravel PFP Framework

Laravel is one of the most common PHP frameworks in the world for building small to large projects for web applications. Laravel is the preference of professional developers due to its characteristics, performance, and scalability. To fulfill your web application requirements, you can configure Laravel so easily to create your project structure. By using Laravel’s elegant syntax, you can write a code that is expressive and self-explicit.

How does it make working so easy?

In many operations, Laravel is a fantastic tool and brings ease to work. It can be seen from the following list of points:

  • Promotes comprehensive coverage: It means that Laravel can send updates across a variety of channels.
  • It assists in the application of authentication straightforwardly. It offers an easy method for organizing authorization.
  • Supports backends with cache: Cache backends such as Redis are compatible with it.
  • Gives a clean API: The Laravel system offers a clean API for users across the libraries on the network.

What is the use of the Laravel Framework?

It’s useful for a variety of items, as described below:

  1. Model view controller: It assists in the management of web task activities. With such power, the overall efficiency is increased.
  2. Layout creation: It’s a great way to get lightweight built templates that help a user for building or creating a layout. Text, photos, code structures are included in this style.
  3.  Artisan utility: It has a default artisan tool that helps to act as a command-line tool.
  4. Bcrypt hashing: Object-Oriented and default libraries are available with Laravel. A user can perform Bcrypt hashing with the implementation of such libraries.
  5. Unit testing: Laravel enables unit testing simply and straightforwardly. It helps to implement new modifications within the program.

Now, let’s step-in towards the Laravel PHP framework Installation process on Ubuntu 20.04. Before following the installation guide, make sure that you are working as a ‘superuser’.

Installing and Configuring Laravel PHP framework with Ngnix on Ubuntu 20.04

Step 1: Open the terminal by pressing ‘CTRL+ALT+T’ or search it in the activities. The package list needs to be updated.

$ sudo apt-get update

Step 2: We will install the additional extensions of PHP required for the installation process.

$ sudo apt install -y php-mbstring php-xml php-fpm php-zip php-common php-fpm php-cli unzip curl nginx

Step 3: Utilize the following command to install PHP Composer on your system.

$ sudo curl -s https://getcomposer.org/installer | php

Save this package and check it’s working by using the given commands.

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

Step 4: You also need a web-based data server for your Laravel framework. MariaDB is a good option for this.

$ sudo apt install -y mariadb-client mariadb-server

Step 5: Enable MariaDB service and configure the user settings according to your choice.

$ sudo systemctl enable --now mariadb.service
$ sudo mysql_secure_installation

Step 6: Write-out the following command to install Laravel using Composer files.

$ sudo composer global require laravel/installer

Step 7: Set the file permissions on the Laravel Directory.

$ sudo chmod -R 755 /var/www/html/example
$ sudo chown -R itslinuxfoss:itslinuxfoss /var/www/html/example
$ sudo composer create-project --prefer dist laravel/laravel example.com

You can also list out the files related to the Laravel Installation.

$ ls -la /var/www/html/example.com/
$ sudo chown -R :www-data /var/www/html/example.com/storage/
$  sudo chown -R :www-data /var/www/html/example.com/bootstrap/cache/
$ sudo chmod -R 0777 /var/www/html/example.com/storage/
$ sudo chmod -R 0775 /var/www/html/example.com/bootstrap/cache/

Step 8: Generate a new key for the security purposes of the Laravel deployment.

$ sudo php artisan key:generate

By the generated key by writing out the following command.

$ grep -i APP_Key /var/www/html/example.com/.env

Step 9: Open-up the configuration file of the Laravel environment and add lines according to your user settings.

$ sudo nano /var/www/html/example.com/.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USERNAME=laravel
DB_USERNAME=root
DB_PASSWORD=

Step 10: Add the following content in the Ngnix configuration to make it work with the Laravel application.

$ sudo nano /etc/nginx/sites-available/example.com.conf
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-java>
        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;
        }
}
$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
$ sudo rm /etc/nginx/sites-enabled/default

Step 11: Restart the Ngnix service.

$ sudo nginx -t
$  sudo systemctl restart nginx

Also, Checkout the routing table.

$ ip ad

Step 12: Add your IP address to the host file and Enjoy Development on this astonishing framework.

$ echo “192.168.56.11 example.com” | sudo tee -a /etc/hosts

Conclusion:

In this article, we have talked about the Laravel PHP framework, its features, and its uses. We have also provided you a guide to Install and Configure it on your Ubuntu 20.04 using Nginx.