supervisor gunicorn virtualenv flask

  1. Create a Virtual Environment:
  2. Run python3 -m venv venv to create a virtual environment named "venv."

  3. Activate the Virtual Environment:

  4. On Linux/macOS: source venv/bin/activate
  5. On Windows: venv\Scripts\activate

  6. Install Flask:

  7. Execute pip install flask to install Flask within the virtual environment.

  8. Install Gunicorn:

  9. Run pip install gunicorn to install the Gunicorn WSGI server.

  10. Create a Flask App:

  11. Develop your Flask application. Ensure you have a file (e.g., containing the Flask app instance.

  12. Test Flask App Locally:

  13. Run the Flask app locally using flask run to check if it works as expected.

  14. Install Supervisor:

  15. Install Supervisor using sudo apt-get install supervisor (for Debian/Ubuntu). For other systems, use the relevant package manager.

  16. Configure Supervisor:

  17. Create a Supervisor configuration file, e.g., myapp.conf, specifying the Gunicorn command to run your Flask app.

ini [program:myapp] command=/path/to/venv/bin/gunicorn -w 4 -b app:app directory=/path/to/your/app user=your_user

  1. Update Supervisor:
  2. Update Supervisor to recognize the new configuration: sudo supervisorctl reread and sudo supervisorctl update.

  3. Start Supervisor Service:

    • Start the Supervisor service: sudo service supervisor start.
  4. Check Supervisor Status:

    • Confirm the status of your Supervisor process: sudo supervisorctl status.
  5. Access Flask App via Gunicorn and Supervisor:

    • Visit your Flask app via Gunicorn and Supervisor at the specified address (e.g., http://localhost:8000).
  6. Deactivate Virtual Environment:

    • When done, deactivate the virtual environment using deactivate.