How to Deploy a Flask App to Linux (Apache and WSGI)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on welcome to this video where i'm going to show you how to deploy a flask app to an apache web server so let's get into it [Music] all right so to start things off let me show you this super simple flask app i have here on my local environment there's an app.pi file a home.html template file and a styles.css file other than that there's just a pip file here because i used pip env to install the virtual environment and to install flask but let's close these three files and run the app locally so let's have pip env shell to activate the virtual environment and then i'll type in python app.pi because that is the name of my file that has the class app in it and enter so it's running on port 5000 locally as you can see here so i'll open that up in the browser it just renders this one template with a bit of text in it so that's the whole flask app let's get it deployed to an apache web server now i'm going to close the terminal here and i'm going to open up filezilla you can open up any ftp program that you have though so on the left side i have the local project directory and on the right i have ftp into the production web server and because i've already installed apache i have a var www folder for my document root so let's go ahead and create a folder for this website i'll create a directory and i'm going to call it basic blast app you can obviously call this anything you'd like and i'll go into that directory and then i'll drag over static templates pip file and app.pi you don't need to do the log file because we'll actually create a new log file automatically when we install all of our dependencies on the production server and let's create a new directory here for error logs and we'll just call it logs right now let's ssh into the server and install the virtual environment and dependencies there so i've already ssh into the server here and let's see what's in here we have this new directory we created cd into that and here we have the pip file so within the pip file we have information about the dependencies we want to install so if we look at that real quick we can see that we want to install flask as a package and that we require python version 3.9 so if we just try to type pip env install we'll probably get an error that pip emv does not exist so let's install that just like it says here using sudo apt install pep env enter my password great so that finished let's try again so we'll type pip env install and this will use that pip file right so let's hit enter so it could not find a pip file.lock which is fine it will create one you can see it's installing the dependencies from the pip file and now it's completed so we've now just created a virtual environment and installed flask into it in a few minutes we'll need the path to this virtual environment so let me show you how to get that just type pip env dash v-e-n-v for virtual environment and this will tell you where it created that virtual environment so this is the path to it so we'll need this in a few minutes but in the meantime let's go back to the code so to deploy this app we're going to use an apache module called wsgi so we have to create a configuration file for that so let's create a new file here and call it app.wsgi and to start off let's import system and we have to specify the system path to the flask app directory so we'll type cis dot path dot insert zero and then bar www basic flask app because if you remember that is where we put all the flask files on our server and then we have to tell this wsgi module how to activate the virtual environment so we'll type activate this equals and then we have to put the path to our virtual environment so let's go get that again in the terminal so right here copy this whole thing and paste it in and after that path we have to do forward slash bin slash activate this dot pi and then we have to open that so with open activate this which is this path here as file exec file dot read dict equals activate this and i have a little typo here exact there we go so what this is doing is it's going into the virtual environment finding the activate this file and executing it this is the same exact thing as when you create a new virtual environment locally and you activate that virtual environment but apache needs to do that automatically when it runs the site so the last thing we have to do is import our flask app so we'll say from app app meaning this file right here import app because if we look in this file we've named our flask app app if we named our flask app hello world we would have to say from app because that's app.pi import hello world but we didn't name it that we named it app so it's a little redundant but that's why we're doing it this way and then we have to say as application and that's it for our wsgi configuration file so let's upload this to the server if we go back here and refresh we can just upload that perfect so next we have to create an apache virtual host configuration file for the website so let's create a new file here and i'm going to call it basic flask app.conf and i'll paste this in here rather than type the entire thing out and let me just walk through line by line so we want this site to run on port 80 rather than some other random port if you are going to install an ssl certificate then this would be running at port 443 but in this example i'm just going to run on port 80 so it won't have https and here i put my server name my server at the moment is just an ip address i haven't hooked a domain up yet so that's all that is and then we have to say wsgi daemon process and give it a name so i'm just calling it flask app we could call this anything we want but we have that there and there so they have to match and the user that's going to run it is www data same with the group because that's the user and group that apache runs as i'm giving it five threads but you can change that if you'd like depending on your server so then we have wsgi script alias and this is the path to that app.wsgi file that we just created so it's in var www basic flask app slash app.wsgi because once again that's where it lives on the server right here and then here we create a directory block for that flask app directory and then we specify the wsgi process group which is this new group name that we just specified up here wsgi application group is global and then we say allow from all so that this is accessible to random people visiting your website because we want it to be accessible and then i've created an alias for the static directory here so slash static will direct into this static directory and then i've specified that we want to allow all connections to files within the static directory so people can access the css file and any javascript files that sort of thing and finally i've just set up a few custom error logs here so we created that logs directory a few minutes ago and it will spit any errors out into this error.log file so that's all for our virtual host configuration let's upload this to the server so back in filezilla i'm going to go to etc and then apache 2 sites available and i'll refresh on the left side here so now that we've uploaded that configuration we have to actually enable this new site in apache so let's go back to the terminal and type sudo a2n site so apache 2 enable site and then the name of our configuration file which was basicflaskapp.com put in your password and now we just have to reload apache so let's do that and as you can see it failed so let's see if we can find out why so type sudo apache ctl config test to test that configuration that we just uploaded so invalid command wsgi daemon process and that's because apache doesn't know what wsgi is because we haven't installed the wsgi module yet so i'm going to paste in a command to install that here so sudo apt-get install lib apache 2 mod wsgi for python 3. so let's go ahead and install that so when it installs it it will actually enable it as well but if you want to manually enable it or for some reason it didn't you can type sudo a2n mod so apache 2 enable a mod a module and then wsgi but as you can see it is already enabled so let's go ahead and run that config test again to see if we solve the problem all right so we're not getting that error anymore this is just saying that we haven't specified a domain name but that's okay we've gotten rid of that wsgi error so let's restart the server again and let's try to load the app in the browser now so this is the localhost version let's open a new tab and i'll put in my ip address or in your case maybe put in your domain name and hit enter so we're getting an internal server error let's see if we can debug that looking at the error log so we'll go back here and we'll type bim logs error.log so name error file is not defined alright so i probably made a mistake let's go back to the configuration file app.wsgi and here when i said open as file underscore i needed to type file underscore read so let's save that and re-upload right here and let's restart the server again and test it out and there you go we are loading the flask app on a production apache web server so if you do get any other errors make sure to check that logs slash error.log file but this is a pretty simple flask app so there wasn't a whole lot of fancy stuff we had to do all right that's all for this video if it was helpful please make sure to hit that thumbs up button and if you want to see more videos on python flask and web app development in general then make sure to subscribe and i'll see you in the next one you
Info
Channel: Luke Peters
Views: 5,913
Rating: undefined out of 5
Keywords: python, flask, apache2, deploying flask, web app development, how to deploy a flask app
Id: w0QDAg85Oow
Channel Id: undefined
Length: 11min 20sec (680 seconds)
Published: Tue May 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.