Deploying Laravel App To Shared Hosting (Namecheap) Using Laravel Deployer And Github
Tags: laravel, cpanel, git, deployer
Published: 2019-11-24 02:34:25

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](https://github.com/lorisleiva/laravel-deployer) Package.
3. [Namecheap Shared Hosting](https://www.namecheap.com/hosting/shared)

### 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 <b>Manage Shell</b> and <b>Enable SSH access</b>. Note the <b>Access Details</b> Displayed
3. Navigate to <b>Select PHP Version</b> and make sure <b>apcu</b> is enabled
4. Optionally, You can create a database if needed

##### Next, let's setup up our project for deployment
1. Install [Laravel deployer](https://github.com/lorisleiva/laravel-deployer) Package by running on the CLI ```bash
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. ```bash
php artisan deploy:init
```Respond to the CLI Question while replacing
 * <b>Repository URL</b> with your project url on Github
 * <b>Hostname of your deployment server</b> with your domain name
 * <b>Deployment path (absolute to the server)</b> with /home/<b>your-cPanel-username</b>/source
3. On the root directory of the project, create a file called <b>deployer_task.php</b> and paste the following code inside of it. ```php
<?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 <b>config/deploy.php</b>, scroll till you find the <b>hosts</b> 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' 
```
Scroll to the <b>include</b> section add the deployer_task.php we created like this```

    'include' => [
        base_path('deployer_task.php')
    ],
```
Scroll again in deployer_task.php file in the <b>hooks</b> section add <b>app:symlink_public_html</b> in <b>done</b> section like this ```
    'hooks' => [
        .....

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

        .....
     ]
````


### 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](https://res.cloudinary.com/benqoder/image/upload/v1574562067/CLI_idtpxp.png)

### 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 <b>basic</b> like in step 4 above
* Your migration may not run until you follow this post [Here](https://laravel-news.com/laravel-5-4-key-too-long-error/)
* You can read more about Laravel deployer [Here](https://github.com/lorisleiva/laravel-deployer) 


##### 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](https://twitter.com/benqoder)