Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Flask app on website

I'm very new in Flask. I work on a simple searching app in dictionary which is saved in memory while the app runs. For testing this application, I use Flask framework which runs a server on a localhost when I run the *.py file. When I start the Flask script, I can see my webpage in a browser and use the application properly.

if __name__ == '__main__':
    setup()
    app.run()

Now, I want to put it on the Internet to have remote access. I know that just copy+paste it to a directory on some hosting which supports Python would not work. I will appreciate any advices how to make this work.

I suppose, that it should be saved somewhere on the host directory and then I should start the app - app.run(I don't know what to type here as host and port). Or is there another way?

I want just type http://[some url where my app runs] and see the html stored in flask templates file.

like image 298
Milano Avatar asked Oct 22 '25 20:10

Milano


2 Answers

The site https://www.pythonanywhere.com/ provides pretty simple setups for Flask applications and doesn't require a whole lot of configuration from you. They have a free package that would allow you to run an application for your own testing purposes.

If you are looking for more production level hosting, you probably want to take a gander at the Flask documentation, which will provide you with some pretty good information on what you need to do to get an app running.

Be warned, though, the documentation is good but provides you with a lot of options, which can be overwhelming to someone just starting out. You'll have to make choices like whether or not you use Apache or go with nginx for your HTTP server.

like image 165
MyCah Avatar answered Oct 25 '25 11:10

MyCah


Rent a Virtual Private Server from some place like Linode, DigitalOcean, Amazon Lightsail, etc.

Install the Apache 2 Web Server, and follow the steps to deploy your Flask app through mod_wsgi:

Add a file to your source code called yourappnamehere.wsgi with the contents:

import sys
sys.path.insert(0, '/path/to/the/application')
from yourapplication import app as application` 

(change the names, obviously).

Copy your source code directory to the path on the server you've indicated.

Create an Apache config file. On Debain/Ubuntu these go in /etc/apache2/sites-available/nameofsite.conf. It should look like this:

<VirtualHost *:80>
ServerName example.com

WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5
WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi

<Directory /var/www/yourapplication>
    WSGIProcessGroup yourapplication
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

By default your code will execute as the Apache user. If you want to override that, optionally set the user and group to a non root user. The paths should point to the copy of your source code on the server. The ServerName should be the hostname which would appear in the URL you want Apache to respond for.

Run a2ensite nameofconffile to activate this configuration. Reload the Apache webserver process to pick up the new config. (This will be something like service httpd reload or systemctl reload httpd).

Finally, rent a domain name from someplace like Namecheap (a registrar). At your registrar, set your Nameserver as instructed by your VPS provider's documentation. At your VPS provider, create an A record mapping your new domain name to the public IP address of your VPS.

This is about as close as it gets to just uploading your Python source code like you would a static website.

It's not that hard, compared to programming. Make sure you run your Linux distribution's security updates regularly. Make sure you disable SSH password authentication and use SSH keys to log in. If you're running a high traffic, high visibility website with sensitive data and/or a high cost related to going down, things get more complicated. For a hobby project, what I described should be sufficient.

But if being responsible for a whole server isn't your cup of tea, you can also fling slightly more money at Heroku, Amazon Elastic Beanstalk, Google App Engine, etc. to supply your Flask app's repository and have it run on servers they manage.

This is really ServerFault territory more than StackOverflow.

like image 37
closeparen Avatar answered Oct 25 '25 11:10

closeparen