需求說明:
在測試環境下(192.168.1.28)部署一套公司某業務環境,其中:
該業務前臺訪問地址: http://testhehe.wangshibo.com
該業務後臺訪問地址: http://testhehe.wangshibo.com/admin/html
涉及到視頻讀寫需求,要求視頻在測試機上寫入,而後在另外一臺圖片服務器(192.168.1.6)上讀,這就須要作nfs共享。須要給開發同事提供視頻寫入和讀取的url地址:
視頻寫地址:http://testhehe.wangshibo/static/video
視頻讀地址:http://static.wangshibo.com/video前端
部署記錄:
1、測試機上的操做java
(1)nginx的配置
[root@dev-test ~]# cat testhehe.wangshibo.com.conf
server {
listen 80;nginx
server_name testhehe.wangshibo.com;web
access_log /var/log/testhehe.log main;bootstrap
location / {
proxy_pass http://127.0.0.1:8383; #前臺訪問
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}tomcat
location /admin {
proxy_pass http://127.0.0.1:8484/; #後臺訪問跳轉(注意這裏8484後面的斜槓/要加上,否則訪問跳轉會有問題,8484端口的tomcat目錄下不須要有admin實際目錄存在)
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}bash
location /static/video { #視頻寫地址,這裏不須要另啓tomcat進程進行nginx跳轉設置(沒有war包數據),直接nginx設置訪問地址便可
root /Data/app/tomcat-7-hehe/static/video;
}服務器
} ##end serverapp
啓動nginx
[root@dev-test ~]# /Data/app/nginx/sbin/nginx -t
[root@dev-test ~]# /Data/app/nginx/sbin/nginx -s reload
以上nginx的配置說明:
1)該業務前臺http://testhehe.wangshibo.com訪問轉向8383端口的tomcat,代碼放到/Data/app/tomcat-7-hehe/webapp目錄下
2) 該業務後臺http://testhehe.wangshibo.com/admin/訪問轉向8484端口的tomcat,代碼放到/Data/app/tomcat-7-admin-hehe/webapp目錄下
3)視頻寫入的地址是:http://testhehe.wangshibo.com/static/video,根目錄爲/Data/app/tomcat-7-hehe/static/video,因爲tomcat和nginx進程權限不同,最好將此目錄設置成777權限。
[root@dev-test ~]# chmod -R 777 /Data/app/tomcat-7-hehe/static/video
(2)tomcat設置
1)前臺訪問跳轉的tomcat設置(8383端口)
[root@dev-test ~]# cd /Data/app/tomcat-7-hehe/conf/
[root@dev-test conf]# cat server.xml
.......
<Server port="8785" shutdown="SHUTDOWN">
......
<Connector port="8383" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
.....
<Connector port="8789" protocol="AJP/1.3" redirectPort="8443" />
.....
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
</Host>
.....
</Server>
注意:tomcat默認的根目錄是webapps/ROOT,因此現將webapps目錄下默認的文件都刪除
[root@dev-test webapps]# pwd
/Data/app/tomcat-7-hehe/webapps
[root@dev-test webapps]# rm -rf ./*
將前臺代碼的war包拷貝到webapps下,war包名稱最好是ROOT.war,若是不是這個名稱,能夠手動將war包改爲這個ROOT.war名稱.這樣訪問的時候就直接使用tomcat的默認目錄了,好比 http://127.0.0.1:8383;(固然若是不改爲ROOT.war也無妨,好比war包名叫hehe.war,須要提早將webapps目錄清空,那麼tomcat啓動後,訪問就是http://127.0.0.1:8383/hehe,這樣在nginx反向代理裏的proxy_pass配置也要改爲proxy_pass http://127.0.0.1:8383/hehe; )
例如開發提供的業務代碼war包名稱是hehe.war,將其上傳到webapp下,並改名爲ROOT.war
[root@dev-test webapp]# ll hehe.war
-rw-r--r-- 1 root root 52673678 11月 21 17:40 hehe.war
[root@dev-test webapp]# mv hehe.war ROOT.war
[root@dev-test webapp]# ll
-rw-r--r-- 1 root root 124491469 11月 22 17:59 ROOT.war
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
這裏注意一下:
tomcat默認的根目錄是webapp/ROOT,爲了方便tomcat訪問時採用默認目錄(而且前端nginx反向代理裏直接是轉到tomcat默認的端口訪問上),因此須要將業務代碼war包改爲ROOT.war放到webapp下,若是war包名不是ROOT.war,那麼重啓tomcat後,訪問會失敗(404頁面)。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
接着啓動8383端口的tomcat進程,這個ROOT.war包就會自動解壓
[root@dev-test webapps]# ../bin/startup.sh
Using CATALINA_BASE: /Data/app/tomcat-7-hehe
Using CATALINA_HOME: /Data/app/tomcat-7-hehe
Using CATALINA_TMPDIR: /Data/app/tomcat-7-hehe
Using JRE_HOME: /usr/java/jdk1.7.0_79
Using CLASSPATH: /Data/app/tomcat-7-hehe/bin/bootstrap.jar:/Data/app/tomcat-7-hehe/bin/tomcat-juli.jar
Tomcat started.
[root@dev-test webapps]# ll
總用量 121580
drwxr-xr-x 10 root root 4096 11月 22 17:57 ROOT
-rw-r--r-- 1 root root 124491469 11月 22 17:59 ROOT.war
[root@dev-test webapps]# lsof -i:8383
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 26155 root 43u IPv6 214715915 0t0 TCP *:m2mservices (LISTEN)
因爲業務代碼每次發版都須要重啓tomcat,而tomcat沒有自帶的重啓腳本,重啓須要先kill,而後start啓動,這樣很不方面。
這裏分享一個簡單的發版後的tomcat重啓腳本:
[root@dev-test tomcat-7-hehe]# pwd
/Data/app/tomcat-7-hehe
[root@dev-test tomcat-7-hehe]# cat start.sh
#!/bin/bash
cd /Data/app/tomcat-7-hehe/
#pkill -9 java
kill -9 `ps -ef|grep java|grep -v grep|grep tomcat-7-hehe |awk -F" " '{print $2}'`
rm -rf temp/*
rm -rf work/*
rm -rf webapps/ROOT
sh bin/startup.sh
2)同理,設置後臺訪問跳轉的tomcat設置(8484端口)
[root@dev-test ~]# cd /Data/app/tomcat-7-admin-hehe/conf
[root@dev-test conf]# cat server.xml
.......
<Server port="8886" shutdown="SHUTDOWN">
......
<Connector port="8484" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
.....
<Connector port="8889" protocol="AJP/1.3" redirectPort="8443" />
.....
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
</Host>
.....
</Server>
注意:tomcat默認的根目錄是webapps/ROOT,因此現將webapps目錄下默認的文件都刪除
[root@dev-test webapps]# pwd
/Data/app/tomcat-7-admin-hehe/webapps
[root@dev-test webapps]# rm -rf ./*
而後將前臺代碼的war包拷貝到webapps下,war包名稱最好是ROOT.war,若是不是這個名稱,就手動改爲這個ROOT.war名稱.
[root@dev-test webapps]# ll
總用量 121580
-rw-r--r-- 1 root root 124491469 11月 22 17:59 ROOT.war
接着啓動8383端口的tomcat進程,這個ROOT.war包就會自動解壓
[root@dev-test webapps]# ../bin/startup.sh
Using CATALINA_BASE: /Data/app/tomcat-7-admin-hehe
Using CATALINA_HOME: /Data/app/tomcat-7-admin-hehe
Using CATALINA_TMPDIR: /Data/app/tomcat-7-admin-hehe
Using JRE_HOME: /usr/java/jdk1.7.0_79
Using CLASSPATH: /Data/app/tomcat-7-admin-hehe/bin/bootstrap.jar:/Data/app/tomcat-7-admin-hehe/bin/tomcat-juli.jar
Tomcat started.
[root@dev-test webapps]# ll
總用量 121580
drwxr-xr-x 10 root root 4096 11月 22 17:57 ROOT
-rw-r--r-- 1 root root 124491469 11月 22 17:59 ROOT.war
[root@dev-test webapps]# lsof -i:8383
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 26155 root 43u IPv6 214715915 0t0 TCP *:m2mservices (LISTEN)
因爲業務代碼每次發版都須要重啓tomcat,而tomcat沒有自帶的重啓腳本,重啓須要先kill,而後start啓動,這樣很不方面。
這裏分享一個簡單的發版後的tomcat重啓腳本:
[root@dev-test tomcat-7-admin-hehe]# pwd
/Data/app/tomcat-7-admin-hehe
[root@dev-test tomcat-7-admin-hehe]# cat start.sh
#!/bin/bash
cd /Data/app/tomcat-7-admin-hehe/
#pkill -9 java
kill -9 `ps -ef|grep java|grep -v grep|grep tomcat-7-admin-hehe |awk -F" " '{print $2}'`
rm -rf temp/*
rm -rf work/*
rm -rf webapps/ROOT
sh bin/startup.sh
接着說下掛載nfs共享:
即將視頻寫入到本機的/Data/app/tomcat-7-hehe/static/video目錄下(寫地址是http://testhehe.wangshibo/static/video),而後共享給圖片服務器192.168.1.6的/usr/local/nginx/html/hehe/video
[root@dev-test webapp]# /bin/mount -t nfs 192.168.1.6:/usr/local/nginx/html/hehe/video /Data/app/tomcat-7-hehe/static/video
[root@dev-test webapp]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 50G 12G 36G 26% /
tmpfs 32G 72K 32G 1% /dev/shm
/dev/sda1 485M 39M 421M 9% /boot
/dev/mapper/VolGroup-lv_home 844G 69G 732G 9% /home
192.168.1.6:/usr/local/nginx/html/hehe/video 97G 64G 28G 70% /home/Data/app/tomcat-7-hehe/static/video
設置開機掛載
[root@dev-test webapp]# cat /etc/rc.local
......
/bin/mount -t nfs 192.168.1.6:/usr/local/nginx/html/hehe/video /Data/app/tomcat-7-hehe/static/video
nfs的部署參考:nfs服務部署記錄
2、圖片服務器上的操做
(1)nginx的配置
[root@static ~]# cat /usr/loca/nginx/conf/vhost/static.conf server { listen 80; server_name static.wangshibo.com; root /usr/local/nginx/html/hehe; error_page 403 /403.html; location = /403.html { root html; allow all; } location ~ \.mp4(.*)$ { #針對mp4格式的視頻讀取限制 mp4; mp4_buffer_size 4M; mp4_max_buffer_size 10M; } } [root@static ~]# /usr/loca/nginx/sbin/nginx -t [root@static ~]# /usr/loca/nginx/sbin/nginx [root@static ~]# mkdir /usr/local/nginx/html/hehe/video
-----------------------------------------------------------------------------------
下面隨便說下該業務先後臺代碼發版腳本:
腳本在svn機器上:
[root@svn-server ~]# pwd
/Data/webroot_svncode/wangshibo
前臺代碼發版腳本
[root@svn-server ~]# cat up_pub_test_wangshibo.sh
#!/bin/bash
REMOTE_SERVER_IP="192.168.1.28" #測試服務器,ssh端口是25791
DEST_DIR="/Data/app/tomcat-7-hehe/webapps"
cd /Data/webroot_svncode/wangshibo/test/front #在svn本機下載前臺代碼的存放路徑,第一次svn下載須要用戶名和密碼,後面就只須要update更新代碼便可
/usr/bin/svn update
/usr/bin/ssh -p25791 ${REMOTE_SERVER_IP} "rm -rf /Data/app/tomcat-7-hehe/webapps/ROOT*"; #svn裏面的war包名次是ROOT.war,具體理由上面已說明
/usr/bin/rsync -av -e "/usr/bin/ssh -p25791" --rsync-path="/usr/bin/rsync" --progress /Data/webroot_svncode/wangshibo/test/front/ROOT.war ${REMOTE_SERVER_IP}:${DEST_DIR};
sleep 10
/usr/bin/ssh -p25791 ${REMOTE_SERVER_IP} "/bin/sh /Data/app/tomcat-7-hehe/start.sh";
後臺代碼發版腳本
[root@svn-server ~]# cat up_pub_testadmin_wangshibo.sh
#!/bin/bash
REMOTE_SERVER_IP="192.168.1.28"
DEST_DIR="/Data/app/tomcat-7-admin-hehe/webapps"
cd /Data/webroot_svncode/wangshibo/test/bg
/usr/bin/svn update
/usr/bin/ssh -p25791 ${REMOTE_SERVER_IP} "rm -rf /Data/app/tomcat-7-admin-hehe/webapps/ROOT*";
/usr/bin/rsync -av -e "/usr/bin/ssh -p25791" --rsync-path="/usr/bin/rsync" --progress /Data/webroot_svncode/wangshibo/test/bg/ROOT.war ${REMOTE_SERVER_IP}:${DEST_DIR};
sleep 10
/usr/bin/ssh -p25791 ${REMOTE_SERVER_IP} "/bin/sh /Data/app/tomcat-7-admin-hehe/start.sh";