seeb4coding php

Deploying Laravel API on Hostinger: A Complete Guide

Hosting a Laravel API project on Hostinger is a great way to make your application accessible to the web. Here’s a step-by-step guide to get started, including running the application locally and deploying it with Hostinger.

Step 1: Running Laravel Locally

Before deploying to Hostinger, ensure your Laravel application works correctly in a local environment.

1.1. Run Laravel Application Locally

To run your Laravel app locally, use the PHP server:

C:\wamp64\bin\php\php8.1.0\php.exe artisan serve

Alternatively, you can use the shorter command:

php artisan serve

1.2. Run the Frontend (if applicable)

If your project has a frontend built with Vue.js or React, you can run the frontend assets using the command:

npm run dev

1.3. Accessing Uploaded Images

For image access, link your storage folder to the public directory:

php artisan storage:link

Step 2: Essential Laravel Commands for Development

Before deployment, you’ll likely need to run some basic Laravel commands.

2.1. Installing Composer Packages

If you need to install or update dependencies, use:

composer require firebase/php-jwt
composer install

2.2. Creating Controllers, Models, and Migrations

Create a Controller:

php artisan make:controller "Yourfolder/yourControllerName"

Create a Model:

php artisan make:model "yourmodelname"

Create a Migration (Database Table):

php artisan make:migration "Yourlaraveltable"

2.3. Migrating Database Tables

To apply your migrations (create tables in the database):

php artisan migrate

If you need to refresh the database (drop all tables and re-migrate):

php artisan migrate:fresh

Step 3: Deploy Laravel API on Hostinger

Now, let’s deploy the Laravel project to Hostinger.

3.1. Upload Your Project

  1. Zip Your Laravel Project: Compress your entire Laravel project folder.
  2. Login to Hostinger: Go to your Hostinger account and access the File Manager.
  3. Upload the Laravel Project: Upload the zip file to the public directory and extract it.

3.2. Set Up Environment Variables

After uploading, locate the .env file and configure your environment variables (e.g., database connection, API keys, etc.).

Example for DB setup:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password

3.3. Install Dependencies

Connect via SSH: Use SSH to connect to your Hostinger server.

Navigate to the Project Directory: Go to the directory where your Laravel project is uploaded.

Install Composer Dependencies: Run the following command:

composer install

3.4. Migrate Database

Migrate your database tables using SSH:

php artisan migrate

3.5. Set Permissions

Ensure the storage and bootstrap/cache directories are writable:

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

3.6. Setup Public Directory

By default, Hostinger serves files from the public_html directory. Move all contents of your Laravel project’s public directory to public_html.

Edit the index.php file in the public_html directory, and change the paths to your Laravel application folder:

require __DIR__.'/../your-laravel-project-folder/vendor/autoload.php';
$app = require_once __DIR__.'/../your-laravel-project-folder/bootstrap/app.php';

Step 4: Test Your API

Once everything is set up, access your Laravel API by navigating to your domain (e.g., https://yourdomain.com/api/). Ensure everything works correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *