🎉🎉 Larasense is officially launched 🎉🎉
- Your Hub for Laravel News, Trends & Updates

How to Deploy Laravel on Shared Hosting

laravel
deploy
server
shared-hosting
Nabil Hassen
Nabil Hassen
Oct 21, 2024
Shared Hosting Structure
Last updated on Oct 21, 2024
Table of contents:

Introduction

So, you're ready to deploy your Laravel app on a shared hosting in cPanel? While VPS hosting providers like DigitalOcean or AWS coupled with Laravel Forge might be the more common choice for Laravel projects, there are cases where you might have to deploy to a shared hosting.

Now, deploying Laravel in cPanel can be done, but you’ve got to do it the right way to avoid long-term headaches. In this tutorial, I’ll walk you through step-by-step on how to deploy your Laravel app smoothly in cPanel.

Let’s dive in!

Why the "Right Way" Matters

You’ve probably seen tutorials that suggest some risky deployment techniques, like uploading your vendor folder via FTP or moving files from Laravel's public folder to public_html. These shortcuts might work at first but can lead to a whole lot of issues down the road. Managing code changes, version upgrades, and database migrations becomes much harder if you take these approaches.

This guide will help you avoid those traps, so your Laravel app remains easy to maintain and upgrade.

Before we begin the step-by-step guide, make sure you build your frontend locally as it's cumbersome to build and bundle frontend on shared hosting:

npm run build

Step-by-step Deployment Guideline

Step 1: Access cPanel and Check Requirements

  1. Log in to your cPanel: Navigate to https://cpanel.yourdomain.com or https://yourdomain.com:2083 and log in with your credentials.
  2. Check the Advanced section for the Terminal icon (if it's missing, request Shell Access from your hosting provider). We’ll use this terminal for most of the deployment tasks.

terminal in cpanel advanced section

Once you're in, check your PHP version to ensure compatibility.

php -v

Make sure it is the right PHP version for you. If not, go to cPanel's homepage, look for Select PHP Version under the Software section and change the PHP version to your preferred version.

select php version in cpanel software section

Next, verify the PHP extensions:

php -m

Ensure the following extensions are installed:

  • ctype
  • curl
  • dom
  • fileinfo
  • filter
  • hash
  • mbstring
  • openssl
  • pcre
  • pdo
  • pdo_mysql
  • pdo_sqlite
  • session
  • sqlite3
  • tokenizer
  • xml

and any other extension required by your application as well.

Step 2: Install Composer (Optional)

Usually, composer is pre-installed by your hosting provider already. You can check by running:

composer -V

If not pre-installed, you can either ask your hosting provider to enable/install it or install it by yourself. Let's assume that you'll install it by yourself and start by adding an alias to your shell environment:

echo 'alias composer="php -d allow_url_fopen=On /home/username/composer.phar"' >> ~/.bashrc
source ~/.bashrc

Then, download and install Composer:

cd ~
curl -k -O https://getcomposer.org/installer
php -d allow_url_fopen=On installer

Verify the installation:

composer -V

Step 3: Set Up Git

With Composer ready, let’s get Git set up. If Git isn’t pre-installed in your cPanel, ask your hosting provider to enable it.

Next, check if an SSH key exists:

cat ~/.ssh/id_rsa.pub

If not, generate one:

ssh-keygen -t rsa -b 4096 -C "your_cpanel_username"

Copy the public key:

cat ~/.ssh/id_rsa.pub

Add it to your GitHub repo under Settings -> Deploy Keys.

RECOMMENDED — do not check the Allow write access checkbox.

deploy keys for a repo on github

Step 4: Clone the Repository and Set Up the App

Now, we’re ready to clone the app. Run the following command to clone the repository to a folder named "app" and then to navigate to the folder:

git clone [email protected]:username/repo.git app && cd app

Install the dependencies:

composer install --no-dev

While that’s running, create a new MySQL database in cPanel:

mysql database wizard on cpanel

Then configure the .env file:

cp .env.example .env
nano .env

Update your environment settings:

APP_ENV=production
APP_DEBUG=false
 
DB_HOST=localhost
DB_CONNECTION=mysql
DB_DATABASE=yourdbname
DB_USERNAME=youruser
DB_PASSWORD=yourpass

Generate the application key:

php artisan key:generate

Run the migrations:

php artisan migrate --force

If you're using filament, optimize filament components and blade icons:

php artisan filament:optimize

Optimize views, routes, events, and configs:

php artisan optimize

Create the symbolic links configured for the application:

php artisan storage:link

Step 5: Make the App Public

Now, the app is still private. To make it accessible, we’ll create a symbolic link from the app's public folder to public_html. First, back up the existing public_html folder:

mv public_html public_html_old

Then create the symlink:

ln -s /home/yourusername/app/public /home/yourusername/public_html

Now, visit your site and it should be live!

Step 6: Set Up Cron Jobs (Optional)

If your app requires scheduled tasks, set up a Cron job in cPanel:

cron jobs page on cpanel

/usr/local/bin/php /home/yourusername/app/artisan schedule:run >> /dev/null 2>&1

Conclusion

That’s it! You’ve successfully deployed your Laravel app to cPanel the right way. No shortcuts, no risky strategies—just a clean and maintainable setup. To deploy new changes, just push to your GitHub repo and run git pull from cPanel's terminal.

And if you want an even smoother deployment process, consider setting up a GitHub Actions workflow to test and automate deployments.

🎉☁️ Happy deploying!

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts