Serve Flask application with GUnicorn on Localhost

Viewed 2490

I am a noob in this area so please bear with my dumb questions.

I have a Flask application and I want to run that with GUnicorn on my localhost. I looked on Google but almost every tutorial requires a domain name and there isn't much documentation for running it on a mac.

  1. Please tell how can I run the app with GUnicorn on my mac?
  2. I want to use https for the secure communication so how can I change the configuration of Gunicorn to do so?

Any help will be great.

Cheers

1 Answers

Since you want to run it on your localhost you don't need any domain name. You just have to run Python file where you have configured your flask app on your terminal and then you will get a port number where application will be running on your local machine.

Steps to run app

  1. Change your current directory of your terminal to directory where your project is present, using

    cd <your directory address>
    
  2. Now run your app using

    python ./<file_name>.Py
    

Note: If you have both Python 2 and Python 3 installed (Your machine comes with a version of Python 2 but you can install Python 3 as well), you should run

python3 <file_name>.py

Even if you want to deploy on web on a server without your domain name for free you can do it using heroku or any other service as you like.

Running a Flask application on gunicorn is quite simple:

gunicorn <file_name>:app

Gunicorn provides many command-line options – see gunicorn -h. For example, to run a Flask application with 4 worker processes (-w 4) binding to localhost on port 4000 (-b 127.0.0.1:4000):

gunicorn -w 4 -b 127.0.0.1:4000 <file_name>:app
Related