How to run Laravel Project like a PRO? [SOLVED]


Steve Alila

Laravel

Welcome to this comprehensive guide on how to run Laravel project! Laravel has quickly become one of the most popular PHP frameworks for web application development, thanks to its elegant syntax, robust features, and thriving community. If you're new to Laravel or just need a refresher on how to get a project up and running, you've come to the right place.

In this article, we will walk you through the essential steps to build and run Laravel project, from setting up your development environment to launching the application in your browser. We will also cover some common troubleshooting tips, as well as best practices for organizing and maintaining your Laravel projects. By the end of this tutorial, you will have gained the knowledge and confidence to run Laravel projects with ease and efficiency.

 

Install Requirements

The main requirements to run Laravel project are PHP, a database, and PHP composer. PHP is the programming language Laravel framework is written in. You need a database service to store the application's data.

MySQL is the default database. MySQL is a popular open-source relational database management system that is commonly used for web applications and other software projects. It allows for the storage and retrieval of large amounts of data. It is used by many popular web applications, such as WordPress, Drupal, and Joomla.

Composer is a dependency manager for PHP that simplifies the process of installing and managing PHP packages and libraries for your projects.

PHP

sudo apt update
sudo apt install php

We update the system and install the PHP programming language on the system.

MySQL

Install MySQL server on the system. After installing the MySQL server, the next commands are used to set a root password for the MySQL server and secure it.

sudo apt install mysql-server

This command opens the MySQL client and allows you to interact with the MySQL server. The sudo command is used to run the mysql command as a superuser, which is required to access and modify the MySQL server.

sudo mysql

Set a root password for the MySQL server. The alter user command is used to modify the root user account for the MySQL server, and the identified with mysql_native_password by clause is used to set the password for the root user account. Replace <your_password> with your desired password.

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '<your_password>';

This command exits the MySQL client and returns you to the terminal.

mysql> exit;

This command runs a script that guides you through a series of steps to secure the MySQL server, including setting a password for the MySQL root account, removing anonymous user accounts, disabling remote root login, and removing test databases.

mysql_secure_installation

Composer

Install the PHP command line interface and the unzip utility, which are required to run Composer and unzip packages downloaded by Composer.

sudo apt install php-cli unzip

Install the curl utility, which is used to download files from the internet.

sudo apt install curl

Download the Composer setup file from the official website and save it to the /tmp directory.

curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

This command retrieves the hash for the Composer setup file from the official website and saves it to the HASH variable.

HASH=`curl -sS https://composer.github.io/installer.sig`

Verify the integrity of the Composer setup file by comparing its hash to the hash retrieved from the official website.

php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Install Composer by running the setup file and specify the installation directory and filename.

sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

Test the installation of Composer by running the composer command, which should display the Composer version and usage information if the installation was successful.

composer

How to run Laravel Project like a PRO? [SOLVED]

 

Steps to build and run Laravel Project

Step~1: Get the project files

Let's start with the steps to run Laravel project. Open a terminal window and navigate to the root directory of your Laravel project. I have cloned this GitHub project and navigated to it.

How to run Laravel Project like a PRO? [SOLVED]

 

Step~2: Installing the dependencies using Composer

Run the following command to install the necessary dependencies:

composer install

Install php-xml if you get the following error: Your lock file does not contain a compatible set of packages. Please run composer update.

sudo apt-get install php-xml
composer install

How to run Laravel Project like a PRO? [SOLVED]

 

Step~3: Configuring the .env file to manage environment variables

Rename the .env.example file to .env and update the database configuration settings (database name, username, and password).

Using the mv command, let's rename the .env.example to .env file.

mv .env.example .env

 

Step~4: Creating the database for your Laravel application

Now to be able to run laravel project, let us createlaravel_demo_blog database.

CREATE DATABASE laravel_demo_blog;

How to run Laravel Project like a PRO? [SOLVED]

 

as defined in the .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_demo_blog
DB_USERNAME=root
DB_PASSWORD=<your_password>

 

Step~5: Generating a unique application key for security and authentication purposes

Generate a unique application key by running the following command:

php artisan key:generate

The application key is a crucial component required to run Laravel project, and it serves several important purposes:

  1. Security: The application key is used for encrypting cookies, sessions, and other sensitive data. A strong and unique key ensures the confidentiality and integrity of your application's data, making it difficult for attackers to tamper with or access the information.
  2. Authentication: Laravel uses the application key as part of its hashing mechanism for user authentication. It helps in generating unique and secure password hashes, which are then used to verify the user's credentials when they log in.
  3. Cross-site request forgery (CSRF) protection: The application key plays a role in generating CSRF tokens, which are used to protect your application from cross-site request forgery attacks. CSRF tokens help ensure that requests made to your application are genuine and originate from authorized users or clients.

 

Step~6: Migrating the changes to set up the database schema

Let's migrate the database by running the following command

php artisan migrate

Besides the users, we have created the blogs table. If you get database driver not found error, install php-mysql, php-mysqli, and php-pdo-mysql extensions.

sudo apt-get install php-mysql php-mysqli php-pdo-mysql

Optionally, we could seed the database with dummy data by running the following command:

php artisan db:seed
# OR
php artisan migrate --seed

Let's do that right away. Create a factory for the posts.

php artisan make:factory BlogFactory --model=Blog

Open the database/factories/BlogFactory.php file and define the structure of a fake blog post.

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Blog>
 */
class BlogFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'title' => fake()->sentence(),
            'image' => fake()->imageUrl(),
            'body' => fake()->paragraph(),
        ];
    }
}

Next, open the database/seeders/DatabaseSeeder.php file and specify the number of fake records to create.

<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        \App\Models\User::factory(3)->create();
        \App\Models\Blog::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);
    }
}

We have created 3 users and 10 blog posts. Let's seed the database with the data.

How to run Laravel Project like a PRO? [SOLVED]

 

Step~7: Starting the server to successfully run Laravel project

Finally, start the development server by running the artisan serve command.

php artisan serve

This starts a local web server that you can access at http://localhost:8000 in your web browser.

how to run Laravel project

We we were successfully able to build and run laravel project.

 

Summary

This article offers a comprehensive guide on how to run Laravel project, providing step-by-step instructions and covering essential topics to help you efficiently launch your Laravel applications. To run Laravel project, you'll first need to install the necessary requirements, including PHP, MySQL, and Composer. The process involves obtaining the project files, installing dependencies, configuring the .env file, creating the database, generating a unique application key, migrating the changes, and finally, starting the server.

By mastering these steps and understanding the underlying concepts, you will be well-equipped to run Laravel projects with ease and proficiency. Following this guide not only ensures a smooth and efficient process for running Laravel projects but also empowers you with best practices for project organization and maintenance. This knowledge serves as a valuable resource for developers at any stage of their Laravel journey, enabling them to tackle a wide range of Laravel projects with confidence.

 

Views: 68

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on LinkedIn or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel