There is a case where you want to run the flask app on EC2 used in Coud9 and verify the operation. Leave the procedure for reference at that time.
This procedure is expected to be completed in 60 minutes.
Apache Httpd is installed on Cloud9 by default. Therefore, only the setting for automatic startup is entered.
--Check the current status of startup settings
$ sudo chkconfig --list httpd
--Auto start setting
$ sudo chkconfig httpd on
--Check settings
$ sudo chkconfig --list httpd
--Installing apache-dev and gcc
$ sudo yum -y install httpd24-devel
$ sudo yum -y install gcc
--Pip version upgrade
The version of pip originally included in Cloud9 is old, so upgrade it.
$ sudo pip install --upgrade pip
--Hash update
I referred to the following article. If you upgrade pip that was built into Cloud9 from the beginning If you do not update the hash, you will get an error that the command cannot be found, so execute the following. Reference: https://qiita.com/kantoku_305/items/6e22e86bba266a650415
$ hash -r
--Installing mod-wsgi
$ pip install mod-wsgi
--Check the import reference
From here, do the following to find the storage location for mod-wsgi.
$ python -c "import sys; print(sys.path)"
['', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/home/ec2-user/.local/lib/python3.6/site-packages', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6/dist-packages', '/usr/lib/python3.6/dist-packages']
--Search with the find command
$ find /usr/lib64/python3.6 -name mod_wsgi
$ find /home/ec2-user/.local/lib/python3.6/site-packages -name mod_wsgi
/home/ec2-user/.local/lib/python3.6/site-packages/mod_wsgi
This will change it to "/home/ec2-user/.local/lib/python3.6/site-packages/mod_wsgi" It turned out to be stored. After this, the storage location of mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so will be used with an absolute path, so Make a note of the following. 「/home/ec2-user/.local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so」
--installing flask
$ pip install flask
--Creating a flask module
$ vi ~/environment/flask/main.py
↓ The following is the contents of the file
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, Flask!"
if __name__ == "__main__":
app.run()
--Creating a configuration file for wsgi
$ vi ~/environment/flask/adapter.wsgi
↓ The following is the contents of the file
# coding: utf-8
import sys
sys.path.insert(0, '/var/www/cgi-bin/flask')
from main import app as application
--Create a directory for flask in a location that is easily accessible in Cloud9
$ mkdir ~/environment/flask
--Create Apache document root
$ sudo mkdir /var/www/cgi-bin/flask
--Place flask module in Apache document root Make it easy to deploy by batch execution when deploying in the future.
$ mkdir ~/environment/flask/tools
$ vi ~/environment/flask/tools/deploy.sh
↓ The following is the contents of the file
sudo cp -ra /home/ec2-user/environment/flask/* /var/www/cgi-bin/flask
sudo service httpd restart
After creating the shell, execute it and place the module.
$ sh ~/environment/flask/tools/deploy.sh
--Create an Apache configuration file
$ sudo vi /etc/httpd/conf.d/flask.conf
↓ Contents of the file
LoadModule wsgi_module /home/ec2-user/.local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
<VirtualHost *:80>
ServerName flask
DocumentRoot /var/www/cgi-bin/flask
WSGIScriptAlias / /var/www/cgi-bin/flask/adapter.wsgi
<Directory "/var/www/cgi-bin/flask/">
options -Indexes +FollowSymLinks +ExecCGI
</Directory>
</VirtualHost>
--Apache restart
sudo service httpd restart
--Added permissions to port 80 to Cloud9 security group
EC2 Dashboard> Select EC2 for Cloud9> Security Groups> Inbound
EC2 Dashboard> Select EC2 for Cloud9> Check Public IP, Access "http: // [Public IP] /" from your browser.
It suffices if "Hello, Flask!" Is displayed as shown below.
If any error occurs, check the Apache log (under var / log / httpd) Check for errors.
Up to this point, I was able to run the flask app on Cloud9 and check it from the browser.
Recommended Posts