How to automate one-click deployment of PHP Projects

Source: Internet
Author: User
How to automate one-click deployment of PHP Projects

1. Skill Requirements

Complete Automation One-click Deployment PHP needs: PHP (slightly understand), Shell (slightly understand), git (slightly understand)

2. Final effect

Enter password, return, 5-20 seconds (usually one time a day of continuous delivery, the amount of code deployed and SQL scripts are not very large)

Execute the resulting log file, database backup, and database execution results here:

3. How to do the Part1-git

First, there is a VCS system, I use a git client here, both local and server. (I didn't use git to read this article, I just used it to get started.)

Git pull is used here, and other things like SVN are the same.

Linux server loaded git client, I have a script:

#Git Related Yum Install curl-devel expat-devel gettext-devel openssl-devel zlib-devel#gityumInstall git-"YourName"--global user.email yourmail  echo"Git installation completed. "

This section of code failure rate is very high, the reason is unknown, I am Linux is also a beginner, if the failure can try a sentence to hit the hand, it is not possible that the Linux Yum source has a problem, there is no git download address or anything.

Run git--version after installation, like this:

[[email protected] tmp]# git--version

git version 1.8.2.1

Just OK. Next server A suitable location git clone down code base. You can do it.

4. How to do the Part2-shell

Basic work is here, including several parts: update code, backup code, deploy code, modify project config information, backup database, run database script.

The approximate code below, I deleted some items related, you can also cut or add according to your own needs:

1#!/bin/Bash2#--------------------------------------------------------------------------------------------------3 [email protected] dreamingodd4#@201602265 #My project name is OCDP.6#1. Git Pull7#2. Backup PHP8#3. Deploy PHP.9#4. Change ConfiginchPHP project.Ten#5. Backup DB ... One#6. Deploy Sqls A#-------------------------------------------------------------------------------------------------- -  -Sys_time= 'Date "+%y%m%d_%h%m%s"` the #Refresh git folder - Echo "----1.git Processing" -#Let's Pretend your project is In/home/git/yourprojectname - if[-d/home/git] +  Then -     Echo "Git folder exists." +cd/home/git/Yourprojectname A git Pull at Else -     mkdir/home/git -     Echo "created Git folder" -cd/home/git -git clone https://Github.com/yourgitname/yourprojectname - fi in  - #Deploy Yourprojectname to Echo "----2.backing up PHP" +cd/var/www/html/ - if[-d/var/www/html/Yourprojectname] the  Then *     Echo "Yourprojectname exists." $     if[-d/home/Back ]Panax Notoginseng      Then -         Echo "Backup folder exists." the     Else +         mkdir/home/ Back A     fi the     MV/var/www/html/yourprojectname/home/back/Yourprojectname_${sys_time} +     mkdir/var/www/html/Yourprojectname - Else $#some ConfigfileCreation forThe first Timedeployment. Some first-time deployment config settings $     #... -     Echo "yourprojectname config files are created." - fi the Echo "----3.deploying PHP." -cd/var/www/HTMLWuyi CP-r/home/git/yourprojectname./Yourprojectname the #config change. config modifications required for your project deployment - #... Wu  - #MySQL Backup About Echo "----5.backing up DB ..." $backupfile=/tmp/Db_backup${sys_time}.sql -mysqldump-uroot-p${pwd} yourprojectname>${backupfile} -  - #MySQL Script Deployment A Echo "----6.deploying Sqls" + if[-f/home/git/yourprojectname/sql/deployment/*. sql] the  Then - For FILE In/home/git/yourprojectname/sql/deployment/*.sql $  Do the sys_time= ' date ' +%y%m%d_%h%m%s "' the Mysql-uroot-p${pwd}-E "source $FILE" | Tee/tmp/db_log_${sys_time}.sql the  Done the fi

Now run this script test, novice attention to permissions issues, most of the shell script failure is a permissions issue.

In fact, now is finished, log and SQL are available in/tmp for viewing, but we are to even login server This step is to omit the metamorphosis, so we also have to study Apache.

5. How to do the part3-php

To get PHP to run the shell script requires the following steps, I also learn from others, here to talk about the idea, specifically to see this article

You need a user executing the shell (EXECUTOR), in the httpd.conf inside the Apache run user to EXECUTOR, in sudo to EXECUTOR plus no password-free running permissions.

The PHP sample code is as follows:

1 2 3 <title>Ocdp</title>4  -  -  -  + 

Auto-deploy Tool

- Php + $pwd=$_post["PWD"]; A if(Empty($pwd)) { at?> - in Php -}Else { to $command= "sudo/home/git/ocdp/sh/deploy.sh$pwd"; + $logFileName= "/tmp/deploy_".Date(' Ymd_his '). " Log; - $logFile=fopen($logFileName, ' W ') or die(' File: '.$logFileName. ' Open failed! ');; the exec($command,$outputArray,$returnVal); * foreach($outputArray as $line) { $ Echo $line.'
';Panax Notoginseng fwrite($logFile,$line.' \ n '); - } the fclose($logFile); + if($returnVal) { A Echo'
Execution failed!
Details refer To/etc/httpd/logs/error_log'; the}Else { + Echo'
execution finished!
Logs save in '. $logFileName. ''; - } $ } $?> - - the


Passwords can be checked in PHP or in the shell, and I'm typing a database password here.

6. Summary

Trouble is a bit of trouble, but when you do deploy 4 or 5 times a day and add up to only 2 minutes, you can feel a sense of accomplishment.

GitHub Project Address: HTTPS://GITHUB.COM/DREAMINGODD/OCDP

If the above is of little use to you, please give me a star on GitHub, humbly!

Dreamingodd original articles, such as reproduced please indicate the source.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.