Deploy a Python Flask App for Free on Railway (No Credit Card)

 

Here’s a detailed step-by-step guide to deploying a Python Flask app on Railway(Click here):


Step 1: Prepare Your Flask App

Ensure your Flask app meets these requirements:

  1. Basic Flask App Structure
    Your app.py (or main script) should include this:

    from flask import Flask
    import os
    
    app = Flask(__name__)
    
    @app.route("/")
    def home():
        return "Hello, Railway!"
    
    if __name__ == "__main__":
        port = int(os.environ.get("PORT", 5000))  # Railway requires listening on this port
        app.run(host="0.0.0.0", port=port)
  2. Requirements File
    Create a requirements.txt file listing all dependencies. Use this command:

    pip freeze > requirements.txt
  3. Procfile
    Create a Procfile (no file extension) in the root directory:

    web: python app.py

    Replace app.py with the filename of your main Flask script if necessary.


Step 2: Set Up a Git Repository

Railway uses Git for deployment. Initialize a Git repository for your project:

git init
git add .
git commit -m "Initial commit"

Step 3: Push Code to GitHub (Optional)

If you want to host your project on GitHub:

  1. Create a new repository on GitHub.
  2. Link it to your local repository:
    git remote add origin https://github.com/<your-username>/<your-repo>.git
    git branch -M main
    git push -u origin main

Step 4: Deploy to Railway

  1. Go to Railway.app(click here) and log in (or create an account).
  2. Click "New Project" and choose one of the following:
    • Deploy from GitHub: Link your GitHub account and select the repository.
    • Deploy via CLI: If your code isn’t on GitHub, you can use Railway’s CLI.

Step 5: Configure Environment Variables

If your Flask app uses environment variables (e.g., SECRET_KEY or database credentials):

  1. Go to the Settings tab of your Railway project.
  2. Add the environment variables required for your app.

Step 6: Test the Deployment

  1. After deployment, Railway assigns a unique URL to your app (e.g., https://your-app.up.railway.app).
  2. Visit this URL to verify that your app is running successfully.

Step 7: Debugging (if needed)

  • Check the Logs tab in the Railway dashboard for runtime or deployment errors.
  • Common issues:
    • Missing requirements.txt: Ensure all dependencies are listed in this file.
    • Port binding issues: Make sure the app listens on the PORT environment variable.

Example: Folder Structure

Your project directory should look like this:

my-flask-app/ │ ├── app.py ├── requirements.txt ├── Procfile ├── static/ ├── templates/ └── ...

Optional Steps

Environment Variables:

If your Flask project needs any environment variables (e.g., database credentials, API keys), you can set them in Railway:

  1. Go to your project dashboard on Railway.
  2. Navigate to the Environment tab.
  3. Set the necessary environment variables.

These variables will be available to your application during runtime.

Configure Database (if applicable):

If your Flask project uses a database (such as PostgreSQL or MySQL), you can easily add a database to your Railway project:

  1. In your Railway dashboard, click on Add Plugin and choose the database you need.
  2. Railway will set up the database and provide you with connection details, which you can use in your properties file or as environment variables.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete