Home >Backend Development >PHP Tutorial >怎么自动化一键部署PHP项目

怎么自动化一键部署PHP项目

WBOY
WBOYOriginal
2016-06-13 12:29:221114browse

如何自动化一键部署PHP项目

1.技能需求

完成自动化一键部署PHP需要:PHP(略懂),Shell(略懂),git(略懂)

2.最终效果

输入密码,回车,5~20秒后(一般一天一次持续交付,部署的代码量和sql脚本都不会很大)

执行产生的log文件、数据库备份和数据库执行结果在这里:

 

3.如何做的Part1-Git

首先要有一个VCS系统,我这里用的是Git客户端,本地和服务器都是。(没用过Git可以看这篇文章,我就是用它入门的)

这里只用到Git pull,其他的如svn等,都是一样的。

Linux服务器装Git客户端,我有一个脚本:

 

<span style="color: #000000;">#Git related</span><span style="color: #0000ff;">yum</span> -y <span style="color: #0000ff;">install</span> curl-devel expat-devel gettext-devel openssl-devel zlib-<span style="color: #000000;">devel#Git</span><span style="color: #0000ff;">yum</span> -y <span style="color: #0000ff;">install</span> git-<span style="color: #000000;">coregit config </span>--global user.name <span style="color: #800000;">"</span><span style="color: #800000;">YourName</span><span style="color: #800000;">"</span><span style="color: #000000;">git config </span>--<span style="color: #000000;">global user.email YourMail</span><span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">Git installation completed.</span><span style="color: #800000;">"</span>

 

 

 


这一段代码失败率很高,原因不详,我Linux也是初级,如果失败可以尝试一句一句手打,还不行可能是Linux的yum源有问题,没有git下载地址什么的。

 

安装好之后运行git --version,像这样:

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

git version 1.8.2.1

就OK。接下来服务器一个合适的位置git clone 下来代码库。就可以了。

 

4.如何做的Part2-Shell

 

基本工作都在这里,包含几部分内容:更新代码,备份代码,部署代码,修改项目config信息,备份数据库,跑数据库脚本。

大概的代码在下面,我删了一些项目相关的,你也可以根据自己的需求删减或添加:

 

