How to deploy a project to a remote server using git
If you are hosting a website on a remote server, you probably already know that it's really annoying to manually deploy a new feature or a bug fix on your remote server using an FTP client like Filezilla or Win-SCP everytime you make little changes.
Because you are good developers, you already use a source code manager like Github or Gitlab, but you don't know how to make the deployment easier. This post is made for you.
In this tutorial, you will learn how to deploy a project to a remote server using git commands.
In order to make it possible, you will need to satisfy some prerequisites
Because we will use git commandes, you must have
git installed on your local machine and the remote server.
You will also need an SSH access to your remote server.
That's it, we can now jump into it 👍
On your remote server
The first part needs to be done on your remote server.
The first thing we will need to do is to create a
bare repository. This can be done using the following command.
git init --bare project_name.git
--bare means that the folder created using this command will be a bit different compared to the traditional one. The folder structure is different but the main difference come from the fact that it contains no copy of your source files.
Of course, you can name the folder whatever you want.
Now that we have the
git folder, we will need to make few changes inside of it. We will need to create a
post-receive file in the
cd project_name.git cd hooks nano post-receive
In the file we've just created, we will need to indicate the work tree and the git directory.
- The work tree corresponds to you project folder. For instance, if you've hosted your project on
/home/user/project, you have to indicate the same path :
- The git directory corresponds to the path of the git folder we've create on the previous step.
#!/bin/sh # Sync new changes git --work-tree=/home/user/project --git-dir=/home/user/project/project_name.git checkout -f
You can, then, add some more commands to remove files, clear the cache or update the database like this :
# Example of commandes ## Go to project location cd /home/user/project ## Remove a file rm README.md ## Update database php bin/console doctrine:migrations:migrate ## Clearing the cache php bin/console cache:clear
You can now save and exit this file by pressing
y and finally
The remaining thing is to make this file executable.
chmod +x post-receive
Everything is now set up on your remote server to make the deployment easier.
Now, let's switch to your local computer and make some more changes.
On your local computer
Now, on your local computer, you will need to add a new remote to your git project. This can be done using the
git remote add command.
git remote add prod ssh_user@server_ip:/home/user/project/project_name.git
Don't forget to modify the
server_ip and the path to your project. Of course, you can name the remote what you want except
origin because it is reserved to the github remote.
Now, when you want to deploy you new feature on your remote server, just type the following command.
git push prod master
You will then have a preview of the command result on your terminal.
Using these informations, you should be able to implement this deployment system by yourself. You'll see, it is way easier to deploy changes on your remote server, trust me 👍