IT, Programming, & Web Development › Forums › Web Development › AWS › Ensuring continuous availability of your Flask application with Gunicorn and Nginx
- This topic is empty.
-
AuthorPosts
-
July 28, 2024 at 5:18 am #3120
Possible reasons why a connected Flask application on Ubuntu stops working after few hours without any change in code
byu/DigitalSplendid ingithubSource: Generated with the help of ChatGPT
Deploying a Flask application with Gunicorn and Nginx on an Ubuntu server, such as AWS Lightsail, provides a robust solution for hosting your web applications. However, ensuring continuous availability of your application requires proper configuration to handle any potential failures. This article will guide you through the process of setting up your Flask application with Gunicorn and Nginx, and configuring it to automatically restart if it fails, ensuring consistent uptime for your users.
Prerequisites
Before we begin, ensure you have the following:
- A Flask application.
- An Ubuntu server (AWS Lightsail or similar) with SSH access.
- Gunicorn and Nginx installed on the server.
Step 1: Set Up Your Flask Application with Gunicorn
First, let’s configure Gunicorn to serve your Flask application. Create or edit your Gunicorn service file.
- Create Gunicorn Service File:
sudo nano /etc/systemd/system/gunicorn.service
- Add the Following Configuration:
[Unit] Description=gunicorn daemon for my Flask app After=network.target [Service] User=ubuntu Group=www-data WorkingDirectory=/home/ubuntu/SplendidDigital ExecStart=/home/ubuntu/SplendidDigital/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/SplendidDigital/gunicorn.sock app:app Restart=always [Install] WantedBy=multi-user.target
- Reload systemd to Apply the Changes:
sudo systemctl daemon-reload
- Start and Enable the Gunicorn Service:
sudo systemctl start gunicorn sudo systemctl enable gunicorn
- Check Gunicorn Service Status:
sudo systemctl status gunicorn
You should see the Gunicorn service active and running.
Step 2: Configure Nginx as a Reverse Proxy
Next, configure Nginx to act as a reverse proxy to forward requests to Gunicorn.
- Create Nginx Configuration File for Your Site:
sudo nano /etc/nginx/sites-available/aiannum.uk
- Add the Following Configuration:
server { listen 80; server_name aiannum.uk; # Replace with your domain location / { proxy_pass http://unix:/home/ubuntu/SplendidDigital/gunicorn.sock; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # Optional: Serve static files location /static/ { alias /home/ubuntu/SplendidDigital/static/; # Adjust the path if needed } # Optional: Handle media files location /media/ { alias /home/ubuntu/SplendidDigital/media/; # Adjust the path if needed } }
- Enable the Nginx Configuration:
sudo ln -s /etc/nginx/sites-available/aiannum.uk /etc/nginx/sites-enabled
- Test Nginx Configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
Step 3: Ensure Continuous Operation with Auto-Restart
To ensure that Gunicorn automatically restarts if it fails, we’ve already added the
Restart=always
directive in the Gunicorn service file. This configuration will handle unexpected failures by restarting the Gunicorn service.Step 4: Additional Tips for Security and Stability
- Regular Updates:
Ensure your system packages are up to date by running:
sudo apt update && sudo apt upgrade
- Monitor Logs:
Regularly monitor your application logs to catch any potential issues early. You can check Gunicorn logs with:
sudo journalctl -u gunicorn
- Firewall Configuration:
Ensure that your firewall allows traffic on HTTP (port 80) and HTTPS (port 443) if you plan to enable SSL.
- SSL/TLS Configuration:
For securing your application, consider setting up SSL/TLS with Let’s Encrypt. This can be achieved using Certbot to obtain and install SSL certificates.
Conclusion
By following the steps outlined in this article, you can ensure that your Flask application remains continuously available and robust against unexpected failures. The combination of Gunicorn and Nginx provides a powerful and efficient deployment stack for your Flask applications, while the auto-restart configuration ensures high availability and reliability.
Remember to regularly monitor your application and server performance, keep your dependencies updated, and follow best practices for security to maintain a smooth and secure operation.
-
AuthorPosts
- You must be logged in to reply to this topic.