In this comprehensive tutorial, we will guide you through the process of deploying a Flask application with uWSGI and Nginx on Ubuntu 20.04. Flask is a micro web framework written in Python and is known for its simplicity and ease of use. By combining Flask with uWSGI and Nginx, you can create a powerful and scalable web application.
Prerequisites
Before we begin, make sure you have the following:
- A server running Ubuntu 20.04.
- A valid domain name pointed to your server’s IP address.
- Access to the root password of your server.
Step 1: Updating System Packages
The first step is to update your system packages to the latest version. Open your terminal and run the following command:
sudo apt-get update -y
This command will update all the packages on your Ubuntu server.
Step 2: Installing Required Dependencies
Now, let’s install the necessary dependencies to deploy your Flask application. Run the following command in your terminal:
sudo apt-get install nginx python3-pip python3-dev python3-venv build-essential libssl-dev libffi-dev python3-setuptools -y
This command will install Nginx, Python 3, and other required packages.
Step 3: Creating a Virtual Environment
Next, we’ll create a virtual environment for your Flask application. A virtual environment is a isolated Python environment that allows you to manage specific dependencies for your project. In your terminal, run the following commands:
sudo mkdir /var/www/html/myapp cd /var/www/html/myapp sudo python3 -m venv myappenv source myappenv/bin/activate
These commands will create a directory for your Flask application and set up a virtual environment named “myappenv.”
Step 4: Setting Up a Flask Application
Now, let’s set up a basic Flask application within your virtual environment. Run the following commands in your terminal:
pip install uwsgi flask nano /var/www/html/myapp/myapp.py
These commands will install Flask and uWSGI, and then open a text editor to create a new file named “myapp.py.” In the text editor, add the following code:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "<h1 style='color:blue'>Hi, This is My Flask Application</h1>" if __name__ == "__main__": app.run(host='0.0.0.0')
Save and close the file. This code creates a simple Flask application that displays a blue heading when accessed.
Step 5: Running the Flask Application
Let’s run the Flask application to test if it’s working correctly. In your terminal, run the following command:
python /var/www/html/myapp/myapp.py
You should see the following output:
* Serving Flask app "myapp" (lazy loading) * Environment: production Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Now, open your web browser and access your Flask application using the URL http://your-server-ip:5000
. You should see the blue heading displayed on the page.
To stop the Flask application, press CTRL + C
in your terminal.
Step 6: Configuring uWSGI
Now that your Flask application is running, let’s configure uWSGI to serve the application. In your terminal, run the following command:
nano /var/www/html/myapp/wsgi.py
This command will open a text editor to create a new file named “wsgi.py.” In the text editor, add the following code:
from myapp import app if __name__ == "__main__": app.run()
Save and close the file. This code imports your Flask instance from the “myapp” module and runs the application.
To test if uWSGI can serve the application, run the following command in your terminal:
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
Now, access your application again using the URL http://your-server-ip:5000
. You should see the same blue heading displayed on the page.
To stop the application, press CTRL + C
in your terminal.
Step 7: Configuring Nginx as a Reverse Proxy
Next, we’ll configure Nginx as a reverse proxy to serve the Flask application. In your terminal, run the following command:
sudo nano /etc/nginx/sites-available/flask.conf
This command will open a text editor to create a new configuration file named “flask.conf” in the Nginx sites-available directory. In the text editor, add the following code:
server { listen 80; server_name flask.example.com; location / { include uwsgi_params; uwsgi_pass unix:/var/www/html/myapp/myapp.sock; } }
Save and close the file. This configuration tells Nginx to listen on port 80 and forward requests to your Flask application.
Next, check the Nginx configuration for any syntax errors by running the following command:
sudo nginx -t
If there are no syntax errors, you should see the following output:
nginx: configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Activate the Nginx virtual host by creating a symbolic link to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/flask.conf /etc/nginx/sites-enabled/
Finally, restart the Nginx service to apply the changes:
sudo systemctl restart nginx
Your Flask application is now being served by Nginx as a reverse proxy.
Step 8: Securing the Flask Application with Let’s Encrypt SSL
To secure your Flask application with Let’s Encrypt SSL, follow these steps:
- Install Certbot client by running the following command:
sudo apt-get install python3-certbot-nginx -y
- Obtain and install the SSL certificate by running the following command:
sudo certbot --nginx -d flask.example.com
- Follow the prompts to provide a valid email address and agree to the terms of service.
- Choose whether or not to redirect HTTP traffic to HTTPS.
Congratulations! Your Flask application is now secured with Let’s Encrypt SSL.
Conclusion
In this tutorial, we’ve covered the step-by-step process of deploying a Flask application with uWSGI and Nginx on Ubuntu 20.04. By following these instructions, you can create a scalable and secure web application using the power of Flask. If you have any questions or need further assistance, feel free to reach out to us at Shape.host. We provide reliable and secure Linux SSD VPS hosting solutions tailored to your specific needs.