[PYTHON] Deploy Flask servers on virtual machines such as Azure and AWS

To you like this

--I want to build a web server with Python and publish it. --I want to create an API server that uses deep learning

I did a lot of research to build an API server using Flask in a hackathon, so I summarized it from the beginning.

Premise

--Created a virtual machine on Azure, AWS, GCP, etc. --Ssh connection is established --Port 80 is open --OS is Linux (Ubuntu)

environment

--AWS EC2 instance

Main story

1. Installation of required packages

#Install apache2#
$ sudo apt install apache2 apache2-dev

#Python installation#
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
#The last setting displayed~/.Described in bashrc

#Show installable anaconda version
$ pyenv install -l | grep anaconda
#Install anaconda
$ pyenv install anaconda3-5.3.1
#Enable anaconda
$ pyenv global anaconda3-5.3.1

#Create anaconda environment
$ conda create -n <hoge> python=3.6
$ conda activate <hoge>
# flask, mod_wsgi installation
$ conda install flask
$ pip install mod_wsgi

2. Create an application using flask

Create a flask_app directory under/ var / www /and place the following files.

/var/www/flask_app/app.py


from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, Flask!"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

/var/www/flask_app/adapter.wsgi


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

from app import app as application

3. Add the apache2 configuration file

/etc/apache2/conf-available/flask.conf


LoadModule wsgi_module /home/<user>/.pyenv/versions/anaconda3-5.3.1/envs/<hoge>/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGIPythonHome /home/<user>/.pyenv/versions/anaconda3-5.3.1/envs/<hoge>/
<VirtualHost *:80>
  ServerName <server-ip>:80
  DocumentRoot /var/www/flask_app
  WSGIDaemonProcess flask_app user=<user> group=<user> threads=5
  WSGIScriptAlias / /var/www/flask_app/adapter.wsgi
  <Directory "/var/www/flask_app/">
    WSGIProcessGroup flask_app
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptReloading On
    Require all granted
  </Directory>
</VirtualHost>

The above is just an example. Please complete the <user>, <hoge>, and <server-ip> parts with the login user, anaconda environment name, and server IP address (domain), respectively. Also, LoadModule and WSGIPythonHome may differ depending on the environment.

How to find LoadModule

If you execute the following command after enabling the environment where flask is installed, the directory where the package is installed will appear.

$ python -c "import sys; print(sys.path)"

Look for mod_wsgi in it and check the path. Make no mistake if you complete tabs

$ ls /home/<user/.pyenv/versions/anaconda3-5.3.1/envs/<hoge>/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so

4. Activate the added configuration file

$ sudo a2enconf flask

5. Restart apache2

$ sudo systemctl restart apache2.service

Connection confirmation

Access http: // <server-ip> with a browser. (Note that it is not https) If you see Hello, Flask!, You are successful.

If you cannot connect or an error occurs.

--Check if port 80 of the virtual machine is open --You should be able to set it from the server console --If you get an internal error, check the following for the time being.

$ sudo systemctl status apache2.service

$ sudo tail /var/log/apache2/error.log

Future work --SSL compatible

Recommended Posts

Deploy Flask servers on virtual machines such as Azure and AWS
Deploy Flask app on heroku (bitterly)
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Build your Django app on Docker and deploy it to AWS Fargate