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目录下生产以下日志文件,且每天都会生成对应日期的一个文件,造成日志文件众多:

 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日志总结

1 Tomcat 日志信息分为两类 :

  • 一是运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息 。
  • 二是访问日志信息,它记录的访问的时间,IP ,访问的资料等相关信息。

2 访问日志的配置

2.1 默认 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 写出更详细的日志
通过对 2.1 示例中 pattern 项的修改,可以改变日志输出的内容。
该项值可以为: common 与 combined ,这两个预先设置好的格式对应的日志输出内容如下:

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

pattern 也可以根据需要自由组合, 例如 pattern="%h %l"

对于各fields字段的含义请参照 :

http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html 中的 Access Log Valve 项

3 修改Tomcat运行日志的等级
3.1 日志类型与级别
Tomcat 日志分为下面5类:
catalina 、 localhost 、 manager 、 admin 、 host-manager
每类日志的级别分为如下 7 种:
SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)
3.2 日志级别的设定方法
修改 conf/logging.properties 中的内容,设定某类日志的级别
示例:

设置 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,304评论 5 6
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 8,574评论 0 23
  • 天只要一黑 痴颠就醒 枉行千百纵虚度 童心未泯 阑珊萤火虫 星空杂草中 泪痕流过的沟渠 穿过历史和文艺 总是去向不...
    陈破晓阅读 184评论 0 2
  • 他们都是孤儿,被一个老人收养。 阳阳总是抢果果的食物,但是在身材上总是比不上他,而且差距越来越大。 阳阳是同胞中的...
    见优者王斌阅读 338评论 0 0
  • 写作是我的弱项,自己本身对写文字也不感兴趣,从小写作文就头疼,一直就那么耗到现在,作文也没有半点长进,唯一庆幸的是...
    墨_未白阅读 149评论 3 0