- Tomcat是Apache軟件基金會(Apache Software Foundation)的Jakarta項目中的一個核心項目,由Apache、Sun和其餘一些公司及我的共同開發而成。
- java程序寫的網站用tomcat+jdk來運行,tomcat是一箇中間件,真正起做用的,解析java腳本的是jdk
- jdk(java development kit)是整個java的核心,它包含了java運行環境和一堆java相關的工具以及java基礎庫。
- 最主流的jdk爲sun公司發佈的jdk,除此以外,其實IBM公司也有發佈JDK,CentOS上也能夠用yum安裝openjdk。
咱們已經學過了LAMP和LNMP架構;針對的開發語言是PHP;能夠說PHP是一門開發Web程序很是流行的語言;早期的比較流行的是asp,是在windows平臺上運行的一種編程語言;可是由於安全性不高;因此愈來愈多作網站的人用Php去開發網站,相對來講也是比較安全的。php
除了PHP外,還有一門語言叫java;java是一門很是龐大的開發語言;不只僅是能夠開發開網站;也能夠開發大型的軟件、工具、甚至是遊戲均可以;tomcat其實是一箇中間件,用來運行Java語言寫的網站;那咱們Php寫的代碼,要用php去解析;java就要用tomcat加上JDK一塊兒去解析。css
先在windows環境下,下載好jdk,用xftp傳輸到linux虛擬機root下java
[root@ying01 ~]# ls 10 11 221.txt 3 555 8 anaconda-ks.cfg ccc grep ib_logfile1 NBA sed1 test wenben ysb 100 12zhang 222 5 6 9 awk ch ib_logfile0 jdk-8u171-linux-x64.tar.gz sed shiyan test.txt xargs [root@ying01 ~]# mv jdk-8u171-linux-x64.tar.gz /usr/local/src/ [root@ying01 ~]# cd !$ cd /usr/local/src/ [root@ying01 src]# tar zxf jdk-8u171-linux-x64.tar.gz //解壓包 [root@ying01 src]# du -sh jdk1.8.0_171/ 374M jdk1.8.0_171/ [root@ying01 src]# mv jdk1.8.0_171 /usr/local/jdk1.8 //移動並更名
在/etc/profile增長如下代碼mysql
[root@ying01 src]# vim /etc/profile JAVA_HOME=/usr/local/jdk1.8/ JAVA_BIN=/usr/local/jdk1.8/bin JRE_HOME=/usr/local/jdk1.8/jre PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
加載服務後,查看下載版本是否正確;linux
[root@ying01 src]# source /etc/profile //加載服務 [root@ying01 src]# java -version //查看java版本 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)
其實Tomcat只是一箇中間件,真正起做用的就是我們已經安裝的jdk。沒有Tomcat還不能夠,他的做用就是監聽8080;8005;8009端口。假如說,訪問站點,須要一個webserver,而Tomcat就能夠理解爲webserver!nginx
在官網找到二進制包,下載;web
[root@ying01 src]# wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz
解壓、並把解壓包移到 /usr/local/tomcat下;sql
[root@ying01 src]# tar -zxf apache-tomcat-8.5.32.tar.gz [root@ying01 src]# mv apache-tomcat-8.5.32 /usr/local/tomcat
開啓服務與關閉服務數據庫
[root@ying01 src]# /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/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@ying01 src]# ps aux |grep java //java已經啓動 root 7794 9.9 5.1 3000080 96832 pts/0 Sl 19:58 0:05 /usr/local/jdk1.8/bin/java -java.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start root 7852 0.0 0.0 112720 984 pts/0 S+ 19:58 0:00 grep --color=auto java [root@ying01 src]# /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/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@ying01 src]# !ps //沒有java進程 ps aux |grep java root 7897 0.0 0.0 112720 984 pts/0 S+ 20:00 0:00 grep --color=auto java
查看java有關端口apache
[root@ying01 src]# /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/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@ying01 src]# netstat -lntp |grep java tcp6 0 0 :::8080 :::* LISTEN 8986/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8986/java tcp6 0 0 :::8009 :::* LISTEN 8986/java
三個端口釋義:
- 8080爲提供web服務的端口;
- 8005爲管理端口;//顯示調用會慢
- 8009端口爲第三方服務調用的端口,好比httpd和Tomcat結合時會用到
在關閉防火牆後,用瀏覽器訪問其默認主頁:192.168.112.136:8080
[root@ying01 src]# systemctl stop firewalld [root@ying01 src]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
要給Tomcat監聽80端口,那先看戲80監聽端口,是否佔用;
[root@ying01 src]# netstat -lntp |grep 80 //被nginx佔用 tcp 0 0 0.0.0.0:37580 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1072/nginx: master tcp6 0 0 :::8080 :::* LISTEN 8986/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8986/java tcp6 0 0 :::8009 :::* LISTEN 8986/java [root@ying01 src]# /etc/init.d/nginx stop //關閉nginx服務 Stopping nginx (via systemctl): [ 肯定 ] [root@ying01 src]# netstat -lntp |grep 80 tcp 0 0 0.0.0.0:37580 0.0.0.0:* LISTEN - tcp6 0 0 :::8080 :::* LISTEN 8986/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 8986/java tcp6 0 0 :::8009 :::* LISTEN 8986/java
在確認80端口處於空閒狀態,在tomcat主配置文件,把8080端口改成80端口;
[root@ying01 src]# vim /usr/local/tomcat/conf/server.xml
從新加載tomcat主配置文件,並查看80端口狀況;
[root@ying01 src]# /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/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@ying01 src]# /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/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@ying01 src]# netstat -lntp |grep 80 //java佔用80端口 tcp 0 0 0.0.0.0:37580 0.0.0.0:* LISTEN - tcp6 0 0 :::80 :::* LISTEN 9350/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 9350/java tcp6 0 0 :::8009 :::* LISTEN 9350/java
此時再在瀏覽器測試8080,已經打不開網頁,而用80就能夠;
在tomcat的配置文件中,增長一個虛擬主機,代碼以下:
[root@ying01 ~]# vim /usr/local/tomcat/conf/server.xml <Host name="www.123.cn" appBase="" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="" docBase="/data/wwwroot/www.123.cn/" debug="0" reloadable="true" crossContext="true"/> </Host>
以上代碼關鍵詞釋義:
- name 定義域名;
- appBase 定義應用的目錄;
- unpackWARs=」true」 是否自動解壓;(也是就是說,當咱們往站點目錄裏面直接上傳一個war的包,它會自動解壓)
- docBase,這個參數用來定義網站的文件存放路徑,若是不定義,默認是在appBase/ROOT下面,定義了docBase就以該目錄爲主了,其中appBase和docBase能夠同樣。在這一步操做過程當中,可能會遇到過訪問404的問題,其實就是docBase沒有定義對。
下載站點程序zrlog;
[root@ying01 src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
把此war包,複製一份到/usr/local/tomcat/webapps/下,在此目錄下會自動解壓;
[root@ying01 src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/ [root@ying01 src]# ls /usr/local/tomcat/webapps/ //多了一個zrlog的目錄 docs examples host-manager manager ROOT zrlog-1.7.1-baaecb9-release zrlog-1.7.1-baaecb9-release.war
如今咱們作實驗,把zrlog-1.7.1-baaecb9-release解壓包,更名爲zrlog;
[root@ying01 src]# cd /usr/local/tomcat/webapps/ [root@ying01 webapps]# mv zrlog-1.7.1-baaecb9-release zrlog [root@ying01 webapps]# ls docs examples host-manager manager ROOT zrlog zrlog-1.7.1-baaecb9-release.war [root@ying01 webapps]# ls docs examples host-manager manager ROOT zrlog zrlog-1.7.1-baaecb9-release zrlog-1.7.1-baaecb9-release.war
此時在瀏覽器中輸入:http://192.168.112.136/zrlog
進入mysql,建立一個數據庫,並建立用戶
[root@ying01 webapps]# mysql -uroot -pyinglinux Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database zrlog; //建立zrlog數據庫 Query OK, 1 row affected (0.07 sec) mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 indentified by 'yinglinux1'; //指定密碼 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'indentified by 'yinglinux1'' at line 1 mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by 'yinglinux1'; Query OK, 0 rows affected (0.19 sec) mysql> quit Bye
使用新用戶,及密碼登陸
[root@ying01 webapps]# mysql uzrlog -h127.0.0.1 -pyinglinux1 Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) [root@ying01 webapps]# mysql -uzrlog -h127.0.0.1 -pyinglinux1 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; //顯示當前庫,裏面有zrlog數據庫 +--------------------+ | Database | +--------------------+ | information_schema | | test | | zrlog | +--------------------+ 3 rows in set (0.00 sec) mysql> quit
配置好數據庫後,如今回到剛纔的網頁上,按下面填寫;
點擊下一步,按下圖填寫;
點擊下一步,按下圖操做;
此時,博客配置好,裏面有一篇文章;
在上個頁面,點擊管理,進入後臺操做;
在後臺寫一篇文章,自定義,它隨時保存;
點擊保存,刷新頁面,這時主頁出現新建的文章;
在windows下,host文件下,配置www.123.cn;在cmd下ping此域名;
Microsoft Windows [版本 10.0.17134.165] (c) 2018 Microsoft Corporation。保留全部權利。 C:\Users\zqsgq>ping www.123.cn 正在 Ping www.abc.com [192.168.112.136] 具備 32 字節的數據: 來自 192.168.112.136 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.112.136 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.112.136 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.112.136 的回覆: 字節=32 時間<1ms TTL=64 192.168.112.136 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 0ms,最長 = 0ms,平均 = 0ms C:\Users\zqsgq>
按虛擬主機配置文件,建立相應的網站目錄
[root@ying01 webapps]# mkdir /data/wwwroot/123.cn
把zrlog目錄下全部文件,移到新建立的網站目錄123.cn
[root@ying01 webapps]# mv zrlog/* /data/wwwroot/123.cn/
從新加載tomcat配置文件
[root@ying01 webapps]# /usr/local/tomcat/bin/shutdown.sh [root@ying01 webapps]# /usr/local/tomcat/bin/startup.sh [root@ying01 webapps]# netstat -lntp |grep 80 tcp 0 0 0.0.0.0:37580 0.0.0.0:* LISTEN - tcp6 0 0 :::80 :::* LISTEN 10068/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 10068/java tcp6 0 0 :::8009 :::* LISTEN 10068/java [root@ying01 webapps]#
回到瀏覽器下,用域名www.123.cn訪問,此時獲得以下頁面;
網站的圖標和一些重要信息都在/usr/local/tomcat/webapps/ROOT下
[root@ying01 webapps]# ls ROOT/ asf-logo-wide.svg bg-middle.png bg-nav.png favicon.ico RELEASE-NOTES.txt tomcat.gif tomcat-power.gif WEB-INF bg-button.png bg-nav-item.png bg-upper.png index.jsp tomcat.css tomcat.png tomcat.svg [root@ying01 webapps]#
好比ROOT下tomcat-power.gif 動圖,就是主頁上一個圖;
再好比這個按鈕:bg-button.png
Tomcat日誌目錄位置/usr/local/tomcat/logs
[root@ying01 ~]# cd /usr/local/tomcat/ [root@ying01 tomcat]# ls logs catalina.2018-07-16.log localhost.2018-07-17.log catalina.2018-07-17.log localhost_access_log.2018-07-16.txt catalina.out localhost_access_log.2018-07-17.txt host-manager.2018-07-16.log manager.2018-07-16.log host-manager.2018-07-17.log manager.2018-07-17.log localhost.2018-07-16.log
日誌目錄下,各類日誌的做用:
- catalina開頭的日誌爲Tomcat的綜合日誌,它記錄Tomcat服務相關信息,也會記錄錯誤日誌。
- catalina.2017-xx-xx.log和catalina.out內容相同,前者會天天生成一個新的日誌。
- host-manager和manager爲管理相關的日誌,其中host-manager爲虛擬主機的管理日誌。
- localhost和localhost_access爲虛擬主機相關日誌,其中帶access字樣的日誌爲訪問日誌,不帶access字樣的爲默認虛擬主機的錯誤日誌。
訪問日誌默認不會生成,須要在server.xml中配置;
[root@ying01 tomcat]# vim /usr/local/tomcat/conf/server.xml 此爲虛擬主機下的,訪問日誌配置: <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" //關鍵字logs prefix="localhost_access_log" suffix=".txt" //定義的前綴名,後綴名 pattern="%h %l %u %t "%r" %s %b" /> //定義日誌格式
新增長的虛擬主機默認並不會生成相似默認虛擬主機的那個localhost.日期.log日誌; 錯誤日誌會統一記錄到catalina.out中。
關於Tomcat日誌,最須要關注catalina.out,當出現問題時,咱們應該第一想到去查看它。