Table of Contents
1. Purchase a server, domain name and register
2. Build the environment
3. Project deployment
Home Operation and Maintenance Linux Operation and Maintenance How to deploy JavaWeb to Linux server

How to deploy JavaWeb to Linux server

May 13, 2023 am 08:31 AM
linux javaweb

1. Purchase a server, domain name and register

Now the registration is much simplified. You can download the APP directly and scan it to successfully register.

2. Build the environment

Install Nginx, MySQL, JDK, Tomcat environment on Centos7.3 cloud server

Centos7.6 installation mysql5.5 tutorial

Cenos7.3 install mysql8.0

Centos8.0 compile and install the latest stable version of nginx

Centos7.3 Uninstall Nginx (completely uninstall) and reinstall Nginx (RPM source yum installation)

If you still have questions, please direct Baidu

3. Project deployment

Package the java web project to be uploaded into a war package and upload it to the webapps in the Tomcat directory. When you upload the project's war package before starting Tomcat, it will not be decompressed into a folder. Whenever the projects under Tomcat's webapp are changed (added or deleted), Tomcat must be restarted.

Note: The following commands must be entered into the bin directory of Tomcat to be executed. If you configure the environment variables, you can execute it in any path.

./startup.sh		// 启动Tomcat./shutdown.sh		// 关闭Tomcat
Copy after login

The war package uploaded to webapp will be parsed into a folder with the same name as the project name. When you open it, there will be a hierarchical structure similar to the project. If your project war package uploaded to webapp has not been Unzip it into a folder with the same name as the project name, that is, you have not restarted Tomcat. First execute the above shutdown command and then execute the startup command.

Enter the decompressed project, you can find that jsp files can be seen in the root directory of the project, and java files such as javabean and lib directories, web.xml or other configuration files exist in the root directory. Under the WEB-INF folder, because the java code will be compiled into class files and stored in the classes folder.

One thing to pay special attention to here is that if the java web project needs to connect to the database on the Linux server, be sure to change the uploaded project in advance to the user name and password of the mysql database on the Linux server and then package it into a war package. Or directly enter the project path change under the Tomcat compiled project under Linux, save the file after completion, then close Tomcat and then start Tomcat.

Project database file import problem

The access address of the project database under Linux remains unchanged, such as:

url = "jdbc:mysql://localhost:3306/数据库名"; 
或
url = "jdbc:mysql://127.0.0.1:3306/数据库名"; 
或 
url = "jdbc:mysql://服务器IP地址:3306/数据库名";
Copy after login

Because the project is uploaded to the server Finally, it is explained that localhost or 127.0.0.1 also represents the local path of the server. Of course, you can also choose to use the public IP of the service, but it is generally not recommended because localhost or 127.0.0.1 at this time represents the server IP at this time.

Navicat, SQLyog or other database visualization tools under local Windows connect to msyql on the Linux server to facilitate direct import of database files on the project.

Import the local mysql database into the mysql database on the Linux server

First, export the database file under Windows and upload it to the server, and then create a server with the same name as the local Windows Database file on

create database 数据库名
Copy after login

Switch to the created database

use 数据库名
Copy after login

Import data

source 上传的数据库文件路径.sql
Copy after login

If you don’t want to be so troublesome, you can also directly use Navicat to connect to mysql on the server. , just import it directly on Navicat.

Nginx reverse proxy accesses the project through the domain name

Resolve the domain name to be used by the project, usually using the second-level domain name for access.

In fact, you can directly use the IP address on the server to access the project. You don’t have to bother using the domain name to access it. But this method not only requires remembering the domain name but also the port number. It is indeed a bit troublesome to deploy too many projects. For example:

IP:8080IP:8081IP:8082或
域名;8080
域名:8081域名:8082
Copy after login

With reverse proxy, you don’t need to add the port every time you access the project. No.

Open the root path of Nginx, switch to conf/, open the nginx.conf file, and add a server module under the http module

The proxy_pass proxy here is the project under the Tomcat server, Tomcat The default port number is 8080, here it is changed to 8010

server {		#监听的端口号
		listen 80;		#一级域名或二级域名
		server_name	sh.yunxdr.top;		#默认文档
		index index.jsp index.html index.htm;		location / {			proxy_pass http://127.0.0.1:8010/项目名/;
		}
	}
Copy after login

Note:

Special attention here is the IP address of proxy_pass, which is represented by 127.0.0.1 or localhost When selecting the IP of the server, remember not to use the public IP of the server, because after passing the domain name resolution, using the IP as a proxy to access the secondary domain name cannot be accessed.

proxy_pass can be used here.

http://localhost:8010/Tomcat解析的项目名或
http://127.0.0.1:8010/Tomcat解析的项目名
Copy after login

In order to deploy a Tomcat project in the future, you can copy multiple Tomcats on the Linux server, and then modify the Tomcat default port number so that different project names can be accessed using the second-level domain name.

But you can also deploy multiple projects under one Tomcat. At this time, every time you deploy a new project, the previous projects will be executed again with the startup or shutdown of Tomcat, which consumes a lot of service memory. and CPU will make the server become more stuck. I used this method before deploying projects.

Sometimes after deployment, the project cannot be accessed. This may be because nginx does not recognize the default file. Add the default access path of the project after the agent's project name, such as:

proxy_pass http://127.0.0.1:8010/项目名/index.jsp;
Copy after login

After completing the Nginx configuration, save and exit, and execute the reload command to make the newly configured file take effect.

./nginx -s reload
Copy after login

The execution at this time must be switched to the bin directory of nginx to execute successfully. Unless the environment variables are configured, it can be executed in any path.

At this point the deployment is complete and you can access the project under Tomcat through the domain name. Note that the second-level domain name must be resolved before you can access the project

The above is the detailed content of How to deploy JavaWeb to Linux server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles