
Once you've created a Flask app, you'll find that the included web server is best suited for development. In this guide, you'll learn how to host your Flask app on a Debian 10 cloud server instance with an Nginx web server, Certbot for SSL, and uWSGI application server.
You can follow this tutorial without a domain name by using localhost in the Nginx configuration step. You won't be able to install a valid SSL certificate, so skip the Certbot instructions.
Install the Nginx package.
Test Nginx by navigating to the server's IP address in a browser. Verify the Nginx web page appears.
If you have a registered domain, set up DNS entries for your server. This tutorial uses the following values:
Install uWSGI and Flask.
Create a website home directory.
If you have a Flask application to deploy, move it to the /var/www/example directory.
Or, you can create a simple Flask application for testing this guide.
Paste in:
Create config-uwsgi.ini for uWSGI.
Paste in:
The processes line can be set higher for larger servers.
Remove the default Nginx configuration.
Create a new Nginx configuration file.
Paste in the following content. If you do not have a registered domain name, use localhost for server_name in place of example.com and www.example.com.
Create a symbolic link from your Nginx configuration to the conf.d directory.
Restart Nginx.
Nginx is configured to proxy traffic to uWSGI, but the service isn't running yet. Start uWSGI with nohup.
Press Ctrl + C to send the SIGINT signal and see that the process still runs in the background. For production use, we recommend running uWSGI at startup under systemd.
Navigate to your site in a web browser and verify you see the "Hello World!" message.
If you have a public domain name, configure the webserver for HTTPS with a free Let's Encrypt certificate using Certbot.
Install Certbot.
Request an SSL certificate.
Answer the questions when prompted by Certbot and agree to the terms of service.
Certbot will ask which domain names should have SSL support, press Enter to select all names.
When prompted, choose to redirect HTTP traffic to HTTPS.
After following this beginner tutorial, your Nginx web server runs Flask application on uWSGI, secured with Certbot. For full production use, you should also run uWSGI on startup as a normal user with limited permissions. See the uWSGI best practices and Flask documentation for more information.
0 Comments
Be the first to comment and share your perspective with the community.