How to run Spring Boot application on port 80

I cannot run the application on port 80.

I tried on my local computer (using my IDE and on the local server), no luck.

I checked other similar posts and make sure I run jar on the server with root.

This is mistake:

till here all ok ... java.net.SocketException: Permission denied at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.Net.bind(Net.java:425) at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:338) at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:760) at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:472) at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.StandardService.addConnector(StandardService.java:237) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:186) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:149) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at com.andirod.StartApplication.main(StartApplication.java:20) ... ... ... Exception in thread "main" java.lang.IllegalStateException: Tomcat connector in failed state at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:157) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at com.andirod.StartApplication.main(StartApplication.java:20) 
+19
java spring spring-boot
source share
5 answers

On linux, ports below 1024 can only be opened as root, so port 80 is limited by default.

if you want to publish your application on port 80, you need to redirect the request from port 80 to the port where you are going to run springapp port (for example, 8080)

Solution 1: HTTP Proxy

You can use the Apache2 server, which by default is allowed to work through port 80 and can redirect requests for you to Tomcat.

Configuration Example for Debian

 sudo apt-get install apache2 a2enmod proxy a2enmod proxy_http cd /etc/apache2/sites-enabled sudo nano 000-default.conf 

Edit file:

 <VIRTUALHOST *:80> ProxyPreserveHost On # ... ProxyPass / http://localhost:8080/ </VIRTUALHOST> 

Save file: Ctrl + O , ENTER , Ctrl + X

Note. To learn more about virtual host configurations, you can read the detailed Apache tutorial on this topic by clicking here .

Restart Apache2 to apply the changes:

 sudo service apache2 restart 

or

 sudo systemctl restart apache2 

Solution 2: Port Forwarding

Use iptables for redirects

 iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 

if you need to use localhost also add this

 iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080 
+43
source share

Use sudo on Linux.

I ran the Spring Boot application on Ubuntu, and java -jar app.jar --server.port=80 gave me the same error. Since ports below 1024 can only be opened with root access, use "sudo": sudo java -jar app.jar --server.port=80 .

This deployment method is only offered for local tests due to security issues. See comments for details.

+5
source share

Here are the steps I followed on Centos.

Step 1 (optional): port setup

By default, the spring boot application runs on port 8080, if you want to change this, you can change it in the src / main / resources / application.properties file

 server.port = 8082 // any port above than 1024 

Step 2: Install apache if it is not already installed

On Centos 7

 sudo yum install httpd 

Step 3: Edit Your Virtual Host

 /etc/httpd/conf.d/vhost.conf 

Your config should look like this

 <VirtualHost *:80> ServerName yourdomin.com #DocumentRoot /var/www/html ProxyPreserveHost On ProxyPass / http://localhost:8082/ ProxyPassReverse / http://localhost:8082/ </VirtualHost> 

And restart apache

 sudo service httpd restart 
+1
source share

If you are using macO, you can now work on port 80 without any changes to macO Mojave Version 10.14 .

0
source share

Add -Djava.net.preferIPv4Stack=true to the virtual machine settings

JavaMail API for iMail - java.net.SocketException: Permission denied: connect

-2
source share

All Articles