Bring your ideas to life with hassle-free web and Android app development.
Hi everyone, In this post I will show how you can host your flask web app on a Linux Server. I’ll show you one of the easiest and best way of doing it, so stay tuned and read till the end. So without wasting time on small talk, let’s just get straight into the topic. We’re going to show the steps for Ubuntu 24.04 server in this tutorial because it is the most widely used distro for servers but you can follow along with any distro you are using as long as you know about which package manager to use. Let me know in the comments if you want a tutorial on the distro you’re using.
Before you begin hosting your Flask Web App
Make sure your server is up-to-date. Run this command in the terminal to update if not done already.
sudo apt update && sudo apt upgrade -y
Make sure Python3, Pip3, Python3-venv is installed in your server. Run this command and install those if they are not already installed.
sudo apt install python3 python3-venv python3-pip -y
Install nginx, we’re going to use it for hosting our flask app.
sudo apt install nginx -y
After that’s done, we can now move to begin hosting our flask web application.
Want us to create your website?
Guess what? You’re in luck because we are a professional web and app development agency.
Steps to host Flask Web Application on Linux Server
Step 1:
First of all you have to make sure that your app.py doesn’t contain the run statement for your application. If it does then remove the run statement from app.py file.
If you are handling Blueprints and using the app.py file just for initializing your flask app and the blueprints
You have to wrap your flask app init code in a function named create_app.
app.py
def create_app():
app = Flask(__name__)
# More of your app and blueprints init code
return app
After doing that, you need to create a new file in your project directory named wsgi.py and write the following code in the file.
wsgi.py
from app import create_app
app = create_app()
if __name__ == "__main__":
app.run()
If all of your application’s functions are inside the app.py file
Create the wsgi.py in the project directory and use this code in the file.
wsgi.py
from app import app
if __name__ == "__main__":
app.run()
Step 2:
Use this command to list all the packages which needs to be installed in requirements.txt file.
pip3 freeze > requirements.txt
Move your project files to /var/www/yourprojectname directory, make sure to replace yourprojectname with your actual project name. To transfer the files you can ftp to the server using FileZilla or use any other method you want. You will need root access to do so keep that in mind.
Step 3:
SSH into your server by using this command and enter your credentials to login to your account.
ssh user@yourserverip
Replace user with your username in the server and yourserverip with your server’s actual IP Address.
Navigate to the directory you have moved your project files.
cd /var/www/yourprojectname
And again, don’t forget to replace yourprojectname to your actual project name.
Then create a virtual environment using this command.
python3 -m venv env
After doing that, activate the virtual environment using this command.
source env/bin/activate
Then install the packages listed in requirements.txt file using the command down below.
pip3 install -r requirements.txt
After that’s done install gunicorn using the command down below.
pip3 install gunicorn
Want us to manage your website?
We have over 7 years of experience when it comes to web development and we are more than capable of taking care of everything your website needs.
Step 4:
Now, you have to create a gunicorn service file in the /etc/systemd/system/ directory. Use the command down below to do so.
sudo nano /etc/systemd/system/yourprojectname.service
Make sure to replace yourprojectname with the actual project name.
Paste the following configuration data into the file and replace yourprojectname to your actual project name and replace root with your username.
yourprojectname.service
[Unit]
Description=Gunicorn instance to serve yourprojectname
After=network.target
[Service]
User=root
WorkingDirectory=/var/www/yourprojectname
Environment="PATH=/var/www/yourprojectname/env/bin"
ExecStart=/var/www/yourprojectname/env/bin/gunicorn --workers 3 --bind unix:/var/www/yourprojectname/yourprojectname.sock wsgi:app
[Install]
WantedBy=multi-user.target
To save and exit out of the nano window, use CTRL+O and then press Enter.
Now you have to give your user appropriate permissions to access the directory in case you’re not using the root user. Use the commands down below to do so. Make sure to replace username and yourprojectname with actual values.
sudo chown username:username /var/www/yourprojectname/yourprojectname.sock
sudo chmod 660 /var/www/yourprojectname/yourprojectname.sock
After doing all of that, use the following commands one by one to start and enable the gunicorn service. Replace yourprojectname with actual value.
sudo systemctl daemon-reload
sudo systemctl start yourprojectname
sudo systemctl enable yourprojectname
Step 5:
Now you have to configure nginx to serve your gunicorn app, to do so, first create a nginx configuration file using the code down below. Replace yourprojectname with the actual value.
sudo nano /etc/nginx/sites-available/yourprojectname
Then paste the following configuration into the file. Replace SERVERIP with your actual server ip and yourprojectname with actual project name.
yourprojectname
server {
listen 80;
server_name SERVERIP;
location / {
proxy_pass http://unix:/var/www/yourprojectname/yourprojectname.sock;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
To save and exit out of the nano window, use CTRL+O and then press Enter.
Enable the configuration using the command down below. Replace yourprojectname.
sudo ln -s /etc/nginx/sites-available/yourprojectname /etc/nginx/sites-enabled
Now run the followings command one by one to test and reload nginx.
sudo nginx -t
sudo systemctl restart nginx
Step 6:
Now you have to allow port 80 and 443 through your firewall to allow incoming connections. This process will vary based on your VPS provider so make sure to search it up and do it.
And after doing that, your site should be live and accessible via http://SERVERIP, replace SERVERIP with your server IP Address.
Ending
And that’s it, I hope you liked the tutorial and your site is live now. Let me know in the comments if you are facing any kind of issues while doing so and I’ll reply with the solution or even make a new blog post if the issue is a big gone.
That’s it for today. Thanks for reading, Bye Bye.