How to Deploy Flask app on AWS EC2 Linux/UNIX instance

笑着哭i 提交于 2021-01-29 06:26:39

问题


How to deploy Flask app on AWS Linux/UNIX EC2 instance.

With any way either 1> using Gunicorn 2> using Apache server


回答1:


It's absolutely possible, but it's not the quickest process! You'll probably want to use Docker to containerize your flask app before you deploy it as well, so it boils down to these steps:

  1. Install Docker (if you don't have it) and build an image for your application and make sure you can start the container locally and the app works as intended. You'll also need to write a Dockerfile that sets your runtime, copies all your directories and exposes port 80 (this will be handy for AWS later).

The command to build an image is docker build -t your-app-name .

  1. Once you're ready to deploy the container, head over to AWS and launch an EC2 instance with the Linux 2 machine. You'll be required to create a security key (.pem file) and move it to somewhere on your computer. This acts like your credential to login to your instance. This is where things get different depending on what OS you use. On Mac, you need to cd into your directory where the key is and modify the permissions of it by running chmod 400 key-file-name.pem. On Windows, you have to go into the security settings and make sure only your account (ideally the owner of the computer) can use this file, basically setting it to private. At this point, you can connect to your instance from your command prompt with the command AWS gives you when you click connect to instance on the EC2 dashboard.

  2. Once you're logged in, you can configure your instance to install docker and let you use it by running the following:

sudo amazon-linux-extras install docker
sudo yum install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Great, now you need to copy all your files from your local directory to your instance using SCP (secure transfer protocol). The long way is to use this command for each file: scp -i /path/my-key-pair.pem file-to-copy ec2-user@public-dns-name:/home/ec2-user. Another route is to install FileZilla or WinSCP to speed up this process.

  1. Now that all your files are in the instance, build the docker container using the same command from the first step and activate it. If you go to the URL that AWS gives you, your app should be running on AWS!

Here's a reference I used when I did this for the first time, it might be helpful for you to look at too



来源:https://stackoverflow.com/questions/62731569/how-to-deploy-flask-app-on-aws-ec2-linux-unix-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!