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

 

To deploy a Django application on Railway(click here for free registration), follow these steps:

Prerequisites:

  • A Railway account (sign up at Railway click here).
  • A basic Django project (if you don't have one, you can create one using the steps below).

Step 1: Create a Simple Django Project

  1. Install Django: If you don’t have Django installed, create a virtual environment and install Django.

    python -m venv venv
    source venv/bin/activate  # On Windows use: venv\Scripts\activate
    pip install django
  2. Create a New Django Project: Run the following commands to create a new Django project and app.

    django-admin startproject myproject
    cd myproject
    python manage.py startapp myapp
  3. Create a Simple View: In the myapp/views.py file, add a simple view:

    from django.http import HttpResponse
    
    def home(request):
        return HttpResponse("Hello, Railway!")
  4. Map the View to a URL: In myproject/urls.py, add a URL for the home view:

    from django.contrib import admin
    from django.urls import path
    from myapp.views import home
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', home),
    ]

Step 2: Prepare for Deployment

  1. Install Dependencies: Create a requirements.txt file using:

    pip freeze > requirements.txt
  2. Create a Procfile: A Procfile tells Railway how to run your Django app. Create a file named Procfile in the root directory and add the following:

    web: gunicorn myproject.wsgi --log-file -

    Ensure you have gunicorn installed:

    pip install gunicorn
  3. Configure Static Files: Django requires static files to be collected in production. Add the following settings to your settings.py:

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  4. Set up ALLOWED_HOSTS: In settings.py, update the ALLOWED_HOSTS setting:

    ALLOWED_HOSTS = ['your-app-name.railway.app']
  5. Collect Static Files: Run the following command to collect static files for production:

    python manage.py collectstatic

Step 3: Set Up Railway

  1. Sign Up/Login to Railway: Go to Railway and sign up/login.

  2. Create a New Project:

    • Click "New Project".
    • Choose "Deploy from GitHub".
    • Connect your GitHub account if it’s not connected yet.
    • Select the repository with your Django project (or create one if needed).

Step 4: Configure Environment Variables

  1. Configure Environment Variables on Railway:

    • Navigate to the "Variables" tab in the Railway dashboard.
    • Add the following variables:
      • DJANGO_SECRET_KEY: A secret key for your Django app (you can generate one using django.core.management.utils.get_random_secret_key()).

Step 5: Deploy to Railway

  1. Push Your Code to GitHub:

    • If your project isn’t already in a GitHub repository, push it to one.
    • Railway will automatically detect the new repository and begin deployment.
  2. Deploy:

    • Railway will automatically start the deployment process.
    • If the deployment is successful, it will give you a URL (e.g., your-app-name.railway.app).

Step 6: Access Your Application

Once the deployment is complete, you can access your Django app via the Railway URL, and it should display "Hello, Railway!" on the homepage.


Optional Steps

Environment Variables:

If your Django 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 Django 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