How can I run a Spring Boot application on port 80

前端 未结 6 1685
广开言路
广开言路 2020-12-02 10:43

I can\'t start an application on port 80.

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

I have checked other similar pos

相关标签:
6条回答
  • 2020-12-02 11:01

    In the case of using macOs, it is now possible to run on port 80 without any changes on macOs Mojave Version 10.14.

    0 讨论(0)
  • 2020-12-02 11:02

    Add -Djava.net.preferIPv4Stack=true to the VM options

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

    0 讨论(0)
  • 2020-12-02 11:03

    If you're running spring boot with docker.

    Dockerfile:

    FROM adoptopenjdk/openjdk13 AS server
    ADD /target/AppServer-1.0.jar AppServer-1.0.jar
    ENTRYPOINT ["java", "-jar" , "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9000", "-Dcom.sun.management.jmxremote", "-Dcom.sun.management.jmxremote.authenticate=false", "-Dcom.sun.management.jmxremote.ssl=false", "-Dcom.sun.management.jmxremote.local.only=false", "-Dcom.sun.management.jmxremote.port=1099", "-Dcom.sun.management.jmxremote.rmi.port=1099", "-Djava.rmi.server.hostname=127.0.0.1", "-Dlog4j.configurationFile=log4j2-docker.xml", "AppServer-1.0.jar", "--server.port=80"]
    

    (*) Scroll right until the end and see --server.port=80 after the jar name.

    Build the image and run with:

    docker run -it -p 8080:80 --cap-drop all --cap-add net_bind_service <image-name>:<tag>
    

    (!) Please notice that I dropped all capabilities for this process / container and added only the relevant one - net_bind_service which bind a socket to privileged ports (port numbers less than 1024).

    0 讨论(0)
  • 2020-12-02 11:04

    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
    
    0 讨论(0)
  • 2020-12-02 11:06

    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
    
    0 讨论(0)
  • 2020-12-02 11:11

    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.

    0 讨论(0)
提交回复
热议问题