How to Deploy Python flask API on a Windows VPS Server (5-min Definitive Guide)

Deploy Python flask API on a Windows VPS Server

Table of Contents

Spread the love

Today we’ll learn How to Deploy a python Flask API on a Windows VPS server.

So, in simple terms.

How to Deploy a flask API on a Windows VPS Server?

To Deploy a flask API in Windows VPS Server, first, use a VPS Server provider. Then you have to import Waitress into your python code and use it to serve the app to a particular host of your VPS server. I explain this in detail in this Blog Post.

Where can I deploy my flask app?

On your local machine. But, Most commonly you can use a Cloud Windows VPS server. I have shown how to set it up completely. Companies like Vultr, and Contabo, make it easier for you to deploy your flask app.

Now let's get started with the tutorial on How to Deploy Python flask API on a Windows VPS Server.

Tools Required to Deploy Python flask API on a Windows VPS Server

The tools needed for this tutorial Most importantly we need an IDE (I will be using Visual Studio for this Tutorial) 

If you want to download and Setup VS Code. Feel free to Check this Full Guide.

Secondly, we’ll be needing a tool Known as Github for desktop.

Github will be used so we can manage to pull, push, and update our project easily between our local machine and our VPS server.

Let’s go ahead and Download Github for Desktop.

Third we need an Empty Folder (You can Create on the Desktop) to keep our Project in.

First step is to open Github and create a new project.

 

So, I will go here and click add, and create a new repository.

 

And I will name it the python flask API test.

Then I will choose my desktop folder here python flask API select and create a repository. Now click on publish repository.

Then simply open up with Visual studio code.

Now I will create a new file python file and I will call it flask API. 

So now if you go to my folder here you will see the flask API file is created, give it a name and save it.

Mine is flaskAPI.py

Now we’ll not be coding a whole API in this tutorial, so I’m just going to paste the whole code for simplicity.

This is my API code.

#start

#Your Code goes here

#end

In short, the Code I used is a machine learning project that allows you to remove background from images with one click.

Downloading and Installing Postman.

Click This Link to download Postman.

Or search Google for “Download Postman”

Click on Download Postman. And the page below will open, and your current operating systems version will show up. Click it to download. And the installation process is pretty self-explanatory, so accept the Terms and Conditions and Install.

How to Deploy Python flask API on a Windows Local Machine

I also made a Detailed video, and recommend you to watch this to understand the concept fully.

Deploy Python flask API on a Windows VPS Server

Now usually to run this app we use this simple piece of code.

#start

app.run()

#end

But in our case, we want to run this API as a service.

So, I'm going to do an import in my code that you are going to do also and do a small change in this part.

Go to the beginning of the Code and do this simple import.

#start

from waitress import serve

#end

This will allow you to run this API as a service and serve the users.

In case you don't have this model simply go up to the Toolbar and Click on Terminal > New terminal and run this command 

#start

pip install waitress

#end

Then press enter. 

In my case it's already installed.

Then we will go down again here and instead of running the application with

#start

app.run()

#end

I will paste this simple code and run this function serve.

#start

serve(app, host=’0.0.0.0’. port=8080, threads=1) #WAITRESS

#end

This application under this host means runs locally on port 8080, and threads equal to one.

Now after doing all this. Click on Run Code. The play button is on the Top Right corner.

If we run this code after tapping run, now the API is running let's test it.

I will open postman and will issue a post request to localhost on the port

8080/api/removeBg

This is one of the endpoints.

In Postman, Now go here to body.

 I will test this picture. Pick an image.

 And Press “Send.” 

Congratulations, The API is working 

If we open the folder now go here to static inputs you will see the image is uploaded.

And everything is working perfectly on my local machine.

Now we need to host this API on the VPS server but before that,

Make sure to push your changes to Github.

So open the application here, enter details, and commit to main, commit anyway, then push to origin.

While it's pushing it may take some time we need to prepare our VPS server so we need a windows server online to host our API on. You can go with whatever company you want. For Example.

Windows VPS Servers to Host Flask API

Vultr

VULTR has amazing support, and Great service. By the way, you can use my link to get hundred dollars of free credits to test for free for two weeks.

Contabo

It's a really good service, with a more affordable price tag. 

You can also use google cloud, amazon web services, or Microsoft Azure.

Just get a Windows VPS to test with us and practice it live with us.

Deploy Python flask API on a Windows VPS Server

In my case, I have a VPS server that I bought from Contabo. Now I will connect this VPS

And again on your VPS go and download Github desktop and Visual studio code. This will make things easy for you later on if you want to update push and pull between your local machine and this VPS server.

 So open the Github application.

Deploy Python flask API on a Windows VPS Server

And then simply you can clone a repository from your local machine.

I have now cloned the repository to my server here.

And if you go to my D drive here, APIs and here is the python flask API script.

The image background remover script. As you can see the folders and files here on my VPS server.

Deploy Python flask API on a Windows VPS Server

Don't forget to install python on the server as well.

If you don't know how, Here is a tutorial.

Now our API script is ready on our VPS server.

What we are going to do now.

Is to figure out how to make this run automatically on the server and keep it running on the VPS to serve your users.

Let's firstly test it on our server before automating and hosting it. I will open the python script file with visual studio code.

How to Deploy Python flask API on a Windows VPS Server

If you go down here you will see, that I'm using the same code as the waitress model.

Let's run it 

If we go back to our local PC here and open postman again.

And now I will try to change the IP address and replace it with my VPS IP address.

Press “Send.”

So you can see now our API is working on our windows VPS server and we can access it from any machine using the IP address.

Let's see now how we can run this automatically on our server.

What you are going to do is to open any folder on your pc.

Go here to the D drive and then create a new text file.

We are going to create a batch file to run automatically when windows boots.

So, I will Name it as shown below. your name can be different.

Remember to change the text extension to .bat Then say “yes” and save it. 

Now in the .bat file, you will paste this simple piece of code.

//

Python w  (then pass the path of your python flask API script.)

//

Then save it.

And now look at this, if you Double click this batch file open it.

You can see that the API now is running.

Let's test this again.

Go back here to the local machine.

Press “send.”

You can see it's working perfectly.

Now we will go here to our start menu and open schedule tasks.

We'll create a scheduled task that fires this python API at startup.

Simply create a new task. I will call it run flask API.

 Make sure to Run with the highest privileges with the administrator.

Now go to triggers and click on new.

And I will say run this script on start up whenever the windows boots.

Click Action new.

I will simply browse for the script in the Drive, in which I saved the .bat file and click ok

Let's try it. I right-click and select Run.

 

It says the task is currently running.

If you open now task manager and go to details and search for python you will see python w is running in the background.

Finally, Go to Postman, click on “Send” (Blue button on the top right) again and let's test the API.

And as we see, it's working perfectly.

Conclusion on How to Deploy Python flask API on a Windows VPS Server

I Hope you successfully learned How to Deploy Python flask API on a Windows VPS Server.

If you want to learn how to build this API and get the source code for the background image remover with machine learning for free.

You can check this Blog Post here.

If you have any questions. Post them in the comments, I will be happy to help.

Take Care and Happy Coding!


Spread the love