1. Deployment preparation
Before you begin, make sure you meet the following conditions:
- A server running Ubuntu (such as Ubuntu 20.04 or 22.04) with SSH access.
- Executable JAR files for Java applications to ensure they can run properly locally.
2. Install the Java environment
First, you need to install the Java Runtime Environment (JRE) or Java Development Kit (JDK) on Ubuntu to be able to run Java applications.
1. Install JDK
Ubuntu provides OpenJDK (an open source implementation of Java). You can choose to install JDK 8 (the mainstream version in the industry) or OpenJDK 17 (the next mainstream version in the industry).
sudo apt install openjdk-17-jdk -y
2. Verify Java installation
After the installation is complete, you can use the following command to check whether Java is installed successfully:
java -version
You should see an output similar to the following, indicating that the Java installation was successful:
openjdk version "17.0.2" 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-60) OpenJDK 64-Bit Server VM (build 11.0.11+9-60, mixed mode)
3. Install MySQL (if required)
1. Install MySQL Server
sudo apt install mysql-server -y
2. Start and set MySQL boot
sudo systemctl start mysql sudo systemctl enable mysql
3. Verify MySQL installation
Log in to MySQL to confirm that the installation is successful.
sudo mysql -u root -p
MySQL default password is root, just log in directly.
4. Modify the initial password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
5. Create libraries and tables
Here we use SQL statements to build the database needed by the project.
4. Deploy Java applications
You can deploy Java applications by building an executable JAR file.
Suppose you already have a built JAR file that transfers your JAR file to the target directory on your Ubuntu server.
Go to the application directory on the server and run the JAR file.
cd /var/www/java-app java -jar
At this time your Java application is running on port 8080.
5. Configure Nginx as a reverse proxy (optional)
In order to access your Java application through your browser, you need to configure Nginx as the reverse proxy to forward client requests to the Java application. Here are the steps to configure the reverse proxy.
1. Install Nginx
If Nginx is not installed yet, you can use the following command to install:
sudo apt install nginx -y
2. Configure Nginx
Open Nginx's configuration file for editing:
sudo nano /etc/nginx/sites-available/default
Reviseserver
Configuration in the block forwards the request to the port of the Java application (such as 8080):
server { listen 80; server_name ; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Save and exit the configuration file.
3. Test and restart Nginx
Test whether the Nginx configuration is correct:
sudo nginx -t
If the configuration is correct, restart Nginx:
sudo systemctl restart nginx
At this point, Nginx has been successfully configured to forward all requests to the Java application.
6. Set Java applications as system service (optional)
To ensure that the Java application starts automatically when the system starts, it can be passedsystemctl
Management, we need to configure the application as a systemd service.
1. Create the systemd service file
Create a new systemd service file to manage Java applications:
sudo nano /etc/systemd/system/
Add the following to the file:
[Unit] Description=Java Web Application After= [Service] User=your-user ExecStart=/usr/bin/java -jar /var/www/java-app/ SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure WorkingDirectory=/var/www/java-app [Install] WantedBy=
-
ExecStart
: Specify the command to start the application. -
WorkingDirectory
: Specify the working directory of the application. -
Restart
: Ensure that the application will automatically restart when it crashes.
2. Start and enable the service
Reload the systemd configuration to make the new service take effect:
sudo systemctl daemon-reload
Start Java application service:
sudo systemctl start
Enable the service to start automatically when the system starts:
sudo systemctl enable
3. Check service status
You can use the following command to view the status of the service:
sudo systemctl status
Make sure that your Java application is running and has no errors.
7. Log Management
In order to effectively monitor the running status of Java applications, it is recommended to set up logging.
1. Configure log files
You can use Java's logging framework (such asLogback
orLog4j
) to configure application logs. Make sure the logs are output to the file, not just to the console.
existor
Specify the path to the log file in the configuration.
For example, useLogback
Configuration file:
<configuration> <appender name="FILE" class=""> <file>/var/log/</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="FILE"/> </root> </configuration>
2. View application log
You can usetail
orjournalctl
Let’s view the log file:
sudo tail -f /var/log/
If you start the application through systemd, you can also usejournalctl
Command to view the application's log:
sudo journalctl -u -f
8. Test deployment
- Access your server domain name or IP address in your browser to ensure that your Java application is loaded correctly.
- Use tools such as curl or Postman to test the API interface to ensure that the backend services are working properly.
9. Summary
Through the above steps, you can successfully deploy Java applications on Ubuntu and configure reverse proxy using Nginx to ensure that the application can provide services efficiently and stably. At the same time, with systemd, you can also conveniently manage application startup, stop and logging to ensure high service availability.
The above is the detailed operation steps for deploying SpringBoot applications on Ubuntu. For more information about deploying SpringBoot applications on Ubuntu, please follow my other related articles!