CS50x threads to aide as a supplementary resource Forums Web Development AWS Ensuring continuous availability of your Flask application with Gunicorn and Nginx

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #3120

    Possible reasons why a connected Flask application on Ubuntu stops working after few hours without any change in code
    byu/DigitalSplendid ingithub

    Source: 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.

    1. Create Gunicorn Service File:
    sudo nano /etc/systemd/system/gunicorn.service
    
    1. 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
    
    1. Reload systemd to Apply the Changes:
    sudo systemctl daemon-reload
    
    1. Start and Enable the Gunicorn Service:
    sudo systemctl start gunicorn
    sudo systemctl enable gunicorn
    
    1. 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.

    1. Create Nginx Configuration File for Your Site:
    sudo nano /etc/nginx/sites-available/aiannum.uk
    
    1. 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
    }
    }
    
    1. Enable the Nginx Configuration:
    sudo ln -s /etc/nginx/sites-available/aiannum.uk /etc/nginx/sites-enabled
    
    1. Test Nginx Configuration:
    sudo nginx -t
    
    1. 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

    1. Regular Updates:

    Ensure your system packages are up to date by running:

    sudo apt update && sudo apt upgrade
    
    1. Monitor Logs:

    Regularly monitor your application logs to catch any potential issues early. You can check Gunicorn logs with:

    sudo journalctl -u gunicorn
    
    1. Firewall Configuration:

    Ensure that your firewall allows traffic on HTTP (port 80) and HTTPS (port 443) if you plan to enable SSL.

    1. 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.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Scroll to Top