Skip to content

How to deploy a flask app in vps like aws or google cloud console, we'll learn from here

Notifications You must be signed in to change notification settings

TufayelLUS/deploy-a-flask-webapp-in-vps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Deploy a Flask or Django app in VPS

How to deploy a Flask app in VPS like AWS or Google Cloud console, we'll learn from here

1. Connecting to the VPS using FTP for file uploads

At first, we need to upload all our project files inside a folder in the VPS. You can use any FTP software such as WinSCP in Windows or Filezilla for Linux or Mac or anything else as you prefer.

2. Connecting to the VPS SSH terminal

Now, we have to connect to the VPS terminal using Putty or any other SSH software
Once connected, let's run these commands
First, we update the package list

sudo apt update

Now we install nginx to use it as a server software

sudo apt install nginx

Now we install MySQL server, we'll configure it later

sudo apt install mysql-server

Now we will start the MySQL service in the background

sudo systemctl start mysql.service

Time for installing Python

sudo apt install python3

and pip

sudo apt install python3-pip

As it will be in a production server, we'll use gunicorn for this

sudo apt install gunicorn

3. Installing the library dependency

We need to install the libraries in the server using pip command. If you have a requirements.txt containing the libraries list, you can run this command to install all of them together

sudo pip install -r requirements.txt

If you're using an AWS EC2 instance, it may not allow you to install using the pip command, it will require you to install the libraries using the command like this

sudo apt install python3-library-name

For example, if you want to install Flask in AWS EC2, you have to install it like this

sudo apt install python3-flask

Although some libraries may not work this way, you can use this command in that case (python-pptx is an example of such a library)

sudo python3 -m pip install python-pptx --break-system-packages

4. Setting up MySQL server configuration

Let's configure the MySQL server now

sudo mysql

Create a strong password and use this command and change the value 'password' with your desired password below (only copy the content after mysql>)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> exit
mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
exit
sudo mysql_secure_installation

then follow the instructions and finish. Copy your previously generated strong password. Use this command and change the value 'password' with your desired password below

sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Now you can create your database or import tables/databases as needed from this console. Type exit to exit from the MySQL console.
Note down the login and user information or alter it according to how you want it to work and update your Flask/Django config with the MySQL info.

5. Configuring the server proxy to listen to the Flask port

First, get the server IP address. We'll create a configuration file for the proxy server first. Let's run this command

sudo nano /etc/nginx/sites-enabled/"server IP here"

This will open an editor. Paste this code inside (replace with your server IP)

server {
    listen 80;
    listen [::]:80;
    server_name ;
        
    location / {
        proxy_pass http://127.0.0.1:5000;
        include proxy_params;
    }
}

press ctrl+o to write the changes and press ctrl+x to exit nano editor Now we will restart the nginx server

sudo systemctl restart nginx

6. Setting up gunicorn and deploying our server

For Django tutorial, check Official Gunicorn Tutorial
For Flask, go to the main folder where your app.py or the main python file is. Create a file named wsgi.py and place this

from app import app

if __name__ == "__main__":
    app.run()

Now, back to the terminal again. Run this command (make sure to stay on the same path where you have wsgi.py)

gunicorn --bind 0.0.0.0:5000 wsgi:app

If everything is good, it will show no error and should serve the website (confirm it by accessing the server IP in a browser)
If it's working, come back to the terminal and press ctrl+c to close the process as we're not done yet.
Run this command (change the path correctly)

gunicorn --workers 3 --bind unix:/home/path/to/app/app.sock -m 777 wsgi:app

press ctrl+c to terminate the process again after no output is printing for a moment.

sudo nano /etc/nginx/sites-available/app

Now we use the same path for app.sock from the previous command and paste it inside the editor

  server {
   listen 80;
 
   location / {
       include proxy_params;
       proxy_pass http://unix:/home/path/to/app/app.sock;
   }
}

Press ctrl+o to save the changes and press ctrl+x to exit
run the command

sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
gunicorn --bind 0.0.0.0:5000 wsgi:app --daemon

And we're done! Your website is running perfectly.
To stop and restart your flask app after any changes, run this command after coming to the folder where you have your wsgi.py file

sudo pkill -f gunicorn
gunicorn --bind 0.0.0.0:5000 wsgi:app --daemon

Thanks for reading. If this helps, please star this repository and share it with your friends.
If you need a developer to deploy your web app, reach out to me @ Fiverr