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
1. Create a website on hostinger
Got to websites list, click "Add Website" button and select "Empty PHP/HTML website" from dropdown
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.
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.
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)
- Download and install NVM using cURL:
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
- Reload your shell configuration to apply changes:
source ~/.bashrc
- Verify NVM installation:
command -v nvm
If installed correctly, it should return nvm
.
Step 2: Install Node.js Using NVM
- List available Node.js versions:
nvm list-remote
- Install the latest LTS version:
nvm install --lts
- Set the installed version as the default:
nvm use --lts
nvm alias default node
- Verify the Node.js and npm versions:
node -v
npm -v
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"
Then, reload the shell:
source ~/.bashrc
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
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/*
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 .
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
Then install node packages.
npm install
now let's create the .env
cp .env.example .env
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
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&$#@"
php artisan migrate
php artisan db:seed
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
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
now let's run npm build
npm run build
Now your website is ready. Build something useful and make the world a better place and make me proud. Cheers
Top comments (0)