Tomcat 是由 Apache 開發的一個 Servlet 容器,實現了對 Servlet 和 JSP 的支持,並提供了做爲Web服務器的一些特有功能,如Tomcat管理和控制平臺、安全域管理和Tomcat閥等。因爲 Tomcat 自己也內含了一個 HTTP 服務器,它也能夠被視做一個單獨的 Web 服務器。可是,不能將 Tomcat 和Apache HTTP 服務器混淆,Apache HTTP 服務器是一個用 C 語言實現的 HTTP Web 服務器;這兩個 HTTP webserver 不是捆綁在一塊兒的。Tomcat 包含了一個配置管理工具,也能夠經過編輯XML格式的配置文件來進行配置java
/bin -Tomcat 腳本存放目錄(如啓動,關閉腳本等)mysql
/conf -Tomcat 配置文件目錄linux
/logs —Tomcat 默認日誌目錄nginx
/webapps —webapp 運行的目錄web
通常web項目路徑結構sql
[web@CentOS-7-1 tomcat]$ tree -d -L 2 /home/web/web1/ROOT/ |--webapp # 站點根目錄 |--META-INF # META-INF 目錄
| `--MANIFEST.MF # 配置清單文件 |--WEB-INF # WEB-INF 目錄 | |--classes # class文件目錄 | | |--*.class # 程序須要的class 文件 | | `--*.xml # 程序須要的xml 文件 | |-- lib # 庫文件 | | |--*.jar # 程序須要的jar 包 | `---web.xml # web應用程序須要的部署描述文件 | --(userdir) # 自定義的目錄 | --(userfiles) # 自定義的資源文件
webapp :工程發佈文件夾。其實每一個 war 包均可以視爲 webapp 的壓縮包。數據庫
META-INF :META-INF 目錄用於存放工程自身相關的一些信息,元文件信息,一般由開發工具,環境自動生成。apache
WEB-INF :Java web應用的安全目錄。所謂安全就是客戶端沒法訪問,只有服務端能夠訪問的目錄bootstrap
/WEB-INF/classes :存放程序所須要的全部 Java class 文件。vim
/WEB-INF/lib :存放程序所須要的全部 jar 文件。
/WEB-INF/web.xml :web 應用的部署配置文件。它是工程中最重要的配置文件,它描述了 servlet 和組成應用的其它組件,以及應用初始化參數、安全管理約束等。
(1)、使用wget 下載JDK
[root@CentOS-7-1 tmp]# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u171-linux-x64.tar.gz"
(2)、Jdk 環境設置
[root@CentOS-7-1 tmp]# tar -xf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@CentOS-7-1 tmp]# ln -s /usr/local/jdk1.8.0_171 /usr/local/java [root@CentOS-7-1 tmp]# vim /etc/profile.d/java.sh JAVA_HOME=/usr/local/java PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME PATH
[root@CentOS-7-1 tmp]# source /etc/profile
[root@CentOS-7-1 tmp]# java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@CentOS-7-1 tmp]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat-9.0.10.tar.gz
[root@CentOS-7-1 tmp]# useradd web [root@CentOS-7-1 tmp]# su - web 上一次登陸:二 7月 24 18:54:03 CST 2018pts/0 上 [web@CentOS-7-1 ~]$ mv /tmp/apache-tomcat-9.0.1.tar.gz ./ [web@CentOS-7-1 ~]$ tar xf apache-tomcat-9.0.1.tar.gz [web@CentOS-7-1 ~]$ ln -s apache-tomcat-9.0.1 tomcat [web@CentOS-7-1 ~]$ /home/web/tomcat/bin/startup.sh Using CATALINA_BASE: /home/web/tomcat Using CATALINA_HOME: /home/web/tomcat Using CATALINA_TMPDIR: /home/web/tomcat/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar Tomcat started.
[web@CentOS-7-1 ~]$ ps -ef | grep java web 11745 1 5 19:12 pts/0 00:00:07 /usr/local/java/bin/java -Djava.util.logging.config.file=/home/web/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/home/web/tomcat -Dcatalina.home=/home/web/tomcat -Djava.io.tmpdir=/home/web/tomcat/temp org.apache.catalina.startup.Bootstrap start web 11795 11697 0 19:15 pts/0 00:00:00 grep --color=auto java [web@CentOS-7-1 ~]$ lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 11745 web 48u IPv6 41272 0t0 TCP *:webcache (LISTEN)
處理流程:用戶發送請求到web服務器,該請求會被正在監聽的Connector鏈接器接收,並把該請求交給Service下的Engine來處理,並等待Engine處理的結果。Engine得到請求後會根據請求的主機信息來匹配相應的Host主機,Host主機會根據請求的路徑匹配對應的Context,Context web應用匹配上以後就構建request、response請求對象,調用指定的Servlet來處理請求。請求處理完成後會將response對象返回給Host主機,Host主機將response對象返回給Engine引擎,Engine再將response對象返回給Connector連接器,最後Connector鏈接器將response返回給瀏覽器。
[web@CentOS-7-1 tmp]$ mkdir mysql [web@CentOS-7-1 tmp]$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar
[web@CentOS-7-1 tmp]$ tar xf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar -C /tmp/mysql [web@CentOS-7-1 tmp]$ cd mysql/ [web@CentOS-7-1 mysql]$ rm mysql-community-server-minimal-5.7.22-1.el7.x86_64.rpm mysql-community-test-5.7.22-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.22-1.el7.x86_64.rpm -f [web@CentOS-7-1 mysql]$ yum -y install mysql-community-* [root@CentOS-7-1 /]# mkdir /data [root@CentOS-7-1 /]# mv /var/lib/mysql /data/ [root@CentOS-7-1 /]# ln -s /data/mysql /var/lib/ [root@CentOS-7-1 /]# ll /var/lib/mysql lrwxrwxrwx. 1 root root 11 7月 24 19:37 /var/lib/mysql -> /data/mysql
[root@CentOS-7-1 ~]# vim /etc/my.cnf #添加參數
max_allowed_packet=64M
wait_timeout=2880000
[root@CentOS-7-1 /]# systemctl start mysqld [root@CentOS-7-1 /]# grep pass /var/log/mysqld.log 2018-07-24T11:40:42.346028Z 1 [Note] A temporary password is generated for root@localhost: qpbhOGbT#6IZ [root@CentOS-7-1 ~]# mysqladmin -uroot -p'>Hq7kr*npwQs' password 'Tomcat@123' mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. [root@CentOS-7-1 ~]# mysql -p'Tomcat@123'
mysql> create database jspgou default character set utf8; Query OK, 1 row affected (0.00 sec) mysql> grant all privileges on jspgou.* to 'jspgouadmin'@'%' identified by 'Tomcat@123'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> select @@sql_mode; +-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> set global -> sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> \q Bye
Jspgou配置mysql,主要參考jspgouV6-ROOT/必讀.txt 文件
[root@CentOS-7-1 ~]# su - web [web@CentOS-7-1 ~]$ vim tomcat/webapps/ROOT/WEB-INF/config/jdbc.properties
導入jspgou庫,導入數據時間有可能比較長(若是沒有報錯,請耐心等待,若是一秒就完成了,那說明你沒有將數據導入成功,請從新配置數據庫,在次導入)
[root@CentOS-7-1 DB]# mysql -u'jspgouadmin' -p'Tomcat@123' jspgou < /home/web/web1/DB/jspgou.sql mysql: [Warning] Using a password on the command line interface can be insecure.
啓動 jspgou web site
[root@CentOS-7-1 ~]# su - web 上一次登陸:二 7月 24 20:39:14 CST 2018pts/1 上 [web@CentOS-7-1 ~]$ ls apache-tomcat-9.0.1 apache-tomcat-9.0.1.tar.gz jdk-8u181-linux-x64.tar.gz jspgouV6-ROOT.zip tomcat web1
[web@CentOS-7-1 ~]$ /home/web/tomcat/bin/startup.sh
Using CATALINA_BASE: /home/web/tomcat
Using CATALINA_HOME: /home/web/tomcat
Using CATALINA_TMPDIR: /home/web/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar
Tomcat started.
[web@CentOS-7-1 ~]$ lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 13738 web 48u IPv6 46855 0t0 TCP *:webcache (LISTEN)
[web@CentOS-7-1 ~]$ ps -ef |grep java
web 13738 1 99 20:56 pts/1 00:01:03 /usr/local/java/bin/java -Djava.util.logging.config.file=/home/web/tomcat/conf/logging
.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handle
r.pkgs=org.apache.catalina.webresources -classpath /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar -Dcatalina.
base=/home/web/tomcat -Dcatalina.home=/home/web/tomcat -Djava.io.tmpdir=/home/web/tomcat/temp org.apache.catalina.startup.Bootstrap st
art
web 13921 13660 0 20:57 pts/1 00:00:00 grep --color=auto java
Tomcat單機多實例部署配置(拷貝必須文件目錄)
[web@CentOS-7-1 ~]$ cd tomcat/ [web@CentOS-7-1 tomcat]$ mkdir web{1..3} [web@CentOS-7-1 tomcat]$ ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp web1 web2 web3 webapps work [web@CentOS-7-1 tomcat]$ cp -r /home/web/tomcat/{conf,logs,temp,work} /home/web/tomcat/web1 [web@CentOS-7-1 tomcat]$ cp -r /home/web/tomcat/{conf,logs,temp,work} /home/web/tomcat/web2 [web@CentOS-7-1 tomcat]$ cp -r /home/web/tomcat/{conf,logs,temp,work} /home/web/tomcat/web3
分別在三個配置文件中修改端口,將端口改成不一樣的(web1:8081,web2:8082,web3:8083)
[web@CentOS-7-1 ~]$ vim /home/web/tomcat/web1/conf/server.xml <Host name="localhost" appBase="/home/web/tomcat/webapps" unpackWARs="true" autoDeploy="true">
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<!-- 註釋這一行內容 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
-->
分別在三個web實例中添加此腳本,(必需要改export CATALINA_BASE="/home/web/tomcat/web{1,2,3}")
[web@CentOS-7-1 ~]$ vim /home/web/tomcat/web1/insl.sh #!/bin/bash #web1 . /etc/init.d/functions export CATALINA_BASE="/home/web/tomcat/web1"
case "$1" in start) $CATALINA_HOME/bin/startup.sh ;; stop) $CATALINA_HOME/bin/shutdown.sh ;; restart) $CATALINA_HOME/bin/shutdown.sh
sleep 5 $CATALINA_HOME/bin/startup.sh ;; esac export JAVA_OPTS='-Xms251m -Xmx512m'
~
[root@CentOS-7-1 ~]# vim /etc/profile #添加系統環境 CATALINA_HOME=/home/web/tomcat export CATALINA_HOME [root@CentOS-7-1 ~]# source /etc/profile #加載系統環境
[web@CentOS-7-1 ~]$ /home/web/tomcat/web1/insl.sh start Using CATALINA_BASE: /home/web/tomcat/web1 Using CATALINA_HOME: /home/web/tomcat Using CATALINA_TMPDIR: /home/web/tomcat/web1/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar Tomcat started. [web@CentOS-7-1 ~]$ /home/web/tomcat/web2/insl.sh start Using CATALINA_BASE: /home/web/tomcat/web2 Using CATALINA_HOME: /home/web/tomcat Using CATALINA_TMPDIR: /home/web/tomcat/web2/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar Tomcat started. [web@CentOS-7-1 ~]$ /home/web/tomcat/web3/insl.sh start Using CATALINA_BASE: /home/web/tomcat/web3 Using CATALINA_HOME: /home/web/tomcat Using CATALINA_TMPDIR: /home/web/tomcat/web3/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /home/web/tomcat/bin/bootstrap.jar:/home/web/tomcat/bin/tomcat-juli.jar Tomcat started. [web@CentOS-7-1 ~]$ ss -tnlp | grep :80 |column -t LISTEN 0 100 :::8081 :::* users:(("java",pid=15864,fd=48)) LISTEN 0 100 :::8082 :::* users:(("java",pid=15893,fd=48)) LISTEN 0 100 :::8083 :::* users:(("java",pid=15462,fd=48)) LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",pid=15462,fd=64))
[root@webserver ~]# useradd nginx [root@webserver ~]#tar xf nginx-1.9.9.tar.gz [root@webserver ~]# cd nginx-1.9.9/ ./configure \ --prefix=/usr/local/nginx \ --without-select_module --without-poll_module \ --with-debug \ --with-http_ssl_module --with-http_realip_module \ --with-http_addition_module --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_xslt_module \ --with-http_gzip_static_module --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_stub_status_module \ --with-cc=`which gcc` [root@webserver nginx-1.9.9]# make -j 4 && make install
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf http { upstream tomcat_pool { #ip_hash; server 10.18.43.183:8081 weight=1 max_fails=2 fail_timeout=2; server 10.18.43.183:8082 weight=1 max_fails=2 fail_timeout=2; server 10.18.43.183:8083 weight=1 max_fails=2 fail_timeout=2; } server { location / { proxy_pass http://tomcat_pool;
proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
session(會話)暫沒有使用共享方式,目前採用的會話保持。即將同一個client的訪問始終調度到同一後端實例。
1.會話保持
2.會話同步
3.會話共享