Deploying a Java Application on a VPS: My Experience with Complete Setup
Rohan Bankar
J2EE, RESTful Web Services, React.js, Microservices Architecture, Spring Boot, Deployment Pipelines (CI/CD), Maven.
As a developer, deploying a Java application to a VPS (Virtual Private Server) for the first time can seem daunting. However, with a bit of patience and a step-by-step approach, the process becomes quite straightforward. Recently, I had the opportunity to deploy a Java application on an Ubuntu VPS, complete with MySQL integration, SSL certificates, and a custom subdomain. Here’s my experience.
I am Using putty client for access the VPS.
Step 1: Accessing Your VPS with PuTTY or Terminal
To begin, I used PuTTY, a lightweight and easy-to-use SSH client for Windows, to connect to my VPS.
Visit PuTTY's official website and download the installer for Windows.
Complete the installation process.
Open PuTTY.
In the Host Name (or IP address) field, I entered the IP address of my VPS.
Set the Port to 22 (default SSH port).
Click Open to initiate the connection.
Once the terminal opened, I entered the username root and the password provided by my VPS provider.
On the first connection, PuTTY displayed a security alert to confirm the server’s authenticity. I clicked Yes to proceed.
Step 2: Installing Java (JDK) on the VPS
The first requirement for running a Java application is installing the Java Development Kit (JDK). I chose OpenJDK 17 for my setup. Here's how I did it.
I always ensure my VPS is up-to-date before installing any software.
sudo apt update
sudo apt upgrade -y
sudo :- This allows you to run the command as a superuser (administrator), granting you the necessary permissions to manage system files and configurations.
apt: The package management tool for Debian-based systems like Ubuntu. It is used to handle software packages.
update: This command updates the local package index (a database of available packages and their versions) from the repositories listed in the /etc/apt/sources.list file.
upgrade: This command upgrades all installed packages to their latest versions, based on the updated package index.
To install JDK, I ran the following command.
sudo apt install openjdk-17-jdk -y
I checked the installed Java version to confirm the installation.
java -version
The output displayed the installed version, confirming success.
Step 3: Installing MySQL for the Database
Since my application required a database, I installed MySQL on the VPS.
sudo apt install mysql-server -y
I set a strong root password.
Removed anonymous users and test databases.
Disabled remote root login for added security.
I accessed MySQL to configure the database.
sudo mysql -u root -p
Inside the MySQL shell, I created a database and user.
CREATE DATABASE myappdb;
CREATE USER 'myappuser'@'%' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON myappdb.* TO 'myappuser'@'%';
FLUSH PRIVILEGES;
EXIT;
If you are not using Hibernate Create tables as per the requirement.
Step 4: Building the Java JAR for Production
On my local machine, I used Maven to package my application into a JAR file:
I navigated to my project folder and ran.
mvn clean package
This created a file named myapp-1.0-SNAPSHOT.jar in the target folder.
Using PSCP (PuTTY Secure Copy), I uploaded the JAR file to the VPS.
pscp target/myapp-1.0-SNAPSHOT.jar root@your_vps_ip:/opt/myapp.jar
Step 5: Configuring a Subdomain
To make my application accessible through a custom URL like app.mydomain.com, I set up a subdomain.
In my domain registrar’s dashboard, I created.
An A Record pointing to the VPS IPv4 address.
An AAAA Record pointing to the VPS IPv6 address.
Step 6: Installing SSL Certificates
To secure my application, I used Certbot to install SSL certificates.
sudo apt install certbot python3-certbot-nginx nginx -y
I created an Nginx configuration file
sudo nano /etc/nginx/sites-available/myapp
I am not adding my Configurations here because security Purposes.
server {
listen 80;
server_name app.mydomain.com;
location / {
proxy_pass https://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
This are dummy configurations change as per your setup.
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo certbot --nginx -d app.mydomain.com
Step 7: Creating a Systemd Service for the JAR
To ensure my application starts automatically and runs in the background, I created a systemd service.
I opened a new service file
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Java Application
After=network.target
[Service]
User=root
ExecStart=/usr/bin/java -jar /opt/myapp.jar
Restart=always
Environment=SPRING_PROFILES_ACTIVE=prod
[Install]
WantedBy=multi-user.target
I enabled and started the service
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp
Step 8: Testing the Deployment
Finally, I opened https://app.mydomain.com in my browser, and my application was live!
Conclusion
Deploying a Java application on a VPS was an enriching experience. From using PuTTY to install software, managing databases, and setting up SSL certificates to running my application as a service, every step taught me something new. I hope my experience helps you deploy your application successfully. Happy coding! ??
Finance Software Developer | Bookkeeping | Accounting
1 个月Useful content, thanks for sharing ??