How to deploy Laravel in cPanel the right way
Many articles that I read has wrong way of deploying Laravel application on shared hosted environment inside cPanel.
Most of them stated that uploading the project folders inside the cPanel and copy the public folder to public_html and modify the Laravel framework ( public/index.php).
I believe the following steps is the correct way to deploy and easier way to maintain continuous integration and continuous delivery.
Step 1 : Login to cPanel
- Navigate to https://cpanel.yourdomain.com/ or http://yourdomain.com:2083
- Login with your cPanel credentials
- Go into Advance section > terminal (Make sure you have SSH access from your provider)
Step 2 : Install Composer
Configure your shell environment to use composer
echo 'alias composer="php -d allow_url_fopen=On /home/USERNAME/composer.phar"' >> ~/.bashrc
source ~/.bashrcChange
USERNAMEto your own username.
Download composer
curl -k -O https://getcomposer.org/installerInstall composer
php -d allow_url_fopen=On installerStep 3: Configure Git
Generate SSH key
ssh-keygen -t rsa -b 4096 -C "cPANEL USERNAME"Copy the key content for adding to your Github/Bitbucket repo
cat ~/.ssh/id_rsa.pubUsing Bitbucket for this project
Go to https://bitbucket.org/USERNAME/REPO/admin/access-keys
Add key>Paste the content and save.
Using Github
Go to https://github.com/USERNAME/REPO/settings/keys and follow the above step.
Change
USERNAME and REPOto your own username and repo.
Step 4: Setup your application
Create a folder for your application let say “myapp” and clone your project inside that folder
git clone git@github.com:username/repo.git myappInside your “myapp” folder run
composer installThen change your environmental variables in the .env file
Generate a new application key
php artisan key:generateThen migrate all your database
php artisan migrateSet the correct folder permissions
chmod -R 775 storageStep 5:Make your app accessible for public
Back up your public_html folder and remove it.
Then create a symlink
ln -s /home/user/app/public /home/user/public_htmlCreate a symlink for your storage
ln -s /home/user/app/storage/app/public /home/user/public_html/storageRun storage link
php artisan storage:linkYour app should work now and you can easily git pull in the cPanel.
You can also have a deploy script that can pull from github and run composer update and database migration.
# Turn on maintenance modephp artisan down # Pull the latest changes git pull origin master# Install/update composer dependeciescomposer install --no-interaction --prefer-dist --optimize-autoloader --no-dev # Run database migrationsphp artisan migrate --force # Clear cachesphp artisan cache:clear# Clear and cache routesphp artisan route:clear
php artisan route:cache # Clear and cache configphp artisan config:clear
php artisan config:cache# Turn off maintenance modephp artisan up
YOU ARE DONE! HAPPY CODING!
