简体   繁体   中英

deploy flask app to amazon linux 2 ec2 instance

I have seen lot of examples on how to deploy a Flask web app to AWS, but all of those examples are using Ubuntu or Red hat instances of the EC2.

Could any one give me the steps to deploy the flask app to an EC2 instantiated from an Amazon Linux 2 (free tier), and using Python3

update

David Buck, thanks for that link, but I am trying to use httpd with mod_wsgi; after going through so many google searches I came up with the following steps:

yum install python3-devel httpd-devel gcc
pip3 install mod_wsgi
mod_wsgi-express module-config >> /etc/httpd/conf/httpd.conf

but with the last command I am getting mod_wsgi-express: command not found

once I pass the above step, I can continue with setting up the flask application

Thanks

I followed the following steps and got the flask application served from an amazon ec2 instance (Amazon Linux 2 AMI t2.micro)

Login to the ec2 instance via ssh, and run the following commands:

01 sudo su
02 yum install python3-devel httpd-devel httpd gcc git
03 pip3 install mod_wsgi
04 mod_wsgi-express start-server

the above command didn't work, but it worked after restarting the instance

05 mod_wsgi-express module-config >> /etc/httpd/conf/httpd.conf

again this didn't work; kept getting permission denier error, even when I run with sudo; so I piped to a temp file, and copied the contents of the temp file into the /etc/httpd/conf/httpd.conf

The contents of the temp file should like the following

LoadModule wsgi_module "/usr/local/lib64/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so"
WSGIPythonHome "/usr"

restart the http service and make sure everything is ok

06 systemctl restart httpd

now for setting up the flask application

07 pip3 install flask
08 car /var/www
09 git clone https://github.com/nshathish/flask-lab2.git
10 cd flask-lab2
11 nano flask-lab2.wsgi

enter the following contents into the above file

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/flask-lab2")
from app import app as application


12 sudo nano /etc/httpd/conf.d/flask-lab2.conf

now copy the following content into the above file

<VirtualHost *:80>
  ServerName flask-lab2.myserver.com
  ServerAdmin root@nshathish.com1
  WSGIScriptAlias / /var/www/flask-lab2/flask-lab2.wsgi
  WSGIDaemonProcess flask-lab2
  <Directory /var/www/flask-lab2>
      WSGIProcessGroup flask-lab2
      WSGIApplicationGroup %{GLOBAL}
      Order deny,allow
      Allow from all
  </Directory>
  ErrorLog /var/log/httpd/error.log
  LogLevel warn
  CustomLog /var/log/httpd/access.log combined
</VirtualHost>

final check

13 curl http://localhost

and if you got Hello World for the above command everything is working fine

if anyone can shed some light into why I couldn't run step 5, it would be really useful; also any improvements to the above steps are much welcome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM