supervisor gunicorn virtualenv flask
- Create a Virtual Environment:
Run
python3 -m venv venv
to create a virtual environment named "venv."Activate the Virtual Environment:
- On Linux/macOS:
source venv/bin/activate
On Windows:
venv\Scripts\activate
Install Flask:
Execute
pip install flask
to install Flask within the virtual environment.Install Gunicorn:
Run
pip install gunicorn
to install the Gunicorn WSGI server.Create a Flask App:
Develop your Flask application. Ensure you have a file (e.g.,
app.py
) containing the Flask app instance.Test Flask App Locally:
Run the Flask app locally using
flask run
to check if it works as expected.Install Supervisor:
Install Supervisor using
sudo apt-get install supervisor
(for Debian/Ubuntu). For other systems, use the relevant package manager.Configure Supervisor:
- 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 0.0.0.0:8000 app:app
directory=/path/to/your/app
user=your_user
- Update Supervisor:
Update Supervisor to recognize the new configuration:
sudo supervisorctl reread
andsudo supervisorctl update
.Start Supervisor Service:
- Start the Supervisor service:
sudo service supervisor start
.
- Start the Supervisor service:
Check Supervisor Status:
- Confirm the status of your Supervisor process:
sudo supervisorctl status
.
- Confirm the status of your Supervisor process:
Access Flask App via Gunicorn and Supervisor:
- Visit your Flask app via Gunicorn and Supervisor at the specified address (e.g.,
http://localhost:8000
).
- Visit your Flask app via Gunicorn and Supervisor at the specified address (e.g.,
Deactivate Virtual Environment:
- When done, deactivate the virtual environment using
deactivate
.
- When done, deactivate the virtual environment using