第36-37 Tomcat & SVN

1. Tomcat簡介


tomcat是一個web服務器,相似nginx,apache的http
nginx,http只能處理html等靜態文件(jpg)
網頁分爲靜態網頁(以.html或者.htm結尾的url地址)和動態網頁
nginx和http配合php就可以處理動態的網頁(.php結尾)
tomcat也能夠處理靜態的html文件
tomcat配合jdk就可以處理動態的網頁(.jsp結尾)

http://blog.oldboyedu.com/java-tomcat/


2. Tomcat安裝


cd /application/tools/
# 上傳軟件包

2.1 安裝JDK


jdk==》java虛擬機(jvm) 

tar xf jdk-8u161-linux-x64.tar.gz -C /application/
ln -s /application/jdk1.8.0_161 /application/jdk
#
#export JAVA_HOME=/application/jdk
#export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
#export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
#



sed -i.ori '$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
source /etc/profile
java -version

2.2 安裝Tomcat

tar xf apache-tomcat-8.0.50.tar.gz -C /application/
ln -s /application/apache-tomcat-8.0.50 /application/tomcat

echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile
source /etc/profile

chown -R root.root /application/jdk/ /application/tomcat/

/application/tomcat/bin/version.sh


3. Tomcat目錄結構


[root@oldboyedu tools]# cd /application/tomcat/
[root@oldboyedu tomcat]# tree -L 1
.
├── bin     # 啓動文件目錄,startup.sh,shutdown.sh,catalina.sh(核心)
├── conf    # 配置文件目錄,server.xml(主配置文件),tomcat-users.xml(用戶管理)
├── lib
├── LICENSE
├── logs    # 日誌目錄
├── NOTICE
├── RELEASE-NOTES
├── RUNNING.txt
├── temp    # 臨時目錄
├── webapps # 站點存放目錄,相似nginx的html目錄
└── work    # #→用以產生有JSP編譯出的Servlet的.java和.class文件

7 directories, 4 files

關注點:bin、conf、logs、webapps

4. 啓動/關閉tomcat

/application/tomcat/bin/startup.sh
/application/tomcat/bin/shutdown.sh

[root@oldboyedu conf]# netstat -tunlp|grep java
tcp6       0      0 :::8009                 :::*                    LISTEN      5322/java          
tcp6       2      0 :::8080                 :::*                    LISTEN      5322/java          
[root@oldboyedu conf]# ps -ef|grep [j]ava



5. 查看日誌,讀懂日誌



[root@oldboyedu logs]# ls
catalina.out               # 實時記錄tomcat運行信息,文件會愈來愈大,定時清空
catalina.2017-10-24.log    # catalina.out文件天天的日誌切割文件


[root@oldboyedu logs]# pwd
/application/tomcat/logs


[root@oldboyedu logs]# tailf catalina.out

Server startup in 108301 ms  # tomcat已經啓動的標識




6. Tomcat在CentOS7啓動慢的緣由


見羣文件《CentOS Tomcat 啓動過程很慢》

 


7. 搭建jpress--java版本的wordpress


JSP網頁

Linux + Nginx + PHP + MySQL
Linux + Tomcat + JDK + MySQL (db01)

7.1 鏈接db01的數據庫

create database jpress DEFAULT CHARACTER SET utf8;
grant all on jpress.* to jpress@'172.16.1.0/255.255.255.0' identified by 'jpress';
flush privileges;



7.2 部署站點代碼

上傳jpress-web-newest.war到webapps目錄
會自動解壓
先更名,再把它移動走。

mv jpress-web-newest jpress

http://10.0.0.200:8080/jpress/install



 



 

8.Tomcat配置文件

語法:
註釋:以<!--開頭,以-->結尾,中間包含的都是註釋,段註釋

vim /application/tomcat/conf/server.xml

# 指定關閉tomcat的端口號及暗號
<Server port="8005" shutdown="SHUTDOWN">

查看日誌(最詳細)
[root@cs7 ~]# tailf /application/tomcat/logs/catalina.out


# 指定tomcat的web站點監聽端口
<Connector port="8080" protocol="HTTP/1.1"
   connectionTimeout="20000"
   redirectPort="8443" />

  
# 專門和apache通訊的端口,不用,註釋掉
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

           

2.自定義網站目錄


默認在webapps/ROOT

之前地址:  http://10.0.0.201:8080/memtest/meminfo.jsp


安裝memtest.war同前面

想要的地址:http://10.0.0.201:8080/meminfo.jsp


第一種方法:
將meminfo.jsp放入ROOT目錄(默認站點都是從ROOT開始)不經常使用

第二種方法: 
nginx的location規則

location /oldboyedu/ {
  ....
}

vim /application/tomcat/conf/server.xml

