How can I run a Spring Boot application on port 80
Here are steps I followed on centos.
Step 1 (Optional): Set port
By default spring boot app run on port 8080, if you wish to change this you can change on your src/main/resources/application.properties file
server.port = 8082 // any port above than 1024
Step 2: Install apache if not installed already
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
Use sudo
on linux.
I was running a 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 by root access, so use"sudo": sudo java -jar app.jar --server.port=80
.
This way of deployment is only suggested for local tests due to security concerns. See comments for details.
On linux ports below 1024 can be opened only by root, so the port 80 is restricted by default
if you want to publish your app on 80 port you need to redirect request from port 80 to the port you gonna run your springapp (e.g 8080) port
Solution 1: HTTP Proxy server
You can use Apache2 server which is allowed by default to work on port 80 and can forward requests for you to Tomcat
Example configuration 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 check out the detailed Apache manual on the subject by clicking here.
Restart Apache2 to apply 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-ports 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