DEV Community

Akinwande Tomisin
Akinwande Tomisin

Posted on

HOW TO HOST A DJANGO PROJECT ON RAILWAY

Hey everyone πŸ‘‹πŸΎ. This article is going to show the updated way to host a Django application on Railway. Let's get started.

Brief Introduction

Railway is a cloud hosting platform where you can host your projects and provision infrastructure like databases. It's popularly known as an alternative to Heroku.

Tutorial

Step 1: To get started, you'll need to have a GitHub account containing a repository of the project you want to be hosted. You'll also need to create a Railway account if you don't have one already. Click here to Sign up for Railway.

Step 2: In your project, run the command:



pip install gunicorn


Enter fullscreen mode Exit fullscreen mode

Step 3: Run the command below to get all the packages used in your project:



pip freeze > requirements.txt


Enter fullscreen mode Exit fullscreen mode

Step 4: Create a file called railway.json in your projects root folder. Copy & Paste the following code into the file.



{
    "$schema": "https://railway.app/railway.schema.json",
    "build": {
        "builder": "NIXPACKS"
    },
    "deploy": {
        "startCommand": "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn your_app_name.wsgi",
        "restartPolicyType": "ON_FAILURE",
        "restartPolicyMaxRetries": 10
    }
}


Enter fullscreen mode Exit fullscreen mode

Replace the string 'your_app_name' in "startCommand" with the name of your Django project i.e The folder that contains a wsgi.py file.

Step 4: Next, you'll make some changes to the settings.py file. Check for the line ALLOWED HOSTS = [].

Change the line of code to: ALLOWED HOSTS = ['*'] to allow all hosts for the mean time.

Still on your settings.py file, navigate to the static files section and add the following code:



STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')


Enter fullscreen mode Exit fullscreen mode

Step 5: Next, run the following command to collect all your static files into a folder.



python manage.py collectstatic


Enter fullscreen mode Exit fullscreen mode

Step 6: You can now push the code to a repository on GitHub. Run the following commands consecutively to do that:



git status
git add .
git commit -m "initial commit"
git push origin main


Enter fullscreen mode Exit fullscreen mode

Step 7: Log in to your Railway app Account

  • Select New Project New Project
  • Click on 'Deploy from GitHub Repo' Deploy from GitHub Repo
  • Click on 'Configure GitHub App'. Configure GitHub App
  • Log in to your GitHub Account in the Dialog window. Navigate to the Repository Access section and select the repository you would like to deploy.
  • When you are done and redirected to the Railway App page, select 'Deploy from GitHub Repo' and choose the repository you want to deploy. The deployment should start immediately.

Step 7: Once the deployment is complete, you'll need to make some modifications to the settings.py file of your project.
Getting Domain Name
Go to the Settings section of your newly deployed app, Go down to the Domain section under Environment, click on 'Generate Domain' and copy the domain name of the app. In the settings.py file replace the '' in `ALLOWED_HOSTS = ['']` with the your app's domain name without the 'https://' like so:



ALLOWED_HOSTS = ['purple-field-production.up.railway.app']


Enter fullscreen mode Exit fullscreen mode

Add the following code to your settings.py and replace the value of the variable with your app's full URL.



CSRF_TRUSTED_ORIGINS = ['https:purple-field-production.up.railway.app']


Enter fullscreen mode Exit fullscreen mode

Finally, commit the changes made to your repository using the commands below and Railway will automatically redeploy the site with your changes.



git status
git commit -am "updated settings.py"
git push origin main


Enter fullscreen mode Exit fullscreen mode

CONGRATULATIONS!!, your project has been deployedπŸ‘πŸΎ

Top comments (0)