[PYTHON] Run the flask app on Cloud9 and Apache Httpd

Introduction

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.

Time required

This procedure is expected to be completed in 60 minutes.

Apache Httpd settings

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

Installation of required packages

--Installing apache-dev and gcc

$ sudo yum -y install httpd24-devel
$ sudo yum -y install gcc

Install mod-wsgi

--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」

Creating a flask module

--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

Set Apache to use mod-wsgi

--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

image.png

Access and check

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. image.png

If any error occurs, check the Apache log (under var / log / httpd) Check for errors.

in conclusion

Up to this point, I was able to run the flask app on Cloud9 and check it from the browser.

Recommended Posts

Run the flask app on Cloud9 and Apache Httpd
Run the app with Flask + Heroku
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
[Python] Run Flask on Google App Engine
Let's integrate Django and apache (httpd) on Mac! !!
Run Apache2 + WSGI + Flask
Install Apache 2.4 on Ubuntu 19.10 Eoan Ermine and run CGI
Django-Overview the tutorial app on Qiita and add features (1)
Until you run a Flask application on Google App Engine for the time being
How to integrate Apache httpd 2.4 and Tomcat 9 on Cent OS 8
Django --Overview the tutorial app on Qiita and add features (2)
Start data science on the cloud
How to install OpenCV on Cloud9 and run it in Python
Install and run dropbox on Ubuntu 20.04
Preparing to run Flask on EC2
Build a detonation velocity website with Cloud Run and Python (Flask)
Run Pylint and read the results
Detect app releases on the App Store
Deploy Flask app on heroku (bitterly)
The road to installing Python and Flask on an offline PC
[First personal development] The story of deploying the Flask app and Twitter's automatic reply bot on Heroku
Install the latest version of Apache httpd 2.4 from source on Cent OS 8
Work memo to migrate and update Python 2 series scripts on the cloud to 3 series
Run Cloud Dataflow (Python) from App Engine
Run the IDCF cloud CLI with Docker
Use Tabpy with Cloud Run (on GKE)
Link WAS and FLASK applications on docker
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Run flake8 and pytest on GitLab CI
How to install Apache (httpd) on CentOS7
Run py.test on Windows Anaconda and MinGW
How to install Apache (httpd) on CentOS8
Add lines and text on the image
The key to deploying Flask apps using Python Buildpack on Cloud Foundry (Diego)
Web server construction with Apache 2.4 (httpd 2.4.43) + PHP 7.4 on Linux ―― 4. Security (chown and firewalld)
Build a flask app made with tensorflow and dlib to work on centos7
How to install Fast.ai on Alibaba Cloud GPU and run it on Jupyter notebook
Encrypt ACME on Alibaba Cloud: Build an ACME request and sign the JWS payload
Until you install Python with pythonbrew and run Flask on a WSGI server
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction
Install and run Python3.5 + NumPy + SciPy on Windows 10
Until you install Caffe and run the sample
Run django applications on Windows + Apache + mod_wsgi + services.
Run the task in the background on the sshed server
Vienna with Python + Flask web app on Jenkins
Launch and use IPython notebook on the network
Run yolov4 "for the time being" on windows
Use AppSync on the front and back ends
Run Python web apps on NGINX + NGINX Unit + Flask
Easily realize microservices with Cloud Run x Flask
Response the resized image using Flask and PILImage
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
(For myself) AWS_Flask_3 (Install / Run Flask on AWS)
Run OpenVino on macOS and pyenv and pipenv environment
Run Python on Apache to view InfluxDB data
The nice and regrettable parts of Cloud Datalab
[CGI] Run the Python program on the server with Vue.js + axios and get the output data