Most of the people know how to write python services on local host and when time comes to deploy it, they are confused. If you don’t know how to write application in flask? then learn here. To host flask application, you have following options.
In this chapter i will be focusing on host flask application on cpanels like NameCheap. The simple steps to host flask application on CPanel are:-
- Step 1: Log in to cPanel
- Step 2: Create a Python Application from there (option available in Account)
- Step 3: Flask Application
- Write Flask Application OR
- Upload the Flask Application to cPanel
- Step 4: Configuring our WSGI file (file name passenger_wsgi.py)
- Step 5: Install Project Requirements
- Step 6: Restart the Flask Application
- Step 7: Open URL
1: Log in to cPanel
Open your cpanel, enter user name and password and wait for panel to open as shown.
2: Create a Python Application
In Search bar type “Python” or manually search for “Setup Python App”. Click on this option and wait for next window.
After clicking on Setup Python App, You will have following window. In my case there are many already running services (don’t worry about that). Now its time to create your first application. Click on Create Application option as shown given image.
After clicking on Create Application, you will enter into another windows as shown below. There will be following options:
- Python Version: You can select from available Python versions
- Application root: It is the physical address to your application on server. This is name of folder where all files and folders related to this project will be stored
- Application URL: This is the public URL, with which people will access this application
- Application Startup File: Passenger_wsgi.py file will be automatically created in folder
- Application Entry Point: This variable will be used in Flask Application
For this tutorial i filled values as shown:
Click on Create, this will take sometime and your service will be ready. In order to verify you should go to File Manager and check that folder and all other files.
Click on File Manager and Search for Project name you have entered in previous step.
Following files and folders will be available there:
3: Flask Application
Now it’s time to create Flask Application. To do this you can upload already coded files by using Upload option or you can create file here as shown:
You will see that file with name “myapp.py” will be created there in same folder. Select edit file option in order to write flask code or make changes in already written code. Write following code
from flask import Flask app = Flask(__name__) # this is the entry point application = app @app.route("/") def hello(): return "Congratulation! You service Hosted on CPanel" if __name__ == "__main__": app.run()
Save file by clicking on save changes (top right corner) and close the file.
4: Configuring our WSGI file
Now edit passenger_wsgi.py file, comment everything and add following line to this file
from myapp import application
If you hit URL “https://skilllx.com/flaskapp/”, you will recieve 500 server error, because you don’t have libraries such as Flask installed. You have to installed libraries first.
5: Install Project Requirements
There are two ways to install project requirements.
- Using Terminal
- Using Requirements File
From terminal follow given steps.
Click on Terminal and once opened, open Python Virtual Environment here. The path of virtual environment can be taken from:
Copy this line just by clicking on line. Paste in Terminal and you will enter Virtual Environment, Now you can install any library you want using pip command such as:
pip install flask
The other way is to create requirements file and upload that file on server and then apply command as shown:
6: Restart the Flask Application
Finally go and restart flask application. Now the service is available.
7: Open URL
Type URL in browser such as Google Chrome (in my case it is “skilllx.com/flaskapp”)