Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tufayellus/deploy-a-flask-webapp-in-vps
How to deploy a flask app in vps like aws or google cloud console, we'll learn from here
https://github.com/tufayellus/deploy-a-flask-webapp-in-vps
aws aws-ec2 django-deploy django-deploy-script django-deployment django-deployment-guide flask-application flask-deploy flask-deployment google-cloud google-cloud-platform mysql mysql-installation mysql-server mysql-setup python-server server-management technical-writing
Last synced: 14 days ago
JSON representation
How to deploy a flask app in vps like aws or google cloud console, we'll learn from here
- Host: GitHub
- URL: https://github.com/tufayellus/deploy-a-flask-webapp-in-vps
- Owner: TufayelLUS
- Created: 2024-09-30T14:20:39.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-20T17:40:44.000Z (17 days ago)
- Last Synced: 2025-01-20T18:33:35.160Z (17 days ago)
- Topics: aws, aws-ec2, django-deploy, django-deploy-script, django-deployment, django-deployment-guide, flask-application, flask-deploy, flask-deployment, google-cloud, google-cloud-platform, mysql, mysql-installation, mysql-server, mysql-setup, python-server, server-management, technical-writing
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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 listsudo apt update
Now we install nginx to use it as a server softwaresudo apt install nginx
Now we install MySQL server, we'll configure it latersudo apt install mysql-server
Now we will start the MySQL service in the backgroundsudo systemctl start mysql.service
Time for installing Pythonsudo apt install python3
and pipsudo apt install python3-pip
As it will be in a production server, we'll use gunicorn for thissudo 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 togethersudo 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 thissudo apt install python3-library-name
For example, if you want to install Flask in AWS EC2, you have to install it like thissudo 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 nowsudo 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> exitmysql -u root -pmysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
exitsudo 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 belowsudo mysqlmysql> 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 commandsudo nano /etc/nginx/sites-enabled/"server IP here"
This will open an editor. Paste this code inside (replace <YOUR INSTANCE IP> with your server IP)
server {
listen 80;
listen [::]:80;
server_name <YOUR INSTANCE IP>;
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 serversudo 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 namedwsgi.py
and place this
from app import appif __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 commandsudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl restart nginxsudo ufw allow 'Nginx Full'gunicorn --workers 3 --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 yourwsgi.py
filesudo pkill -f gunicorngunicorn --workers 3 --bind 0.0.0.0:5000 wsgi:app --daemonThanks 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