How to Install Laravel on Ubuntu?

07-Jul-2023

.

Admin

How to Install Laravel on Ubuntu?

Hello Friends,

In this short tutorial we will cover How to Install Laravel on Ubuntu. this example will help you How To Install and Configure Laravel with Nginx on Ubuntu. we will help you to give an example of installing Laravel on Ubuntu. I explained simply step by step How to install Laravel globally in Ubuntu. Alright, let’s dive into the steps.

Install and configure Laravel on Ubuntu 22.04 with Nginx. In this tutorial, we will learn how to install and configure Laravel on Ubuntu 22.04 with Nginx.

Step 1: Install Required PHP Modules


First of all, install the required php modules, so open the command prompt and execute the following command to install the required php modules:

$ sudo apt update

$ sudo apt php-common php-json php-mbstring php-zip php-xml php-tokenizer

Step 2: Creating a Database for Laravel

Create a MySQL database for the Laravel application; so execute the following command on the command line to create a database for the Laravel app:

$ 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

Then use the following command to install composer (a dependency manager for PHP) on Ubuntu 22.04 system:

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

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

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

$ 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 address to navigate.

http://www.example.com/

I hope it can help you...

#Ubuntu