開發環境部署
軟硬件環境
名稱 | 系統環境 | 軟件環境 |
---|---|---|
寫代碼的電腦(W1) | windows 7 | Sublime Text |
開發虛擬機(V1) | CentOS-6.8 -x64 | lnmp,git(>1.8.0) |
測試服務器(T1) | CentOd-6.5-x64 | lnmp,git(>1.8.0) |
GIT託管平臺
www.coding.net
或www.github.com
- 註冊賬號,創建項目(test)
W1配置
- 創建一個文件夾(最好與項目同名),然後共享之
V1配置
安裝samba-client和cifs-utils
yum install -y samba-client cifs-utils
將W1共享的文件夾掛載到特定目錄
查看共享目錄:smbclient -L //10.36.137.230 掛載目錄:mount.cifs -o username="Everyone",password="" //10.36.136.230/test /var/www/html
配置祕鑰,完成無密碼的push
ssh-keygen #連續點擊回車即可完成密鑰對的生成 #會在~/ssh下生成 id_rsa、id_rsa.pub兩個文件 #將id_rsa.pub中的內容粘貼到項目的公鑰部署處
安裝GIT,yum安裝版本過低(yum remove git),需要手動編譯安裝
下載:wget -c https://www.kernel.org/pub/software/scm/git/git-2.9.4.tar.gz 解壓:tar -zxvf git-2.9.4.tar.gz 進入:cd git-2.9.4 配置:./configure --prefix=/usr/local/git 編譯:make #可能會報錯,缺少perl5,安裝即可:yum install -y perl-devel 安裝:make install 將git可執行程序路徑添加到環境變量$PAH中 在/etc/profile文件最後添加如下內容 export PATH=$PATH:/usr/local/git/bin 立即生效:source /etc/profile 查看版本: git --version 配置: git config --global user.name xxxxx #配置用戶名 git config --global user.email xxxxxx@126.com #配置郵箱 查看配置: git config -l
項目初始化
cd /var/www/html #進入項目目錄 git init #初始化項目 touch index.php #新建一個文件,添加測試代碼 <?php echo 'ok'; git add . #添加所有文件 git commit -m 'init' #首次提交 git remote add origin git@git.coding.net:CodingJerry/test.git #添加遠程倉庫地址,使用ssh協議 git push origin master #將本地主分支推送遠程master分支
在項目目錄創建虛擬主機
在/usr/local/nginx/conf/vhost/目錄下創建www.test.com.conf文件,添加內容如下: server { listen 80; server_name www.test.com test.com; index index.html index.htm index.php; root /var/www/html; location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 重啓nginx服務:service nginx restart 說明:在W1上修改host文件,添加如下內容 10.36.137.231 www.test.com 10.36.137.231 test.com
T1配置
配置祕鑰對,完成對項目的無密碼push
修改www用戶的shell爲:/bin/bash 切換到www用戶:su - www 祕鑰對配置:ssh-keygen 將公鑰部署到項目中,一遍無密碼的pull su - #切換到root用戶
安裝git,版本大於1.8.0即可
將項目克隆下來,並在項目目錄創建虛擬主機
chown www:www /home/wwwroot #設置權限,否則無法克隆項目 su - www #切換到www用戶 cd /home/wwwroot #切換到網站目錄 git clone git@git.coding.net:CodingJerry/test.git #克隆項目 su - #切換到root用戶 chown root:root /home/wwwroot #重新改爲root權限 lnmp vhost add #創建虛擬主機,指定項目目錄,然後域名解析一下
在已存在虛擬主機(jerry.mxspider.com)下創建一個文件(index.php),作爲鉤子文件
cd /home/wwwroot/jerry.mxspider.com touch index.php #新建文件,添加測試代碼 file_put_contents('./test.txt','hello'); 將http://jerry.mxspider.com/index.php添加到test項目的webhook下 測試是否成功(一旦項目push時就會訪問該url) 測試成功後,將內容改爲:exec('./webhook.sh');
創建腳本webhook.sh
#!/bin/bash cd /home/wwwroot/test /usr/local/git/bin/git pull origin master --allow-unrelated-histories chmod +x webhook.sh #添加可執行權限
修改/usr/local/php/etc/php.ini
搜索disabled_functions,將禁用函數exec去掉 重啓php:service php-fpm restart
項目測試完畢,使用rsync將測試服務器上的代碼同步到線上服務器