Setting up Flask with Red Hat on AWS

-

Setting up Flask on a new Red Hat Enterprise Linux (RHEL) instance can be initially confusing. You need to make sure that Apache is configured properly, and then you need to set up the Flask server. In this tutorial, I'll walk you through these two steps on an AWS instance.

Prerequisite: Setting up the Red Hat EC2 instance

First, you need to start an Amazon Web Services EC2 instance running Red Hat:

The Red Hat icon on the EC2 launch screen

Once this is launched, set the security group configuration to the following:

HTTP port (80) and SSH port (22) are set up to allow all incoming requests

Next, connect to your instance via SSH, as described when you click the "Connect" button:

The 'Connect' button, which walks you through how to connect via SSH.

1) Starting Apache

First, install and start Apache with the following commands:

sudo yum update

sudo yum install httpd

sudo yum install mod_wsgi

sudo chkconfig —-levels 235 httpd on

sudo service httpd restart

Now let's create a folder called "example", in which we'll put an "index.html" file.

cd /var/www/html
sudo mkdir example
cd example

In this folder, use your text editor of choice to create an "index.html" file. If you navigate to {your public DNS}/example, you should see the page:

A successful Apache configuration

2) Starting the Flask server

Now we just need to set up Flask. First, install:

sudo yum install python-setuptools

sudo easy_install pip

sudo pip install flask

Now we're going to create the Flask "Hello World" app.

In your "example" folder, in a file called "example.py", put the following:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
  return 'Hello from Flask!'

if __name__ == '__main__':
  app.run()

In "example.wsgi":

import sys
sys.path.insert(0, '/var/www/html/example')

from example import app as application

And in "/etc/httpd/conf/httpd.conf", right after the line “DocumentRoot /var/www/html", put:

WSGIDaemonProcess example threads=5
WSGIScriptAlias / /var/www/html/example/example.wsgi

<Directory example>
    WSGIProcessGroup example
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

Now restart Apache:

sudo apachectl restart

Now go to {your public DNS}, and you should see the following:

A successful Flask configuration

Conclusion

Hope you enjoyed the post. Let me know if you run into any difficulties.

Tags: flask red hat rhel python aws tutorial set up flask

See also: Simple Facebook PHP SDK 4 tutorial

Back to all posts

Neel Somani

About the Author

Neel Somani, a student at the University of California, Berkeley, is the founder of Apptic LLC. In addition to computer science, he's interested in philosophy and entrepreneurship. You can follow him on Google+, LinkedIn, and Twitter.

comments powered by Disqus