<span style="color: #008080;"> 1</span> #!/bin/<span style="color: #000000;">bash</span><span style="color: #008080;"> 2</span> #--------------------------------------------------------------------------------------------------<span style="color: #008080;"> 3</span> <span style="color: #000000;">[email protected] dreamingodd</span><span style="color: #008080;"> 4</span> #@<span style="color: #800080;">20160226</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">#My project name is ocdp.</span><span style="color: #008080;"> 6</span> #<span style="color: #800080;">1</span><span style="color: #000000;">.git pull</span><span style="color: #008080;"> 7</span> #<span style="color: #800080;">2</span><span style="color: #000000;">.backup PHP</span><span style="color: #008080;"> 8</span> #<span style="color: #800080;">3</span><span style="color: #000000;">.deploy PHP.</span><span style="color: #008080;"> 9</span> #<span style="color: #800080;">4</span>.change config <span style="color: #0000ff;">in</span><span style="color: #000000;"> PHP project.</span><span style="color: #008080;">10</span> #<span style="color: #800080;">5</span><span style="color: #000000;">.backup DB...</span><span style="color: #008080;">11</span> #<span style="color: #800080;">6</span><span style="color: #000000;">.deploy SQLs</span><span style="color: #008080;">12</span> #--------------------------------------------------------------------------------------------------<span style="color: #008080;">13</span> <span style="color: #008080;">14</span> sys_time=`<span style="color: #0000ff;">date</span> <span style="color: #800000;">"</span><span style="color: #800000;">+%Y%m%d_%H%M%S</span><span style="color: #800000;">"</span><span style="color: #000000;">`</span><span style="color: #008080;">15</span> <span style="color: #000000;">#Refresh git folder</span><span style="color: #008080;">16</span> <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">----1.git processing</span><span style="color: #800000;">"</span><span style="color: #008080;">17</span> #Let<span style="color: #800000;">'</span><span style="color: #800000;">s pretend your project is in /home/git/YourProjectName</span><span style="color: #008080;">18</span> <span style="color: #0000ff;">if</span> [ -d /home/<span style="color: #000000;">git ]</span><span style="color: #008080;">19</span> <span style="color: #0000ff;">then</span><span style="color: #008080;">20</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">Git folder exists.</span><span style="color: #800000;">"</span><span style="color: #008080;">21</span>     cd /home/git/<span style="color: #000000;">YourProjectName</span><span style="color: #008080;">22</span> <span style="color: #000000;">    git pull</span><span style="color: #008080;">23</span> <span style="color: #0000ff;">else</span><span style="color: #008080;">24</span>     <span style="color: #0000ff;">mkdir</span> /home/<span style="color: #000000;">git</span><span style="color: #008080;">25</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">created Git folder</span><span style="color: #800000;">"</span><span style="color: #008080;">26</span>     cd /home/<span style="color: #000000;">git</span><span style="color: #008080;">27</span>     git clone https:<span style="color: #008000;">//</span><span style="color: #008000;">github.com/YourGitName/YourProjectName</span><span style="color: #008080;">28</span> <span style="color: #0000ff;">fi</span><span style="color: #008080;">29</span> <span style="color: #008080;">30</span> <span style="color: #000000;">#Deploy YourProjectName</span><span style="color: #008080;">31</span> <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">----2.backing up PHP</span><span style="color: #800000;">"</span><span style="color: #008080;">32</span> cd /var/www/html/<span style="color: #008080;">33</span> <span style="color: #0000ff;">if</span> [ -d /var/www/html/<span style="color: #000000;">YourProjectName ]</span><span style="color: #008080;">34</span> <span style="color: #0000ff;">then</span><span style="color: #008080;">35</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">YourProjectName exists.</span><span style="color: #800000;">"</span><span style="color: #008080;">36</span>     <span style="color: #0000ff;">if</span> [ -d /home/<span style="color: #000000;">back ]</span><span style="color: #008080;">37</span>     <span style="color: #0000ff;">then</span><span style="color: #008080;">38</span>         <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">Backup folder exists.</span><span style="color: #800000;">"</span><span style="color: #008080;">39</span>     <span style="color: #0000ff;">else</span><span style="color: #008080;">40</span>         <span style="color: #0000ff;">mkdir</span> /home/<span style="color: #000000;">back</span><span style="color: #008080;">41</span>     <span style="color: #0000ff;">fi</span><span style="color: #008080;">42</span>     <span style="color: #0000ff;">mv</span> /var/www/html/YourProjectName /home/back/<span style="color: #000000;">YourProjectName_${sys_time}</span><span style="color: #008080;">43</span>     <span style="color: #0000ff;">mkdir</span> /var/www/html/<span style="color: #000000;">YourProjectName</span><span style="color: #008080;">44</span> <span style="color: #0000ff;">else</span><span style="color: #008080;">45</span>     #some config <span style="color: #0000ff;">file</span> creation <span style="color: #0000ff;">for</span> the first <span style="color: #0000ff;">time</span><span style="color: #000000;"> deployment.一些首次部署的config设置</span><span style="color: #008080;">46</span> <span style="color: #000000;">    #...</span><span style="color: #008080;">47</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">YourProjectName config files are created.</span><span style="color: #800000;">"</span><span style="color: #008080;">48</span> <span style="color: #0000ff;">fi</span><span style="color: #008080;">49</span> <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">----3.deploying PHP.</span><span style="color: #800000;">"</span><span style="color: #008080;">50</span> cd /var/www/<span style="color: #000000;">html</span><span style="color: #008080;">51</span> <span style="color: #0000ff;">cp</span> -r /home/git/YourProjectName ./<span style="color: #000000;">YourProjectName</span><span style="color: #008080;">52</span> <span style="color: #000000;">#config change.你的项目部署需要的config修改</span><span style="color: #008080;">53</span> <span style="color: #000000;">#...</span><span style="color: #008080;">54</span> <span style="color: #008080;">55</span> <span style="color: #000000;">#MySQL backup</span><span style="color: #008080;">56</span> <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">----5.backing up DB...</span><span style="color: #800000;">"</span><span style="color: #008080;">57</span> backupFile=/tmp/<span style="color: #000000;">DB_backup${sys_time}.sql</span><span style="color: #008080;">58</span> mysqldump -uroot -p${<span style="color: #0000ff;">pwd</span>} YourProjectName><span style="color: #000000;">${backupFile}</span><span style="color: #008080;">59</span> <span style="color: #008080;">60</span> <span style="color: #000000;">#MySQL script deployment</span><span style="color: #008080;">61</span> <span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">----6.deploying SQLs</span><span style="color: #800000;">"</span><span style="color: #008080;">62</span> <span style="color: #0000ff;">if</span> [ -f /home/git/YourProjectName/sql/deployment<span style="color: #008000;">/*</span><span style="color: #008000;">.sql ]</span><span style="color: #008080;">63</span> <span style="color: #008000;">then</span><span style="color: #008080;">64</span> <span style="color: #008000;">    for FILE in /home/git/YourProjectName/sql/deployment/*.sql</span><span style="color: #008080;">65</span> <span style="color: #008000;">    do</span><span style="color: #008080;">66</span> <span style="color: #008000;">        sys_time=`date "+%Y%m%d_%H%M%S"`</span><span style="color: #008080;">67</span> <span style="color: #008000;">        mysql -uroot -p${pwd} -e "source $FILE" | tee /tmp/DB_log_${sys_time}.sql</span><span style="color: #008080;">68</span> <span style="color: #008000;">    done</span><span style="color: #008080;">69</span> <span style="color: #008000;">fi</span>

 

