DEV Community

vimuth
vimuth

Posted on • Edited on

Effortless Laravel Deployment on Hostinger Using Git: A Step-by-Step Guide

This tutorial talks about how to deploy a Laravel app on Hostinger Hpanel. And I'm talking about adding Node JS (Via NVM) build using composer and may more interesting things.

Here we imagine you use popular business plan on hostinger. But deploying on other plans are also same

Image description

1. Create a website on hostinger

Got to websites list, click "Add Website" button and select "Empty PHP/HTML website" from dropdown

Image description

After that you will be redirected to that domains dashboard. Inside this dashboard go to "Advanced -> SSH Access" and enable SSH status. And don't forget to add a password.

Image description

Then login with putty using these data.

2. Adding relevant aspects such as database creation and Node.js setup.

We are going to add relevant aspects. And let us start with database creation. First go to "Databases -> Management" section in your website dashboard and create a Database and a User.

Image description

Now database has created. Lets login to putty and get node installed.

To install Node.js on Hostinger using NVM (Node Version Manager), follow these steps:

Step 1: Install NVM (Node Version Manager)

  1. Download and install NVM using cURL:
   curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
Enter fullscreen mode Exit fullscreen mode
  1. Reload your shell configuration to apply changes:
   source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  1. Verify NVM installation:
   command -v nvm
Enter fullscreen mode Exit fullscreen mode

If installed correctly, it should return nvm.

Step 2: Install Node.js Using NVM

  1. List available Node.js versions:
   nvm list-remote
Enter fullscreen mode Exit fullscreen mode
  1. Install the latest LTS version:
   nvm install --lts
Enter fullscreen mode Exit fullscreen mode
  1. Set the installed version as the default:
   nvm use --lts
   nvm alias default node
Enter fullscreen mode Exit fullscreen mode
  1. Verify the Node.js and npm versions:
   node -v
   npm -v
Enter fullscreen mode Exit fullscreen mode

This should display the installed versions of Node.js and npm.

Step 3: Keep Node.js Available for All Sessions

Since Hostingerโ€™s shared hosting resets the environment on logout, add the following line to ~/.bashrc or ~/.bash_profile to auto-load NVM:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Enter fullscreen mode Exit fullscreen mode

Then, reload the shell:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Now, Node.js should work consistently across sessions.


3. Clone the website and get it running

Again login to putty. And go to your website directory.

cd /home/youruser/domains/yourdomain.hostingersite.com
Enter fullscreen mode Exit fullscreen mode

User and domain can be easily found on website dashboard on Hostinger.

And then remove the public_html folder inside. Actually we are removing everything inside the folder

rm -rf /home/u749865644/domains/saddlebrown-spider-721569.hostingersite.com/*
Enter fullscreen mode Exit fullscreen mode

I know public_html folder is needed in Hostinger hosting. But it's existence will make it hard to clone and deploy a Laravel app. We will add a public_html with a neat trick later

Now we should clone the Laravel app to your domain folder from git.

git clone https://github.com/your-git/your-repo.git .
Enter fullscreen mode Exit fullscreen mode

Since the folder is empty we can easily clone our app here. And if your repository is private, try using a personal access token.

Now lets do a composer install. But there is an important point. Hostinger has installed both composer 1 and composer 2. And we need composer 2. So our command should look like this

composer2 install
Enter fullscreen mode Exit fullscreen mode

Then install node packages.

npm install
Enter fullscreen mode Exit fullscreen mode

now let's create the .env

cp .env.example .env
Enter fullscreen mode Exit fullscreen mode

And don't forget to update that .env using 'nano .env' to access the db we created earlier.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hostingeruser
DB_USERNAME=hostingerdb
DB_PASSWORD=hostingerpassword

And make note that we can keep DB_HOST and DB_CONNECTION

Now lets generate keys.

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

now let's run migrate and seed commands. And remember if there is an error, check weather password contains special characters and if so include it inside double quotes

DB_PASSWORD="hostingerpassword&$#@"
Enter fullscreen mode Exit fullscreen mode
php artisan migrate
php artisan db:seed
Enter fullscreen mode Exit fullscreen mode

Now lets create symbolic links. But there is a small alteration. If you run php artisan storage:link command there will be an error.

This error occurs because Hostinger disables the symlink() function for security reasons. Laravel uses symlink() to create a symbolic link from public/storage to storage/app/public when running above command.

So you will have to manually Create a Storage Link Using a Hard Link

ln -s /home/youruser/yourdomain.hostingersite.com/storage/app/public /home/youruser/yourdomain.hostingersite.com/public/storage
Enter fullscreen mode Exit fullscreen mode

This is just a linux command to create a symlink or shortcut. just replace 'youruser' and 'yourdomain'.

now we have another important thing. Hostinger needs public_html to host and laravel needs public. To satisfy both what we can do is create a public_html symbolic link which will be linked to public folder

ln -s public public_html
Enter fullscreen mode Exit fullscreen mode

now let's run npm build

npm run build
Enter fullscreen mode Exit fullscreen mode

Now your website is ready. Build something useful and make the world a better place and make me proud. Cheers

Top comments (0)