Dan Devins

Deploying a Flask App on DreamHost

Project: Secure Form Submission System

Today, I successfully moved a local Flask development project into a live production environment on DreamHost. The journey involved transitioning from a simple script to a database-backed web application. Here is a breakdown of the technical milestones.

1. Environment Architecture

Instead of using the default system Python, I established a Virtual Environment (venv). This ensures that all dependencies (Flask, MySQL Connector, Dotenv) are isolated and won't be broken by server-wide updates.

2. Secure Data Management

I implemented a .env file strategy to manage sensitive credentials. By keeping the Database Host, Username, and Password in a hidden environment file, I’ve ensured that the main application code (app.py) remains secure and shareable without exposing private keys.

3. Database Integration

We moved beyond static files by connecting the app to a MySQL database. * Created a schema to store user names, messages, and timestamps. * Implemented a persistent connection function in Flask to handle data insertion. * Verified data integrity using the MySQL command-line interface.

4. The "Bridge" to the Web: FastCGI

Because standard Proxy protocols can be restricted on shared hosting, we implemented a FastCGI (flup) bridge. * app.fcgi: This script acts as the "translator" between the Apache web server and the Python WSGI application. * .htaccess: Configured custom rewrite rules to route all incoming traffic through the FastCGI bridge while maintaining access to static files like test.txt.

5. Admin Dashboard

To wrap up the project, I built a secondary Admin Route. * Backend: Added a new @app.route('/admin') that fetches all records from MySQL. * Frontend: Created a clean, responsive HTML table using Jinja2 templating to display submissions in real-time.


Tech Stack: Python 3, Flask, MySQL, Apache, FastCGI


Back to Blog Index