现在运行这个脚本测试一下,新手注意权限问题,大多shell脚本的失败都是权限问题。

 

其实现在已经算是完成了,log和sql都在/tmp中可供查看,但是我们是要连登陆服务器这一步都要省略掉的变态,所以我们还要研究一下apache。

 

5.如何做的Part3-PHP

想要让php运行shell脚本需要以下步骤,我也是从跟别人学的,这里说一下思路,具体看一看这篇文章

你需要一个执行shell的用户(EXECUTOR),在httpd.conf里面把apache运行用户改成EXECUTOR,在sudo里面给EXECUTOR加上无需密码的运行权限。

PHP示例代码如下:

 

<span style="color: #008080;"> 1</span> <span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <title>OCDP</title><span style="color: #008080;"> 4</span> <style type="text/css"><span style="color: #008080;"> 5 <span style="color: #008000;">#<span style="color: #008000;">container {<span style="color: #008080;"> 6     margin: 0<span style="color: #000000;"> auto;<span style="color: #008080;"> 7     border:<span style="color: #000000;"> 1px solid grey;<span style="color: #008080;"> 8     width:<span style="color: #000000;"> 500px;<span style="color: #008080;"> 9     padding:<span style="color: #000000;"> 15px;<span style="color: #008080;">10     margin-top:<span style="color: #000000;"> 100px;<span style="color: #008080;">11 <span style="color: #000000;">}<span style="color: #008080;">12 <span style="color: #000000;">h3 {<span style="color: #008080;">13     text-align:<span style="color: #000000;"> center;<span style="color: #008080;">14 <span style="color: #000000;">}<span style="color: #008080;">15 </style><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <div id="container">
<span style="color: #008080;">19</span> <h3>Auto-deploy Tool</h3>
<span style="color: #008080;">20</span> <span style="color: #000000;">php </span><span style="color: #008080;">21</span> <span style="color: #800080;">$pwd</span> = <span style="color: #800080;">$_POST</span>["pwd"<span style="color: #000000;">];</span><span style="color: #008080;">22</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$pwd</span><span style="color: #000000;">)) {</span><span style="color: #008080;">23</span> ?><span style="color: #008080;">24</span>     <form method="post" id="form">
<span style="color: #008080;">25</span>         Input Password:<span style="color: #008080;">26</span>         <input type="password" id="pwd" name="pwd" value="<?php echo <span style=" color:>$_POST['pwd'];?>"><span style="color: #008080;">27</span>         <input type="submit" value="Deploy"><span style="color: #008080;">28</span>     </form>
<span style="color: #008080;">29</span> <span style="color: #000000;">php </span><span style="color: #008080;">30</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {</span><span style="color: #008080;">31</span>     <span style="color: #800080;">$command</span> = "sudo /home/git/ocdp/sh/deploy.sh <span style="color: #800080;">$pwd</span>"<span style="color: #000000;">;</span><span style="color: #008080;">32</span>     <span style="color: #800080;">$logFileName</span> = "/tmp/deploy_".<span style="color: #008080;">date</span>('Ymd_His').".log"<span style="color: #000000;">;</span><span style="color: #008080;">33</span>     <span style="color: #800080;">$logFile</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$logFileName</span>, 'w') or <span style="color: #0000ff;">die</span>('File: '.<span style="color: #800080;">$logFileName</span>.' open failed!'<span style="color: #000000;">);;</span><span style="color: #008080;">34</span>     <span style="color: #008080;">exec</span>(<span style="color: #800080;">$command</span>, <span style="color: #800080;">$outputArray</span>, <span style="color: #800080;">$returnVal</span><span style="color: #000000;">);</span><span style="color: #008080;">35</span>     <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$outputArray</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$line</span><span style="color: #000000;">) {</span><span style="color: #008080;">36</span>         <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$line</span>.'<br>'<span style="color: #000000;">;</span><span style="color: #008080;">37</span>         <span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$logFile</span>, <span style="color: #800080;">$line</span>.'\n'<span style="color: #000000;">);</span><span style="color: #008080;">38</span> <span style="color: #000000;">    }</span><span style="color: #008080;">39</span>     <span style="color: #008080;">fclose</span>(<span style="color: #800080;">$logFile</span><span style="color: #000000;">);</span><span style="color: #008080;">40</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$returnVal</span><span style="color: #000000;">) {</span><span style="color: #008080;">41</span>         <span style="color: #0000ff;">echo</span> '<br><span style="color:red;">执行失败!<br>Details refer to /etc/httpd/logs/error_log</span>'<span style="color: #000000;">;</span><span style="color: #008080;">42</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {</span><span style="color: #008080;">43</span>         <span style="color: #0000ff;">echo</span> '<br><span style="color:green;">执行完毕!<br>Logs save in '.<span style="color: #800080;">$logFileName</span>.'</span>'<span style="color: #000000;">;</span><span style="color: #008080;">44</span> <span style="color: #000000;">    }</span><span style="color: #008080;">45</span> <span style="color: #000000;">}</span><span style="color: #008080;">46</span> ?><span style="color: #008080;">47</span> </div><span style="color: #008080;">48</span> <span style="color: #008080;">49</span> 

 


密码可以在php或者shell中检查,我这里输入的是数据库密码。

 

 

6.总结

麻烦是麻烦了点,但是完成之后当你每天部署4、5次,加起来只用了2分钟的时候,你就能感觉到慢慢的成就感了。

github 项目地址:https://github.com/dreamingodd/ocdp

如果以上对你有一点点用处的话,请到github上给我点个star吧,拜谢!

 

dreamingodd原创文章,如转载请注明出处。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn