How to deploy JavaWeb to Linux server

WBOY
Release: 2023-05-13 08:31:10
forward
1729 people have browsed it

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!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template