ArticleDBDevOpsJavaSpring Boot

Simple Project on Spring Boot, Ngnix, Certbot, Docker, MySQL

In this article, we explain how to start a simple project with Spring Boot, Ngnix, Certbot, and MySQL.

Working with Ngnix

Ngnix installation can be done with the following command.

sudo apt install nginx

There is a simple configuration for the website. As you can see the Ngnix server works as a load balancer between 8080 and 8090 ports of backend services.

upstream samplecluster {
  # The upstream elements lists all
  # the backend servers that take part in 
  # the Nginx load balancer example

  server localhost:8090;
  server localhost:8080;

}



server {
    charset utf-8;
    access_log off;
    server_name stackthrow.com;
    absolute_redirect off;
    root /var/www/stackthrow.com;

    location / {
        proxy_pass http://samplecluster;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /public {
        access_log   off;
        expires      30d;

        alias /var/www/stackthrow.com/public;
    }

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/stackthrow.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/stackthrow.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}



server {
    if ($host = stackthrow.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name stackthrow.com;

    listen 80;
    return 404; # managed by Certbot


}



Restart Ngnix. In this case you can be sure that all configuration is ok.

sudo systemctl restart nginx

Working with Certbot

These software we need to provide SSL connection between the client’s browser and our server.

sudo apt-get install certbot

apt-get install python3-certbot-nginx

Setup of SSL for the website.

sudo certbot --nginx -d stackthrow.com

Tune cron for certbot.

crontab -e

Insert into file:

0 12 * * * /usr/bin/certbot renew --quiet

Working with Docker

Docker-compose file. As you can see we launch two identical services on two different ports. Main aim why we need it only to provide uptime 100% when we increment new version of website.

version: '3'
services:

  app_stackthrow8080:
    restart: always
    build: ./app
    container_name: main-app8080
    working_dir: /app
    volumes:
      - /var/www/stackthrow.com/public/uploads:/uploads
      - /var/www/stackthrow.com/public/sitemaps:/sitemaps
    ports:
      - "127.0.0.1:8080:8080"
    environment:
      DB_HOST: host.docker.internal
      WEB_CONTEXT: "/public/"
      APP_UPLOADS_PATH: "/uploads"
      APP_SITEMAP_PATH: "/sitemaps"
      APP_SHOW_SQL_IN_DEBUG: "false"
      APP_ACTIVE_PROFILE: "production"
      APP_PORT: "8080"
    extra_hosts:
      - host.docker.internal:host-gateway
  app_stackthrow8090:
    restart: always
    build: ./app
    container_name: main-app8090
    working_dir: /app
    volumes:
      - /var/www/stackthrow.com/public/uploads:/uploads
      - /var/www/stackthrow.com/public/sitemaps:/sitemaps
    ports:
      - "127.0.0.1:8090:8090"
    environment:
      DB_HOST: host.docker.internal
      WEB_CONTEXT: "/public/"
      APP_UPLOADS_PATH: "/uploads"
      APP_SITEMAP_PATH: "/sitemaps"
      APP_SHOW_SQL_IN_DEBUG: "false"
      APP_ACTIVE_PROFILE: "production"
      APP_PORT: "8090"
    extra_hosts:
      - host.docker.internal:host-gateway

Docker app file.

FROM openjdk:11-slim
MAINTAINER stackthrow.com
COPY target/main-0.0.1-SNAPSHOT.jar main-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/main-0.0.1-SNAPSHOT.jar"]

Working with MySQL

Installing MySQL and new DB creation.

wget https://dev.mysql.com/get/mysql-apt-config_0.8.20-1_all.deb

sudo dpkg -i mysql-apt-config_0.8.20-1_all.deb

sudo apt update

sudo apt install -y mysql-community-server

mysql -u root -p

CREATE DATABASE stackthrow CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Conclusion

Please do not forget to refer A record of your domain service to the IP of VPS.

Hi, I’m Vlad

Leave a Reply