How to Deploy Flask with Gunicorn and Nginx (on Ubuntu)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm going to show you how to take your development flask app and deploy it to a production server and the way we're going to do this is with something called G unicorn which is a web server Gateway interface that's going to interface with your flask cap and on the other side of that you're going to have an nginx proxy server so basically a user somewhere on the Internet is going to visit your website via the nginx proxy server that's going to talk to G unicorn which is going to talk to your flask app execute the python code and send it back down the chain this is part of a two-part video series the first part was making that flask app I'm going to assume that you already went through that tutorial or you have a flask app that you want to deploy otherwise I have code on GitHub which is linked below that you can catch up with us as part of this tutorial so otherwise the only change that I made since the previous video is I um while I'm working with vulture and this is the IP address of my server so all I did was I went into Google domains which is where I registered tonybony.com and created in a record that maps tonybony.com to the IP address so before we are accessing our flask app via the IP address directly and now if I start the server back up instead of using the IP address which should still work let's type in tonybony.com Colon 5000 instead and we will serve the same exact web page okay so let's get on to the tutorial the first thing I'm going to do let's get out of here um as you see we are using the root user which is not a good idea for production servers whether that's flask app or something else let's make a new user and run the app under that new user okay so we're going to do add user Tony and that's going to add that user ask for your password for that user I'm going to type in a password and I'll confirm it give them my name and I'm just going to leave the rest of this blank that looks good and if this user needs pseudo privileges which we will to install some packages we can do user mod Dash a capital G sudo and then the name of the user hit enter and now that user has pseudo privileges to make sure this user can log into the remote server via SSH let's just double check inside of our Etc SSH SSH D underscore config file and then here's there's something called password authentication uh let's set that to yes so that way the user that we just created with this password can log into the server and to apply those changes we will do a system CTL restart SSH D and now we can exit out of the server as root and log back in with our new user so SSH Tony at in this time we don't have to use the IP address we can use the domain name if you're following along so Tony bony.com hit enter type in that user's password and now we are logged in as the new user to our remote Ubuntu Server now like I was saying I have some code up here on GitHub at github.com tonyflow if you go into my repository section you can clone the flask app demo that we've been working on up until the previous video so just come in here copy that URL and then open up your terminal window and we'll do git clone that URL and we will clone it into the hike app just like we did before so that's going to download all that code now we have a hike folder and um because this is a new user we have to set up our python virtual environment again so let's do that next let's do Python 3 Dash m v e n v and then we're going to make that virtual environment inside of EnV folder in our desktop called Teton t-t-o-n and when that's finished create it being created we can activate the virtual environment source EnV Teton bin activate all right we are now inside of our virtual environment from this point any from this point forward any python or pip commands will be self-contained in this environment so that being said let's do pip install flask because we will need that and with that installed let's go into our hike directory and let's bring it up Python peak.pi and let's just make sure everything's working out so we will refresh the page over here and yep there we go let's go climb a mountain all right next let's set up the web server Gateway interface with G unicorn so let's control C to get out of our development server and we'll do a pip install G unicorn hit enter and that installs really quick so inside of our hike directory right next to peak.pi let's make a web server Gateway interface.pi file and this is going to be very similar to let me just show you over here so as a reminder in peak.pi we have from flask import flask and then if name equals main we do this we're going to do something very similar in our web server Gateway interface so from Peak import app okay so let's let's review from this in in this directory there's a file called Peak and inside Peak we Define an app variable okay so that's all that's doing we're importing that app variable here and then if like we did before if name equals Main we're going to run our app app dot run and we're going to run the app without any of those other host names or port numbers before we were doing the development server stuff here but now we are running the app directly and now after this we're going to set up the infrastructure to handle that in a production environment so but let's make sure G unicorn's working we'll run G unicorn dash dash bind 0.0.0.0 colon 5000 kind of like we were doing before and then we're going to specify this web server Gateway interface file that's in this current directory colon app which we import it into that file so let's run that and let's see if our website still loads okay there you go so let's go back here get out of our development server and deactivate our python virtual environment now what we're going to do is set up the system process system service file that's going to basically point to our python virtual environment and point to our flask Gap so that way if the server ever reboots or the process crashes in some cases it'll automatically bring it back up and we can access the system CTL start stop restart status commands as I'll show you in just a bit so let's go ahead and do that um let's make a new file in the ETC system D system directory and we'll call it Peak dot service okay in here rather than uh typing everything in front of you I'm gonna just copy and paste them code this code is also available on the GitHub and I'll just walk through it line by line to show you kind of what's going on so this is just a description of the the service that we are creating that's the unit G unicorn instance to serve Peak flask app it's going to start up after the network is ready and then the service itself is going to be run under the user Tony which we just created and the group www data which is a standard group for serving HTTP requests and the working directory is going to be our home directory slash hike which is our flask app the environment is going to be our python virtual environment named Teton and then inside of there's the bin directory and then the actual command that's going to execute is the G unicorn command which we kind of experimented with just a bit ago it's going to spin up three workers or three separate processes it's going to bind a Unix socket a peak called Peak dot sock which is going to be created in our flask app root directory and as we saw before we have that new file in there called web server Gateway interface and inside of there there is an app variable so that's basically what that means and then this is a install wanted by multiple user Target that's I believe that's so this is accessible for every user in the system so let's save that file and what we're going to do is start it up so sudo system CTL start Peak and if you get this kind of error message you can just reload as it's helping you out here so sudo system Daemon reload and then we'll try that again start the P CAP and then we can do pseudo system CTL enable Peak and what enable does is it's going to start this process automatically when the system starts up and then we'll do pseudo system CTL so status Peak and everything looks pretty good it's active and running so the next thing you want to do since we're taking care of the flask app we've taken care of the web server Gateway interface let's go ahead and set up the nginx web server the proxy server so we can do that with sudo apt install nginx and as is standard with most nginx deployments we'll make a configuration file with are at the location of Etc nginx sites available and we'll call it peak.comf so inside of the server block we're going to listen on Port 80 instead of the 5000 development Port that we used before since I have a domain name I'm going to specify that here the non-verse WWE version and the www version and then the location we're going to map it to slash so there's no path after you go to tonybony.com we're going to include the standard proxy parameters and this is the Unix socket that is going to be created inside of our flask app and it's going to be called peak.sock which we referenced before in the system the systemd file so let's save that and also as is standard with most nginx deployments we'll Create a Sim link from the nginx sites available directory to the nginx sites enabled directory and what this does is basically publishes those changes as far as nginx is concerned now we can test out our configuration file to make sure there's no errors with the configuration with sudo nginx-t and everything to come back comes back looking good and finally we will restart nginx with system CTL okay before we test anything out we have to remember that we have a firewall running uh let's check it out sudo ufw status right now we have the development Port open let's close that back down so we'll do sudo ufw delete allow 5000 and there's a cool shorthand here sudo ufw allow nginx full and what that's going to do is basically allow Port 80 Port 443 all the standard HTTP ports so if we do sudo ufw status you'll see that we have our standard SSH port and then nginx ports as well all right so our app is running nginx is up and I think it's time to test that out so let's go ahead and refresh this actually no we're gonna let's go directly get rid of this port Go to http colon slash our domain name or IP address hit enter and we get a 502 bad gateway error if you do run into this issue it's a permissions issue and I'm going to show you how to debug it if we look at the if we use the tail command which just shows the end of a log file at VAR log nginx and there's and then there there's a file called error.log we'll see that let's see permission denied when it was trying to access the socket file okay so we can change that really easily in with the chmod command we have to change the permission of the home Tony directory since we're serving it out of there to 750 775 excuse me so with that one simple change we can refresh the page and now our flask app is being served via nginx VIA the web server Gateway interface called G unicorn that's about it for this one if you want your flask app to work over https you can get a free SSL certificate with let's encrypt and this video should point you in the right direction thanks guys subscribe and I'll see you over there
Info
Channel: Tony Teaches Tech
Views: 40,510
Rating: undefined out of 5
Keywords: deploy flask, deploy flask app, deploy flask app nginx, deploy flask app with gunicorn, deploy flask app with nginx, deploy flask app with nginx using gunicorn, deploy flask gunicorn, flask deploy, flask deployment, flask gunicorn, flask gunicorn nginx, flask nginx, flask nginx gunicorn, gunicorn flask, nginx flask, nginx gunicorn flask
Id: KWIIPKbdxD0
Channel Id: undefined
Length: 13min 49sec (829 seconds)
Published: Thu Mar 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.