Cpanel Git: Part 3 - Deploying the Laravel API
Tags: git, github, react, laravel
Published: 2022-12-24 15:34:20

This part explains how to deploy our laravel application to our hosting.

As always every Cpanel Deployment requires a `.cpanel.yml` file and deploy keys.

### Adding `.cpanel.yml`
Using the content of this github gist: https://gist.github.com/sadiqsalau/e1b8c416503c40769c0ecd10cdd1f345

We are going to create just two new files under our laravel project
`.cpanel.yml`
`.deploy.example.sh`

There is already a `.gitignore` at the root of our laravel application, we will just merge the content of `.gitignore` in that gist with ours.


![Cpanel deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q25ixrns3a63opkqd9il.png)

Now open `.gitignore` and add `.deploy.sh` at the end

![Editing .gitignore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/occib0qhv1fxha9xyjt5.png)

With that done, let's commit and push our changes.
`git add .`
`git commit -m "Added cpanel deploy script"`
`git push`


The `.cpanel.yml` file in that gist will download composer, install your application dependencies and run migrations.
Uncomment the line that run migrations if you want to run migrations. 


### Generating and adding deploy keys
Following the instructions in the first part of this series. We will generate another deploy key.

 Open a new terminal and run
`ssh-keygen -t ecdsa-sha2-nistp256 -C "your_email@example.com"`


![New deploy key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2hygw6q8b3imnc4uf90.png)

Rename your key. For this tutorial it will be called `cpanel_laravel_example`

![Renamed deploy key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a6hjymzkbqvf3v27wuf.png)

You should add the public key to your github repository deploy keys. (Take a look at part 1 of this series)


### Deploying to shared hosting.
Login to your hosting. Open file manager and navigate to  the `.ssh` folder.


![.ssh folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hizi9ffzck7g99szez19.png)

Upload the new keys and set the permission of the private key to 0600 (take a look at the first part of this series)


![Uploading keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wj3o62p604hhs031pgw.png)


![Set deploy key permission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlfezac68xjebh7k9x84.png)

Edit the config file under `.ssh` folder and register the new deploy key


![Edit config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6merx65k8mi6jamjg3v8.png)


![Registering second key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d40mlq3gbfrgsn9zw6ya.png)


#### Create a new cpanel repository


![Create new repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wouepkpefpsob6cykpa0.png)


![Repository created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voprik7sq5j5vefkdgy8.png)


The deployment of the laravel project is similar to that of the react app. You will need to deploy thrice if you are running migrations.

Based on the content of `.cpanel.yml` in that gist:

1. First deploy will generate `.deploy.sh` file - edit if you want to deploy to a different folder - default is `api` folder at the root of your hosting

2. Second deploy will copy the laravel application to `api` folder at the root of your hosting unless the `.deploy.sh` file was modified, then it will download composer, install dependencies and generate `.env` for your laravel application.

3. Third deployment will not run unless a `.env` file has been detected. It will optimize your laravel app and run migrations if uncommented. Make sure to setup your `.env` file before running the third deployment.

Check the `.cpanel.yml` if confused.

Once deployed, your `api` folder should contain your laravel project.

A symbolic link to `public` folder of your laravel project will be created at `public_html/api`

A symbolic link to `storage/app/public` folder of your laravel project will be created at `public_html/storage`

This is the default behaviour  unless you edit the `.deploy.sh` file to create links under a different directory. You can always customize your deployment.


![Laravel Project Deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt3f4q2hr17ge1minz43.png)


![API folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1eigp7bevzs01hmr1mr.png)


![Public folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwf56q5zxhtwk6hlqo40.png)

Once the laravel project has been deployed go to the pull and deploy tab of your react app.

Update from remote then deploy again.

![Redeploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qwyvke2a66ijoxrev53.png)


Visit your site again to see the result.

![Updated site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7osv5h8m2q5tv1npsch4.png)