Skip to content

Instantly share code, notes, and snippets.

@identor
Last active February 6, 2023 16:28
Show Gist options
  • Save identor/419040b6e743c47b0368c2079cf8e040 to your computer and use it in GitHub Desktop.
Save identor/419040b6e743c47b0368c2079cf8e040 to your computer and use it in GitHub Desktop.
Git deployment in Centos 7

General

Created a user named git, this shall function as the user where git repos are put into. The git repos in the home directory of the user are supposed to be bare repositories. To initialize a repository issue the command:

git init --bare <site-name>.git

Create new site script for host

#!/bin/bash

SITE_NAME=$1
DB_NAME=$2
SITES_DIR=/etc/httpd/sites-available
SITES_ENABLED_DIR=/etc/httpd/sites-enabled

SITE_CONF=$SITES_DIR/$SITE_NAME.conf
SITE_TMPL=/root/templates/site.conf
POST_HOOK_TMPL=/root/templates/post-update
WP_CONFIG_TMPL=/root/templates/wp-config.php

WWW_DIR=/var/www
SITE_DIR=$WWW_DIR/$SITE_NAME

GIT_DIR=/var/git
SITE_GIT_DIR=$GIT_DIR/$SITE_NAME.git
GIT_TMPLS_DIR=/var/git/templates

echo 'Creating files for site:' $SITE_NAME

echo 'Creating' $SITE_CONF
cat $SITE_TMPL | sed "s/site/$SITE_NAME/g" > $SITE_CONF

echo 'Enabling site configuration...'
ln -s $SITE_CONF $SITES_ENABLED_DIR/.

systemctl restart httpd

echo 'Creating site directory..' $SITE_DIR
mkdir -p $SITE_DIR
chown -R git:apache $SITE_DIR

echo 'Creating database...'
echo "CREATE DATABASE $DB_NAME;" | mysql

echo 'Initiating git dir...'
git init --bare $SITE_GIT_DIR
cat $POST_HOOK_TMPL | sed "s/site-db/$DB_NAME/g" | sed "s/site/$SITE_NAME/g" > $SITE_GIT_DIR/hooks/post-update
chomod +x $SITE_GIT_DIR/hooks/post-update
chown -R git:git $SITE_GIT_DIR

echo 'Creating wp-config template...'
mkdir -p $GIT_TMPLS_DIR/$SITE_NAME
cat $WP_CONFIG_TMPL | sed "s/site-db/$DB_NAME/g" > $GIT_TMPLS_DIR/$SITE_NAME/wp-config.php
chown -R git:git $GIT_TMPLS_DIR/$SITE_NAME

echo ''
echo 'Site creation finished'

Steps

Here are the rough steps you need to follow in order to deploy a site through sg1.capitelsolutions.net:

  1. Initiate Create the local site repo, add .gitignore and package.json (NPM build scripts are here)
  2. Create virtual host file in the server in /etc/httdp/sites-available
  3. Soft link the virtual host file by ln -s /etc/httpd/sites-available/<site-name>.conf /etc/httpd/sites-enabled/.
  4. Restart httpd systemctl restart httpd
  5. Create site directory mkdir -p /var/www/<site-name>
  6. Initiate bare git repo. git init --bare <site-name>.git
  7. Add post hook to bare git repo
  8. Push local commits to remote bare git repo

After creating the bare git repo a virtual host should be created, also along with it the root directory of the www files should also be created. Conventions for naming the directories should be using the repo-name.

Also creating the database where the application will be storing it's content is necessary. The naming convention for the database is to use the repo-name but replace any special characters with the underscore character _.

Standard files

There are certain files in which any site should have. These files have purposes in a specific install.

db.sql.gz - will be the file that will contain the database 

Adding post-update hooks

Post update hooks are scripts that are executed after the bare repository receives an update. The post update hook described in here will update the files in /var/www/<site-name>, the files that will be used in the update is the files in the git repo.

A template of the post-update hook is shown below:

#!/bin/bash
echo 'Updating /var/www/wp-test...'
git --work-tree=/var/www/wp-test --git-dir=/var/git/wp-test.git checkout -f

echo 'Copying wp-config.php template...'
cp /var/git/templates/wp-config.php /var/www/wp-test/wp-config.php -f

echo 'Updating database...'
gunzip -c /var/www/wp-test/db.sql.gz | mysql -u root thespotseo

.gitignore

When initiating a repo use this as your standard .gitignore:

/nbproject/
wp-config.php
*_log
*.log
.ftpquota

NPM build scripts

For simplicity's sake, there are things that are automated using npm scripts, some of the tasks include: automatically creating the database, update the urls, etc. These used to build the files sent to the server. A sample of an npm script used:

{
  "name": "thespotseo.com",
  "version": "1.0.0",
  "description": "<!DOCTYPE html> <html> <head> \t<meta name=\"viewport\" content=\"width=device-width\" /> \t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> \t<title>WordPress &#8250; ReadMe</title> \t<link rel=\"stylesheet\" href=\"wp-admin/css/install.css?ver=20100228\" type=\"text/css\" /> </head> <body> <h1 id=\"logo\"> \t<a href=\"https://wordpress.org/\"><img alt=\"WordPress\" src=\"wp-admin/images/wordpress-logo.png\" /></a> \t<br /> Version 4.6.1 </h1> <p style=\"text-align: center\">Semantic Personal Publishing Platform</p>",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "npm run update-devurls && php -S localhost:3000",
    "update-devurls": "npm run update-to-dev-siteurl && npm run update-to-dev-homeurl",
    "update-to-dev-siteurl": "wp option update siteurl http://localhost:3000",
    "update-to-dev-homeurl": "wp option update home http://localhost:3000",
    "update-siteurl": "wp option update siteurl http://wp-test.sg1.capitelsolutions.net",
    "update-homeurl": "wp option update home http://wp-test.sg1.capitelsolutions.net",
    "update-urls": "npm run update-siteurl && npm run update-homeurl",
    "archive-db": "mysqldump thespotseo | gzip -9 > db.sql.gz",
    "deploy": "npm run update-urls && npm run archive-db"
  },
  "author": "",
  "license": "ISC"
}

Server tools

In this section the tools and notes for maintaining The Spot SEO's server deployments in Digitalocean is discussed.

Digitalocean

CentOS 7 is the operating system used in every VPS deployed.

Servers under The Spot SEO

Here is a list of the server hostnames under The Spot SEO:

  1. sg1.capitelsolutions.net

Server software stack

Wordpress sites are the main web applications that are hosted in servers, the server software stack is solely based on this. The main software packages are listed here.

  1. Apache
  2. MySQL
  3. Php

Tools for maintainers

Several tools are used for maintaining the VPS. Some are documented below.

Site creation tool

A site creation tool was developed in order to automate virtualhost creation, database creation, git repository creation, and the post-update hook for the git repository. The site creation tool is runnable by the root user of the VPS. A private git repo hosts the latest version of this tool, checkout this (link)[https://gitlab.com/synerg/spot-server-tools].

Using the site creation tool

The site creation tool is an executable bash shell script which can be executed by the root user. The script is found at the home directory of the root user. The script basically use only two arguments for it to run, the first argument should serve as the website name (ex. www.test.com), and the second argument is for the database name (ex. test_db).

The site creation tool should execute properly if arguments presented to it are valid. Here are some conditions needed to be considered:

  1. Make sure that the site name argument used is unique (not existing in the system).
  2. Make sure that the database name argument used is unique (not existing in the system).
  3. Database name should use valid MySQL characters for database naming. Alphanumeric characters and underscores should be a safe bet.

The site creation tool should produce the following output sequentially:

  1. Create site virtualhost configuration based on the template site.conf file.
  2. Enable the site configuration.
  3. Restart the apache service via systemctl.
  4. Create the database.
  5. Initialize the bare git repository.
  6. Create the post-update hook (auto deploy files on push).
  7. Create the wp-config template (contains db configuration).

Sample usage of the script is provided below:

# This creates a virtual host for www.test.com, and creates a template to link the wordpress site to the test_db
./create-new-site www.test.com test_db

Adding a public key to git user

To allow the use of git services for developers adding the public key of their machines to the host is necessary. To add a key the root user must add a new line indicating a new key file. Edit the file @ /var/git/.ssh/authorized_keys. If for example the contents of the public file is:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCeIFZEm/ncsTGakcIhR9BRsShY3/tgYT3WZVi+Mh78Uyr5xq4Uxw4sj+8Un3LLghD9wczBkYjEEFLDRkU/jXqVPQxGxbWxUK/Xwtc2DcFE5JSfJADaoa0PJam21pNVzUDWOnUbJddUPWbvlNuagjG1cs45go40zDyTdLAz4rMXR7aSbmPBCQl0civmCKPeMurPRdugqYg71HLTzBTnDVGIP9n/nHtpgQAHG5w6Lq/croHGuGoivlt1R5AMwjYzYHvbvpbZg9l+CUZo1pEabh7YIiJUPtk/w9bjJFa55dMYmiwNbWCUoBi58GO2C9Eig1czT9J2Wih+I1FnXgC6z/Y1 irvin@luna

To add this to the server add a new line to the /var/git/.ssh/authorized_keys file. The file should look like this:

### Previously added keys are above
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCeIFZEm/ncsTGakcIhR9BRsShY3/tgYT3WZVi+Mh78Uyr5xq4Uxw4sj+8Un3LLghD9wczBkYjEEFLDRkU/jXqVPQxGxbWxUK/Xwtc2DcFE5JSfJADaoa0PJam21pNVzUDWOnUbJddUPWbvlNuagjG1cs45go40zDyTdLAz4rMXR7aSbmPBCQl0civmCKPeMurPRdugqYg71HLTzBTnDVGIP9n/nHtpgQAHG5w6Lq/croHGuGoivlt1R5AMwjYzYHvbvpbZg9l+CUZo1pEabh7YIiJUPtk/w9bjJFa55dMYmiwNbWCUoBi58GO2C9Eig1czT9J2Wih+I1FnXgC6z/Y1 irvin@luna

Server monitoring tools

Use htop to see current CPU and memory usage.

Developers tools setup

Tools used and how to setup the tools is documented in this section.

Tools for windows developers

Several tools are used for developing wordpress sites using the Digitalocean VPS setup for The Spot SEO client sites. The tools should be present in the developer workstation, installation is needed. Tools that will be used in this document are as follows:

  1. Git bash (https://git-scm.com/downloads)
  2. XAMPP (https://www.apachefriends.org/download.html)
  3. Node.js (https://nodejs.org/en/download/)

Setting up ssh in git-bash

To begin first generate a private key / public key combination by executing the following command in the git-bash window:

ssh-keygen

You would need to hit the enter key three times to generate the keys. These keys will be used in order to clone the repositories in the server. Once the creation of the keys is finished, the contents of the public key file in (~/.ssh/id_rsa.pub) should be sent to the server maintainer (vps-maintainers@capitelsolutions.net) via email. This will be added to the server. A reply message will be sent to confirm the addition of the public key by following the Adding a public key in the maintainers guide. To view the contents of ~/.ssh/id_rsa.pub a command can be easily executed via git bash:

cat ~/.ssh/id_rsa.pub

Since the ssh server of the VPSs run at a non standard port configuring the client ssh is recommended in order to clone easily. Edit the ~/.ssh/config file. The config file should contain the following after edit:

Host sg1.capitelsolutions.net
	HostName	sg1.capitelsolutions.net
	Port		2230

After setting up ssh in git, cloning a repository will now be possible, since standard naming conventions are followed when creating a site. Cloning the files for www.luxuryfootspa.com can be done through the command:

git clone git@sg1.capitelsolutions.net:www.luxuryfootspa.com.git

Standard files

A set of files should be present in the repository. These files will be used by the server for several things.

  1. db.sql.gz - A gunziped sql file which contains the current state of the application. This should be re-generated every time code is pushed to the server.
  2. wp-config.php - Should contain local settings for the wordpress instance.

Access control

Every server deployed in Digitalocean should be accessible using the private key default-do.ppk or its open ssh counter part openssh-default-do.ppk. The keys should enable root access to all VPSs. Both keys are shared to VPS Maintainers group members (vps-maintainers@capitelsolutions.net) via capitel solutions drive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment