How To Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel is the most popular, free, and open-source PHP framework in the world, known for its expressive and elegant syntax. Laravel is accessible, powerful, and offers some of the best web development tools required for large, robust, and modern applications.

In this article, you will learn how to install the Laravel PHP Framework on Ubuntu 20.04 server running on the Nginx web server.


Step 1: Installing Required PHP Modules

After setting up the LEMP stack on your Ubuntu 20.04 server as described in the guide in the link above, you need to install additional PHP extensions required by Laravel as follows:

$ sudo apt update
$ sudo apt php-common php-json php-mbstring php-zip php-xml php-tokenizer
Install PHP Modules in Ubuntu
Install PHP Modules in Ubuntu

Step 2: Creating a Database for Laravel

Next, you need to create a MySQL database for your Laravel application. So, log into your mysql shell and create the database as follows.

$ sudo mysql
MariaDB [(none)]> CREATE DATABASE laraveldb;
MariaDB [(none)]> GRANT ALL ON laraveldb.* to 'webmaster'@'localhost' IDENTIFIED BY 'tecmint';
MariaDB [(none)]> quit
Create Database for Laravel
Create Database for Laravel

Step 3: Installing Composer in Ubuntu 20.04

Laravel utilizes the composer (a dependency manager for PHP) to manage its dependencies. Therefore, before using Laravel, ensure you have Composer installed on your system as shown.

$ curl -sS | php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer
Install Composer in Ubuntu
Install Composer in Ubuntu

Step 4: Installing Laravel in Ubuntu 20.04

After installing the composer, use it to install the Laravel files. Move into your /var/www/html directory where web files are stored, then install Laravel using the composer as shown. Remember to replace with the name of the directory where the Laravel files will be stored.

$ cd /var/www/html
$ composer create-project --prefer-dist laravel/laravel
Install Laravel in Ubuntu
Install Laravel in Ubuntu
Note that while configuring NGINX to serve Laravel as described below, the absolute path (or root path) for your site in NGINX configuration file will be /var/www/html/

Step 5: Configuring Laravel in Ubuntu 20.04

To list the contents of the new Laravel installation, run the following ls command. You will notice that a .env file has been automatically created, which in the past, would have to be manually created.

$ ls -la /var/www/html/
List Laravel Files
List Laravel Files

Next, set the appropriate permissions on the Laravel directory as follows.

$ sudo chown -R :www-data /var/www/html/
$ sudo chown -R :www-data /var/www/html/
$ sudo chmod -R 0777 /var/www/html/
$ sudo chmod -R 0775 /var/www/html/

Next, Laravel uses an application key to secure user sessions and other encrypted data. The default .env contains a default application key but you need to generate a new one for your laravel deployment for security purposes.

$ sudo php artisan key:generate

The generated key will be appended in the .env file as the value of the APP_KEY. You can view the appended key using grep command.

$ grep -i APP_Key /var/www/html/
View Laraval Key
View Laraval Key

You also need to configure the Laravel database connection details in .env as shown in the following screenshot.

$ sudo nano /var/www/html/
Laravel Database Settings
Laravel Database Settings

Step 6: Configuring NGINX to Serve Laravel Application

For NGINX to serve your new application, you need to create a server block for it within the NGINX configuration, under the /etc/nginx/sites-available/ directory.

$ sudo nano /etc/nginx/sites-available/

In the configuration below, update the root directive to the Laravel application’s public directory and make sure to replace with the domain name of your website as shown.

Also, set the fastcgi_pass directive should point to the medium PHP-FPM is listening on for requests (for example fastcgi_pass unix:/run/php/php7.4-fpm.sock):

        root        /var/www/html/;
        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;

Save the file and then enable the Laravel site configuration by creating a link from /etc/nginx/sites-available/ to the /etc/nginx/sites-enabled/ directory. Besides, remove the default server block configuration.

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
$ sudo rm /etc/nginx/sites-enabled/default

Next, check if the NGINX configuration syntax is correct by running the following command before restarting the service.

$ sudo nginx -t
$ sudo systemctl restart nginx

Step 7: Accessing Laravel Application from a Web Browser

At this stage, you need to test if your Laravel deployment is working fine and whether it can be accessed from a browser. To use the dummy domain,, let’s use the /etc/hosts file on your local computer to create local DNS.

Run the following commands to get the IP address of the Laravel server and add it to the /etc/hosts file (replace the value according to your settings).
$ ip ad
$ echo “” | sudo tee -a /etc/hosts

Now open a web browser on the local computer and use the following address to navigate.
Laravel Web Page
Laravel Web Page

Now that you have Laravel installed, you can start building your web application or site. For more information, see the Laravel documentation.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.