Deploying your Django app on Railway
The Heroku alternative you've been looking for

Search for a command to run...
The Heroku alternative you've been looking for

No comments yet. Be the first to comment.
Adding a few cities to a delivery zone is instant. Adding 5,000 cities ran over 5,000 database round-trips and regularly timed out the request. The root causes were two common Django patterns that don

Every day, a report runs: "what did each store sell yesterday?" One of the stores is in Lagos, the other in London. The report showed both stores as having zero sales for the last two hours of the day

You don't always control the schema you work with. Sometimes you inherit a codebase where GenericForeignKey is threaded through the models, and a migration isn't on the table. Loading a page of 20 aud

Your users want live updates: an order flips to paid, a notification pops, a dashboard number ticks up without a refresh. The obvious answer in Django is Channels. I reached for it first too. But as o

Change a product's price anywhere in your app, and it instantly syncs to a third-party marketplace. No manual triggers, no polling, no fragile save() overrides. Here's the signal pattern that powers i

In this article, we will be deploying our Django app on Railway. Railway is a Platform as a Service (PAAS) and they offer web server and database hosting which is sufficient to deploy our app to the web.
Before moving on with this tutorial, there is a prerequisite which is detailed in this guide. You can call it pre-deployment. Please go through it and come back once it's checked ☑️
Also, you need to create an account on Railway(obviously).
Now, let's begin.
Run the command below in your terminal if you use Ubuntu or WSL2. Else you can check for other methods here.
curl -LO https://github.com/railwayapp/nixpacks/releases/download/v2.0.11/nixpacks-v2.0.11-amd64.deb sudo dpkg -i nixpacks-v2.0.11-amd64.deb
You can install this using the shell script below if you're on macOS, Linux or Windows via WSL. Just run the command in your applicable terminal. You can check for other methods here.
bash <(curl -fsSL cli.new)
railway login
A project is like a Github Organization or an Heroku Pipeline that defaults to the production environment. You can also create other environments for staging, testing, etc
railway init
Basically, this links the current directory (folder) you're in, to your Railway project. Very useful, especially as you can have multiple projects.
railway link
Navigate with the arrow keys and hit enter to select a project.
Railway may have asked you to let them set up your environment variables from your .env file while you were deploying with railway up. That works but you may want to modify or add more. To see all existing environment variables, run
railway variables
To set or modify a variable
railway variables set {VARIABLE_NAME}:{VARIABLE_VALUE}
Procfiles are a popular format used to specify a web/release process. To use this you need to create a Procfile in your root directory.
touch Procfile
Then paste this into it
web: gunicorn {YOUR_DJANGO_PROJECT_NAME}.wsgi
release: python manage.py migrate
At this point, our app should be ready for a smooth deployment. You can do this with
railway up
It then builds and deploys your app on Railway 🎉
If you had a successful deployment, you can skip this. However, if you have a psycop2g dependency, you'll run into an error while trying to deploy (as at the time of writing this). The fix for this is to install a postgresql nix package. First, create a file called nixpacks.toml in your root directory touch nixpacks.toml Then paste this snippet into it [phases.setup] nixPkgs = ["...", "postgresql"]
After deploying your app, you need to set up a port number as Railway doesn't do this for us out of the box. Login to Railway on your web browser. Navigate to your project, then to your web deployment, then to your settings, and then set your port to 8000 as is the default for Django. Or whatever you want to use.

There are where we would need to run some commands directly in the hosted environment. With Railway, we can do this in a subshell that makes use of our hosted app's variables.
railway shell
This opens up that subshell. And for example, we can then create a superuser as we usually do with
python manage.py createsuperuser
After our app is deployed, we need to generate a domain name for it so that it becomes accessible as Railway does not generate one by default. Pretty much the same process for setting up our port earlier.
And that's all that needs to be done! Thanks for following through to the end, and let me know if you encountered any issue not mentioned here.
¡Hasta luego!