Setup Git deployment on push to remote origin with post-update hook

Git

1. Generate SSH keys pair.
Use ssh-keygen command to generate SSH keys pair.
Example usage:

$ ssh-keygen -t rsa -b 4096 -C "taras@shkodenko.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tarasius/.ssh/id_rsa):

2. Setup SSH key authorization.
To setup autorization by SSH key add contents of your public key
($HOME/.ssh/id_rsa.pub) to $HOME/.ssh/authorized_keys file.
For example:

$ cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys

Or use command ssh-copy-id command like:

$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub tarasius@remote.server2.com
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tarasius@remote.server2.com's password:

3. Setup working copy Git repository.

$ cd $HOME/web/remote.server2.com
$ git init
Initialized empty Git repository in $HOME/web/remote.server2.com/.git/

4. Setup bare Git repository.

$ cd
$ git init --bare remote2.git
Initialized empty Git repository in /home/tarasius/remote2.git/

5. Git web hook script.

$ cp -fvp /home/tarasius/remote2.git/hooks/post-update.sample /home/tarasius/remote2.git/hooks/post-update
$ vim /home/tarasius/remote2.git/hooks/post-update


#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
#
# exec git update-server-info

unset GIT_INDEX_FILE

export GIT_WORK_TREE=/home/tarasius/web/remote.server2.com/
export GIT_DIR=/home/tarasius/remote2.git/

/usr/bin/git checkout -f

$

6. Add remote origin
To view list or remote origins:

$ git remote -v

To add remote origin:

$ git remote add origin tarasius@remote.server2.com:/home/tarasius/remote2.git

7. To deploy changes
To deploy your changes push commit:

$ git push origin master

In this example:

/home/tarasius - example user home directory $HOME or ~
/home/tarasius/web/remote.server2.com/ - folder with application working copy
/home/tarasius/remote2.git/ - bare Git repository
remote.server2.com - host name of server with git repositories

mysql and mysqldump –defaults-file without a password

MySQL has an amazing option called –defaults-file. It can be used to store credentials in configuration file as it shown in example below:


[client]
user=dbUser
password=dbPassword

Make sure you did not store such configration files anywhere inside public_html accessible via HTTP or HTTPS on your website. It is real security breach. Do not do it. Consider to change –defaults-file ../some_secure_path_outside_www/.db1.my.cnf permissions as low as 600 only accessible to user to read/write it. Nobody other should have access to it. Store such files outside of any virtual host htdocs, www or public_html folders.

With such configation files you can skip authorization when using mysql, mysqldump commands.
Example commands are:
1. To make db backup you can run command like:

# mysqldump --defaults-file=../some_secure_path_outside_www/.db1.my.cnf db1 > db1.sql

2. To restore db from SQL dump file you can execute command below:

# mysql --defaults-file=../some_secure_path_outside_wwwpath/.db1.my.cnf db1 < db1.sql

You could also use my PHP cli scripts to generate such .cnf configuration files automatically from a popular CMS and frameworks at: https://github.com/podlom/get_mysql_db_php_from_cms

Example usage of command below:

# php get_create_my_cnf.php /home/taras/public_html >../some_secure_path_outside_www/.db1.my.cnf

Please, also look at documentation for more details: https://dev.mysql.com/doc/refman/5.5/en/option-file-options.html

Yii2 Framework console built-in web server can’t start

If you can`t start built-in web server in Yii2 Framework on Windows with error message like Document root “…\console/web” does not exist. in advanced application template you should use the following commands:

1. For backend admin application:


yii.bat serve/index --docroot="backend/web/"

2. For frontend application:


yii.bat serve/index --docroot="frontend/web/"

For basic application template use command:


yii.bat serve/index --docroot="web/"

On Linux or MacOS use ./yii instead of yii.bat

Check PHP files syntax for list of files on Linux

To check all PHP files syntax in current folders including subfolders you can use command like:

# find . -type f -name "*.php" -exec php -l {} \; | grep -v 'No syntax errors'

If you want to check few files syntax you can add them to some file e.g. /tmp/check-php-files-syntax.list and then run command below:

# for file in `cat /tmp/check-php-files-syntax.list`; php -l ${file}; done

Git: create archive

Git

As you may know Git has wonderful feature create archive of files in repository.

For example, you can create Bash script to automate making of Git archives:

#!/bin/bash

GIT_DIR="/home/taras/git_working_copy_dir/"
ZIP_NAME="_git_archives/taras-`date +'%Y%m%d%H%M'`.zip"

cd ${GIT_DIR}
pwd
# make zip archive from current git HEAD branch
git archive HEAD --format=zip -o ${ZIP_NAME}
# delete some files from zip archive
zip -d ${ZIP_NAME} ./.gitignore
#
echo "Archive file ${ZIP_NAME} is:"
ls -alh ${ZIP_NAME}
#
echo "Archive content:"
unzip -l ${ZIP_NAME} |more

Also, you can use tar format instead of zip and then to gzip it.

# git archive --format=tar --prefix=git-1.0/ v1.0 | gzip >git-1.0.tar.gz

v1.0 – is tag name of release to archive
–prefix=git-1.0/ – adds folder git-1.0/ inside archive
–format=tar – use tar format

Git: list files between two commits

Git

To display changes between two commits you need to know SHA sum of commit.
You can take it from

# git log

command output.

Example command output:

commit 5b75d2ecf30b2ee9b28ca1febf60fb96b4d8625c
Merge: 78ac4ff4 ccb47ced
Author: Author Name <autor.name@email.com>
Date:   Mon Feb 13 22:27:55 2017 +0000

    Commit description goes here...

commit 4b6197f19f8c196e246d6277059f1d784e635a67
Author: Author Name <autor.name@email.com>
Date:   Mon Feb 13 20:34:38 2017 +0600

    Another commit description goes here...

Then you can run command:

# git diff --name-only 4b6197f19f8c196e246d6277059f1d784e635a67 5b75d2ecf30b2ee9b28ca1febf60fb96b4d8625c

it will show you list of changed files.

Alternatively, you can also use command:

# git diff --name-only HEAD~7 HEAD~14

to see the difference between seventh and fourteenth latest commits.