Laravel Install on Ubuntu 22.04 with Nginx

Dec 28, 2022 . Admin



Hello Friends,

You will learn how to instal Laravel on Ubuntu 22.04 using the nginx example in this example. You'll discover how to instal Laravel with nginx on Ubuntu 22.04. This article will demonstrate how to instal Laravel on Ubuntu 22.04 with nginx using a straightforward example. You can learn how to instal and set up Laravel on Ubuntu with nginx here. Making a simple example of how to use nginx and Laravel together.

Configuring and installing Nginx and Laravel on Ubuntu 22.04; We will learn how to instal and set up Laravel on Ubuntu 22.04 with nginx with this guide.

Now let's start.

How To Install and Configure Laravel with Nginx on Ubuntu 22.04

To instal and set up Laravel with Nginx on Ubuntu 22.04, just adhere to these steps:

(1). Install Required PHP Modules

(2). Creating a Database for Laravel

(3). Install Composer in Ubuntu 22.04

(4). Install Laravel in Ubuntu 22.04

(5). Configure Laravel in Ubuntu 22.04

(6). Configure NGINX to Serve Laravel Application

(7). Accessing Laravel Application from a Web Browser

Step 1: Install Required PHP Modules

First thing first, instal the necessary php modules. To do this, open command prompt and type the command below:

$ sudo apt update
$ sudo apt php-common php-json php-mbstring php-zip php-xml php-tokenizer    
Step 2: Creating a Database for Laravel

Establish a MySQL database for the Laravel application by using the following command on the command line:

$ sudo mysql
MariaDB [(none)]> CREATE DATABASE laraveldb;
MariaDB [(none)]> GRANT ALL ON laraveldb.* to 'webmaster'@'localhost' IDENTIFIED BY 'tecmint';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit    
Step 3: Install Composer in Ubuntu 22.04

Install composer (a PHP dependency manager) on an Ubuntu 22.04 system by running the command line:

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer    
Step 4: Install Laravel in Ubuntu 22.04

Once the composer installation has been done; execute the following command on command line to install laravel apps in ubuntu 22.04 system:

$ cd /var/www/html
$ composer create-project --prefer-dist laravel/laravel example.com
Step 5: Configure Laravel in Ubuntu 22.04

Now configure laravel apps using the following commands:

Set permissions on the Laravel directory using the following command:

$ 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/    

he 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    

As seen in the following screenshot, we also need to configure the Laravel database connection information in.env.

$ sudo nano /var/www/html/example.com/.env    
Step 6: Configure NGINX to Serve Laravel Application

Create a server block for it within the NGINX configuration, under the /etc/nginx/sites-available/ directory:

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

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):

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;
    }
}    

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

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /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

Now open a web browser on the local computer and use the following URL:

http://localhost:8000/    

I hope it can help you...

#Laravel