Deploying Your Projects with Git
There are so many different ways to deploy a web project, many IDEs will have deployment tools to assist with this using ftp/sftp.
For web projects I generally use JetBrain’s PHPStorm as my IDE. You can configure a deployment server and sync the files on save or manually sync them. However, as projects grow to significant sizes, the file comparison tool and sync tool slow down considerably, even becoming unusable when you are trying to compare changes in the complete project.
One of my preferred ways to deploy a project, is by using a git hook on the server and git pushing my branch up to it.
I haven’t worked on any project that has not use git for source control in a very long time. Since I am already working in git, having a git command to deploy becomes very convenient. Especially when you have many environments to deploy to.
Another benefit is, that it allows you to set some rules for deploying into specific environments, such as only the master branch can be pushed to the production server. You may also add in any other operations you like as well during the deployment, such as applying migrations, updating composer or npm and anything else. You can even do things like change the contents of a config file for that specific environment or start a Continuous Integration run. Or it also makes it simple to allow your CI tool to automatically push a specific branch to your server on a successful run.To begin with you will need to make sure you have git installed on your server. You can verify that it is installed by executing the following command on the server:
If it is not installed, install it using the following command.
For Debian and Ubuntu.
For RedHat, CentOS and Fedora.
We will need to create a directory on the server to store the repository. This can be anywhere you like. Generally I use “/usr/local/git”. You can name the project anything you like as well, in this case I will use “my-project.git”.
Next we need to initialize the repository as a bare repo.
Now we will to add in our git hook that will do all the work. This hook can take on many different instructions depending on what you want to enforce. But we will keep it simple and only allow the master branch to be pushed to this server. I am assuming your web project root is located at “/var/www/html”. You can rename the path as appropriate in the “post-receive” file.
Using your favorite editor, paste in the following into a file called “post-receive”.
Next we need to fix the repo permissions.
Set the user and group permissions appropriately for all the files and directories so that all users you want to allow to push to that server can. In this case I am assuming you have a user: deploy and a group: webdev.
Lastly our hook file needs to be executable as well.
Now we need to let our git client know about the new git remote. At the root of your local project directory execute the following commands. You may name the remote anything you like, here I am assuming we are pushing to a production server.
And that is it, we are now ready to “git push” our project on the server.
All you will need to do from here on is use the following command to deploy to your server.
Happy Deploying!