How to set up a continous deployment pipeline for a Laravel App on a Linux server
Tags: linux, laravel, github, backend
Published: 2024-01-01 20:11:08



We have seen examples of how to automate application builds by [_dockerising_](https://dev.to/dreywandowski/how-to-dockerize-laravel-applications-48m7) and [_deploying them on azure cloud servers_](https://dev.to/dreywandowski/deploying-dockerised-laravel-applications-on-azure-581). This continous deployment which is being managed by Azure removes the burden of deployment from developers who can focus on the application itself. 

In some cases, we have provisioned Linux servers for ourselves and wish to have full control of the CI/CD process (and to stop using the old FTP upload method).

For this tutorial, we have a Linux Ubuntu server (> 18.0) from Azure and have a laravel app already pushed to github which we wish to run on the server. See the latter part of [_deploying laravel apps on a Linux server_](https://dev.to/dreywandowski/how-to-deploy-a-laravel-app-on-an-azure-ubuntu-server-1fl) after completing this tutorial to set up the deployed laravel app on the server.

1.cd to the desired directory you wish the app on your server and clone the app from Github:
```php
cd /var/www/html
git clone https://github.com/dreywandowski/my-repo.git
cd my-repo
```
2.Next, we need to create an SSH key-pair that would be used to access your server from Github:
```php
ssh-keygen -t ed25519 -C "your_email@email.com"
```
Please note that the email to use is the one used to sign in to your Github.
Press "Enter" to save to the default location. Also press "Enter" when asked for a paraphrase.

3.The next step is to start this key in background and add it
```php
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
``` 
4.Test the ssh connection to github:
```php
ssh -T git@github.com
```
You should see something like this, which confirms that you have successfully added the ssh key to your server:

![Git connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6gtpdfe7zxfuwj1zb8a.png)

5.We need to copy the contents of this ssh key and add them to Github:
```php
cat ~/.ssh/id_ed25519.pub
```

Go to your Github > Settings > SSH and GGP keys > New SSH Key 
Give it any title you wish and paste the content under the "key" column and save.

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

6.Next, we need to create a workflow file to sync changes from the repository to our server.
Navigate to the repository > Actions > Laravel >configure

![Laravel action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v696224ab8rf2m5sb3c.png)

7.Click "Commit changes", navigate to the directory on your server and pull the changes:

![commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8xyzfsrse9t34uy3ile.png)

You will see a .github folder in your directory where there exists a laravel.yaml file, open it up and edit thus:

![yaml file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zblgfujitvohsuadkhh.png)
Commit and push this to master. The deployment will fail because we are yet to define what HOST, USERNAME, PASSWORD all mean. Navigate to Settings > Actions secrets and variables > Repository secrets > New Repository secret.

Define **HOST**, **USERNAME**, and **PASSWORD** in turn, where HOST is your server Public IP, Username and password are your server login credentials.


Now, any push we make to the master branch from our IDE will be directly deployed on the server. You can check the workflow status under Actions.


![Action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2kw6r27dtki9mjxflhc.png)