Deploying a Java Spring Boot web application to Ubuntu 24.04 with Nginx and MySQL Step by Step

mahabub.devs3
Mahabubur Rahman
Published on Nov, 10 2024 2 min read 0 comments
image

Deploying a Java Spring Boot web application to Ubuntu 24.04 with Nginx and MySQL involves several steps. Here’s a step-by-step guide to help you through the process:

Step 1: Install Java Development Kit (JDK)

Update Package List: Open a terminal and run:

sudo apt update

Install OpenJDK: Install the OpenJDK package:

sudo apt install openjdk-17-jdk

Verify Installation: Check the Java version:

java -version

Step 2: Install MySQL

Update Package List: Run:

sudo apt update

Install MySQL: Install MySQL server:

sudo apt install mysql-server

Secure MySQL Installation: Run the security script:

sudo mysql_secure_installation

Verify Installation: Check MySQL status:

sudo systemctl status mysql

Step 3: Install Maven

Download Maven: Download the Maven binary archive:

wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz

Extract Maven: Extract the archive:

tar -xvf apache-maven-3.9.6-bin.tar.gz
mv apache-maven-3.9.6 /opt/

Set Environment Variables: Add Maven to the PATH:

export M2_HOME=/opt/apache-maven-3.9.6
export PATH=$PATH:$M2_HOME/bin

Verify Installation: Check Maven version:

mvn -version

Step 4: Install Nginx

Update Package List: Run:

sudo apt update

Install Nginx: Install Nginx:

sudo apt install nginx

Verify Installation: Check Nginx status:

sudo systemctl status nginx

Step 5: Deploy Spring Boot Application

Copy Application: Copy your Spring Boot application JAR file to a directory on your server (e.g., /var/lib/spring-boot-app).

Create a Service File: Create a service file to manage the Spring Boot application:

sudo nano /etc/systemd/system/spring-boot-app.service

Add the following content:

[Unit]
Description=Spring Boot Application
After=network.target

[Service]
User=your-username
WorkingDirectory=/var/lib/spring-boot-app
ExecStart=/usr/bin/java -jar app.jar

[Install]
WantedBy=multi-user.target

Start and Enable Service: Start and enable the service:

sudo systemctl start spring-boot-app
sudo systemctl enable spring-boot-app

Step 6: Configure Nginx as a Reverse Proxy

Create Nginx Configuration File: Create a configuration file for Nginx:

sudo nano /etc/nginx/sites-available/spring-boot-app

Add the following content:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost: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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable Configuration File: Create a symbolic link to enable the configuration:

sudo ln -s /etc/nginx/sites-available/spring-boot-app /etc/nginx/sites-enabled/

Restart Nginx: Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 7: Test Deployment

Access Application: Open your web browser and navigate to http://your-domain.com to verify that your Spring Boot application is accessible.

That’s it! Your Spring Boot application should now be deployed on Ubuntu 24.04 with Nginx and MySQL. If you encounter any issues or need further assistance, feel free to ask!

0 Comments