Deploying a Django Website for free in 2024: A Step-by-Step Guide to Free Hosting

nahianbinrahman

Nahian Bin Rahman

Posted on January 9, 2024

Deploying a Django Website for free in 2024: A Step-by-Step Guide to Free Hosting

Have you successfully developed your Django website and are eager to showcase it online? No worries, this guide will comprehensively explain how to deploy your Django website for free in 2023. While there are various platforms available for hosting Django projects, this tutorial will focus on my preferred choice—Railway.

Image description

Introduction:
Mastering the skill of deploying websites is crucial for your web development journey. Deploying a Django website is a pivotal step as it makes the website accessible to users. Without deployment, the website remains confined to the developer's local machine, serving no purpose to others.

What is Railway?
Railway stands out as a cloud-based platform facilitating the setup of infrastructure, local development, and seamless deployment of your app to the cloud. It simplifies the hosting process by allowing direct project imports from GitHub. Additionally, Railway offers fast hosting services and provides 500 hours of free deployment each month in its Starter Plan, ideal for hosting personal Django projects.

Railway Features:
Railway boasts various features, including Automagic Builds, Multiple Environments, Deployment Rollbacks, and more.

Automatic Builds: Railway automatically detects code changes on GitHub and initiates project builds for deployment.
Multiple Environments: Users can effortlessly evolve their app over time using fork-joinable environments.
Deployment Rollbacks: In case of bugs or undesirable changes, Railway supports one-click, instant rollbacks for every modification.
How to Deploy Django Website for Free in 2023:
To host your Django website for free on Railway, follow the steps outlined below:

  1. Install the Python library named Gunicorn with the command: pip install gunicorn.
  2. Create a file named Procfile (without any extension) in the project's root directory and add the following code: web: gunicorn project.wsgi. Note: Replace project.wsgi with your actual project name.
  3. Create a requirements.txt file containing all required modules with the command: pip freeze > requirements.txt.
  4. Once the module is installed add the following middleware in the settings.py file just below the django.middleware.security.SecurityMiddleware
`MIDDLEWARE = [
    # ...
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    # ...
]`
Enter fullscreen mode Exit fullscreen mode

Now add the following code in the settings.py file.

`STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'`
Enter fullscreen mode Exit fullscreen mode

This step will tell the django to serve the staticfiles in the production. Add the following code to the settings.py file.

`STATIC_URL = 'static/'

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

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')`
Enter fullscreen mode Exit fullscreen mode

5.Run the command:

`py manage.py collectstatic`
Enter fullscreen mode Exit fullscreen mode

to collect static files.
6.Commit and push all changes to GitHub.
7.Visit the Railway website, start a new project, deploy from the GitHub repo, and authorize necessary permissions.
8.Select your project, click "Deploy Now," and within seconds, your Django project will be hosted on Railway for free.
9.Customize the website URL under the domain options in the settings section.

Railway offers the quickest way to showcase your Django project online. This guide aimed to provide detailed steps for deploying your Django website for free.

💖 💪 🙅 🚩
nahianbinrahman
Nahian Bin Rahman

Posted on January 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024