How to Deploy a Django App on Railway Using GitHub

  • Post author:
  • Post comments:0 Comments
  • Reading time:27 mins read

Railway is a cloud-based platform for deploying and scaling web applications. It offers a simple and easy-to-use interface for deploying and managing applications, as well as a variety of tools and services for building and maintaining applications at scale. With Railway, developers can focus on building and improving their applications, rather than worrying about infrastructure and deployment. Railway supports a wide range of programming languages and frameworks, including Django, Ruby on Rails, Node.js, and more.

In this article, we will explain how to successfully deploy your Django application on Railway and Get started

To Get started ensure that you have set up a railway app account if you don’t you can signup from here Signup

Next, you will need to create a repository for your Django application on GitHub and push your code to it. You can find more information on how to do this in the GitHub documentation.

Before getting started to deploy you want to follow the below-mentioned steps

#1 Installing gunicorn on the Django project

What is Gunicorn?

Gunicorn is a Python WSGI HTTP Server for UNIX. It is a pre-fork worker model, ported from Ruby’s Unicorn project. The Gunicorn server is robust and simple to use, with a number of important features.

In Django, Gunicorn is used as a way to run the Django application and make it available to the web. Django is a web framework that runs on the Python language, and Gunicorn is an HTTP server that can run Python web applications. By using Gunicorn to run your Django application, you can make your application available to the web, allowing users to access it through a web browser.

How to Install Gunicorn?

You can simply install gunicorn by the following command

pip install gunicorn

You can refer to Gunicorn documentation here

After successfully installing Gunicorn next step is to add the environment’s packages list to the requirements. txt file. You can do this by the following command

#2 Adding packages list to the requirements.txt file

pip freeze > requirements.txt

This will add the environment’s packages list to the requirements. txt file.

#3 Creating a Procfile

What is Procfile

A Procfile is a text file that is used to specify the commands that are needed to run an application. It is typically used in conjunction with a system like Gunicorn to run a Django application.

The Procfile is named Procfile without a file extension. It is placed in the root directory of the Django project and consists of a single line that specifies the command that is needed to start the application

For example, a Procfile for a Django project that is using Gunicorn might look like this:

web: gunicorn name-of-application.wsgi

This Procfile tells Gunicorn to run the Django application using the myproject.wsgi file. The web in the Procfile specifies that this is a web process, and Gunicorn will run this process when starting the application.

Note: Procfile has no file extension

#4 Create a runtime.txt file

What is a runtime.txt file?

The runtime.txt file is used by the hosting platform or deployment environment to know which version of Python to use when running the Django project. For example, if you are deploying a Django project to a platform like Heroku, the runtime.txt file will be used to determine which version of Python should be used to run the project.

It is important to specify the correct Python runtime version in the runtime.txt file, as using the wrong version can cause issues with the Django project.

#5 Make changes in the file

To be properly rendered and serve your static files you need to add the following lines of code to your file

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

STATIC_ROOT =os.path.join(BASE_DIR, 'staticfiles')

Also, you need to make changes ALLOWED_HOST = [ ] after successfully deploying on Railway. For now, you can add ALLOWED_HOST = [*]

#5 Collecting static files

After successfully doing all the above-mentioned points now it’s time to collect static files. You can do this by the following command

python collectstatic

After doing this next we can push our code to the GitHub repo you created for this project. You can do this by the following command

git status

git add .

git commit -m ‘first commit’

git push

#6 Moving to the Railway app

Open the Railway app website Click on the new project on the right side

After that select Deploy from GitHub repo from the drop-down menu

Next, select your Repositorie. If you don’t find Repositorie try to configure it with Github. After successfully configuring you are able to see the Repository there as mentioned below image

Next, select the option Deploy now. Your website will be live and you can go to the browser and see it live

You can see the dashboard as mentioned below if it is successfully deployed

In conclusion, deploying a Django application to Railway is a straightforward process that can be done using the Railway CLI and GitHub. By following the steps outlined in this tutorial, you can easily get your Django app up and running on Railway, taking advantage of its robust infrastructure and powerful tools for scaling and managing your applications. Whether you are just getting started with Django or you are an experienced developer looking for a reliable platform for deploying your applications, Railway is a great choice.

Latest posts by Publisher (see all)


Publisher @ideasorblogs

Leave a Reply