Flask Website

CHALLENGE

You've got a blank linux box (mine was hosted on AWS). You want to quickly get a website up and running


Note, if you have an Amazon EC2 instance make sure that it is open to traffic. 

Acknowledgement "https://www.codementor.io/@dushyantbgs/deploying-a-flask-application-to-aws-gnva38cf0" 

We first need to prepare our server so that it is accessible via a browser. 

sudo apt-get install apache2 

Flask uses WSGI to power the website, so we need to install that.

sudo apt-get install libapache2-mod-wsgi-py3 


Now we want to enable WSGI 

sudo a2enmod wsgi 

Where are we going to put our stuff? 

Make 2 subdirectories for your app. And under the last subdirectory, create template and static directories 


Note: People are able to access your static directory very easily via a browser. This is kind of the point... 

Templates contain all of your HTML, styles, code for your pages. 
Nobody can access this. 

Now create the main file for your web page. 
Create a file call __init__.py 

#All of your URL paths have to be in here 
  
from flask import Flask 
  
app = Flask(__name__) #The name of our app 
  
@app.route('/') #This is the homepage 
def homepage(): 
    return "Hello World" 
  
if __name__ == "__main__": 
    app.run() 

Make sure that everything is up to date, or we might hit problems later on 
sudo apt-get update 
sudo apt-get upgrade 

Create a virtual environment in case we want to share our environmen on GIT, or move the website to another server later on 
sudo pip3 install virtualenv 

..now in your application directory, install the virtual environment 


Now switch to your virtual environment 

source venv/bin/activate 


(If at any time you want to leave , type "deactivate") 


Now install flask 
sudo pip3 install flask 

How do we know that eveyrthing is OK so far? 
sudo python3 __init__.py 



Setup IP address, how people can access 

sudo vi /etc/apache2/sites-available/FlaskApp.conf 


Enable this config 
sudo a2ensite FlaskApp 

If you're prompted to reload, do so: 
sudo systemctl reload apache2 

Configure the WSGI file 


Note: Do not ever share this file because of the secret key! Or change it!! 

sudo service apache2 restart #(Note everytime you change any files, you need to restart apache) 













Comments