Deploying Jar на Linux-сервере с Nginx

2021.11.17  |  15

Hosting java application as jar file and running on server behind nginx web server

We create a service:
nano /etc/systemd/system/demo.service

[Unit] 
Description=Demo Java App
After=syslog.target 
After=network.target[Service] 
User=username 
Type=simple  

[Service] 
ExecStart=/usr/bin/java -jar /var/www/sitename.com/demo.jar 
Restart=always 
StandardOutput=syslog 
StandardError=syslog 
SyslogIdentifier=demo 

[Install] 
WantedBy=multi-user.target
Create a proxy web config for nginx:
nano /etc/nginx/sites-available/sitename.com.conf

server {
   listen 80;
   server_name sitename.com;
   location / {
           proxy_pass http://localhost:8080/;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header X-Forwarded-Port $server_port;
   }
}
We turn on the created config:
ln -s /etc/nginx/sites-available/sitename.com.conf /etc/nginx/sites-enable/
Restart the web server to apply the config:
systemctl restart nginx
We start the jar service:
systemctl start demo