Install and configure JDK and Tomcat under Ubuntu system

Apache Tomcat

http://tomcat.apache.org/

System Ubuntu, install and configure JDK, Tomcat.

One, install and configure JDK

The JDK of the Linux platform is generally divided into two versions: OpenJDK and oracle's official JDK.

the difference:

License agreement: OpenJDK adopts GPL V2 agreement, and SUN JDK adopts JRL. Both agreements are open source, GPL V2 allows commercial use, and JRL only allows personal research and use.

OpenJDK does not include Deployment (deployment) functions: Browser Plugin, Java Web Start, and Java Control Panel. The OpenJDK source code is incomplete, and OpenJDK only contains the most streamlined JDK.

Version display: OpenJDK cannot use the Java trademark. Entering "java -version" shows OpenJDK, but using Oracle to install java, it shows java.
Insert picture description here

1. Install the default JRE and JDK

Open JDK is a completely open source JDK, the default version of JDK (Open JDK):

$ sudo apt install default-jdk

Insert picture description hereInsert picture description here
Specify version installation:

$ sudo apt-get install openjdk-15-jre

Insert picture description here

Set the JAVA_HOME environment variable:

$ gedit ~/.bashrc
#  在文件末尾加上一行:
	export JAVA_HOME=/usr/lib/jvm/java-15-openjdk-amd64
$ source ~/.bashrc

The difference between bashrc and profile

•	/etc/profile,/etc/bashrc 是系统全局环境变量设定
•	~/.profile,~/.bashrc 用户家目录下的私有环境变量设定
•	profile,对应的是用户登录系统时生效
•	bashrc,对应的使用户登录系统后,打开终端时生效

2. Install oracle official JDK

https://www.oracle.com/
Developer Downloads
https://www.oracle.com/downloads/
Java SE Downloads
https://www.oracle.com/java/technologies/javase-downloads.html
Insert picture description here
下载文件 dk-15.0.1_linux-x64_bin.deb 安装:

$ sudo dpkg -i jdk-15.0.1_linux-x64_bin.deb
# 注册 jdk
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-15.0.1/bin/java 500
$ sudo update-alternatives --install /usr/local/jdk jdk /usr/lib/jvm/jdk-15.0.1 500

Set the JAVA_HOME environment variable:

$ gedit ~/.bashrc
#  在文件末尾加上:	
	export JAVA_HOME=/usr/lib/jvm/jdk-15.0.1 # jdk 安装后的位置
	export JRE_HOME=${JAVA_HOME}/jre
	export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
	export PATH=${JAVA_HOME}/bin:$PATH	
$ source ~/.bashrc

$ echo $JAVA_HOME

linux software version management command update-alternatives

$ update-alternatives --config java

Two, install and configure Tomcat

1、Tomcat 9 Software Downloads Insert picture description here

Insert picture description here
https://tomcat.apache.org/download-90.cgi

Suppose the downloaded file is unzipped to ~/apache-tomcat-9.0.41

Start the Tomcat server

yake@Ubuntu:~/apache-tomcat-9.0.41/bin$ sudo ./startup.sh

Using CATALINA_BASE:   /home/yake/apache-tomcat-9.0.41
Using CATALINA_HOME:   /home/yake/apache-tomcat-9.0.41
Using CATALINA_TMPDIR: /home/yake/apache-tomcat-9.0.41/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /home/yake/apache-tomcat-9.0.41/bin/bootstrap.jar:/home/yake/apache-tomcat-9.0.41/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

http://localhost:8080/

2. Tomcat configuration

conf/server.xml

1) Tomcat starts the default port

Insert picture description here

2) Tomcat virtual directory mapping

a. Configure in the host element of the server.xml file

Add the Context tag within the Host tag:

<Context path="/JavaWebApp" docBase="\web\www\html" />

Context represents the context, which represents a JavaWeb application. The Context element has two attributes,
  Ⅰ.path: virtual directory, which must begin with "/".
  Ⅱ.docBase: App catalog.

 	http://localhost:8080/JavaWebApp/aa.jsp	 	

b, Tomcat automatic mapping

The Tomcat server automatically manages all web applications in the webapps directory and maps it to a virtual directory.
Copy JavaWebDemo to the webapps directory, and the Tomcat server will automatically map a virtual directory "/JavaWebDemo" with the same name for the JavaWebDemo application.

c, add xml file

Add an xml file in the \conf\Catalina\localhost directory of the Tomcat server with any file name, such as the demo.xml below.

The context path and version will be derived from the base name of the file

$CATALINA_BASE refers to the root directory of the Tomcat server, [enginename] refers to the engine name used by the Tomcat server, and the engine used by Tomcat is Catalina.  
Add the Context element to the demo.xml file to map the JavaWeb application, the code is as follows:

<Context docBase=" \web\www\html" />

Note:
The virtual directory name mapped by the path attribute "\web\www\html" is not specified in the Context element is the xml file name demo

http://localhost:8080/demo/aa.jsp

3) Tomcat server configuration virtual host

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

Virtual host manages all web applications under the webapps folder

<Host name="localhost"  appBase="\web\www">
</Host>

Note: \web\www is not the root directory of a project, but the folder where JavaWeb applications are stored.

http://localhost:8080/html/index.jsp

Three, configure Nginx

conf/nginx.conf

events {   worker_connections  1024;	}
http {	
    upstream tomcat{	
        server localhost:8080;	
    }	
    server {	
        root /home/yake/www/html;	
        location ~ (\.jsp)|(\.do)$ {	
            proxy_pass http://tomcat;	
        }	
        location /img/ {  	
            alias /home/yake/www/html/img/;  	
        }	
    }
}

Suppose the directory structure is as follows:

yake@Ubuntu:~/www$ tree ../www
../www
└── html
    ├── conf
    │   └── nginx.conf
    ├── img
    │   └── example.jpg
    ├── index.html
    ├── index.jsp
    └──logs
         ├── access.log
         ├── error.log
         └── nginx.pid

Guess you like

Origin blog.csdn.net/weixin_43955170/article/details/112424418