Tomcat

Apache Tomcat

Introduction

Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Tomcat 8 on your Ubuntu 14.04 server.

Prerequisites

Before you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by completing steps 1-3 in the initial server setup for Ubuntu 14.04. We will be using the demo user created here for the rest of this tutorial.

Install Java

Tomcat requires that Java is installed on the server, so any Java web application code can be executed. Let's satisfy that requirement by installing OpenJDK 7 with apt-get.

First, update your apt-get package index:

sudo apt-get update

Then install the Java Development Kit package with apt-get:

sudo apt-get install default-jdk

Answer y at the prompt to continue installing OpenJDK 7.

Now that Java is installed, let's create a tomcat user, which will be used to run the Tomcat service.

Create Tomcat User

For security purposes, Tomcat should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the Tomcat service.

First, create a new tomcat group:

sudo groupadd tomcat

Then create a new tomcat user. We'll make this user a member of the tomcat group, with a home directory of /opt/tomcat (where we will install Tomcat), and with a shell of /bin/false (so nobody can log into the account):

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Now that our tomcat user is set up, let's download and install Tomcat.

Install Tomcat

The easiest way to install Tomcat 8 at this time is to download the latest binary release then configure it manually.

Download Tomcat Binary

Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the time of writing, the latest version is 8.0.23. Under the Binary Distributions section, then under the Core list, copy the link to the "tar.gz".

Let's download the latest binary distribution to our home directory.

First, change to your home directory:

cd ~

Then use wget and paste in the link to download the Tomcat 8 archive, like this (your mirror link will probably differ from the example):

wget http://mirror.sdunix.com/apache/tomcat/tomcat-7/v7.0.67/bin/apache-tomcat-7.0.67.tar.gz

We're going to install Tomcat to the /opt/tomcat directory. Create the directory, then extract the the archive to it with these commands:

sudo mkdir /opt/tomcat
sudo tar xvf apache-tomcat-7*tar.gz -C /opt/tomcat --strip-components=1

Now we're ready to set up the proper user permissions.

Update Permissions

The tomcat user that we set up needs to have the proper access to the Tomcat installation. We'll set that up now.

Change to the Tomcat installation path:

cd /opt/tomcat

Then give the tomcat user write access to the conf directory, and read access to the files in that directory:

sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*

Then make the tomcat user the owner of the work, temp, and logs directories:

sudo chown -R tomcat work/ temp/ logs/

Now that the proper permissions are set up, let's set up an Upstart init script.

Install Upstart Script

Because we want to be able to run Tomcat as a service, we will set up an Upstart script.

Tomcat needs to know where Java was installed. This path is commonly referred to as "JAVA_HOME". The easiest way to look up that location is by running this command:

sudo update-alternatives --config java

Output:

There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

Nothing to configure.

The JAVAHOME will be in the output, without the trailing /bin/java. For the example above, the JAVAHOME is highlighted in red.

Now we're ready to create the Upstart script. Create and open it by running this command:

sudo nano /etc/init/tomcat.conf

Paste in the following script, and modify the value of JAVA_HOME if necessary. You may also want to modify the memory allocation settings that are specified in CATALINA_OPTS:

/etc/init/tomcat.conf

description "Tomcat Server"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5

setuid tomcat
setgid tomcat

env JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
env CATALINA_HOME=/opt/tomcat

# Modify these options as needed
env JAVA_OPTS="-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
env CATALINA_OPTS="-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

exec $CATALINA_HOME/bin/catalina.sh run

