目錄php
Tomcat是Apache軟件基金會(Apache Software Foundation)的Jakarta 項目中的一個核心項目,由Apache、Sun和其餘一些公司及我的共同開發而成。
Tomcat服務器是一個免費的開放源代碼的Web應用服務器,屬於輕量級應用服務器,在中小型系統和併發訪問用戶不是不少的場合下被廣泛使用,是開發和調試JSP程序的首選。
Tomcat和Nginx、Apache(httpd)、lighttpd等Web服務器同樣,具備處理HTML頁面的功能,另外它仍是一個Servlet和JSP容器,獨立的Servlet容器是Tomcat的默認模式。不過,Tomcat處理靜態HTML的能力不如Nginx/Apache服務器。html
下載地址:https://tomcat.apache.org/download-80.cgijava
[root@linux-node1 ~]# ll apache-tomcat-8.0.50.tar.gz jdk-8u161-linux-x64.tar.gz -rw-r--r-- 1 root root 9417189 3月 17 11:27 apache-tomcat-8.0.50.tar.gz -rw-r--r-- 1 root root 189756259 3月 17 11:51 jdk-8u161-linux-x64.tar.gz [root@linux-node1 ~]# mv apache-tomcat-8.0.50 /usr/local/ [root@linux-node1 ~]# ln -s /usr/local/apache-tomcat-8.0.50/ /usr/local/tomcat [root@linux-node1 ~]# mv jdk1.8.0_161 /usr/local/ [root@linux-node1 ~]# ln -s /usr/local/jdk1.8.0_161 /usr/local/jdk
[root@linux-node1 ~]# vim /etc/profile export JAVA_HOME=/usr/local/jdk export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar TOMCAT_HOME=/usr/local/tomcat [root@linux-node1 ~]# source /etc/profile [root@linux-node1 ~]# java -version java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
[root@linux-node1 ~]# useradd -u 601 tomcat [root@linux-node1 ~]# chown -R tomcat.tomcat /usr/local/jdk [root@linux-node1 ~]# chown -R tomcat.tomcat /usr/local/tomcat [root@linux-node1 ~]# su - tomcat [tomcat@linux-node1 ~]$ /usr/local/tomcat/bin/startup.sh
[tomcat@linux-node1 logs]$ pwd /usr/local/tomcat/logs [tomcat@linux-node1 logs]$ ll 總用量 20 -rw-rw-r-- 1 tomcat tomcat 6449 3月 17 13:49 catalina.2018-03-17.log -rw-rw-r-- 1 tomcat tomcat 6449 3月 17 13:49 catalina.out ==>啓動日誌查看,有任何tomcat啓動相關能夠進行查看 -rw-rw-r-- 1 tomcat tomcat 0 3月 17 13:49 host-manager.2018-03-17.log -rw-rw-r-- 1 tomcat tomcat 465 3月 17 13:49 localhost.2018-03-17.log -rw-rw-r-- 1 tomcat tomcat 0 3月 17 13:49 localhost_access_log.2018-03-17.txt -rw-rw-r-- 1 tomcat tomcat 0 3月 17 13:49 manager.2018-03-17.log [tomcat@linux-node1 conf]$ pwd /usr/local/tomcat/conf [tomcat@linux-node1 conf]$ vim server.xml #tomcat的主配置文件 <Connector port="8080" protocol="HTTP/1.1" HTTP協議鏈接tomcat,鏈接超時20000ms,重定向用8443端口 connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> AJP協議鏈接tomcat [root@linux-node1 tomcat]# tree -L 1 . ├── bin #→用以啓動、關閉Tomcat或者其它功能的腳本(.bat文件和.sh文件) ├── conf #→用以配置Tomcat的XML及DTD文件 ├── lib #→存放web應用能訪問的JAR包 ├── LICENSE ├── logs #→Catalina和其它Web應用程序的日誌文件 ├── NOTICE ├── RELEASE-NOTES ├── RUNNING.txt ├── temp #→臨時文件 ├── webapps #→Web應用程序根目錄 └── work #→用以產生有JSP編譯出的Servlet的.java和.class文件 7 directories, 4 files
測試功能,生產環境不要用。
Tomcat管理功能用於對Tomcat自身以及部署在Tomcat上的應用進行管理的web應用。在默認狀況下是處於禁用狀態的。若是須要開啓這個功能,就須要配置管理用戶,即配置前面說過的tomcat-users.xml。node
[tomcat@linux-node1 webapps]$ pwd /usr/local/tomcat/webapps [tomcat@linux-node1 webapps]$ ll 總用量 4 drwxr-xr-x 14 tomcat tomcat 4096 3月 17 11:33 docs #→tomcat幫助文檔 drwxr-xr-x 6 tomcat tomcat 83 3月 17 11:33 examples #→web應用實例 drwxr-xr-x 5 tomcat tomcat 87 3月 17 11:33 host-manager #→管理 drwxr-xr-x 5 tomcat tomcat 103 3月 17 11:33 manager #→管理 drwxr-xr-x 3 tomcat tomcat 306 3月 17 11:33 ROOT #→默認網站根目錄 [tomcat@linux-node1 conf]$ pwd /usr/local/tomcat/conf [tomcat@linux-node1 conf]$ vim tomcat-users.xml #配置用戶管理界面的用戶和密碼,Server status/Manager App/Host Manager <role rolename="manager-gui" /> <role rolename="admin-gui" /> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui" /> [tomcat@linux-node1 conf]$ /usr/local/tomcat/bin/shutdown.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [tomcat@linux-node1 conf]$ /usr/local/tomcat/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. 訪問192.168.56.110:8080可進行分別點擊查看
每次重啓,都須要將temp/ work/目錄下的東西清空,避免影響tomcat的運行。因此寫一個腳本在tomcat中止時進行清除。linux
[tomcat@linux-node1 ~]$ vim tomcat.sh #!/bin/bash useage(){ echo "Useage: $0 [start|stop|restart|status]" } status_tomcat(){ ps -axu |grep java |grep tomcat|grep -v "grep" } start_tomcat () { /usr/local/tomcat/bin/startup.sh } stop_tomcat() { TPID=$(ps -axu |grep java |grep tomcat|grep -v "grep" |awk '{print $2}') kill -9 $TPID sleep 5 TSTAT=$(ps -axu |grep java |grep tomcat|grep -v "grep" |awk '{print $2}') if [ -z $TSTAT ];then echo "tomcat stop." else kill -9 $TSTAT fi } main(){ case $1 in start) start_tomcat ;; stop) stop_tomcat ;; restart) stop_tomcat && start_tomcat ;; status) status_tomcat ;; *) useage; esac } main $1
JVM的內存優化(堆內存和非堆內存)
爲了安全,須要將webapps下的東西進行清除。Sever status能夠保留,使用nginx進行反向代理,並限制只能內網進行訪問。nginx
[tomcat@linux-node1 webapps]$ pwd /usr/local/tomcat/webapps [tomcat@linux-node1 webapps]$ ll 總用量 4 drwxr-xr-x 14 tomcat tomcat 4096 3月 17 11:33 docs drwxr-xr-x 6 tomcat tomcat 83 3月 17 11:33 examples drwxr-xr-x 5 tomcat tomcat 87 3月 17 11:33 host-manager drwxr-xr-x 5 tomcat tomcat 103 3月 17 11:33 manager drwxr-xr-x 3 tomcat tomcat 306 3月 17 11:33 ROOT [tomcat@linux-node1 webapps]$ mv docs examples host-manager /tmp
若 Tomcat 都是放在內網的,則針對 Tomcat 服務的監聽地址都是內網地址 標準配置:<Connector port="10000" server="webserver"/>
修改默認的 8005 管理端口不易猜想(大於1024),但要求端口配置在8000~8999之間 修改SHUTDOWN命令爲其餘字符串 標準配置:<Server port="8578" shutdown="dangerous">
修改默認的ajp 8009端口爲不易衝突(大於1024),但要求端口配置在8000~8999之間 經過iptables規則限制ajp端口訪問的權限僅爲線上機器,目的在於防止線下測試流量被apache的mod_jk轉發至線上tomcat服務器 標準配置:<Connector port="8349" protocol="AJP/1.3"/>
刪除默認$CATALINA_HOME/conf/tomcat-users.xml文件,重啓tomcat將會自動生成新的文件 刪除$CATALINA_HOME/webapps下載默認的全部目錄和文件 將tomcat應用根目錄配置爲tomcat安裝目錄之外的目錄 標準配置: a.server.xml配置 一種直接修改Host節點信息,表示全局配置 <Host name="localhost" appBase="/data/www/tomcat_webapps" unpackWARs="true" autoDeploy="false"></Host> 另外一種直接在Host節點中新增Context節點,指定具體的項目: <Context path="" docBase="/usr/local/tomcat/webapps/jenkins" debug="0" reloadable="false" crossContext="true"> </Context> b.在$CATALINA_HOME/conf/Catalina/locathost目錄下新增文件 test##20160506172651.xml <Context displayName="test" docBase="/data/www/tomcat_webapps/test##20160506172651.war" reloadable="false" />
針對該信息的顯示是由一個jar包控制的,該jar包存放在$CATALINA_HOME/lib目錄下,名稱爲 catalina.jar,經過 jar xf 命令解壓這個 jar 包會獲得兩個目錄 META-INF 和 org ,修改 org/apache/catalina/util/ServerInfo.properties 文件中的 serverinfo 字段來實現來更改咱們tomcat的版本信息web
$ cd $CATALINA_HOME/lib $ jar xf catalina.jar $ cat org/apache/catalina/util/ServerInfo.properties |grep -v '^$|#' $ mkdir -p org/apache/catalina/util $ vim ServerInfo.properties server.info=nolinux # 把這個值改爲其它值就好了
自定義錯誤頁面:修改$CATALINA_HOME/conf/web.xml重定向 403/404/500等錯誤到指定的錯誤頁面shell
Tomcat啓動用戶權限必須非root權限,儘可能下降tomcat啓動用戶的目錄訪問權限,如需直接對外使用80端口,可經過普通帳號啓動後,配置iptables規則進行轉發, 爲了防止 Tomcat 被植入 web shell 程序後,能夠修改項目文件。要將 Tomcat 和項目的屬主作分離,即使被破壞也沒法建立和編輯項目文件
$CATALINA_HOME/conf/web.xml文件中的default部分的listings的配置必須爲false(默認),表示不列出文件列表
經過配置,限定訪問的IP來源
全局設置限定IP和域名訪問:數據庫
<Host name="localhost" appBase="/data/www/tomcat_webapps" unpackWARs="true" autoDeploy="false"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="192.168.1.10,192.168.1.30,192.168.2.*" deny=""/> <Valve className="org.apache.catalina.valves.RemoteHostValve" allow="www.test.com,*.test.com" deny=""/> </Host>
控制CATALINAHOME/bin目錄下的start.sh、catalina.sh、shutdown.sh的可執行權限,chmod−R 744 CATALINAHOME/bin目錄下的start.sh、catalina.sh、shutdown.sh的可執行權限,chmod−R744 CATALINAHOME/bin/*
開啓tomcat默認訪問日誌中Referer和User-Agent記錄
標準配置:apache
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b %{Referer}i %{User-Agent}i %D" resolveHosts="false" /> 在HTTP Connector配置中加入server的配置,server=」chuck-server」
下載zrlog源碼程序,並放置在webapps目錄中
[root@localhost ~]# wget 'http://dl.zrlog.com/release/zrlog-2.1.0-3617b2e-release.war?attname=ROOT.war&ref=index' [root@localhost ~]# mv zrlog-2.1.0-3617b2e-release.war\?attname\=ROOT.war\&ref\=index zrlog-2.1.0.war [root@localhost ~]# mv zrlog-2.1.0.war /usr/local/tomcat/webapps/ [root@localhost ~]# cd !$ [root@localhost webapps]# mv ROOT ROOT.bak [root@localhost webapps]# mv zrlog-2.1.0 ROOT
建立zrlog數據庫並受權
MariaDB [(none)]> create database zrlog default charset utf8; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all privileges on zrlog.* to zrlog@"%" identified by "123456"; Query OK, 0 rows affected (0.05 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
訪問:192.168.56.110:8080,如圖:
刷新頁面,訪問,如圖:
[root@linux-node1 ~]# cat /usr/local/nginx/conf/vhost/test.conf server { listen 80; server_name www.linuxlong.com; root /webroot/web-demo; index index.htm index.html index.php index.jsp; access_log /usr/local/nginx/logs/test.log access_json; location ~* \.(jsp|do)$ { #使用location匹配以jsp或do結尾的對象進行反向代理 proxy_pass http://node1.long.com:8080; } }
[root@linux-node1 ~]# ll /data/webapps/ROOT/ total 4 drwxr-xr-x 2 root root 6 Mar 21 07:32 classes -rw-r--r-- 1 root root 187 Mar 21 07:48 index.jsp drwxr-xr-x 2 root root 6 Mar 21 07:32 lib drwxr-xr-x 2 root root 6 Mar 21 07:32 META-INF drwxr-xr-x 2 root root 6 Mar 21 07:32 WEB-INF [root@linux-node1 ~]# cd /data/webapps/ROOT/ [root@linux-node1 ROOT]# cat index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>JSP Test Page on web1</title> </head> <body> <% out.println("Hello, Web1!"); %> </body> </html> [root@linux-node1 ~]# cat /usr/local/tomcat/conf/server.xml <Host name="node1.long.com" appBase="/data/webapps" autoDeploy="true"> <Context path="" docBase="ROOT" /> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs" prefix="linuxlong_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> [root@linux-node1 ~]# /usr/local/tomcat/bin/catalina.sh stop [root@linux-node1 ~]# /usr/local/tomcat/bin/catalina.sh start
[root@linux-node1 ~]# yum install -y httpd [root@linux-node1 ~]# grep ^[a-Z] /etc/httpd/conf/httpd.conf ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User apache Group apache ServerAdmin root@localhost ServerName www.linuxlong.com ErrorLog "logs/error_log" LogLevel warn AddDefaultCharset UTF-8 EnableSendfile on IncludeOptional conf.d/*.conf [root@linux-node1 ~]# cat /etc/httpd/conf.d/vhosts.conf #配置反向代理虛擬主機 <VirtualHost *:80> ServerName www.linuxlong.com ProxyVia On #開啓via ProxyRequests Off #關閉正向代理,即開啓了反向代理 ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass /status ! ProxyPass / ajp://node1.long.com:8009/ #這裏使用ajp協議進行和tomcat通訊,也能夠更換成http協議。此處也可使用ip地址進行反向代理,這樣能夠不用進行域名解析。 ProxyPa***everse / ajp://node1.long.com:8009/ <Location /> #定義訪問屬性 Require all granted </Location> </VirtualHost> [root@linux-node1 ~]# httpd -t Syntax OK [root@linux-node1 ~]# systemctl restart httpd
[root@lb01 nginx]# cat nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; server_name _; root /usr/share/nginx/html; error_log /var/log/nginx/tomcat_error.log; include /etc/nginx/default.d/*.conf; location / { #靜態反向代理 proxy_pass http://192.168.56.120; } location ~* \.(jsp|do)$ { #動態反向代理 proxy_pass http://192.168.56.110; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
[root@linux-node2 vhost]# cat oss.conf server { listen 80; server_name localhost; root /webroot/web-demo; index index.php index.html index.htm; }
[root@linux-node1 conf]# cat /etc/httpd/conf.d/vhosts.conf <VirtualHost *:80> ServerName 192.168.56.110 ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass /status ! ProxyPass / ajp://node1.long.com:8009/ ProxyPa***everse / ajp://node1.long.com:8009/ <Location /> Require all granted </Location> </VirtualHost>