How To Deploy A Flask App On AWS EC2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi today i would like to share with you one of my most popular medium articles how to deploy your flask application to aws running on top of ubuntu and python 3. so let's get started okay the first thing you need to do is sign into the console make sure you have an account before starting this tutorial once you're signed in you can find a link to the ec2 dashboard and recently visited services or you can look in all services and click on the ec2 link and then what you want to do you want to launch an instance for this tutorial we're going to be using an ubuntu server uh version 20.04 um running python3 so make sure you select this version and also you select the 64-bit 86 version of that server here we'll be using a type t2.micro mostly because it's free you can use it for a year so let's just select that click on review and launch after that what you want to do is uh edit the security groups and i usually like to select an existing group if i have it so that i don't keep uh building up new security groups as i launch new servers um here you can see that i already have four but we can just create a new one make sure you give your security group a meaningful name so that you know what it does and also give it a description let's just call it what server demo demo security group alright and also you want to make sure that you have a cssh enabled um you also give it an http so you're able to access it from the browser here you can change it to be accessed from anywhere or your ip you might want to do that for your ssh type and also if you're going to add https you want to enable that here too alright so go to review and launch right here um also i like to reuse my keys so i don't keep building a new one every time um i already have one here i'm gonna use web server admin actually let's just create a new one now let's call this server demo and also make sure you download you put it somewhere safe so you're able to find it so launch instances and you have to give it a few minutes you can click on view instances and you'll see that right now it's spending what i like to do also is give it a meaningful name that i know what it is all right and now you can see that is the state is running so we're ready to go the next step is now to connect that to our ec2 instance with ssh the fastest way to do that is to click on the connect button on the ac2 dashboard and locate your um your connection settings um here you'll get three different tabs the one you want this is each client and it'll give you instructions on what to do first things first we want to make sure that we give our private key file the correct permissions and that we move this file to the correct place um so what you want to do you want to open up your terminal i'm using item 2 and you should have a dot ssh directory in your root directory if you don't have one make sure that you uh you create one all you have to do is run this command okay okay there ssh and that will create it for you also note that i'm in my home directory in my machine so that's where you should create that ssh directory um so if you already have one just go into there and uh open that open up that directory um here you can see that i'm using the uh oh command that's a custom command that i added from a machine um you can also find this directory in your machine by um by going to your home directory here so the easiest way to get to this directory is to um use item um you can also use the finder but you don't you won't be able to see it um as you can see you can just click on the on your on the home directory of your machine and you you can't see it yet so the quickest way is to press this this keys on your keyboard so basically you press and hold command shift and then um dot make sure that the finder wind is selected so command shift dot and now you should be able to see it it's right here all right so let's bring the this move our private key file from the desktop and place it here to hide this director again what you have to do is just run the same command again and it'll hide the it'll stop showing the hidden files um as you can see you can do it again if you like all right so now that we have no that we have our private key in the correct place we're going to have to run the this command to change the permissions all right and then it also tells you to um that you can connect with ssh by using this command um you can you can do that through your eye term easily and what you want to do before you run this command make sure you tell it that we put it in the ssh directory and this is how you do it so once you copy and paste that command and then you update it and point it to the correct directory um you hit enter and it'll also ask you um if you want to continue connecting and add that fingerprint to your machine so that it knows that you're allowed to connect to this instance all right so now you're inside your um your freshly launched ubuntu server um as you can tell here is the version um you can just clear that out also as you can see there's there's nothing here alright so the this is one way to connect to your instance i prefer to use terminus uh all right so let let's configure terms um the reason i like to use this is because i can keep um i can save my settings to different instances that i um that i have currently running um and i don't have to um go back to aws and copy all this information that i need so i can just like automatically just save it and click on it and connect so let's let's get that configured so give it a meaningful label um for address what you need is public dns copy that you want ssh username is ubuntu in this case and for password you want to give it a key that we just uh let me just download it um all right so it's it's a private key file so click on file and let's find that in our ssh directory here make sure you hit save all right everything looks good and then now you can just click on it and agree to continue all right so as you can tell we have the same uh sort of a similar interface but now we can use the close this connection click on the link and we'll be able to faster connect our server right once you're connected uh the first thing you want to do is make sure you update the local package index and upgrade your system to the to the latest versions to make sure we don't have any conflicts when we're running our app so just run sudo apt get update allow that finish and the next command do you want to run is sudo app hyphen get hyphen why upgrade um the why here is um so that you won't ask any questions when you're installing the packages that you need um this is a shortcut as you can tell um there's a lot of packages being upgraded um so it might take a few a few minutes depending on the on your machine um so it's a lot of finish okay the upgrade is now complete i'm going to do version 20.04 it comes with python 3 built in but we'll just double check and make sure that we have the latest version um to do that run the python3 command with the v flag and as you can tell we are running the uh some of the one of the latest versions of python 3.8.5 all right once we check that we're running the latest version of python um we want to install pip which is a package manager for python to do that run this command as you can tell i'm using the studio command just so that i have the correct permissions to be able to install these packages and there's a few other libraries that we need to install so use this command to install the the rest of the libraries that we need to have a robust development environment in our machine all right so now python is ready to go we also will be using um get to clone uh one of our projects from github uh directly into the server um and uh this version of undo it's already uh comes built in it's already installed um to double check you can just run this command git version and you'll see that um it's already installed and that's the current version click that all right so now the most difficult step is to install and configure apache in wsgi um so to get that started run the following command and this will install apache into your ubuntu server all right once it's complete uh we can test out that apache is um installed correctly uh we go into the to your instances um public dns so go back to to your chrome browser or whichever browser you prefer paste it in and here you can see the um the apache ubuntu default page this means that it was successfully installed it also gives you some instructions on how to configure your server so we'll go over that next we also want to install wsgi and basically that is a um it is necessary because apache servers don't speak python so it's the interface between um apache and our python obligation just so that it allows that communication to happen um so run this command also um i would like to point out that make sure that you're installing this version of wsgi for python3 otherwise you might run into some errors related to your environment so don't don't don't install the don't install this version and this step also automatically enables wsgi so you don't have to worry about that all right let's quickly build a simple flask app so that we um make sure that everything is as basic as possible or as simple as possible so that we minimize the number of errors we run into when configuring our server and we'll be able to make sure that everything is working correctly um so it just makes things easier first things first let's just create a directory um and i'm creating this directory in the ubuntu machine root root directory and this is the name of the project that we're going to be using in this tutorial so just name it microservices.com or whatever the name of your project is all right um next we want to create a sim link to the um apache site root that's defined in the configuration just hit enter all right okay so now now city into that directory and you can see current is currently empty so let's install a flask with pip3 and as you can tell i'm using studio um because i need the correct permissions to be able to install these packages and make changes to the server so make sure you're running all these commands with sudo okay so now i'm going to add a production wsgi file you can name this development or local whatever you like but i think it's easier just to follow this tutorial step by step and not change things yet just to make sure that you're uh you don't run into extra trouble all right and also i i want to mention that i'm using them to edit files so that's what i'm doing with that command vi all right so now open that wsgl file with vm and copy and paste copy and paste this configuration um to to exit um i'm going to press the escape key and then column wq and what that that'll do it'll save your changes if you don't want to save them you can just press the q key on your keyboard and add the exclamation mark at the end all right so hit enter exit that okay let's just create a simple flask application once again open that with them and copy and paste the following also um this um this tutorial or the written version of this tutorial it's uh in the link below uh so make sure you have that open so you can actually and quickly just copy and paste okay now once we have uh those two files ready to go uh the next step is to uh configure our virtual host so that they know where our application is running um so actually cd into your directory and now we're going to make some changes to the apache configuration files which is located and this bath so this is the default um virtual host for apache now we won't be using this so you can go ahead and close that and we're going to create our own i'm going to name this micro services.com hit enter all right and once again um you want to add the following to this file as you can tell i have this placeholder values right now so don't worry about this right now um what you want to [Music] focus on is document root which points to the apache root server and some link that we created microservices.com the damn process it's also set to the name of the application that we're building and the script alias wsgi which is pointing to a production wsgi also you want to tell it the numerous directories here let's go microservices.com you want to add that here to wsgi group to edit this file if you have to just press the i key on your keyboard and that'll put you in edit mode i recommend that you open up a vim cheat sheet or just quickly google have to use them okay so once you make those changes go ahead and save this file now we're gonna disable the apache default config run this command and it'll tell you that it has been disabled and then let's activate our new configuration just like i told you right here so as you can tell i run into an authentication error here so so we need to run that with two all right next we want to reload apache all right so now let's go back to our browser and make sure that our application is running so just refresh the page and you will see hello i love flask been displayed on your page which is the same thing that we have our hello route returning so at this point you can you know that everything is working well okay at this point you should have a simple flask application running in your aws server on my next video i'll go over on how to deploy a more complex flask application for now feel free to comment or connect on twitter if you have any questions and also don't forget to subscribe thank you for watching
Info
Channel: jQN
Views: 1,164
Rating: undefined out of 5
Keywords: aws, flask, python3, ubuntu, step by step, jqn, deploy, apache
Id: LAApT542Rtk
Channel Id: undefined
Length: 30min 14sec (1814 seconds)
Published: Sat May 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.