基於 Jenkins + Git + Ansible 發布 PHP 項目
1、部署 PHP 運行環境
PHP 是一個動態程序,負責解析 PHP-FPM 服務,而這個服務不支持靜態網頁處理,一般結合 Nginx 解決這個問題。Nginx 本身是一個靜態 Web 服務器,並不支持解析 PHP 程序,但它支持了 FastCGI 接口來調用動態服務來解析 PHP 程序。
當客戶端請求 PHP 頁面時,Nginx 通過 fastcgi 接口轉發給本地 9000 端口的 PHP-FPM 子進程處理,處理完成后返回 Nginx。
1)安裝 Nginx
配置 Nginx 網絡源
[root@tomcat ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
安裝並啟動
[root@tomcat ~]# yum -y install nginx
[root@tomcat ~]# systemctl start nginx
2)安裝 PHP
安裝 PHP 依賴的第三方庫,命令如下:
[root@tomcat ~]# yum -y install gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel gcc gcc-c++ make openssl-*
編譯安裝 PHP
[root@tomcat ~]# tar xf php-5.6.39.tar.gz -C /usr/src/
[root@tomcat ~]# cd /usr/src/php-5.6.39/
[root@tomcat php-5.6.39]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-openssl --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-iconv --enable-fpm --enable-zip --enable-mbstring && make && make install
配置 php-fpm,命令如下:
[root@tomcat php-5.6.39]# cp php.ini-production /usr/local/php/etc/php.ini
[root@tomcat php-5.6.39]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@tomcat php-5.6.39]# vim /usr/local/php/etc/php-fpm.conf
149 user = nginx
150 group = nginx[root@tomcat php-5.6.39]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@tomcat php-5.6.39]# chmod +x /etc/init.d/php-fpm[root@tomcat php-5.6.39]# service php-fpm start
Starting php-fpm done
3)Nginx 代理 PHP
添加虛擬主機配置如下:
[root@tomcat ~]# vim /etc/nginx/conf.d/default.conf
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}location ~\.php${
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam
e;
include fastcgi_params;
}[root@tomcat ~]# systemctl restart nginx
2、安裝 Ansible 插件

3、上傳 PHP 項目代碼到 Git 倉庫
1)在 Git 服務器創建 wordpress 版本倉庫
[root@git ~]# su - git
[git@git ~]$ mkdir wordpress.git
[git@git ~]$ cd wordpress.git/
[git@git wordpress.git]$ git --bare init
初始化空的 Git 版本庫於 /home/git/wordpress.git/
2)下載開源 PHP 博客系統 wordpress
[root@jenkins ~]# wget https://wordpress.org/latest.tar.gz
[root@jenkins ~]# tar xf latest.tar.gz
[root@jenkins ~]# cd wordpress/
3) 提交到 Git 倉庫
[root@jenkins wordpress]# git init
[root@jenkins wordpress]# git remote add origin git@192.168.200.127:/home/git/wordpress.git
[root@jenkins wordpress]# git add .
[root@jenkins wordpress]# git commit -m "wp"
[root@jenkins wordpress]# git tag 1.0.0
[root@jenkins wordpress]# git push origin 1.0.0
4、Jenkins 創建項目並發布測試








1)模擬實際生產環境提交代碼,作用是可以清楚看到兩次發版代碼的不同
[root@jenkins ~]# cd wordpress/
[root@jenkins wordpress]# echo "Hello Wordpress" > test.html
2)將修改的 test.html 提交到 Git 倉庫
[root@jenkins wordpress]# git add .
[root@jenkins wordpress]# git commit -m "hw"
[master f1f2d1c] hw
1 file changed, 1 insertion(+)
create mode 100644 test.html
[root@jenkins wordpress]# git tag 1.0.1
[root@jenkins wordpress]# git push origin 1.0.1
枚舉對象: 4, 完成.
對象計數中: 100% (4/4), 完成.
使用 2 個線程進行壓縮
壓縮對象中: 100% (2/2), 完成.
寫入對象中: 100% (3/3), 270 bytes | 270.00 KiB/s, 完成.
總共 3 (差異 1),復用 0 (差異 0)
To 192.168.200.127:/home/git/wordpress.git
* [new tag] 1.0.1 -> 1.0.1
3)在 Jenkins 執行構建任務


然后呢,代碼回滾操作,你就按版本來構建就行了。
