How to Deploy Laravel on Shared Hosting


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
- Log in to your cPanel: Navigate to
https://cpanel.yourdomain.com
orhttps://yourdomain.com:2083
and log in with your credentials. - 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.
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.
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"' >> ~/.bashrcsource ~/.bashrc
Then, download and install Composer:
cd ~curl -k -O https://getcomposer.org/installerphp -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.
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:
Install the dependencies:
composer install --no-dev
While that’s running, create a new MySQL database in cPanel:
Then configure the .env file:
cp .env.example .envnano .env
Update your environment settings:
APP_ENV=productionAPP_DEBUG=false DB_HOST=localhostDB_CONNECTION=mysqlDB_DATABASE=yourdbnameDB_USERNAME=youruserDB_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:
/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!
Stay Updated.
I'll you email you as soon as new, fresh content is published.