Moving a Flask Python and SQLite application from a Windows desktop to Amazon EC2 instance running nginx and gunicorn
After adding a Webex Teams BOT function to a small team performance tracking application I created it was essentially a requirement to move it to a public server with a fixed IP address / DNS name. To keep it low cost and simple I chose to use a free-tier Amazon AWS EC2 instance running Ubuntu. While Elastic Beanstalk is a free service AWS designed to simplify these deployments, the additional features deployed with it (S3, load balancing, etc) would only really complicate management. After a few failures using Elastic Beanstalk I also decided troubleshooting the deployment process wouldn't be a great use of time. With that I deployed the server manually, configured the environment and moved the application manually. Here are the bullet points, not unlike dozens of other tutorials out there:
I am not listing steps to deploy an AWS EC2 instance but here is a sample of what the production server looks like:
- Create a requirements.txt file on the desktop to aid in Flask and python configuration on the target server:
- Decide on the install location o the target server. Here a directory in the default AWS EC2 user ubuntu home directory was used (/home/ubuntu/bttb)
- I SCP'd my files to the bttb folder using WinSCP here. See the picture above.
- update list of available packages:
- upgrade installed packages
- install pip for python3
- install venv to enable virtual environment use
- create a virtual environment
- activate the virtual environment
source venv/bin/activate
- install python requirements
- set environment variable to run Flask app and test
- Flask should now be running the app at your AWS server IP address on port 5000 (default)
- Test web access and fix stuff if necessary. Then stop Flask wit Ctrl-C.
- install nginx and gunicorn to replace Flask as the web framework
- nginx will be the webserver
- gunicorn will be running python code
sudo apt install gunicorn
- setup nginx and guicorn
sudo nano /etc/nginx/sites-enabled/bttb
- the nginx configuration file contents are like so:
server{listen 80;server_name 100.25.168.210;location /static {alias /home/ubuntu/bttb/app_BTTB/static;}location / {proxy_pass http://localhost:8000;include /etc/nginx/proxy_params;proxy_redirect off;}}
- restart nginx
- you should now be able to view files in static folder, served by nginx
- via http://100.25.168.210/static/my_picture.png for example
- the pages requiring python will fail now though
- test static file access and troubleshoot if required
- now, run gunicorn and specify 'workers' where worker is 2 x number of cores on server
- you should know this from the EC2 deployment or find it via
- here the create_app() represents a function in the __init__.py file in the app_BTTB directory
- You should now have access to the python based pages as well as the static content
- test your application access and troubleshoot if required
- now install supervisor to start the application with the server and keep it running
- create a supervisor config file for your app
- the app_BTTB.conf contents are like so. Note you may need to reference the python virtual environment gunicorn path specifically:
[program:app_BTTB]
user=ubuntudirectory=/home/ubuntu/bttbcommand=/home/ubuntu/bttb/venv/bin/gunicorn --chdir /home/ubuntu/bttb 'app_BTTB:create_app()' (note this line may appear word wrapped)autostart=trueautorestart=truestopasgroup=truekillasgroup=truestderr_logfile=/var/log/app_BTTB/app_BTTB.err.logstdout_logfile=/var/log/app_BTTB/app_BTTB.out.log
- create the supervisor log files referenced above
sudo touch /var/log/app_BTTB/app_BTTB.err.log
sudo touch /var/log/app_BTTB/app_BTTB.out.log
- reload supervisor
It is amazing to visit your site. Thanks for sharing this information, this is useful to me...
ReplyDeleteWorkday Integration Course India
Workday Online Integration Course Hyderabad
It is very amazing blog, thanks for sharing this blog! printland offer! Shop for vacuum Personalised Flask and insulated stainless steel flasks online from printland. Order online and get free home delivery and cashless payment options.
ReplyDeleteThanks for Sharing This Article.It is very so much valuable content.
ReplyDeleteDevOps Training
DevOps Online Training
ReplyDeleteAmazing Blog Since the beginning of civilization the practice of gift-giving has been accepted as a beauty of royalty and a solemn oath. Today's generation is within the same track too; the only representation that changed the flamboyance wrap, that was the new addition. Therefore, the fashion of custom-made pens has gained explicit popularity. However, to get a
pierre cardin pen, one has to follow a certain pen print.
See https://serverfault.com/questions/947301/nginx-displaying-failed-13-permission-denied-when-trying-to-access-new-site and sudo -u www-data namei /home/ubuntu/bla/front/dist/vendor.js for issues nginx with permissions
ReplyDelete