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:
Once this is launched, set the security group configuration to the following:
Next, connect to your instance via SSH, as described when you click the "Connect" button:
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:
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:
Conclusion
Hope you enjoyed the post. Let me know if you run into any difficulties.