資料連接:https://pan.baidu.com/s/1XOUlneFqt-_1tOLSmc-E1g 網盤分享的文件在此css
Tomcat是一個web服務器html
web服務器:httpd,nginxjava
web 處理靜態文件:html css、js、jpg,pngmysql
Tomcat 處理 html文件 linux
php軟件解析php文件nginx
jdk處理jsp文件web
tomcat+JDK算法
JDK java開發者工具包sql
java 寫一次,到處使用
jdk java虛擬機
跨平臺
生產環境版本和開發協商
tomcat和jdk版本要並行 tomcat8 -- jdk 8
環境 :hostname web03 ip 10.0.0.17/172.16.1.17
mkdir -p /application/tools/
tar xf jdk-8u60-linux-x64.tar.gz -C /application/ ln -s /application/jdk1.8.0_60 /application/jdk 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 /etc/profile source /etc/profile java -version
tar xf apache-tomcat-8.0.27.tar.gz -C /application/ ln -s /application/apache-tomcat-8.0.27 /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
[root@web03 tools]# cd /application/tomcat/ [root@web03 tomcat]# tree -L 1 . ├── bin startup.sh shutdown.sh catalina.sh(核心) ├── conf server.xml(主配置文件) tomcat-users.xml ├── lib ├── LICENSE ├── logs 日誌目錄 catalina.out tomcat實時日誌 ├── NOTICE ├── RELEASE-NOTES ├── RUNNING.txt ├── temp ├── webapps 站點目錄 └── work 7 directories, 4 files 6. 啓動關閉tomcat /application/tomcat/bin/startup.sh /application/tomcat/bin/shutdown.sh netstat -tunlp|grep java
冒號中文沒法顯示
1.現象
在CentOS7啓動Tomcat時,啓動過程很慢,須要幾分鐘,通過查看日誌,發現耗時在這裏:是session引發的隨機數問題致使的。Tocmat的Session ID是經過SHA1算法計算獲得的,計算Session ID的時候必須有一個密鑰。爲了提升安全性Tomcat在啓動的時候會經過隨機生成一個密鑰。
22-Apr-2017 19:33:07.623 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of
SecureRandom instance for session ID generation using [SHA1PRNG] took [55,507] milliseconds.
22-Apr-2017 19:33:07.653 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web appli
cation directory /application/apache-tomcat-8.0.27/webapps/ROOT has finished in 55,935 ms
主要緣由是生成隨機數的時候卡住了,致使tomcat啓動不了。
是否有足夠的熵來用於產生隨機數,能夠經過以下命令來查看
[root@oldboy tools]# cat /proc/sys/kernel/random/entropy_avail
爲了加速/dev/random提供隨機數的速度,你能夠經過操做設備的外設,讓其產生大量的中斷(如網絡傳輸數據,按鍵,移動鼠標,在命令行敲幾個不一樣的命令,俗稱聚氣。
cat /dev/random 會消耗能量
方法1:
vim $JAVA_HOME/jre/lib/security/java.security
securerandom.source=file:/dev/random
改成
securerandom.source=file:/dev/urandom
方法2:
vim $TOMCAT_HOME/bin/catalina.sh
if [[ "$JAVA_OPTS" != *-Djava.security.egd=* ]]; then
JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom"
fi
這個系統屬性egd表示熵收集守護進程(entropy gathering daemon)
方法3:(推薦)
yum install rng-tools # 安裝rngd服務(熵服務,增大熵池)
systemctl start rngd # 啓動服務
vim /application/tomcat/conf/tomcat-users.xml [root@web03 webapps]# tail -4 /application/tomcat/conf/tomcat-users.xml <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/> </tomcat-users> /application/tomcat/bin/shutdown.sh /application/tomcat/bin/startup.sh
從管理網頁能夠上傳war包,由於是root用戶,別人能夠上傳木馬病毒,因此不安全,不建議生產環境使用web界面,不建議公網訪問web界面,不安全。測試環境能夠給開發使用,省事
http://10.0.0.17:8080/memtest/meminfo.jsp
vim /application/tomcat/conf/server.xml #三個端口 <Server port="8005" shutdown="SHUTDOWN"> #將shutdown 傳給8005會直接停掉tomcat,不安全,能夠改掉SHUTDOWN這個默認口令 #echo shutdown|nc 10.0.0.17 8805 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />-->
cp /application/tomcat/conf/server.xml{,.ori}
cp /application/tomcat/conf/server.xml{,.ori} vim /application/tomcat/conf/server.xml +125 <Context path="" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/> <Context path="/40team" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/> /application/tomcat/bin/shutdown.sh /application/tomcat/bin/startup.sh
訪問並測試:http://10.0.0.17:8080/meminfo.jsp
http://10.0.0.17:8080/
http://10.0.0.17:8080/40team/meminfo.jsp
# 至關於nginx server root html;
=============
nginx location
root html
location /
location /oldboy/
{
root /data/oldboy/
}
=====
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/>
<Context path="/40team" docBase="/application/tomcat/webapps/memtest" debug="0" reloadable="false" crossContext="true"/>
1應該是虛擬主機 ,2是默認站點目錄,不建議直接修改站點目錄,想讓服務器訪問指定目錄能夠用相似nginx的location的功能,即以下。3,4是war包在站點目錄下自動解壓的配置 5是至關於locationp匹配/,後面6是至關於 root html; 便是訪問的目錄。7是訪問這個目錄時自動定位到目錄8去,由此能夠知道,訪問的目錄並不必定是真實存在的,有可能是重定位的,這樣一來,站點目錄結構的安全性可以獲得增強,
比較差異:
diff /application/tomcat/conf/server.xml{,.ori}
相似wordpess博客站點
環境:Linux Tomcat JDK MySQL(Mariadb)
yum -y install mariadb-server systemctl start mariadb.service mysql create database jpress DEFAULT CHARACTER SET utf8; grant all on jpress.* to jpress@'localhost' identified by '123456';
上傳jpress的war到站點目錄/application/tomcat/webapps 下,因爲tomcat主配置文件設置,他會自動解壓war包,固然只要解壓包目錄還在,內容不管有沒有,它就不會從新解壓
訪問目錄 http://10.0.0.17:8080/jpress-web-newest/
/application/tomcat/bin/shutdown.sh
/application/tomcat/bin/startup.sh
首頁: http://10.0.0.17:8080/jpress-web-newest/
管理頁: http://10.0.0.17:8080/jpress-web-newest/admin
jpress 有bug,要先啓數據庫,否則tomcat的日誌會在開機時一直報錯,而後過一段時間磁盤空間全被填滿
mv jpress-web-newest/* /tmp/jpress
JAVA企業級應用TOMCAT實戰 http://blog.oldboyedu.com/java-tomcat/
原文連接:http://www.zyops.com/java-tomcat
多虛擬主機:nginx 多個Server標籤(域名,ip,端口) 進程數量固定 master+worker
多實例(多進程):同一個程序啓動屢次,分爲兩種狀況:第一種:一臺機器跑多個站點;第二種:一個機器跑一個站點多個實例,配合負載均衡
cd /application/tools/ tar xf apache-tomcat-8.0.27.tar.gz cp -a apache-tomcat-8.0.27 tomcat8_1 cp -a apache-tomcat-8.0.27 tomcat8_2 sed -i 's#8005#8011#;s#8080#8081#' tomcat8_1/conf/server.xml sed -i 's#8005#8012#;s#8080#8082#' tomcat8_2/conf/server.xml diff tomcat8_1/conf/server.xml tomcat8_2/conf/server.xml
# 將多實例tomcat模板打包備用
tar zcf muti_tomcat8.tar.gz ./tomcat8_1 ./tomcat8_2 cp -a tomcat8_1 tomcat8_2 /application/ /application/tomcat8_1/bin/startup.sh /application/tomcat8_2/bin/startup.sh netstat -tunlp|grep java
# 將每一個實例的網頁差別化
echo 8081 >>/application/tomcat8_1/webapps/ROOT/index.jsp echo 8082 >>/application/tomcat8_2/webapps/ROOT/index.jsp
nginx反向代理:LNMP機器 tomcat
Nginx + LNMP機器
upstream server_pools{ server 10.0.0.7:80; server 10.0.0.8:80; }
server {
……
location /{
……
proxy_pass http://server_pools;
}
}
Nginx + Tomcat
upstream server_pools{
server 10.0.0.17:8081;
server 10.0.0.17:8082;
server 10.0.0.18:8081;
server 10.0.0.18:8082;
}
server {
……
location /{
……
proxy_pass http://server_pools;
}
}
CentOS6 CentOS7 融合
# 在CentOS6的lb01操做
mv /application/nginx/conf/nginx.conf{,.ori} egrep -v '#|^$' /application/nginx/conf/nginx.conf.default > /application/nginx/conf/nginx.conf
[root@lb01 ~]# cat /application/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_pools { server 10.0.0.17:8081; server 10.0.0.17:8082; } server { listen 80; server_name localhost; location / { root html; index index.jsp index.html index.htm; proxy_pass http://web_pools; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
jps -lvm
排除tomcat故障步驟:
a. 查看catalina.out
b. sh show-busy-java-threads.sh ============
遠程監控tomcat
windows軟件監控 先安裝Java C:\Program Files\Java\jdk1.8.0_31\bin
zabbix 監控
vim /application/tomcat8_1/bin/catalina.sh +97 CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.17" echo "10.0.0.17 web03" >>/etc/hosts /application/tomcat8_1/bin/shutdown.sh /application/tomcat8_1/bin/startup.sh netstat -tunlp|grep 12345
右擊
3.2.1 zabbix Server 須要開啓java監控功能(作1次)
sed -i -e '220a JavaGateway=127.0.0.1' -e '236a StartJavaPollers=5' /etc/zabbix/zabbix_server.conf systemctl restart zabbix-server.service
sed -e 能夠執行多條命令,而a後面不能用;會被當成字符加進去的
zabbix c ,tomcat java ,javagateway 翻譯官
3.2.2 安裝zabbix_java_gateway(java程序),也須要jdk(openjdk) (作1次)
yum install zabbix-java-gateway systemctl start zabbix-java-gateway.service netstat -tunlp|grep java
須要有zabbix的安裝源的
3.2.3 tomcat須要開啓遠程監控功能 (每個實例都要作)
關閉端口保護 8005 SHUTDOWN
ajp鏈接端口保護 8009 註釋
禁用管理端 ###將webapps下的內容全移走,再建一個ROOT目錄
降權啓動 下降用戶權限啓動
[root@web03 ~]# pkill java [root@web03 ~]# useradd tomcat [root@web03 ~]# cp -a /application/tools/tomcat8_1 /home/tomcat/ [root@web03 ~]# chown -R tomcat.tomcat /home/tomcat/tomcat8_1/ [root@web03 ~]# su -c '/home/tomcat/tomcat8_1/bin/startup.sh' tomcat Using CATALINA_BASE: /home/tomcat/tomcat8_1 Using CATALINA_HOME: /home/tomcat/tomcat8_1 Using CATALINA_TMPDIR: /home/tomcat/tomcat8_1/temp Using JRE_HOME: /application/jdk Using CLASSPATH: /home/tomcat/tomcat8_1/bin/bootstrap.jar:/home/tomcat/tomcat8_1/bin/tomcat-juli.jar Tomcat started. [root@web03 ~]# ps -ef|grep tomcat
tomcat性能取決於你的內存大小
上策:優化代碼
中策:jvm優化機制 垃圾回收機制 把不須要的內存回收
優化jvm--優化垃圾回收策略
優化catalina.sh配置文件。在catalina.sh配置文件中添加如下代碼:
#tomcat分配1G內存模板
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m"
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms800m -Xmx800m -XX:NewSize=400m -XX:MaxNewSize=400m -XX:PermSize=400m -XX:MaxPermSize=400m"
下策:加足夠大的內存
下下策:天天0點定時重啓tomcat
vim /home/tomcat/tomcat8_1/bin/catalina.sh +97 JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms800m -Xmx800m -XX:NewSize=400m -XX:MaxNewSize=400m -XX:PermSize=400m -XX:MaxPermSize=400m"
su -c '/home/tomcat/tomcat8_1/bin/shutdown.sh' tomcat su -c '/home/tomcat/tomcat8_1/bin/startup.sh' tomcat