Https://mp.weixin.qq.com/s/WH_JXah47BhQyviuuPAunw
Background
In small sites, it's quite handy to deploy PHP directly with git, and your remote site and local repository have a version control, and it's easy to track down problems or rollback.
Because in the small company to work, the original system is still using the traditional FTP upload deployment, deployment is too cumbersome, and not control the code on the line. On the Internet to find the next Git deployment tutorial, along the way down to meet a lot of pits, and now the whole process sent out, hoping to help everyone, the following words do not say more, come together to see the detailed introduction bar.
Account related
// Add git account // Key generation, if you already have a can skip // switch to git account // switch to the GIT account root directory mkdir // create the. SSH directory // setting the public key
Here must pay attention to the permissions problem, otherwise the key login cannot take effect, the corresponding permissions of each folder are as follows
. SSH folder permissionsid_rsa. Pub 644600
File permission settings
Put git in the same user group as the site that is running Nginx or Apache, such as www
VIM/ETC/PASSWD find git account and www account, change the group ID of git account to be consistent with WWW group identity
Site owner is set to git, user group is set to WWW//assuming nginx and git belong to www user group
Directory permission settings 775, file permissions set to 664
Warehouse Code
Server-side
// Initialize directory
Git configuration
git config receive.denyCurrentBranch ignore //设置仓库接受代码提交
Setting sudo free-secret
vim/etc/sudoers# Defaults Secure_path If there is no command you want, note that the command to add # php is required by default in Secure_ The last side of path is added:/usr/local/php/bin " in Root all=(all) The following line adds gitall = (all) nopasswd:# Save the exit so that the command to restart the queue for Laravel is ready to be used. # sudo php artisan queue:restart
Hook settings
CD. Git/hooks//Switch to site Hook directoryTouchPost-receive//When you create a receive submission, the hook//hook file reads as follows:#!/bin/sh#Set default permissions for account creation filesumask002unsetGIT_DIRCD..git checkout-F#Execute PHP Hook logic/usr/bin/curl http (s)://domain/Hook text position/hook.php#If you use the Laravel queue, you need to restart the queue process for the new code to take effect#sudo php artisan queue:restartExit0
hook.php Content
<? PHP /* *///TODO Security Restrictions//todo Other hooks behavior//Clear Opcacheif (version_compare (php_version, ' 5.5.0 ', ' >= ')) {Opcache_reset ();}
Add hooks to execute permissions
chmod a+x. git/hooks/post-receive
Local code
Git remote add repository name SSH://[email protected] Address:/Site Directory//Add remote repository git push remote repository name Master
Special attention
The user uploads the picture directory must be good to do the file neglect action, otherwise it is possible to clear the non-tracking files when this part of the file deleted, resulting in catastrophic results
A tutorial on how to implement GIT deployment in PHP