Table of contents
2.1. Download the tomcat installation package
2.2. Upload the tomcat installation package
3. Deploy the Maven project to tomcat
3.2. Upload the packaged war package to Linux
3.3. Modify the ip in the connection database configuration file
3.5, direct access to ip access
3.6. Possible problems in the cloud server
1. Preparation stage
1. You need to prepare a server. Here we recommend you to install a local virtual machine. VMware is a good virtual machine software.
2. You need a tool to remotely connect to the virtual machine. The tool I use here is MobaXterm.
Note: This article needs to start tomcat. The jdk environment must be installed on the linux server. If it is not installed, please read my previous article linux installation jdk
2. Specific steps
2.1. Download the tomcat installation package
Download the apache-tomcat-8.5.66.tar.gz installation package:
Link: https://pan.baidu.com/s/1G1irvm9BlLEZ-zkbK9TkWA
Extraction code: 2wbc
2.2. Upload the tomcat installation package
Use the connection tool to upload the installation package to the directory /usr/local. This directory is only recommended here, and you can feel free.
After uploading, you can see a red installation package of apache-tomcat-8.5.66.tar.gz through ll query, here we need to decompress
use command
tar -xzvf apache-tomcat-8.5.66.tar.gz
After the decompression is complete, you will get a blue folder apache-tomcat-8.5.66
2.3, start tomcat
cd to enter the directory, and then enter the bin directory of tomcat, where there is the tomcat startup program (statrup.sh) and shutdown program (shutdown.sh).
Enter the following command in the current directory
sh startup.sh
If Tomcat started is prompted, it means that tomcat has started successfully
2.4. Visit the page
After tomcat is started, in order to verify whether the home page of tomcat can be accessed, we enter the virtual machine ip:8080 in the browser url address to access, but it turns out that it cannot be accessed. Why can't I access the page after starting tomcat successfully? That's because the virtual machine is a Linux system, and it also has a firewall, which is a security mechanism, so we need to turn off the firewall. Enter the following command to turn off the firewall
systemctl stop firewalld temporarily closes the firewall
systemctl status firewalld checks the firewall status
systemctl disable firewalld permanently closes the firewall
systemctl start firewalld opens the firewall
After closing the firewall, we visit the address again
This means the visit was successful!
3. Deploy the Maven project to tomcat
3.1. Package Maven project
The packaging method in the pom.xml file is changed to war
Add a finalName tag to the build tag, which is the packaged name, just the packaged name of the project.
In the maven column on the right side of the idea, first click clean to clear the target package, and then click package to package
3.2. Upload the packaged war package to Linux
The war package needs to be uploaded to the webapps package of tomcat
The war package does not need to be decompressed, it will be automatically decompressed into a folder for us after starting tomcat
Restart the tomcat server, an SSM (here is my project name) folder will be generated
3.3. Modify the ip in the connection database configuration file
Because the project is uploaded to a virtual machine or cloud server, and the request path of mysql in the project is still localhost or 127.0.0.1, then we need to change it to the current linux ip
Enter the following command
cd your project name /WEB-INF/classes/
Find your database connection configuration file, here is jdbc.properties
Enter the following command to edit the file
vim jdbc.properties
Change the ip to the ip address stored in the database, because mysql is also stored in the server here, so I fill in the ip of the current server.
3.4. Access items
To access the project, you need to add your project name after ip:8080
Project deployment complete!
3.5, direct access to ip access
Some people may think that it is a bit troublesome to enter the 8080 port and project name for this kind of access. Is there any way to directly enter the ip to access the page without having to enter the port and project name? Yes!
Let's solve the problem step by step. First of all, we all know that the default port number of the browser is 80, so as long as the port of tomcat is changed to 80, you can directly access the page
Modify the tomcat configuration file and find the server.xml file in the conf directory under tomcat
vim server.xml
press i to add
Find the connector label, change the 8080 in the port attribute to 80, press the esc key and enter: wq to save
Then in the second step, you don’t need to write the project name, just enter the ip to access. Here you need to delete all the projects that were previously placed in the webapps, and the ROOT directory that comes with tomcat must also be deleted. We need to repackage it as ROOT, and then put to the webapps.
rm -rf ROOT
rm -rf SSM
rm -rf SSM.war
The idea is repackaged as ROOT (note: before the project name can be started casually, but now it must be called ROOT, which is mandatory)
Change the finalName tag to ROOT
Find the target package in the project path, you can see that there is already a ROOT.war package
Upload the ROOT.war package to the webapps package in linux tomcat
Restart tomcat, enter webapps again, and find that the automatic decompression is the ROOT folder, enter the ROOT folder, find the database connection configuration file again and modify the database ip.
Start tomcat again, enter ip directly, and the access is successful! ! !
3.6. Possible problems in the cloud server
When deploying a project on a cloud server, the ip may be modified and the project has been deployed, but the page cannot be accessed. Then the port of the cloud server is not developed. Here I give an example of the Alibaba Cloud server.
First enter the instance, click the blue instance ID
Click Configure Security Group Rules
Click Configure Rules
Manually add the development ports in the red box below, one is 3306, which is the port of the mysql database, and the other is port 80, which is the default port for browser access, and the last three are built-in by the cloud server.
After developing the port, try again in the cloud server, the project runs successfully, and there is no problem with data access, which means success!