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
Install Django: If you don’t have Django installed, create a virtual environment and install Django.
Create a New Django Project: Run the following commands to create a new Django project and app.
Create a Simple View: In the
myapp/views.py
file, add a simple view:Map the View to a URL: In
myproject/urls.py
, add a URL for the home view:
Step 2: Prepare for Deployment
Install Dependencies: Create a
requirements.txt
file using:Create a
Procfile
: AProcfile
tells Railway how to run your Django app. Create a file namedProcfile
in the root directory and add the following:Ensure you have
gunicorn
installed:Configure Static Files: Django requires static files to be collected in production. Add the following settings to your
settings.py
:Set up
ALLOWED_HOSTS
: Insettings.py
, update theALLOWED_HOSTS
setting:Collect Static Files: Run the following command to collect static files for production:
Step 3: Set Up Railway
Sign Up/Login to Railway: Go to Railway and sign up/login.
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
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 usingdjango.core.management.utils.get_random_secret_key()
).
Step 5: Deploy to Railway
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.
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:
- Go to your project dashboard on Railway.
- Navigate to the Environment tab.
- 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:
- In your Railway dashboard, click on Add Plugin and choose the database you need.
- Railway will set up the database and provide you with connection details, which you can use in your
properties
file or as environment variables.