Django, Flask and other Python web frameworks make it incredibly easy to create and run web apps locally, making the development process smooth and enjoyable. However, when it comes to taking the application from a local dev machine to a production environment, things can get a bit more complex. In this article, I explain the key components that are in reality needed for a full stack production deployment of a Python web app, with some recommended options. They are roughly in order of priority!
1. VPS Deployment: Scalability and Control
Deploying an app to a Virtual Private Server (VPS) is by far the best hardware to run a Python web app (of course it’s possible to deploy on hardware at home or work but this would require a) hardware purchase b) Potentially unsafe access into your local network c) bandwidth may not be enough or needs paying for from an ISP. VPS companies allow renting a server by the minute, including hardware, storage, bandwidth and internet connection – which can be upgraded with a few clicks as demand changes.
2. Domain Name, DNS and Incoming Email
A domain name is the web application’s online address, serving as the bridge between the server IP address and users’ browsers. The Domain Name Server (DNS) ensures that when someone enters the domain name (e.g., andypi.co.uk), they are directed to the IP address of server where the web app is hosted. Usually the Domain Name registrar offers free DNS and often free email forwarding as well.
3. Web Server
Python web frameworks all come with a built in development server to run a web app on the local machine, but these are not recommended for production use. Therefore a standard web server AND a gateway between python and the server are also required. These need to be installed to run as permanent services and need configuring to work with the web app.
4. SSL Certificate
SSL certificates are required to encrypt the traffic between a users browser and the web app. It’s the default these days and most web browsers display some kind of warning if the website is only operating over HTTP. The good news is SSL certificates are available free and can be set to auto renew with Let’s Encrypt’s Certbot.
Recommended: Let’s Encrypt
5. Version Control
Version control, typically managed through platforms like Git, is an absolute must for any coding work. It allows tracking of changes, the ability to roll back to previous versions if something goes wrong, and also allows multiple team members to collaborate on the codebase simultaneously. Version control ensures code stability and provides a history of the project’s development, making it easier to identify and fix issues.
6. Dependency Management with Virtual Environments and / or Containerisation
Virtual environments are crucial for isolating the application’s dependencies. When you deploy a web app, you want to ensure that it uses specific versions of libraries and packages, not those installed system-wide. Virtual environments provide this isolation, preventing conflicts and making the deployment environment consistent and reliable. For non-Python dependencies there also needs to be some though about how to control the versions and deployment of those too. An alternative to a Python virtual environment is to use Docker containers to isolate the application from the rest of the VPS system.
For most web applications, a database is essential for storing and retrieving data. Whether it’s user information, product details, or any other data, it’s a fundamental component for any data-driven web app. SQLite is a great choice for a database as a file, and requires very little setup. If the database needs to be accessible across distributed systems then a you might need one with database management system, such as MySQL or MongoDB – but these require additional complexity to setup and will require a process to manage them running as a service.
8. Outgoing Email
Email functionality is essential for sending notifications, user registration confirmations, and password reset emails. It’s a primary means of communication between the app and its users, making it a vital component for user engagement and interaction. If the app requires a login, an email sending service is a must.
Recommended: AWS SES
Effective logging is essential for monitoring a web application’s health and diagnosing issues in a production environment. It provides insights into how the app is behaving, making it easier to identify and fix problems. Without proper logging, troubleshooting becomes a daunting task. Logs can be saved to a local file on the app’s server, but for a better interface for log analysis and debugging you could use a cloud based provider.
Recommended: Local file, for cloud based, try: Datadog
10. Deployment or Continuous Deployment / Integration tools
Deployment tools streamline the deployment process by the automating repetitive tasks needed to setup a server install everything required for a web app. They ensure consistent and correct deployment of a web app and dependencies across different environments. This automation reduces the risk of human error and saves valuable time, making the deployment more efficient and reliable. Ansible is a server configuration tools that can deploy apps and is run from a local machine, but there are alternatives that run remotely and can be setup to build and deploy an app whenever a change is made to its code and check into version control. GitHub Actions and Jenkins are examples of this kind of remote Continuous Deployment tools.
11. Message Queue for Handling Background Tasks
Message queues are indispensable for handling background tasks in the web app. They ensure that long running tasks, such as sending emails, API calls or intensive data processing, don’t block the main application thread. This keeps the app responsive and efficient. All but the simplest web apps are going to need this which will require the extra complexity of managing services for both a message queue (such as the Python Celery package) and a message broker (such as Redis).
Analytics tools enable you to collect and analyze user behaviour. Although there is some crossover with logging, in general logging is more about the web app behaviour and errors, whereas analytics is user-centric. This information is invaluable for making data-driven decisions, improving user experience, and identifying areas for optimisation. Analytics help you understand user behaviour, track conversions, and measure the success of the app.
13. Payment Processing
If the app is paid for then a payment processor to collect money and pay it to your bank account is required.
This doesn’t fit so neatly with the deployment theme in this article but if a web app has all the other components in place then testing it is also likely to be very helpful, and can be done automatically as part of the version control system (e.g. git precommit hook) or with the continuous deployment tools.
Recommended: Python Testing
Conclusion and some alternatives…
In conclusion, while developing a Flask or Django web app locally is a breeze, deploying a user facing commercial app in a production environment requires time to learn and integrate these other key components. The first 5 are essential, but in reality anything but the very simplest app are going to need all of these components for a full stack Python web application. I think they are worth learning to setup yourself but there are other (more expensive!) options that do some of the above list for you, such as:
Personally I would either go for Zappa on AWS for a simple app, but for anything else I use the full stack on a VPS – see my guide for how I deploy a Django app which utilises most of the components above. Another good resource I’d recommend is the Full Stack Python website and book by Matt Makai.