How to automatically deploy a PHP project with one click

Source: Internet
Author: User
How to automate one-click deployment of PHP Project 1. skills required to automate one-click deployment of PHP: PHP (slightly understood), Shell (slightly understood), git (slightly understood) 2. enter the password for the final result, press enter, 5 ~ The execution results of log files, database backups, and databases generated after 20 seconds (generally once a day for continuous delivery, the amount of code deployed and SQL scripts are not very large) are as follows: & nbsp; 3. how to do the Part1-Git first need to have a VCS system, I am here using G how to automate one-click deployment of PHP projects

1. skill requirements

To automate one-click PHP deployment, you need to: PHP (slightly understood), Shell (slightly understood), and git (slightly understood)

2. Final results

Enter the password, press enter, 5 ~ 20 seconds later (generally, once a day for continuous delivery, the amount of deployed code and SQL scripts will not be large)

The execution results of log files, database backups, and databases are as follows:

3. how to make Part1-Git

First, we need to have a VCS System. here we use the Git client, both local and server. (I have never used Git to read this article. I just used it to get started)

Git pull is used only here, and the rest, such as svn, are the same.

Install the Git client on the Linux server. I have a script:

#Git relatedyum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel#Gityum -y install git-coregit config --global user.name "YourName"git config --global user.email YourMailecho "Git installation completed."


The failure rate of this piece of code is very high, and the cause is unknown. I am also a beginner in Linux. if the failure happens, I can try to open a sentence, but it may not be because the yum source of Linux has a problem, and there is no git or something.

Run git -- version after installation, as shown in the following figure:

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

Git version 1.8.2.1

OK. Next, the server will clone the code library from git in a proper location. You can.

4. how to make Part2-Shell

The basic work is here, which includes several parts: Update code, back up code, deploy code, modify the project config information, back up the database, and run the database script.

The rough code is below. I deleted some project-related items. you can also delete or add them as needed:

1 #! /Bin/bash 2 # Comment 3 [email protected] dreamingodd 4 # @ 20160226 5 # My project name is ocdp. 6 # 1.git pull 7 #2. backup PHP 8 #3. deploy PHP. 9 #4. change config in PHP project.10 #5. backup DB... 11 #6. deploy SQLs12 # listen 13 14 sys_time = 'date "+ % Y % m % d _ % H % M % S" '15 # Refresh git folder16 echo "---- 1.git processing" 17 # let's pretend your project is in/home/git/YourProjectName18 if [-d/home/git] 19 then20 echo "Git folder exists. "21 cd/home/git/YourProjectName22 git pull23 else24 mkdir/home/git25 echo" created Git folder "26 cd/home/git27 git clone https://github.com/YourGitName/YourProjectName28 Fi29 30 # Deploy YourProjectName31 echo "---- 2. backing up PHP "32 cd/var/www/html/33 if [-d/var/www/html/YourProjectName] 34 then35 echo" YourProjectName exists. "36 if [-d/home/back] 37 then38 echo" Backup folder exists. "39 else40 mkdir/home/back41 fi42 mv/var/www/html/YourProjectName/home/back/YourProjectName _ $ {sys_time} 43 mkdir/var/www/html/YourProjectName44 else45 # some config file creation for the first time deployment. some configuration settings for the first deployment: 46 #... 47 echo "YourProjectName config files are created. "48 fi49 echo" ---- 3. deploying PHP. "50 cd/var/www/html51 cp-r/home/git/YourProjectName. /YourProjectName52 # config change. you need to modify the config 53 #... 54 55 # MySQL backup56 echo "---- 5. backing up DB... "57 backupFile =/tmp/DB_backup $ {sys_time }. sql58 mysqldump-uroot-p $ {pwd} YourProjectName >$ {backupFile} 59 60 # MySQL script deployment61 echo "---- 6. deploying SQLs "62 if [-f/home/git/YourProjectName/SQL/deployment /*. SQL] 63 then64 for FILE in/home/git/YourProjectName/SQL/deployment /*. sql65 do66 sys_time = 'date "+ % Y % m % d _ % H % M % S" '67 mysql-uroot-p $ {pwd}-e "source $ FILE" | tee/tmp/DB_log _ $ {sys_time }. sql68 done69 fi

Now, run this script to test the permissions. most shell scripts fail due to permission issues.

In fact, it is now complete. Both log and SQL are available for viewing in/tmp, but we need to omit this step to even log on to the server, so we need to study apache.

5. how to make Part3-PHP

The following steps are required for php to run shell scripts. I also learned from other users. here I will explain my ideas and take a look at this article.

You need a shell execution user (EXECUTOR) to change the apache running user to EXECUTOR in httpd. conf, and add the execution permission without a password to EXECUTOR in sudo.

The PHP sample code is as follows:

123OCDP4161718

19 Auto-deploy Tool20 24 29 '; 37 fwrite ($ logFile, $ line.' \ n'); 38} 39 fclose ($ logFile); 40 if ($ returnVal) {41 echo'
Execution failed!
Details refer to/etc/httpd/logs/error_log '; 42} else {43 echo'
Execution completed!
Logs save in '. $ logFileName. ''; 44} 45} 46?> 47

4849


The password can be checked in php or shell. here I enter the database password.

6. Summary

The trouble is troublesome, but after the deployment is completed four or five times a day, it takes only two minutes to complete, and you will feel a sense of accomplishment.

Github address: https://github.com/dreamingodd/ocdp

If the above is helpful to you, please go to github and give me a star. thank you!

Original dreamingodd article. if the article is reproduced, 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.