Hosting a Flask web server on Railway [FREE]

Hosting a Flask web server on Railway [FREE]

Introduction

Welcome 👋 to this blog. Do you want to host or deploy your flask web server on the production completely free and want to access different production features like configuring environment variables, choose a different version of frameworks or language, etc.? Then you are in the correct place. In this blog, I’ll show how to deploy/host a Flask web server in Railway.

Railway is basically a deployment platform where you can deploy your project and perform many tasks.

A Flask web server

First, we create a Flask web server for the deployment purpose.

You may notice that the Flask web server is basic, i.e., doing some basic stuff. I intentionally do this for the simplicity of this tutorial but you are free to build any kin d of Flask web server and follow along with the tutorial, at the end you can host the web server as well.

This is the code of the business logic of Flask web application.

from flask import Flask, render_template

app = Flask(__name__)

@app.route(/, methods=[GET])
def hello():
return render_template(index.html)

@app.route(/python-2, methods=[POST])
def python2():
return render_template(python2.html)

@app.route(/python-3, methods=[POST])
def python3():
return render_template(python3.html)

if __name__ == __main__:
app.run(debug=True)

The main task of my web server is to serve the index page when getting requests from the / route. Similarly python2 page from /python-2 route & python3 page from the/python-3 route.
The overall file structure will look like this:

If you only want to focus on the deploying part of the tutorial please feel free to use my repository for the same

Configuring the project folder for Hosting

Using virtual environment:
We will use a virtual environment so that there will be no conflict in the versions of the different modules.
To create the virtual environment follow these commands

$ python3 m venv venv # create the virtual environment
$ source venv/bin/activate # activate the virtual environment
$ pip install <package name> # install the required package

Convert unversioned project to git repository:
For this run the following commands:

$ git init
hint: Using master as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of master are main, trunk and
hint: development. The justcreated branch can be renamed via this command:
hint:
hint: git branch m <name>
Initialized empty Git repository in /home/ankur/Desktop/development/hostingflaskapp/.git/

Create the requirement.txt file:
Follow this command to create the requiremnt.txt file automatically.

$ pip3 freeze > requirements.txt

You will notice a requirements.txt file will be created and some dependencies along with their version are listed there.

I suggest you add the new line gunicorn==20.1.0, I’ll tell you why in one minute

Commit all the changes and push them to the remote repository:
Your repository will look something like this.

Now time to tell the server how to start our application:
First, in our local system, the server is running in localhost but this will no longer be the same when we discuss hosting. Okay! We will be using gunicorn which is Python web server gateway for the hosted or production version.

To tell the server what to do with our project, I mean which command you need to execute with the project, we need to create a new file named Procfile, which basically contains the commands to start our project.

web: gunicorn app:app

Now our project is completely configured for hosting. Let’s host our project in Railway.

Hosting the Flask server

Firstly create the account in the Railway and configure your GitHub account as well. Then:

Go to home page

You will see the Start the new project button on the home page. Click on this button.

Select Deploy from Github Repo

Then you can Select the project which you have created or forked(if you are using my project

Now simply click on the Deploy Now:

You will be redirected to this UI:

Here you will see when your deployment gets completed. You get a message Add a Domain to expose this web server, or Add Custom Domain when you click on Add a Domain then you will get your own hosted link.
Here is mine: https://hosting-flask-app-production.up.railway.app/

You can share your project link in the comments or any kind of feedback in the comments.

🎉 You nailed it

You have learned how to deploy or host your Flask backend web server completely from scratch. Now time to host your own web server/application and share down the links below.

Thank you for reading! If you enjoyed this content and are looking for a talented writer, feel free to reach out to me:

📧 Email: ankursingh91002@gmail.com

🔗 LinkedIn: Ankur Singh

🔗 Twitter: @ankur_136

Let’s connect and create something great together!

Leave a Reply

Your email address will not be published. Required fields are marked *