# context === location   path="" === /oldboyedu/
<Context path="" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/>




sed -i.ori '125a <Context path="" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/>' /application/tomcat/conf/server.xml

 

/application/tomcat/bin/shutdown.sh
/application/tomcat/bin/startup.sh


3.Tomcat多實例

多虛擬主機:nginx
多實例:tomcat,儘量使用咱們系統的資源(資源相對多)
32cores 256G  8*600G


cd /application/tools/
tar xf apache-tomcat-8.0.50.tar.gz

cp -a apache-tomcat-8.0.50 /application/tomcat8_1
cp -a apache-tomcat-8.0.50 /application/tomcat8_2

sed -i 's#8005#8011#;s#8080#8081#' /application/tomcat8_1/conf/server.xml
sed -i 's#8005#8012#;s#8080#8082#' /application/tomcat8_2/conf/server.xml


# 生產經驗:將已經配置的實例打包,之後方便批量配置多實例

tar zcf tomcat_muti.tar.gz ./tomcat8_1/ ./tomcat8_2/
[root@cs7 ~]# tar tf /tmp/webpp_multi.tar.gz 
 
/application/tomcat8_1/bin/startup.sh
/application/tomcat8_2/bin/startup.sh

netstat -tunlp|grep java

http://10.0.0.200:8081/
http://10.0.0.200:8082/



4.Tomcat反向代理集羣


 
root@cs7 conf]# vim nginx.conf

    worker_connections 1024;
}   
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    upstream web_pools {
        server 10.0.0.200:8081;
        server 10.0.0.200:8082;
    }   
    server {
        listen 80;
        server_name localhost;
        location / { 
            index index.jsp index.html index.htm;
            proxy_pass http://web_pools;
            proxy_set_header Host $http_host;
            }   
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }   
    }   
}

上述部署完一直瀏覽器沒法訪問,報400錯誤
在 / location 添加  proxy_set_header Host $http_host;
[root@cs7 conf]# curl 10.0.0.201
[root@cs7 conf]# curl 10.0.0.201
[root@cs7 conf]# wget 10.0.0.201
--2018-09-23 22:22:45-- http://10.0.0.201/
正在鏈接 10.0.0.201:80... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 400 Bad Request
2018-09-23 22:22:45 錯誤 400:Bad Request。
改正後
[root@cs7 conf]# curl 10.0.0.201
tomcat8_1
[root@cs7 conf]# curl 10.0.0.201
tomcat8_2
[root@cs7 conf]# curl 10.0.0.201
tomcat8_1
[root@cs7 conf]# curl 10.0.0.201
tomcat8_2
[root@cs7 conf]# curl 10.0.0.201
tomcat8_1



  


 

5.Tomcat監控



jps -lvm  可以看到當前運行的java進程及pid號


6.Tomcat優化


6.1 安全優化(必會)

telnet、nc管理端口保護(8005)
ajp鏈接端口保護(8009)
禁用管理端

[root@oldboyedu webapps]# pwd
/application/tomcat8_1/webapps
[root@oldboyedu webapps]# mv * /tmp/
[root@oldboyedu webapps]# ls
[root@oldboyedu webapps]# mkdir ROOT
[root@oldboyedu webapps]# ls
ROOT


降權啓動:下降權限啓動tomcat
不要以root啓動








[root@oldboyedu ~]# useradd tomcat
[root@oldboyedu ~]# cp -a /application/tomcat8_2 /home/tomcat/
[root@oldboyedu ~]# ls /home/tomcat/
tomcat8_2
[root@oldboyedu ~]# chown -R tomcat.tomcat /home/tomcat/tomcat8_2/
[root@oldboyedu ~]# su - tomcat
[tomcat@oldboyedu ~]$ ls
tomcat8_2
[tomcat@oldboyedu ~]$ tomcat8_2/bin/startup.sh
Using CATALINA_BASE:   /home/tomcat/tomcat8_2
Using CATALINA_HOME:   /home/tomcat/tomcat8_2
Using CATALINA_TMPDIR: /home/tomcat/tomcat8_2/temp
Using JRE_HOME:        /application/jdk
Using CLASSPATH:       /home/tomcat/tomcat8_2/bin/bootstrap.jar:/home/tomcat/tomcat8_2/bin/tomcat-juli.jar
Tomcat started.
[tomcat@oldboyedu ~]$ ps -ef|grep tomcat
root       5801   1752  0 12:27 pts/0    00:00:00 su - tomcat
tomcat     5802   5801  0 12:27 pts/0    00:00:00 -bash
tomcat     5837      1 42 12:27 pts/0    00:00:02 /application/jdk/bin/java -Djava.util.logging.config.file=/home/tomcattomcat8_2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/home/tomcat/tomcat8_2/endorsed -classpath /home/tomcat/tomcat8_2/bin/bootstrap.jar:/home/tomcat/tomcat8_2/bin/tomcat-juli.jar -Dcatalina.base=/home/tomcat/tomcat8_2 -Dcatalina.home=/home/tomcat/tomcat8_2 -Djava.io.tmpdir=/home/tomcat/tomcat8_2/temp org.apache.catalina.startup.Bootstrap start
tomcat     5858   5802  0 12:27 pts/0    00:00:00 ps -ef
tomcat     5859   5802  0 12:27 pts/0    00:00:00 grep --color=auto tomcat

