Common configuration of Tomcat

Index of this article:

  • Configure tomcat to listen on port 80
  • Tomcat virtual host
  • Tomcat logs

Configure tomcat to listen on port 80

By default, tomcat listens to port 8080. If you want to directly enter the ip to access the web page, you need to do the following:

  • Modify the configuration file
[root@localhost src]# vim /usr/local/tomcat/conf/server.xml 
/定位到8080
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
修改8080为80,保存退出
  • restart the service
# 先关闭后启动
[root@localhost src]# /usr/local/tomcat/bin/shutdown.sh 
[root@localhost src]# /usr/local/tomcat/bin/startup.sh 
  • Verify that it works
[root@localhost src]# netstat -lntp | grep java
tcp6       0      0 :::80                   :::*                    LISTEN      2823/java           
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      2751/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      2751/java                 

If the apache/nginx web service has been installed and started in the system, and port 80 has been occupied, you need to shut down these web servers, and then restart the tomcat service.


Tomcat virtual host

Like the apache/nginx web server, tomcat can also set up virtual hosts.

The configuration between <Host> and </Host> in the tomcat configuration file is the virtual host configuration part, the name defines the domain name, and the appBase defines the application directory. The Java application is usually a war compressed package. The compressed package can be placed under the appBase directory.

[root@localhost src]# vim /usr/local/tomcat/conf/server.xml 
// 在</Host>下添加下列代码

<Host name="www.123.cn" appBase=""
    unpackWARs= "true" autoDeploy="true"
    xmlValidation="false" xmlNamespaceAware="false">
    <Context path="" docBase="/data/wwwroot/123.cn/" debug="0" reloadable="true" crossContext="true"/>
</Host>

# 部分参数说明:
# name:需要填写的是虚拟主机的域名
# appBase:应用项目(WAR包)所在目录,配合unpackWARs将自动解压WAR包成一个程序目录
# WAR包:开发人员开发的组建网站的各种网页、配置、jsp代码、数据库相关文件会被打包成一个WAR包,便于存放和传输。
# unpackWARs: 自动解压WAR包
# docBase:在该处定义的目录内存放的是解压后的WAR包的文件,定义了该项后,以该目录为主,appBase可以不需要定义,也可以定义成一样;如果不定义,默认文件存放在appBase/ROOT下,两者取其以即可。如果docBase没有定义正确,任意产生404错误。

appBase和docBase

The relative path written in appBase is /usr/local/tomcat/

When using a WAR package, you need to write appBase, and unpackWARs needs to be set to "True", otherwise it cannot be automatically decompressed.

If the provided website is a directory, all the files involved are placed in the directory specified by the docBase parameter.

ROOT directory under the webapps directory

[root@localhost webapps]# ls ROOT/
asf-logo-wide.svg  bg-upper.png       tomcat.gif
bg-button.png      favicon.ico        tomcat.png
bg-middle.png      index.jsp          tomcat-power.gif
bg-nav-item.png    RELEASE-NOTES.txt  tomcat.svg
bg-nav.png         tomcat.css         WEB-INF

It can be accessed directly through localhost/index.jsp/ localhost/bg-nav.png, that is, the ROOT directory is accessed by default.

The program directory defined by yourself also needs to create a ROOT directory, and put pictures and static files in the ROOT directory, so that you can directly access the custom website.

Virtual host instance --- the construction of zrlog blog

After building the tomcat environment, we can build a simple blog application website to experience it.

  1. Download the WAR package of zrlog
[root@localhost src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
[root@localhost src]# du -sh zrlog-1.7.1-baaecb9-release.war 6.8M	zrlog-1.7.1-baaecb9-release.war
  1. Move to the webapps directory under the tomcat directory
[root@localhost src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/
# 改名的目的是为了访问的方便,毕竟包名有点长
[root@localhost webapps]# mv zrlog-1.7.1-baaecb9-release zrlog
  1. Create related databases and users
# 前提是系统内有mysql并启动
[root@localhost webapps]# ps aux | grep mysql
# 登录mysql,去创建zrlog用户
[root@localhost webapps]# mysql -uroot -p
Enter password: 
# 创建数据库zrlog
mysql> create database zrlog;
Query OK, 1 row affected (0.03 sec)
# 创建新用户zrlog,并赋予其权限
mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by 'zrlog';
Query OK, 0 rows affected (0.04 sec)
mysql> quit
Bye

# 使用zrlog用户登录测试
[root@localhost webapps]# mysql -uzrlog -h127.0.0.1 -p
Enter password: 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| zrlog              |
+--------------------+
3 rows in set (0.03 sec)

mysql> quit
Bye
  1. Sign in and sign in with your browser

    • Enter the newly created zrlog user and its password, the email address is optional

    step1

    • Set admin password, customize website labels and subtitles

    step2

    • complete registration

    step3

    • Blog renderings

    web

After the registration is successful, you can use the admin account to log in to the blog to edit and publish the blog freely!


Tomcat logs

[root@localhost tomcat]# ls /usr/local/tomcat/logs/
catalina.2018-01-13.log
catalina.2018-01-14.log
catalina.out
host-manager.2018-01-13.log
host-manager.2018-01-14.log
localhost.2018-01-13.log
localhost.2018-01-14.log
localhost_access_log.2018-01-13.txt
localhost_access_log.2018-01-14.txt
manager.2018-01-13.log
manager.2018-01-14.log

Tomcat logs are divided into 4 categories:

  • The log at the beginning of catalina is the comprehensive log of Tomcat, which records the information related to the Tomcat service and also records the error log. tomcat will generate a new log catalina.YYYY-mm-dd.log every day. catalina.out has the same content
  • host-manager is the management-related log, where host-manager is the management log of the virtual host. log content may be empty
  • The manager is also a management related log, similar to the host-manager
  • The log starting with localhost is the related log of the virtual host, the log with the word access is the access log, and the log without the word access is the error log of the default virtual host

Access logs are not generated by default and need to be defined in server.xml

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="123.cn_access" suffix=".log"
         pattern="%h %l %u %t &quot;%r&quot; %s %b" />

# directory定义日志存放的目录,这里是/usr/local/tomcat/logs目录
# prefix定义访问日志的前缀
# suffix定义日志的后缀
# pattern定义日志格式。

The newly added virtual host will not generate the localhost.date.log log similar to the default virtual host by default, and the error log information will be uniformly recorded in catalina.out. Therefore, when there is a problem, you should check the catalina.out file as soon as possible.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324462717&siteId=291194637