Get the most out of your Centmin Mod LEMP stack
Become a Member

use python flask on a different port

Discussion in 'Other Web Apps usage' started by hitman, Mar 15, 2019.

Tags:
  1. hitman

    hitman Member

    126
    11
    18
    Jul 18, 2014
    Ratings:
    +15
    Local Time:
    10:23 PM
    Hello everyone :D


    I am trying to run a flask app on port 8080. What I have done is the following
    1. created a nginx vhost domain, through option 2
    2. followed this guide to set up everything needed for the application
    3. Opened port 8080 on csf
    I added a listen directive to port 8080 on the extra server block I created in the /usr/local/nginx/conf/conf.d/mydomainname.com.conf as you can see in the following snippet
    Code:
    server {
        listen 8080;
        server_name mydomainname.com;
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/home/user/myproject/myproject.sock;
        }
    }

    so the entire file becomes
    Code:
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    #server {
    #            listen   80;
    #            server_name mydomainname.com;
    #            return 301 $scheme://www.mydomainname.com$request_uri;
    #       }
    
    
    server {
        listen 8080;
        server_name mydomainname.com;
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/home/user/myproject/myproject.sock;
        }
    }
    
    
    
    server {
     
      server_name mydomainname.com;
    
    
      #add_header X-Frame-Options SAMEORIGIN;
      add_header X-Xss-Protection "1; mode=block" always;
      add_header X-Content-Type-Options "nosniff" always;
      #add_header Referrer-Policy "strict-origin-when-cross-origin";
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/mydomainname.com/log/access.log combined buffer=256k flush=5m;
      error_log /home/nginx/domains/mydomainname.com/log/error.log;
    
      include /usr/local/nginx/conf/autoprotect/mydomainname.com/autoprotect-mydomainname.com.conf;
      root /home/nginx/domains/mydomainname.com/public;
      include /usr/local/nginx/conf/503include-main.conf;
    
      location / {
      include /usr/local/nginx/conf/503include-only.conf;
      include /usr/local/nginx/conf/block.conf;
    
      # Enables directory listings when index file not found
      autoindex  on;
      }
    
     
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
     
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    

    the result is I get "502 Bad Gateway", any ideas what I am doing wrong?
     
  2. hitman

    hitman Member

    126
    11
    18
    Jul 18, 2014
    Ratings:
    +15
    Local Time:
    10:23 PM
    any ideas at all?
     
  3. eva2000

    eva2000 Administrator Staff Member

    54,873
    12,239
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,810
    Local Time:
    6:23 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    you accessing app from mydomainname.com:8080 ? as you only configured that to reverse proxy to the flask python app. If getting 502, that means flask python app isn't running so nginx gives 502 error as it can't connect to python app.
     
  4. hitman

    hitman Member

    126
    11
    18
    Jul 18, 2014
    Ratings:
    +15
    Local Time:
    10:23 PM
    No i can not access it.
    Please note that I have set up a Systemd Unit File as described here for the process to autostart.
    But it does not start even when I start it manually. The error I receive is the following

    Code:
    python mainform.py
     * Serving Flask app "mainform" (lazy loading)
     * Environment: production
       WARNING: Do not use the development server in a production environment.
       Use a production WSGI server instead.
     * Debug mode: off
    Traceback (most recent call last):
      File "mainform.py", line 43, in <module>
        app.run(host='0.0.0.0')
      File "/usr/lib64/python2.7/site-packages/flask/app.py", line 943, in run
        run_simple(host, port, self, **options)
      File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 814, in run_simple
        inner()
      File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 774, in inner
        fd=fd)
      File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 660, in make_server
        passthrough_errors, ssl_context, fd=fd)
      File "/usr/lib/python2.7/site-packages/werkzeug/serving.py", line 577, in __init__
        self.address_family), handler)
      File "/usr/lib64/python2.7/SocketServer.py", line 419, in __init__
        self.server_bind()
      File "/usr/lib64/python2.7/BaseHTTPServer.py", line 108, in server_bind
        SocketServer.TCPServer.server_bind(self)
      File "/usr/lib64/python2.7/SocketServer.py", line 430, in server_bind
        self.socket.bind(self.server_address)
      File "/usr/lib64/python2.7/socket.py", line 224, in meth
        return getattr(self._sock,name)(*args)
    socket.error: [Errno 98] Address already in use
    
     
  5. eva2000

    eva2000 Administrator Staff Member

    54,873
    12,239
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,810
    Local Time:
    6:23 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    port 8080 already in use ? or your flask app has issues/bugs
     
  6. wmtech

    wmtech Active Member

    175
    44
    28
    Jul 22, 2017
    Ratings:
    +132
    Local Time:
    9:23 PM
    It seems you have configured your Nginx to run at port 8080. Then it is not possible to run flask also at 8080.

    You should configure flask to run at 8080 at localhost and Nginx to connect to localhost:8080 for proxying.

    You need someone with sysadmin knowledge to setup your server.
     
  7. hitman

    hitman Member

    126
    11
    18
    Jul 18, 2014
    Ratings:
    +15
    Local Time:
    10:23 PM
    indeed i need someone with sysadmin knowledge but I am trying to do it myself.

    thank you for your tip, I solved the problem using the proxy_pass directive
     
    Last edited: Mar 19, 2019