Quick start guide for Laravel on a PHP server

Posted in PHP Laravel on 30 December 2022

Laravel is a powerful and popular PHP framework designed to make web development easier and more efficient. This quick start guide will help you set up Laravel on your PHP server.

Prerequisites:

Before installing Laravel, ensure that the following requirements are met:

  • PHP >= 8.1
  • Composer (a PHP dependency manager)
  • A web server such as Apache or Nginx
  • MySQL or PostgreSQL (optional, for database-driven projects)

Step 1: Install Composer

Laravel is installed using Composer, so you’ll need to have it on your system.

  1. Download Composer:
    Visit getcomposer.org to download and install Composer for your operating system.

  2. Verify Composer Installation:
    Open your terminal or command prompt and run the following command:

    composer
    

    If Composer is installed correctly, you should see a list of Composer commands.

Step 2: Create a New Laravel Project

Once Composer is set up, you can create a new Laravel project.

  1. Navigate to the directory where you want to create the project:
    cd /path/to/your/directory
    
  2. Run the following command to create a new Laravel project:
    composer create-project --prefer-dist laravel/laravel project-name
    

    Replace project-name with your desired project name.

Step 3: Set Up Environment Variables

Laravel uses an .env file to store environment variables such as database credentials, the app URL, and more.

  1. In the root directory of your project, you’ll find a .env file. Open it and configure your database settings:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your_database
    DB_USERNAME=your_username
    DB_PASSWORD=your_password
    
  2. Update other environment variables as needed, such as APP_URL for the correct URL of your application.

Step 4: Set Permissions

On a Unix-based system, ensure that the storage and bootstrap/cache directories are writable:

sudo chmod -R 775 storage
sudo chmod -R 775 bootstrap/cache

Step 5: Start Laravel Development Server

You can quickly start Laravel’s built-in development server to test your application.

  1. Navigate to your project directory:
    cd project-name
    
  2. Start the Laravel development server:
    php artisan serve
    
  3. Open your browser and go to http://localhost:8000. You should see the default Laravel welcome page.

Step 6: Set Up a Database (Optional)

If your project requires a database, run the following migrations to set up the database schema:

  1. Ensure your .env file is correctly configured for your database.

  2. Run the migration command:

    php artisan migrate
    

Step 7: Deploying to a Live Server

When deploying to a production environment, ensure your server meets Laravel’s requirements.

  1. Copy your project files to the server and set the necessary environment variables in your .env file.

  2. Set up a Virtual Host (for Apache or Nginx) pointing to the public directory of your Laravel project.

  3. Run Composer to install the project’s dependencies:
    composer install --optimize-autoloader --no-dev
    
  4. Run the following Artisan commands:
    php artisan key:generate
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    
  5. Ensure correct permissions for the storage and bootstrap/cache directories as mentioned earlier.

You have now successfully set up a Laravel application on a PHP server. You can begin developing your web application, utilising Laravel’s powerful tools and features.

For further details, visit the official Laravel documentation: Laravel Docs.

Related PHP Posts

June 2024

Popular PHP frameworks

Here’s detailed look at some of the most popular PHP frameworks, each of which can help speed up development, improve code organisation, and enhance application...

Continue reading

March 2024

PHP Security in 2024: navigating the evolving landscape

As PHP continues to evolve, so do the threats that target its vulnerabilities. Ensuring robust PHP security practices is paramount to safeguarding sensitive data and...

Continue reading

December 2022

Quick start guide for Laravel on a PHP server

Laravel is a powerful and popular PHP framework designed to make web development easier and more efficient. This quick start guide will help you set...

Continue reading

August 2021

Google Sheets to PHP array

This PHP function accepts a public Google Sheets URL and converts it into a PHP array. You can use the array to either display the...

Continue reading

March 2021

PHP cURL Requests with JSON or XML

The following post will explain how to use PHP/cURL to retrieve data in JSON or XML and process it for using in your PHP application...

Continue reading

February 2021

Global Payments: Strong Customer Authentication (SCA) and 3D Secure 2

The roll out of 3D Secure 2 has been a long drawn-out process not helped by the COVID–19 pandemic. Part of the update has included...

Continue reading

More PHP Posts

Related Laravel Posts

September 2024

Combining Laravel with WordPress

Combining Laravel with WordPress offers a unique and powerful approach to web development, blending the strengths of both platforms to create highly efficient, flexible, and...

Continue reading

December 2022

Quick start guide for Laravel on a PHP server

Laravel is a powerful and popular PHP framework designed to make web development easier and more efficient. This quick start guide will help you set...

Continue reading

October 2021

What is Laravel?

Laravel is an open-source web application framework written in PHP, designed to make the development process faster, easier, and more streamlined. It follows the Model-View-Controller...

Continue reading

December 2020

Laravel tutorial: Rest API call

To build a Laravel application that pulls posts from a REST API and displays them on a WordPress website, the process involves a few main...

Continue reading

July 2018

Laravel for PHP developers

As a PHP developer with several years of experience, my workflow has often been heavily reliant on procedural coding and custom-built solutions. For a long...

Continue reading

More Laravel Posts