简体   繁体   中英

How to run a FLASK app on a ubuntu server machine

I am trying to deploy my machine learning model on to a ubuntu-based server to reach it through a browser.

Before deploying I've checked it on my local desktop by running:

env FLASK_APP=project/webapp/app.py FLASK_ENV=development env USE_CUDA=False flask run --host=127.0.0.1 --port=5000

When I run it on my local, I am able to use the app from

127.0.0.1:5000/html.index

Next, I created an ubuntu 18.04 instance. I've installed Apache and Anaconda. It can be reachable from http://193.196.52.255/ this address.

Next, I SSHed to the server and run the same FLASK command there:

env FLASK_APP=project/webapp/app.py FLASK_ENV=development env USE_CUDA=False flask run --host=127.0.0.1 --port=5000

And finally, I opened a browser in my local laptop and tried to connect http://193.196.52.255:5000/html.index but I got

This site can't be reached193.196.52.255 took too long to respond.

error.

To check if the system actually works, I did ssh-forwarding and saw that it was working this time.

ssh -L 5000:127.0.0.1:5000 -i ~/Downloads/mypemfile-bwcloud.pem ubuntu@193.196.52.255
env FLASK_APP=project/webapp/app.py FLASK_ENV=development env USE_CUDA=False flask run

Ufw output of the server is:

$sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Apache Full                ALLOW       Anywhere
Apache                     ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
80,443/tcp                 ALLOW       Anywhere
5000/tcp                   ALLOW       Anywhere
Apache Full (v6)           ALLOW       Anywhere (v6)
Apache (v6)                ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
80,443/tcp (v6)            ALLOW       Anywhere (v6)
5000/tcp (v6)              ALLOW       Anywhere (v6)

In the cloud management portal, I also tried to open related ports: 在此处输入图像描述

My project hierarchy is:

project
|_ models
|_ webapp
       |__ app.py
       |__ static
             |_____ index.html

This is the first time I am trying to do something with web-servers etc and therefore I cannot estimate what could be the problem. Any ideas?

Did you create any WSGI files for Apache? It will not work without it

Can provide you with the following example how to create wsgi file. It's name should be yourprojectname.wsgi

import sys
import os
activate_this = '/var/www/yourprojectname/env/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))
sys.path.insert(0, '/var/www/yourprojectname/')  # where your web projects live in the server in our case it is /var/www in your it could be some other way

from yourprojectname import app as application  # yourprojectname.py - main file of your program

Our routine way to start webapp on server with Apache:

cd /var/www
git clone http://****.git
cd yourprojectname 
virtualenv env
. env/bin/activate
python3.6 setup.py install
cd .. chown apache:apache yourprojectname -R
nano /etc/httpd/conf.d/yourprojectname .conf
service httpd restart

Helpful Oficcial Link about Flask Deployment

Hope it will help you to solve the issue with deployment to server

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