# cleanup temp directory after stop
post-stop script
rm -rf $CATALINA_HOME/temp/*
end script

Save and exit. This script tells the server to run the Tomcat service as the tomcat user, with the settings specified. It also enables Tomcat to run when the server is started.

Now let's reload the Upstart configuration, so we can use our new Tomcat script:

sudo initctl reload-configuration

Tomcat is ready to be run. Start it with this command:

sudo initctl start tomcat

Tomcat is not completely set up yet, but you can access the default splash page by going to your domain or IP address followed by :8080 in a web browser:

Open in web browser:

http://server_IP_address:8080

You will see the default Tomcat splash page, in addition to other information. Now we will go deeper into the installation of Tomcat.

Configure Tomcat Web Management Interface

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

sudo nano /opt/tomcat/conf/tomcat-users.xml

This file is filled with comments which describe how to configure the file. You may want to delete all the comments between the following two lines, or you may leave them if you want to reference the examples:

tomcat-users.xml excerpt

<tomcat-users>
...
</tomcat-users>

You will want to add a user who can access the manager-gui and admin-gui (webapps that come with Tomcat). You can do so by defining a user similar to the example below. Be sure to change the username and password to something secure:

tomcat-users.xml — Admin User

<tomcat-users>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

Save and quit the tomcat-users.xml file. To put our changes into effect, restart the Tomcat service:

sudo initctl restart tomcat

Access the Web Interface

Now that Tomcat is up and running, let's access the web management interface in a web browser. You can do this by accessing the public IP address of the server, on port 8080:

Open in web browser:

http://server_IP_address:8080

You will see something like the following image:

Tomcat自帶log的配置

問題

tomcat每次啟動時襟士,自動在logs目錄下生產(chǎn)以下日志文件,且每天都會生成對應(yīng)日期的一個文件,造成日志文件眾多:

 localhost.2012-07-05.txt
 catalina.2012-07-05.txt
 manager.2012-07-05.txt
 host-manager.2012-07-05.txt

目的

Tomcat以上日志都輸出到同一個文件中跑杭。

修改步驟

打開Tomcat目錄conf\logging.properties况增,修改如下畦攘,所有日志輸出到tomcat開頭的文件中

1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
# 1catalina.org.apache.juli.FileHandler.prefix = catalina.
1catalina.org.apache.juli.FileHandler.prefix = tomcat.
 
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
# 2localhost.org.apache.juli.FileHandler.prefix = localhost.
2localhost.org.apache.juli.FileHandler.prefix = tomcat.
 
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
# 3manager.org.apache.juli.FileHandler.prefix = manager.
3manager.org.apache.juli.FileHandler.prefix = tomcat.
 
4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
# 4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
4host-manager.org.apache.juli.FileHandler.prefix = tomcat.

Tomcat日志總結(jié)

1 Tomcat 日志信息分為兩類 :

  • 一是運(yùn)行中的日志闺鲸,它主要記錄運(yùn)行的一些信息菠劝,尤其是一些異常錯誤日志信息 。
  • 二是訪問日志信息淤刃,它記錄的訪問的時間晒他,IP ,訪問的資料等相關(guān)信息逸贾。

2 訪問日志的配置

2.1 默認(rèn) tomcat 不記錄訪問日志陨仅,如下方法可以使 tomcat 記錄訪問日志
編輯 ${catalina}/conf/server.xml 文件. 注 :${catalina} 是 tomcat 的安裝目錄
把以下的注釋 () 去掉即可。

 <!--
<Valve className="org.apache.catalina.valves.AccessLogValve"
 directory="logs"  prefix="localhost_access_log." suffix=".txt"
 pattern="common" resolveHosts="false"/>
  -->

2.2 配置tomcat 寫出更詳細(xì)的日志
通過對 2.1 示例中 pattern 項(xiàng)的修改铝侵,可以改變?nèi)罩据敵龅膬?nèi)容灼伤。
該項(xiàng)值可以為: common 與 combined ,這兩個預(yù)先設(shè)置好的格式對應(yīng)的日志輸出內(nèi)容如下:

common 的值: %h %l %u %t %r %s %b
combined 的值: %h %l %u %t %r %s %b %{Referer}i %{User-Agent}i

pattern 也可以根據(jù)需要自由組合, 例如 pattern="%h %l"

對于各fields字段的含義請參照 :

http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html 中的 Access Log Valve 項(xiàng)

3 修改Tomcat運(yùn)行日志的等級
3.1 日志類型與級別
Tomcat 日志分為下面5類:
catalina 咪鲜、 localhost 狐赡、 manager 、 admin 疟丙、 host-manager
每類日志的級別分為如下 7 種:
SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)
3.2 日志級別的設(shè)定方法
修改 conf/logging.properties 中的內(nèi)容颖侄,設(shè)定某類日志的級別
示例:

設(shè)置 catalina 日志的級別為: FINE

1catalina.org.apache.juli.FileHandler.level = FINE

禁用 catalina 日志的輸出:

1catalina.org.apache.juli.FileHandler.level = OFF

輸出 catalina 所有的日志消息均輸出:

1catalina.org.apache.juli.FileHandler.level = ALL
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市享郊,隨后出現(xiàn)的幾起案子览祖,更是在濱河造成了極大的恐慌,老刑警劉巖炊琉,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件展蒂,死亡現(xiàn)場離奇詭異,居然都是意外死亡苔咪,警方通過查閱死者的電腦和手機(jī)锰悼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悼泌,“玉大人松捉,你說我怎么就攤上這事」堇铮” “怎么了隘世?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鸠踪。 經(jīng)常有香客問我丙者,道長,這世上最難降的妖魔是什么营密? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任械媒,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘纷捞。我一直安慰自己痢虹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布主儡。 她就那樣靜靜地躺著奖唯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪糜值。 梳的紋絲不亂的頭發(fā)上丰捷,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機(jī)與錄音寂汇,去河邊找鬼病往。 笑死,一個胖子當(dāng)著我的面吹牛骄瓣,可吹牛的內(nèi)容都是我干的停巷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼累贤,長吁一口氣:“原來是場噩夢啊……” “哼叠穆!你這毒婦竟也來了少漆?” 一聲冷哼從身側(cè)響起臼膏,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎示损,沒想到半個月后渗磅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡检访,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年始鱼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脆贵。...
    茶點(diǎn)故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡医清,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出卖氨,到底是詐尸還是另有隱情会烙,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布筒捺,位于F島的核電站柏腻,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏系吭。R本人自食惡果不足惜五嫂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沃缘,春花似錦躯枢、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至峰档,卻和暖如春败匹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背讥巡。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工掀亩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人欢顷。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓槽棍,卻偏偏與公主長得像,于是被迫代替她去往敵國和親抬驴。 傳聞我的和親對象是個殘疾皇子炼七,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,493評論 5 6
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,573評論 0 23
  • 天只要一黑 癡顛就醒 枉行千百縱虛度 童心未泯 闌珊螢火蟲 星空雜草中 淚痕流過的溝渠 穿過歷史和文藝 總是去向不...
    陳風(fēng)正閱讀 200評論 0 2
  • 他們都是孤兒,被一個老人收養(yǎng)布持。 陽陽總是搶果果的食物豌拙,但是在身材上總是比不上他,而且差距越來越大题暖。 陽陽是同胞中的...
    見優(yōu)者王斌閱讀 368評論 0 0
  • 寫作是我的弱項(xiàng)胧卤,自己本身對寫文字也不感興趣唯绍,從小寫作文就頭疼,一直就那么耗到現(xiàn)在枝誊,作文也沒有半點(diǎn)長進(jìn)况芒,唯一慶幸的是...
    墨_未白閱讀 157評論 3 0