# 能夠放入/etc/rc.local開機自啓動
su - tomcat -c '/home/tomcat/tomcat8_2/bin/startup.sh'

 

http://blog.oldboyedu.com/java-tomcat/



JVM memory detail info :
Max memory:1945MB
Total memory:1945MB
Free memory:1907MB
Available memory can be used is :1907MB





svn






http://blog.oldboyedu.com/git/

[root@cs7 ~]# rpm -qa subversion
subversion-1.7.14-10.el7.x86_64



[root@cs7 ~]# mkdir -p /application/svndata  
[root@cs7 ~]# mkdir -p /application/svnpasswd
[root@cs7 ~]#
[root@cs7 ~]#
[root@cs7 ~]# ll -d /application/svn*
drwxr-xr-x. 2 root root 6 Mar  6 23:40 /application/svndata
drwxr-xr-x. 2 root root 6 Mar  6 23:40 /application/svnpasswd

[root@cs7 ~]# svnserve -dr /application/svndata/
[root@cs7 ~]#
[root@cs7 ~]# ss -lntup |grep svn
tcp    LISTEN     0      7         *:3690                  *:*                   users:(("svnserve",pid=21123,fd=3))


建立版本庫     

 svnadmin create /application/svndata/sadoc
[root@cs7 ~]# svnadmin create /application/svndata/sadoc
[root@cs7 ~]# tree /application/svndata/sadoc
/application/svndata/sadoc
├── conf
│ ├── authz
│ ├── passwd
│ └── svnserve.conf
├── db
│ ├── current
│ ├── format
│ ├── fsfs.conf
│ ├── fs-type
│ ├── min-unpacked-rev
│ ├── revprops
│ │ └── 0
│ │ └── 0
│ ├── revs
│ │ └── 0
│ │ └── 0
│ ├── transactions
│ ├── txn-current
│ ├── txn-current-lock
│ ├── txn-protorevs
│ ├── uuid
│ └── write-lock
├── format
├── hooks
│ ├── post-commit.tmpl
│ ├── post-lock.tmpl
│ ├── post-revprop-change.tmpl
│ ├── post-unlock.tmpl
│ ├── pre-commit.tmpl
│ ├── pre-lock.tmpl
│ ├── pre-revprop-change.tmpl
│ ├── pre-unlock.tmpl
│ └── start-commit.tmpl
├── locks
│ ├── db.lock
│ └── db-logs.lock
└── README.txt

10 directories, 27 files

[root@cs7 ~]#
[root@cs7 ~]# cd /application/svndata/sadoc/conf/
[root@cs7 conf]# ll
總用量 12
-rw-r--r-- 1 root root 1080 9月 23 23:03 authz
-rw-r--r-- 1 root root 309 9月 23 23:03 passwd
-rw-r--r-- 1 root root 3090 9月 23 23:03 svnserve.conf



mv authz passwd /application/svnpasswd/

[root@cs7 conf]# diff svnserve.conf*
19,20c19,20
< anon-access = none
< auth-access = write
---
> # anon-access = read
> # auth-access = write
27c27
< password-db = /application/svnpasswd/passwd
---
> # password-db = passwd
34c34
< authz-db = /application/svnpasswd/authz
---
> # authz-db = authz




[root@cs7 svnpasswd]# tail -5 passwd 
# sally = sallyssecret

lewen = 123456
kevin = 123456
root = 123456

[root@cs7 svnpasswd]# tail -5 authz 
# * = r
sa = lewen,kevin,root
[sadoc:/]
lewen = rw
kevin = r
--

[root@cs7 svnpasswd]# svnserve -dr /application/svndata/
[root@cs7 svnpasswd]# ss -lntup|grep svn
tcp    LISTEN     0      7         *:3690                  *:*                   users:(("svnserve",pid=21394,fd=3))


#客戶端下載地址

https://tortoisesvn.net/downloads.zh.html











SVN客戶端密碼保存地點:
C:\Users\oldboy\AppData\Roaming\Subversion\auth\svn.simple






sadoc/

oldboy  rw
alex    r
lee     r


http://blog.51cto.com/xiexiaojun/2049665
相關文章
相關標籤/搜索