In a recent article, I described how to migrate WordPress blog to DigitalOcean server, but uploading the files there was done using SCP, which, like FTP, is quite inconvenient. Also in this case, difficulties arise for projects that require team development.

The correct and convenient approach to publishing a project on a server would be to publish using Git. In this article, I will describe How to create a Git server for automate deployment sites.

When creating, we will assume that the site is already on your server, if not, upload it there using FTP or SCP.

So, creating a Git server will consist of 5 steps:

  1. Adding SSH Key to the Access List on the server
  2. Creating a project repository and bare git repository
  3. Creating Git Hooks
  4. Cloning a project to a computer and push a project
  5. Creating a .gitignore file for WordPress

Adding SSH Key to the Access List on the server

First, we need to be able to access the server using SSH. To do this, add to the ~/.ssh folder in the authorized_keys file the public keys of those users who will be given access to work with this server and publish the project.

To generate an SSH key on your computer, follow these instructions: for Windows, for MacOS or Linux.

Creating a project repository and bare git repository

Suppose your project is already deployed to the server along the path /var/www/project_com/public_html . In this case, you need to create a repository one level above the public_html folder. In no case do not create a repository inside public folder, because this leads to a security breach and has already led to the leak of the source code of many sites. Thus, the following commands will execute correctly in the console.

cd /var/www/project_com/
git init
git add -A
git commit -m 'Initial commit'

Now we need to create a bare git repository somewhere outside the working directory, for example, along the path /var/www/project_com.git/ To do this, run the following commands:

cd  /var/www/
git clone --bare project_com project_com.git
cd project_com
git remote add hub ../project_com.git
git remote show hub

The last command is optional, it is only needed to verify that you did everything right.

Now, in the /var/www/ directory, enter the ls –la command to make sure that the bare repository does not have root privileges. If you have something like this (not right):

Bare git repository folder with root privileges
Bare git repository folder with root privileges

Then enter the command to change the owner of the folder and all files attached to it:

sudo chown -R user:group project_com.git

In this command, in the place of “user” write the name of the owner of the folder (in the example in the photo above – andrey), “group” – the group of the user (in the example in the photo above – andrey), and in the place of “project_com.git” enter your bare git repository.

If it turned out as follows, then you did everything right.

Correct privileges for Bare git repository folder
Correct privileges for Bare git repository folder

Creating Git Hooks

In the /var/www/project_com/.git/hooks/ folder, create a post-commit file and set it to permission 700.

cd  /var/www/project_com/.git/hooks/
touch post-commit
chmod 700 post-commit
nano post-commit

Inside the file, write the following.

#!/bin/sh
echo
echo "Push changes ....."
echo
git push hub

Now in the /var/www/project_com.git/hooks/ folder, create a post-update file and set it to permission 700.

cd /var/www/project_com.git/hooks/
touch post-update
chmod 700 post-update
nano post-update

Inside the file, write the following.

#!/bin/sh
echo
echo "Pull changes....."
echo
cd /var/www/project_com/ || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info

Don’t forget replace “project_com” with your project folder.

Cloning a project to a computer and push to the server

Now everything is ready to work, you can simply clone your project from the server to your computer using the command:

git clone username@server_ip:/var/www/project_com.git

Now, when you make some changes to the project files on the server and want to save them, write the following.

git add .
git commit -m 'Commit name'

And at the first commit on the server you will need to enter the command:

git push --set-upstream hub master
First git commit on the server
First git commit on the server

If you change the project on the local computer, you first get new changes from the server with the git pull command, and then save and send to the server:

git add .
git commit -m 'Commit name'
git push
Push project to the server
Push project to the server

Creating a .gitignore file for WordPress

Please note that you should not add sensitive information in commits, for example, files containing database connection settings. Also, files that are automatically generated or that have a large size and rarely change are often excluded from commits. For example, below I’ll give you a gitignore file for a WordPress blog with standard functionality.

# ignore everything in the root except the "wp-content" directory.
!wp-content/

# ignore everything in the "wp-content" directory, except:
# "mu-plugins", "plugins", "themes" directory
wp-content/*
!wp-content/mu-plugins/
!wp-content/plugins/
!wp-content/themes/

# ignore these plugins
wp-content/plugins/hello.php

# ignore specific themes
wp-content/themes/twenty*/

# ignore node dependency directories
node_modules/

# ignore log files and databases
*.log
*.sql
*.sqlite

I hope after reading this article you can easily create a Git server for automate deployment sites.

Share post
Twitter Facebook