DEV Community

Adinnu Benedict
Adinnu Benedict

Posted on • Updated on

Deploying Laravel App To Shared Hosting (Namecheap) Using Laravel Deployer And Github

There are lot of tutorials and guides out there that treats this same topic but you will run into issues very quickly while trying to follow them.

Recently, I was stuck on a project that I must use the shared hosting provided by my client because of low App development budget.

Most of the guide I found involved

  • Editing the index.php before upload
  • Editing code on production or replacing files after each change
  • No git management or Version control
  • No real deployment
  • Had lots of security issues

So I take it on myself to hack through the process of deploying a Laravel app on Shared hosting server while avoiding the problems listed above and I found out it was easier than I thought.

Tools

  1. A Working Laravel App On Github
  2. Laravel Deployer Package.
  3. Namecheap Shared Hosting

Let's Get To Work

First we have to setup our hosting cPanel to help deploy easily
  1. Login to your Namecheap cPanel Account.
  2. Navigate to Manage Shell and Enable SSH access. Note the Access Details Displayed
  3. Navigate to Select PHP Version and make sure apcu is enabled
  4. Optionally, You can create a database if needed
Next, let's setup up our project for deployment
  1. Install Laravel deployer Package by running on the CLI

    composer require lorisleiva/laravel-deployer
    
  2. Next, we must generate our deployer config file by running. This config file will tell deployer about our Project, Server and Deployment method.

    php artisan deploy:init
    


    Respond to the CLI Question while replacing

    • Repository URL with your project url on Github
    • Hostname of your deployment server with your domain name
    • Deployment path (absolute to the server) with /home/your-cPanel-username/source
  3. On the root directory of the project, create a file called deployer_task.php and paste the following code inside of it.

    <?php
    namespace Deployer;
    desc('Symlink Laravel public To public_html');
    task('app:symlink_public_html', function () {
    run('ln -s /home/{{your-cpanel-username}}/source/current/public /home/{{your-cpanel-username}}/public_html');
    });
    
  4. Open config/deploy.php, scroll till you find the hosts config and in the section that defines your domain, add or update the follow keys

'deploy_path' => '/home/{{your-cpanel-username}}/source',
'user' => {{your-cpanel-username}},
'strategy' => 'firstdeploy',
'port' => 21098, // Port provided by cPanel ssh
'http_user' => {{your-cpanel-username}},
'writable_mode' => 'chmod' 
Enter fullscreen mode Exit fullscreen mode

Scroll to the include section add the deployer_task.php we created like this


    'include' => [
        base_path('deployer_task.php')
    ],
Enter fullscreen mode Exit fullscreen mode

Scroll again in deployer_task.php file in the hooks section add app:symlink_public_html in done section like this

    'hooks' => [
        .....

        'done' => [
             ......
             'app:symlink_public_html'
        ]

        .....
     ]
Enter fullscreen mode Exit fullscreen mode


`

Finally Let's Deploy

All you have to do now is push your code to Github and then run bash
php artisan deploy
If you setup everything correctly, Your Github Repo password and Server password will be requested multiple times during the deployment process
Deployment Image

By Now, your app has been deployed successfully. You can now view your website. πŸš€πŸš€πŸš€πŸš€

Other Notes;

  • After first deployment, you should change you deployment strategy to basic like in step 4 above
  • Your migration may not run until you follow this post Here
  • You can read more about Laravel deployer Here
Thanks For Reading My First Blog Post Ever. I Hope It Has Helped You. If you need help you can reach me on twitter @BenQoder

Top comments (2)

Collapse
 
ibtisam021 profile image
Ibtisam021

Shared hosting is good but managed cloud hosting is way better than shared hosting. Cloud hosting is much more powerful as you get a whole server all by yourself and you don't share its resources with anyone. Because of it your performance, speed and security are not compromised.

Collapse
 
akashdas profile image
Akash Das

Here is another resourceful article
nihardaily.com/21-deploy-guideline...