Hosting a Flask web server on Railway [FREE]
Tags: webdev, tutorial, programming, beginners
Published: 2024-04-30 12:34:56

## Introduction
Welcome 👋 to this blog. Do you want to host or deploy your [flask](https://flask.palletsprojects.com/en/3.0.x/) 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](https://railway.app/). 

Railway is basically a deployment platform where you can deploy your project and perform [many tasks](https://docs.railway.app/overview/about-railway). 

## 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.
```python
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:
![file structure image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6q9su7umzh74t821ksr.png)
*If you only want to focus on the deploying part of the tutorial please feel free to use my [repository](https://github.com/ankur0904/hosting-flask-app) for the same*

## Configuring the project folder for Hosting

1. **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

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

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

    ```js

    $ 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 just-created branch can be renamed via this command:
    hint: 
    hint:   git branch -m <name>
    Initialized empty Git repository in /home/ankur/Desktop/development/hosting-flask-app/.git/
    
    ```

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

    ```js
    
    $ 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*


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

5. **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. 

```js
web: gunicorn app:app
```
> Now our project is completely configured for hosting. Let's host our project in [Railway](https://railway.app/).

## Hosting the Flask server
Firstly create the account in the [Railway](https://railway.app/) and configure your GitHub account as well. Then: 
1. Go to [home page](https://railway.app/)

2. You will see the **Start the new project** button on the home page. Click on this button.
![home page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0m6ncnms49v61fx5ceq.png)

3. Select **Deploy from Github Repo**
![deploy-from-repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48l9iwmog02tzj0ys42j.png)

4. Then you can **Select the project** which you have created or forked(if you are using [my project](https://github.com/ankur0904/hosting-flask-app)
![select-your-project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92mcq3zhpn93bd9x1pfi.png)

5. Now simply click on the **Deploy Now**:
![click-deploy-now](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pg4ye33bg79skvbl2weu.png)

6. You will be redirected to this **UI**:
![UI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lljp8k43hfo4uvh971at.png)

7. 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](mailto:ankursingh91002@gmail.com)  
🔗 LinkedIn: [Ankur Singh](https://www.linkedin.com/in/ankur-singh-161458227/)  
🔗 Twitter: [@ankur_136](https://twitter.com/ankur_136)

Let's connect and create something great together!