Skip to content

Instantly share code, notes, and snippets.

@Rod911
Last active April 19, 2025 09:36
Show Gist options
  • Save Rod911/0e1835451a6ff2ae149dbb1f0f807b46 to your computer and use it in GitHub Desktop.
Save Rod911/0e1835451a6ff2ae149dbb1f0f807b46 to your computer and use it in GitHub Desktop.
Deploy a laravel application on hostinger using ssh

Set up laravel application in hostinger shared hosting

Step 1: Install Composer

Install composer if not already installed, check composer --version or composer2 --version in cli skip to step 2 if it is.
Some Installations come preinstalled with both composer and newer versions of composer with alias composer2, in which case use composer2 for all comopser tasks

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Step 2: Install nvm & node

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.profile
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.profile
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.profile
source ~/.profile

Step 3: Git

git config --global user.name ""
git config --global user.email ""

Check if you already have SSH keys

ls -la ~/.ssh

If no keys exist, generate a new one

ssh-keygen -t ed25519 -C "<email here>"

When you get the prompt "Enter file in which to save the key", you can either:

  • Press Enter to accept the default location (~/.ssh/id_ed25519)
  • Or specify a custom path if you want to store it elsewhere or use a different name

The default location is typically fine for most users.

  • Enter a secure passphrase (recommended for better security)
  • Press Enter twice to use no passphrase (less secure but more convenient)

Once complete, your SSH key pair will be generated - a private key (id_ed25519) and a public key (id_ed25519.pub).

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Add the SSH key to your Git provider:

  • Go to your Git provider (GitHub, GitLab, Bitbucket, etc.)
  • Find the "SSH Keys" section in account settings
  • Add the public key you just displayed
cd domains/<domain name here>
git clone [email protected]:username/repo-name.git <any dir name>

Step 4: Setup directories

This step will reset public_html directory, ensure all important files are backed up elsewhere

cd ~
rm domains/<domain name here>/public_html -rf
cd domains/<domain name here>/<dir name> # dir name used in git clone at the end of step 3
touch deploy.sh

Paste the contents contents of 2.deploy.sh into deploy.sh using hostinger's file manager or nano in the terminal.

nano deploy.sh

Make the file executable

chmod +x deploy.sh

Step 5: Set up laravel

composer install
# or
composer2 install
cp .env.example .env
php artisan key:generate --ansi
php artisan storage:link # if this command fails, run 

If the last command fails, run:

cd public
ln -s ../storage/app/public storage
cd ..

Update the .env

nano .env
  • Update APP_NAME
  • APP_ENV=production
  • APP_DEBUG=false
  • APP_URL=<domain here>
  • Update DB connection details

Create link to laravel's public directory from domain's public_html

cd .. # Go to the domains/<domain name> directory, ls command should show the <dir name> we git cloned into
ln -s <dir name>/public public_html
cd <dir name>
./deploy.sh

The deploy.sh script will pull the latest commit from the git origin and run all the composer, laravel and npm update steps.

You should now be able to access the site if you did not encounter any bugs in the last step

You can manually run the deployment using ssh in the future by running the deploy.sh script or set up a auto deployment pipeline from github.

Sources:

#!/bin/bash
# Pull latest changes from repository
echo "Pulling from origin"
git reset --hard
git pull
# Run Laravel migrations and optimization
echo "Running artisan functions"
php artisan migrate --force
php artisan optimize:clear
php artisan optimize
# Install/update PHP dependencies
echo "Updating composer"
composer2 install
composer2 update
# Install/update Node.js dependencies
echo "Updating npm"
npm install
# Build frontend assets
echo "Building frontend assets"
npm run build
echo "Deployment tasks completed successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment