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
#!/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'
Here are the rough steps you need to follow in order to deploy a site through sg1.capitelsolutions.net:
- Initiate Create the local site repo, add .gitignore and package.json (NPM build scripts are here)
- Create virtual host file in the server in
/etc/httdp/sites-available
- Soft link the virtual host file by
ln -s /etc/httpd/sites-available/<site-name>.conf /etc/httpd/sites-enabled/.
- Restart httpd
systemctl restart httpd
- Create site directory
mkdir -p /var/www/<site-name>
- Initiate bare git repo.
git init --bare <site-name>.git
- Add post hook to bare git repo
- 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 _
.
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
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
When initiating a repo use this as your standard .gitignore:
/nbproject/
wp-config.php
*_log
*.log
.ftpquota
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 › 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"
}