Tutorial

How To Deploy a Flask Application on an Ubuntu VPS

Published on July 4, 2013
author

Kundan Singh

How To Deploy a Flask Application on an Ubuntu VPS
Not using Ubuntu 12.04?Choose a different version or distribution.
Ubuntu 12.04

What the Highlighting Means

The lines that the user needs to enter or customize will be highlighed in this tutorial! The rest should mostly be copy-and-pastable.

Introduction

Flask is a micro-framework written in Python and based on the Werkzeug and Jinja2 template engine for developing web applications. It is intended for developing web apps quickly.

Setup

You need to have Apache already installed and running on your VPS. If this is not the case, follow Step One of our article on installing a LAMP stack on Ubuntu.

Step One— Install and Enable mod_wsgi

WSGI (Web Server Gateway Interface) is an interface between web servers and web apps for python. Mod_wsgi is an Apache HTTP server mod that enables Apache to serve Flask applications.

Open terminal and type the following command to install mod_wsgi:

sudo apt-get install libapache2-mod-wsgi python-dev

To enable mod_wsgi, run the following command:

sudo a2enmod wsgi 

Step Two – Creating a Flask App

In this step, we will create a flask app. We will place our app in the /var/www directory.

Use the following command to move to the /var/www directory:

cd /var/www 

Create the application directory structure using mkdir as shown. Replace "FlaskApp" with the name you would like to give your application. Create the initial directory FlaskApp by giving following command:

sudo mkdir FlaskApp

Move inside this directory using the following command:

cd FlaskApp

Create another directory FlaskApp by giving following command:

sudo mkdir FlaskApp

Then, move inside this directory and create two subdirectories named static and templates using the following commands:

cd FlaskApp
sudo mkdir static templates

Your directory structure should now look like this:

|----FlaskApp
|---------FlaskApp
|--------------static
|--------------templates

Now, create the __init__.py file that will contain the flask application logic.

sudo nano __init__.py 

Add following logic to the file:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, I love Digital Ocean!"
if __name__ == "__main__":
    app.run()

Save and close the file.

Step Three – Install Flask

Setting up a virtual environment will keep the application and its dependencies isolated from the main system. Changes to it will not affect the cloud server's system configurations.

In this step, we will create a virtual environment for our flask application.

We will use pip to install virtualenv and Flask. If pip is not installed, install it on Ubuntu through apt-get.

sudo apt-get install python-pip 

If virtualenv is not installed, use pip to install it using following command:

sudo pip install virtualenv 

Give the following command (where venv is the name you would like to give your temporary environment):

sudo virtualenv venv

Now, install Flask in that environment by activating the virtual environment with the following command:

source venv/bin/activate 

Give this command to install Flask inside:

sudo pip install Flask 

Next, run the following command to test if the installation is successful and the app is running:

sudo python __init__.py 

It should display “Running on http://localhost:5000/” or "Running on http://127.0.0.1:5000/". If you see this message, you have successfully configured the app.

To deactivate the environment, give the following command:

deactivate

Step Four – Configure and Enable a New Virtual Host

Issue the following command in your terminal:

sudo nano /etc/apache2/sites-available/FlaskApp

NOTE: Newer versions of Ubuntu (13.10+) require a ".conf" extension for VirtualHost files -- run the following command instead:

sudo nano /etc/apache2/sites-available/FlaskApp.conf

Add the following lines of code to the file to configure the virtual host. Be sure to change the ServerName to your domain or cloud server's IP address:

<VirtualHost *:80>
		ServerName mywebsite.com
		ServerAdmin admin@mywebsite.com
		WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
		<Directory /var/www/FlaskApp/FlaskApp/>
			Order allow,deny
			Allow from all
		</Directory>
		Alias /static /var/www/FlaskApp/FlaskApp/static
		<Directory /var/www/FlaskApp/FlaskApp/static/>
			Order allow,deny
			Allow from all
		</Directory>
		ErrorLog ${APACHE_LOG_DIR}/error.log
		LogLevel warn
		CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Enable the virtual host with the following command:

sudo a2ensite FlaskApp

Step Five – Create the .wsgi File

Apache uses the .wsgi file to serve the Flask app. Move to the /var/www/FlaskApp directory and create a file named flaskapp.wsgi with following commands:

cd /var/www/FlaskApp
sudo nano flaskapp.wsgi 

Add the following lines of code to the flaskapp.wsgi file:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

Now your directory structure should look like this:

|--------FlaskApp
|----------------FlaskApp
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi

Step Six – Restart Apache

Restart Apache with the following command to apply the changes:

sudo service apache2 restart 

You may see a message similar to the following:

Could not reliably determine the VPS's fully qualified domain name, using 127.0.0.1 for ServerName 

This message is just a warning, and you will be able to access your virtual host without any further issues. To view your application, open your browser and navigate to the domain name or IP address that you entered in your virtual host configuration.

You have successfully deployed a flask application.

Article Submitted by: Kundan Singh

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
125 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

One can simply use tornado and will avoid apache and setting virtual hosts.

Old comment but…yeah you sure can, and i’ve actually been playing with tornado frontended with cloudflare as my nginx/cache/security. It works really well and multi-threading is pretty sweet too. Example deployment with tornado below…

##########

Main

##########

if name == “main”:

# TORNADO WEB SERVER REGISTRATION
server = HTTPServer(WSGIContainer(app))
server.bind(80)
server.start(0)  # Forks multiple sub-processes, 1 per CPU
IOLoop.current().start()

I have followed this exactly, but instead of putting mywebsite.com i put flaskapp.myip in and tried going to http://flaskapp.myip. It came up with a “flaskapp.myip is unavailable or may not exist.” error.

What should I do?

Okay, now I’ve got it to sort of work, but instead of it showing the “Hello I love Digital Ocean” message it just shows me a directory of my files… :/

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 16, 2013

@garethprice: Disable Apache’s default virtualhost:

<pre>sudo a2dissite default</pre>

Edit the virtualhost you created and set ServerName to e.g. <pre> ServerName flaskapp.dev</pre> and restart Apache.

<pre>sudo service apache2 restart</pre>

Edit <pre>/etc/hosts</pre> <strong>on your computer</strong> and add this line to the bottom:

<pre>1.2.3.4 flaskapp.dev</pre>

Where 1.2.3.4 is your droplet’s IP address. Save it and point your browser to http://flaskapp.dev - it should load your flask app properly.

Thanks, Kamal. I’ll give it a go.

But what if I want others to be able to access it?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 17, 2013

@garethprice: I assumed you don’t have a domain name since you set it to something.ip at first. If you own a domain name:

Kamal,

I set up a new droplet and followed this tutorial: https://www.digitalocean.com/community/articles/how-to-launch-your-site-on-a-new-ubuntu-12-04-server-with-lamp-sftp-and-dns

I did what you suggested and all I get is my list of files still. I’ve copied exactly your FlaskApp demonstration. You can see them at: http://opendiscovery.co.uk/FlaskApp/

Sorry for being noobish.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 21, 2013

@garethprice: Try running the following commands:

<pre>sudo a2dissite default sudo service apache2 restart</pre>

Does that fix it?

Yes, thank you! Last question: is there a way of getting the normal apache to load html/php files in the root dir, and wsgi to handle Flask in a subdir (as achieve through WSGIScriptAlias)?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 27, 2013

@garethprice: The recommended way of doing that is to have php/html in a separate directory and your flask app in another directory and <strong>not</strong> mixing both apps together.

Create a separate virtualhost on a separate subdomain/domain for the php/html app.

Hi I have followed the exacts steps as recommended. All the print statements from apache to flaskapp.wsgi to init.py file gets printed. No error reported in apache-error log file From from FlaskApp import views also works fine as it prints the first line in views.py but when I type the url http://ip/hello in the browser and post I get 404.

View.py #from FlaskApp import app app = Flask(name) @app.route(“/hello”) def hello(): return “Hello, World!” init.py from flask import Flask print “Importing 1” #app = Flask(name) app = Flask(‘FlaskApp’) from FlaskApp import views

Can you please guide on how to resolve

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 2, 2013

@itsanjalirk: Please pastebin apache’s config files.

Hi - What is the purpose of creating a FlaskApp directory inside another FlaskApp directory? Is it just for python package management? Couldn’t you just have a main.py file inside one FlaskApp and then

from main import app

?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 17, 2013

@john: It’s so that you can create files such as .wsgi that do not belong to the app itself but are required to make it work.

e.g. FlaskApp.wsgi is created in /var/www/FlaskApp

I am getting an import error:

ImportError: No module named flask

Isn’t the WSGI file supposed to activate the virtual environment we created or something?

If you modify your flaskapp.wsgi as such:

#!/usr/bin/python                                                                
activate_this = '/var/www/FlaskApp/FlaskApp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

Lines 2 and 3 activate the virtual environment containing the Flask module you installed.

When I add those two lines of code, I get

No such file or directory: '/var/www/FlaskApp/FlaskApp/venv/bin/activate_this.py'

error. How can I fix this?

Could it be that you are using Python3?

Please use the command to install the package:

sudo apt-get install libapache2-mod-wsgi-py3

Reference: https://stackoverflow.com/a/64639580/46971

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 18, 2013

@ljernejcic: <pre>ImportError: No module named flask</pre>Flask isn’t installed. Did you follow <strong>Step Two – Install Flask</strong>?

Correct me if I’m wrong but you create a virtualenv and then promptly set up an app which doesn’t use the virtualenv… right?

This worked great for the simple Hello World example. I tried to adapt it to fit my own website, and it’s no longer working. The browser says 500 Internal Error, which turns out to be an ImportError inside the apache logs. I think it’s a different problem from ljernejcic because the ImportError is occurring on my own module.

The apache error.log output is at http://pastebin.com/aRau7TwA.

Thanks in advance for any ideas.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 6, 2013

@brian.schiller: That paste has been removed. Are you still experiencing this issue?

I have used your above described configuration to set up my flask application and I’m experiencing following issue: Basic page (and every static page) is displayed with 404.html message. Only login page is displayed correctly. I did my site similarly as http://www.youtube.com/watch?v=jELLsj1KPNQ; tested on my own linux machine, with flask development server. It works. So, I have renamed main file (the one I called with application name to init.py, as described here. Structure looks like this:

/myapp myappwhatever.wsgi /myapp init.py /templates --------- bunch of html files (with basic html ancestor and childs) /static --------- css module1.py module2.py module3.py

And I made Apache2 and wsgi file according your description. And it does not work as supposed. Do you have any clue why?

Thanks

I can’t for the love of God get my Flask app up and running with my own modules - it works fine with having one .py file in the directory, but as soon as I try to do some “import wtf_forms” or whatever, apache2 spits out an error saying “Module not found” - I have tried with the WSGIPythonPath, “activate_this”, adding the path to the venv.

Why is it not working? I can’t be the only one receiving “ImportError: No module named “XXXX” found”.

(Running python3)

Are your modules installed in a virtual environment? Did you make sure WSGI actually loads the virtual environment?

How can I check if the WSGI is actually loading the virtualenv? I’m pretty sure this is what is causing my problems.

This is the only thing from which I am irritating. Please suggest any answers, it’s a request

i think nginx + gunicorn very easy. i use flask deploy by them

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 15, 2013

@ultrafaca: Are you still experiencing this issue? If so, what do you mean by ‘does not work as supposed’?

Hi Kamal. Still the same issue. My web page is pretty simple. I have links that are pointing to static pages and login page. After login additional links are displayed. My static pages are not displayed properly (they are all inside templates folder); my login link and additional links work fine. Just static pages part puzzles me. Thanks.

Hi Kamal,

I get:

It works! This is the default web page for this server. The web server software is running but no content has been added, yet.

What step should I search for the problem at?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 17, 2013

@dirk.swart: Make sure you followed step 3 and replace mydomain.com with your domain name or your droplet’s IP address.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 17, 2013

@ultrafaca: How are they not displayed properly? Does the content load? Make sure the paths to the CSS files are proper.

@Kamal: Thanks for responding. I reran the commands and at command: sudo a2ensite FlaskApp and reloading apache I now get:

  • Reloading web server config apache2 /usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted) apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for Server Name httpd not running, trying to start (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80 … etc

I used my IP address in the file - Is there any special way I should enter it? With quotes? : instead of . separators?

CSS is loaded. Homepage and other static pages are displayed in “sugar coated mode”, which means following: Links and pictures are displayed, but content of the page is 404. That is done via decorator (in init.py)

@app.errorhandler(404) def page_not_found(error): return flask.render_template(‘404.html’), 404

Now, the 404.html inherits from base.html basic html tags and CSS.

In other module, I have following code:

class Main(flask.views.MethodView): def get(self, page=“index”): page+= “.html” if os.path.isfile(“templates/” + page): return flask.render_template(page) else: flask.abort(404)

I have checked whether the paths are correct via Python interactive mode and they are fine. Everything was working on flask development server. Have any idea where is the problem.

Thanks

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 18, 2013

@dirk.swart: Are you restarting apache as root? It seems like you’re trying to restart it as an unprivileged user.

@Kamal: I run: sudo service apache2 stop and then sudo service apache2 start. But things have got worse - service stopped overnight (don’t know why) and now when I try to start it, it fails:

  • Starting web server apache2 apache2: Could not reliably determine the server’s fully qualified domain name, Using 127.0.0.1 for ServerName Action ‘start’ failed.

??

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 19, 2013

@dirk.swart: Check apache’s error logs: <pre>tail /var/log/apache2/error.log</pre> What does that command output?

On Ubuntu 13.10 and Apache 2.4.6, my virtualhosts configuration in sites-available needed a “.conf” extension.

How can you tell Apache to run this app. using the app.'s virtualenv? Because I have the impression that the setup described here uses the global python installation!

It does use global python, who knows why Digital Ocean tell us to setup a virtual environment and then not how to actually use it!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 30, 2013

I am getting an import error:

ImportError: No module named flask

I followed the instructions to the letter

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
November 26, 2013

@tonybrown67: Please see if the link I posted right above your comment helps.

HI thanks for the great tutorial. I followed the setps and works great, my Flask app run in my server but when y update a file and upload through FTP the changues doesnt reflect on my browser. I had to reboot the server or apache to see my changues, and of course thats not a correct way to update the production server.

Im need help cause i need a worflow to program my app in my computer and upload the changues to the production server without to touch or enter a any single line of code on my server.

In know that i can use git to deploy my updates, but i have no idea to how to do that. Anyone can helpme?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
January 19, 2014

@jc.espinoza27: You will need to reload apache everytime you change a file. Unfortunately that’s how it works. <pre>sudo service apache2 reload</pre>

@Kamal In the site config file /etc/apache2/sites-aviable we can use the directive WSGIScriptReloading On for automatic reloading. Whenever something changes the .wsgi file, mod_wsgi will reload all the daemon processes. And i’m using Fabric and Dstribute to deploy my Flask app has packages

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
January 20, 2014

@jc.espinoza27: Oh, I didn’t know that. I stand corrected :)

Hi. I’d like to deploy more than one flask app with the same IP. To do that, I created, inside the FlaskApp directory, the TestApp directory and did the same procedures showed in this tutorial for this new app.

I created the TestApp: nano /etc/apache2/sites-available/TestApp

Like this:

<VirtualHost *:80> ServerName mywebsite.com ServerAdmin admin@mywebsite.com WSGIScriptAlias /testapp /var/www/FlaskApp/testapp.wsgi <Directory /var/www/FlaskApp/> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

But when I try to access the app in address: http://my_ip/testapp, It can’t be founded.

If I replace the code above in /etc/apache2/sites-available/FlaskApp, I can run the TestApp using http://my_ip/testapp.

So, I’d to know how could I run boths app’s, ie, the FlaskApp and TestApp.

Thanks.

for those with " ImportError: No module named flask", as said here http://flask.pocoo.org/docs/deploying/mod_wsgi/, I added the following lines to my wsgi file : activate_this = ‘/path/to/env/bin/activate_this.py’ execfile(activate_this, dict(file=activate_this))

thanks. Indeed the path to the virtualenv has to be given to mod_wsgi apache.

This step should be included in the tutorial I think:

http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/#working-with-virtual-environments

hence the final flaskapp.wsgi that works for me:

#!/usr/bin/python
activate_this = '/path/to/FlaskApp/FlaskApp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/path/to/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

Thanks for this…solved my problem!

And, of course, to Kundan for the entire post. :-)

It might be nice to add a note that on anything above (I believe) Ubuntu 12.04, you need to name config files with extension .conf

I agree with David. I kept getting the following error message:

sudo a2ensite FlaskApp ERROR: Site FlaskApp does not exist!

The conversation at this link suggested that I add a .conf extension to the FlaskApp file in the /etc/apache2/sites-available/ folder: http://ubuntuforums.org/showthread.php?t=2100563

I followed the steps, but when I go to my domain name, it displays the default index.html file in /var/www/html instead of my /var/www/myapp. How can I switch it over to myapp?

Hmm, I edited /etc/apache2/sites-available/000-default.conf and changed the DocumentRoot, but now when I visit the domain, it just shows the folders of the app. Any help?

Solution to my issue: The file in /etc/apache2/sites-available/ had to end in .conf and the ServerName setting had to begin with www.

@david Yes that should be mentioned: .conf extension for the config file, like:

sudo nano /etc/apache2/sites-available/FlaskApp.conf

Hello,

I tried following this tutorial for deploying my flask application but with no success.

Please have a look at this question

http://stackoverflow.com/questions/24484823/how-to-correctly-deploy-a-flask-application-with-mod-wsgi-on-ubuntu

Thanks

Hello. I am tried to deploy my flask app by wsgi in apache 2 server. But the server could not execute .wsgi file. Have i missed any thing in the configuration

I think you need to update this tutorial, and test it for ubuntu 14.04. I followed it to the letter, twice. I am testing it on a vm, because I do not want to deploy it to a live server until I am confident it will work. One example the command sudo a2ensite FlaskApp is not even recognized. I normally have no troubles with your tuts, the django and rails ones worked perfectly but this one was seriously frustrating. I read the Q&A as well.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
July 17, 2014

@dkearney: Right. This tutorial is for Ubuntu 12.04. The big difference is that the Apache configuration file must end in .conf So this

/etc/apache2/sites-available/FlaskApp

should be

/etc/apache2/sites-available/FlaskApp.conf

Andrew, what about the sudo a2ensite FlaskApp

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
July 17, 2014

@dkearney: That should still work. The command will automatically figure it out with the .conf or without.

Andrew, I edited that post. It needed the .conf in my case. Thank you very much, it worked like a charm. But not quite working yet. I need to step back, and take a break.

Ubuntu 14.04, Apache 2.4.7. I installed Flask over some existing static HTML pages using this guide and renamed FlaskApp to FlaskApp.conf, but for some reason I couldn’t get the Flask page (“Hello, I love Digital Ocean!”) to show up at all. Instead it was the same static HTML that had already been placed there.

I finally solved the problem by disabling the default virtualhost using the commands:

a2dissite 000-default
service apache2 restart

leaving just FlaskApp.conf activated.

I don’t know why it works, and I hope it doesn’t cause any problems in the future, but it does work. Now the flask page appears on my front page.

For Apache 2.4 your FlaskApp (or whatever you called it) VirtualHosts file will need to end with a .conf, otherwise you will get an error that the site does not exist

I followed these steps, but now my website is redirecting to (this tutorial’s equivalent of) /FlaskApp/FlaskApp.wsgi/ and saying the page is not available.

Any ideas why this is happening?

When I try sudo a2ensite FlaskApp, it throws error that “Site FlaskApp does not exist!”

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 18, 2014

On newer versions of Ubuntu, apache virtualhost config files need to end in .conf:

sudo mv /etc/apache2/sites-available/FlaskApp /etc/apache2/sites-available/FlaskApp.conf

if you’re on ubuntu 13 any beyond you have to use the following to disable the stock web page

http://ubuntuforums.org/showthread.php?t=2100563

i have a question, every time i get to the step “sudo a2ensite FlaskApp” it tells me “ERROR: Site FlaskApp does not exist!”. I am at my wits end with this problem.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
October 23, 2014

On newer versions of Ubuntu, apache virtualhost config files need to end in .conf:

sudo mv /etc/apache2/sites-available/FlaskApp /etc/apache2/sites-available/FlaskApp.conf

sudo a2ensite FlaskApp

This comment has been deleted

    sudo a2ensite FlaskApp keeps giving me “ERROR: Site FlaskApp not properly enabled: /etc/apache2/sites-enabled/FlaskApp.me.conf is a real file, not touching it” I have no idea why and it’s ridiculous. :(

    Please update this post to cover Ubuntu 14.04. There are some differences on step three. This thread may help beginners like me.

    http://ubuntuforums.org/showthread.php?t=2100563

    When I try to visit mysite.com it shows app content but when I try www.mysite.com it loads apache2 default page ! How to fix this issue ?

    Where should I see “Hello, I love Digital Ocean!”, I mean what url? Now it displays whats in /var/www/html folder.

    This comment has been deleted

      Oddly specific question. I got my personal website to work on my purchased domain, but i have an odd unix permissions error that maybe you guys can help with. I use Shelve, which is a pretty decent python DB library, and with this tutorial, it’s located in /var/www/XXXX/XXXX/. once i did everything, i kept on getting 500 errors. Found out through the apache error logs that python errors out because of a permission denied error with the db file shelve uses. apparently when apache forks threads, they are given lower privalages, and thus, don’t have permission to write to that file. What should i do for that?

      Hey,

      I’ve been following along and I’m having a little trouble understanding the virtualenv. From what I understand: the purpose of having the virtualenv is so that we can install pacakges to the newly created side python, however, following the commands listed, the flask package gets installed in my /usr/local/lib/python2.7/dist-packages rather than the similar location within FlaskApp/FlaskApp/venv/local/lib/python2.7. Am I missing something? Doesn’t this location of the module contradict the purpose of having a virtualenv?

      Nested directories with the same name make it a complete pain for the user to slightly customize. FlaskApp / FlaskApp… unsure which is which. FlaskApp_a/FlaskApp_b would be far more useful. also makes it clear when invoking the module, since that would be unchanged.

      but how to create many flask application running at the same time ?

      Thanks for this tutorial. I have a question around your virtualenv. I understand virtualenv’s and their purpose and use them when developing. But your configuration…how does your webserver use the virtualenv? Looks to me like you are still installing flask to your main system libraries. I am sure I am missing something. Is the intent to always have your prod webserver use the virtualenv or is it just there for testing?

      I still used these instructions though to get my server going and appreciate it. Well done.

      Originally, I have a PHP website inside /var/www/html/myPHPapp . Hence, I could successfully access it through mydomain.com/myPHPapp

      However, I was trying to add a new flask app and followed the steps in this tutorial, stored the trial flask inside /var/www/FlaskApp/FlaskApp . After all the actions were taken, my original PHP app is very unstable. If I access it through the IP address directly, it showed me a Internal Server Error.

      Could anyone explain to me what I can possibly do to solve this error? Thanks!

      Kamal Nasser
      DigitalOcean Employee
      DigitalOcean Employee badge
      April 9, 2015

      An internal server error means that a PHP error had occurred. It should be stored in the error log file:

      sudo tail -50 /var/log/apache2/error.log
      

      This should output the last 50 lines of the error.log file.

      This comment has been deleted

        @kamaln7 Thank you!

        I found out there might be a clash of port number (80) between the FlaskApp.conf and the default .conf file. I changed the port number for FlaskApp.conf to 99 and the problem was solved.

        I have another question though. After I run python __init__.py , how can I access this page? What URL shall I use?

        Well, step three is wrong I guess… You source the virtual environment but then use sudo pip to install your packages in the globally. That’s why it doesn’t even hurt that you do not load the virtual environment in your .wsgi script.

        which folder contain my python project?

        This comment has been deleted

          I get 500 and don’t understand why. I run tail /var/log/apache2/error.log and it says that no module named flask. I was following instructions but only prepared python 3 instead of 2. I have pip3 and venv created by Python3. I installed flask in venv as well as in the global site-packages. I believe server runs packages from Pythyon2 but not 3 as I had previous app built in Python2 which was totally fine. How can I solve that problem?

          Updata I installed pip and flask on Python 2.7 and it works. How can I configure a server so it runs Python3 ?

          Great Article! Thanks!

          Hello all, In the wsgi file, for the following 2 lines:

          from FlaskApp import app as application application.secret_key = ‘Add your secret key’

          I’m getting an error in the logs saying: ImportError: No module named FlaskApp

          What exactly is it supposed to be importing from, the FlaskApp subdirectory? We didn’t create a FlaskApp file in the tutorial…

          on a separate note, when I type in: deactivate

          it says command not found…could someone guide me as to what I should do about this?

          THANK YOU! mrlevitas

          found my error, the wsgi file wasn’t all lowercase as specified.

          still not sure about deactivate though, or if it’s important to do so

          I’m not able to use the packages installed in my virtualenv while running my web application. The error says “ImportError: No module named …” However, if I directly run the python file in /var/www/, it runs perfectly.

          Great Doc!!! But, there’s a minimal adjust to do… Inside of “xzy.wsgi” file, is necessary include a line that “start” virtualenv.

          Add the following lines to the top of your .wsgi file:

          activate_this = '/path/to/env/bin/activate_this.py'
          execfile(activate_this, dict(__file__=activate_this))
          

          My example file:

          #!/usr/bin/python
          import sys
          import logging
          logging.basicConfig(stream=sys.stderr)
          sys.path.insert(0,"/var/www/myproject_py/")
          
          activate_this = '/var/www/myproject_py/venv/bin/activate_this.py'
          execfile(activate_this, dict(__file__=activate_this))
          
          from project import app as application
          application.secret_key = 'Add your secret key'
          

          Here we have more about VirtualEnv: (Working with Virtual Environments) http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/


          All the others thing works perfectly for me… Great Tutorial and Tks so much!!!

          @garethprice I’ve followed your tutorial etc sudo a2dissite default sudo service apache2 restart but it tells me Site ***doesn’t exist. I’ve added the site on sites-available but still no way out. What should I do?

          Great tut, have successfully deploy the flask application and access it through my IP (xxx.xx.xxx.xx) but the challenge i’m having right now is connecting my domain flask application, i’ve done the setting at my domain end by changing the NameServer to Digital Ocean Nameserver also setup domain under networking on DO but my domain is showing “Apache2 Ubuntu Default Page”.

          Please what can i do?

          I went ahead and made a video of this, installing it on a base install of ubuntu 14.04 x64. It may help clear up some confusion, I had to backtrack once in the video for just a little bit.

          Video => Youtube Setting up flask on a digitalocean droplet

          This tutorial needs a bit renowation. But … let’s try to patch it :)

          Today, january 2016, you are probably interested in python 3

          1. libapache2-mod-wsgi should be libapache2-mod-wsgi-py3

          2. python-pip should be python3-pip

          3. “sudo pip install virtualenv” should be “sudo pip3 install virtualenv” (there is more options for ve but some didn’t work for me)

          4. "sudo pip install Flask " > "sudo pip3 install Flask "

          Now serious stuff …

          apache config between <virtualhost> must have:

          WSGIDaemonProcess <flaskapp> user=flask group=www-data threads=5 python-path=/var/www/<flaskapp>:/var/www/<flaskapp>/<flaskapp>/venv/lib/python3.4/site-packages

          example:

          <VirtualHost *:80> ServerName mywebsite.com ServerAdmin admin@mywebsite.com WSGIDaemonProcess <flaskapp> user=flask group=www-data threads=5 python-path=/var/www/<flaskapp>:/var/www/<flaskapp>/<flaskapp>/venv/lib/python3.4/site-packages WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi <Directory /var/www/FlaskApp/FlaskApp/> Order allow,deny Allow from all </Directory> Alias /static /var/www/FlaskApp/FlaskApp/static <Directory /var/www/FlaskApp/FlaskApp/static/> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

          But this will only work if you have additional 2 lines in flaskapp.wsgi:

          activate_this = ‘/var/www/<flaskapp>/<flaskapp>/venv/bin/activate_this.py’ exec(compile(open(activate_this,“rb”).read(),activate_this, ‘exec’), dict(file=activate_this))

          There is alternative, you can put next line in apache config outside of <virtualhost>:

          #WSGIPythonPath /var/www/<flaskapp>/<flaskapp>/venv/lib/python3.4/site-packages

          Above code was my day … farewell :)

          I want to use Python3. Does this tutorial point falsk to that version?

          No.

          If you’re using Python3, then you need to install libapache2-mod-wsgi-py3 and python3-pip.

          What modifications must i implement to apache and the .wsgi file if I want to host a factory function for application creation instead of a singleton?

          No point of installing virtualenv if you do this:

          sudo pip install virtualenv 
          

          That command installs packages globally.

          Hi, I’m trying to deploy a Flask app using an SQLite database. However, when I go to the website I get a 500 error. My error log says the following:

          File "/var/www/vigilantwebgallery/vigilantwebgallery/utils.py", line 90, in getAllGalleries
            con = sqlite3.connect("imagegallery.db")
          OperationalError: unable to open database file
          

          I’ve changed the permissions of imagegallery.db and the group so www-data can access it, but I still get this error. Any idea why? Thanks,

          Actually, nevermind. I figured out that I need to use the absolute path not the relative one

          This comment has been deleted

            This comment has been deleted

              Does init.py signal the app to look for the local python path instead of the global path? Kind of curious how you tell your app to use the virtualenv instead of the default python path for packages.

              @pcosxlinux I try to use your configuration. I have no error messages, but I only get the default page of Apache2 in my domain. Where could the error be?

              hi All,

              Thank you for the walk-through provided. I am getting the following error when I try to restart apache2 at the last step. The only thing I did different is that I replaced the FlaskApp with parsineynak.

              Thank you in advance.

              Mehdi/Mike

              
              Sep 08 07:13:00 vps92371 apache2[24464]:  * Starting Apache httpd web server apache2
              Sep 08 07:13:00 vps92371 apache2[24464]:  *
              Sep 08 07:13:00 vps92371 apache2[24464]:  * The apache2 configtest failed.
              Sep 08 07:13:00 vps92371 apache2[24464]: Output of config test was:
              Sep 08 07:13:00 vps92371 apache2[24464]: AH00526: Syntax error on line 5 of /etc/apache2/sites-enabled/parsineynak.conf:
              Sep 08 07:13:00 vps92371 apache2[24464]: Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configuration
              Sep 08 07:13:00 vps92371 apache2[24464]: Action 'configtest' failed.
              Sep 08 07:13:00 vps92371 apache2[24464]: The Apache error log may have more information.
              Sep 08 07:13:00 vps92371 systemd[1]: apache2.service: Control process exited, code=exited status=1
              Sep 08 07:13:00 vps92371 sudo[24458]: pam_unix(sudo:session): session closed for user root
              Sep 08 07:13:00 vps92371 systemd[1]: Failed to start LSB: Apache2 web server.
              -- Subject: Unit apache2.service has failed
              -- Defined-By: systemd
              -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
              -- 
              -- Unit apache2.service has failed.
              -- 
              -- The result is failed.
              

              Thats really a great tutorial, thanks my flask web api is up and running.

              Thanks for the tutorial. It works great! However I would like to follow the recommended folder structure as described here http://flask.pocoo.org/docs/0.11/patterns/packages/. The famous Mega Flask tutorials by Miguel Grinberg also follow this structure. With this there is a run.py file outside the app directory with contents:

              from yourapplication import app app.run(debug=True)

              How would I get this type of structure to work with this example?

              i had to install flask outside the virtual environment to make it work. i wonder how come it worked for everyone else here.

              I have followed everything step by step and it works fine with the ‘Hello World’ app you use an example; but, when I upload my own application and try it in my browser, ****the page is loading indefinitely (‘waiting for host…’) ****so I do not get any errors to help me debug. Any idea what this could be??

              Thanks for this amazing article!

              I have the same error. Have you found a solution?

              wsgi error no moduled name mysql.connector even it is fine working with python3 flask.py but when i am using apache server then it showing the error

              Awesome tutorial. The exact ingredient that I needed to cook my flask app on DO’s droplet. Now done with the sweet part, let’s talk about the bitter part. /var/www/FlaskApp/FlaskApp/ Both the folder are having the same name, which caused a lot of confusion. Please rewrite the solution with different folder names so that such problems don’t arise.

              Hi team; i have followed this tutorial to deploy my python, i am stuck with the folwing error :

              [Wed Mar 22 21:13:53.244471 2017] [mpm_prefork:notice] [pid 2883] AH00169: caught SIGTERM, shutting down
              [Wed Mar 22 21:13:54.292692 2017] [wsgi:warn] [pid 3004] mod_wsgi: Compiled for Python/2.7.11.
              [Wed Mar 22 21:13:54.292711 2017] [wsgi:warn] [pid 3004] mod_wsgi: Runtime using Python/2.7.12.
              [Wed Mar 22 21:13:54.294889 2017] [mpm_prefork:notice] [pid 3004] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
              [Wed Mar 22 21:13:54.294910 2017] [core:notice] [pid 3004] AH00094: Command line: '/usr/sbin/apache2'
              [Wed Mar 22 21:14:22.525193 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242] mod_wsgi (pid=3007): Target WSGI script '/var/www/assoapp/assoapp.wsgi' cannot be loaded as Python module.
              [Wed Mar 22 21:14:22.525292 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242] mod_wsgi (pid=3007): Exception occurred processing WSGI script '/var/www/assoapp/assoapp.wsgi'.
              [Wed Mar 22 21:14:22.525353 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242] Traceback (most recent call last):
              [Wed Mar 22 21:14:22.525421 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242]   File "/var/www/assoapp/assoapp.wsgi", line 10, in <module>
              [Wed Mar 22 21:14:22.525509 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242]     from main import app as application
              [Wed Mar 22 21:14:22.525564 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242]   File "/var/www/assoapp/main.py", line 1, in <module>
              [Wed Mar 22 21:14:22.525636 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242]     from flask import Flask
              [Wed Mar 22 21:14:22.525687 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242]   File "/usr/local/lib/python2.7/dist-packages/flask/__init__.py", line 17, in <module>
              [Wed Mar 22 21:14:22.525763 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242]     from werkzeug.exceptions import abort
              [Wed Mar 22 21:14:22.525822 2017] [wsgi:error] [pid 3007] [remote 173.38.220.35:5242] ImportError: No module named werkzeug.exceptions
              

              i have installed probrely the venev and flask is there

              if any one can help please

              thnaks

              Hi guys, I’ve followed this tutorial but without using a virtual environment, I used the same very basic app, but at the end I get a 500 internal server error. Then, using sudo tail /var/log/apache2/error.log I discovered the error was no module named flask. However if I run a python interpreter and import flask there, it imports with no issues. Any help? Extra: My default python and flask are set to those that come with anaconda3

              This comment has been deleted

                Hi!

                Looks like you’re using Python3. I ran into the exact same issue (500 Internal Server Error) despite following all the steps in this tutorial to the letter. I discovered that libapache2-mod-wsgi is a package for Python2. Instead, you need to install libapache2-mod-wsgi-py3. Please let me know if it worked for you!

                Yes this was the exact issue and fix, thanks for the reply. -D

                This just saved my bacon! Thank you so much!

                Thank you very much! this was very handy !

                Hi there! Just one question: What the 2 code lines is supposed to do ?

                from FlaskApp import app as application application.secret_key = ‘Add your secret key’

                thks in advance

                Hey guys. If you are having trouble getting your domain to work check your firewall.

                sudo ufw app list
                sudo ufw allow Apache
                
                # Apache = port 80
                # Apache Secure = port 443
                # Apache Full = both port 80 and 443
                

                I did every step exactly as described, but I get the following error when trying to access it via IP:

                Not Found The requested URL / was not found on this server.

                How can I fix this?

                THANK YOU – since I am taking a class, we MUST do whatever we are doing THIS way. I cannot thank you enough for not only helping, but making sure it (mostly) makes sense to me. One day I’ll be able to recreate this myself because it ALL makes sense. ~Teraisa

                This tutorial is short and concise except one thing which can confuse anyone i.e the name of the folder containing flask application is FlaskApp which is same as the wsgi app FlaskApp. Can’t be they different ?

                Hey Kamaln

                First of all thanks for a super nice tutorial! Its working super good at my droplet, when accessing the ip in a browser. I now used DigitalOcean’s DNS service to set my domain name to point at the ip address. The PROBLEM is now that when accessing the Flask app via the domain example.com i get the directory of the app instead of the front page of the app.

                Do you have any suggestion on how to fix this? I read that the problem is a no existing index.html file in the directory…?!

                found out :)

                how to handle or use static files? I don’t want to use templates all the time and need some static files so as to reduce process resources.

                i tried to deploy 2 instances of flask in a single app. One returning Hello World123!, another returning hello world345!. It was running fine till yesterday but started creating problem since today morning. what could be the problem? It is getting totally freezed. Is there any issye with apache, should I use nginx?

                Hello, I was following this tutorial to the end. However, I hade to make a small adjustment - instead of using ‘python’ I used ‘python3’ instead.

                Despite all the steps described here and restarting the Apache server as the very last step, when I try to reload the web site with an IP address, I still get the Apache2 Ubuntu Default Page.

                Any suggestions what to do in order to get the Flask app up and running? Thank you very much!

                i want do streaming webcam but host doesn’t work please please help me !!!

                Is there any updated method to do the same work? Because I tried to deploy my Flask API using apache2 by taking reference to this blog but was unable to make it run. The server page itself is showing some Internal Server Error, I was able to reach the default apache homepage for server but after integrating my Flask code to it stopped running. Could anyone guide me in doing it. Thanks in advance.

                For no module named error ,

                make sure the file inside FlaskApp/FlaskApp is named as <2 underscores>init<2 underscores>.py

                Hi, thank you for your help, I followed all the steps, but when I open a brwoser with the Ip address of my server, I received the following error:

                403 Forbidden nginx/1.10.3 (Ubuntu)

                Your help will be apriaciate.

                Can I use these above mentioned steps to deploy a flask api in production server so that I can use the API globally?? Please comment on these…

                Great Blog, Don’t You think it would be easy if u use flask-script extension to achieve the purpose. python3 <appname>.py runserver --host 0.0.0.0 -D Just learn how to add flask script extension in ur app. By doing these u have an essay way to edit wsgi file and u can configure ur setup on cloud as well easily.

                Hey, I have tried to implement exact same way but when I am going to the host Ip… I am getting the directory view of WWW, Instead of Print “I love digital ocean”

                Could you help what is the problem ?

                Using this tutorial, I had a few issues. But then I seemed to get through them. For instance I had to specify python3-pip and to use pip it had to be pip3. Also I had to try few times to get Flask installed. Finally I got it and started the server in venv then deactivated venv and proceeded with all the rest of the instructions.

                But then I get Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.

                Oddly enough it has my email so I know it is reading the FlaskApp.conf file correctly.

                Help please?

                Hi thank you very much for this tutorial it was very helpful. One think you might be able to help, I have created an app that runs a crawler with Selenium Driver and Chromedrive. When I run it on the debug mode it runs smooth and works well but when I deploy it behind Apache on a Ubuntu 20.04 server it does not, Chromedriver starts but does not reach url destination on the app and it crashes automatically giving the user a Internal Server Error. Everything else on the app works, the templates, the DB calls but not the chromedriver, not sure is there is any configuration I’m missing on the virtual server or on Ubuntu server, any idea what might be?

                Thank you so much for the help.

                Thanks for this! So, assuming you are able to get this to serve your flask app, I’m confused at what URL I access the application. For example, if I have a subdomain configured for this app:

                sub.mydomain.com

                and a folder structure of /var/www/sub/sub/init.py

                and it serves a route called ‘/test’ - would its url be:

                sub.mydomain.com:5000/test/ ?

                I’m confused about the port numbers, and so far I have not been able to access the app via any URL schema even though the app says it’s running successfully at http://127.0.0.1:5000/

                This article needs to be updated. Fortunately this Youtube channel has the current set up https://youtu.be/YFBRVJPhDGY

                A great article, like so many others provided by Digital Ocean community. Thanks for the contributor. I will follow it with care tomorrow.

                Why are you using sudo for all of these steps? Why do you want your virtual environments to be owned by root?

                Try DigitalOcean for free

                Click below to sign up and get $200 of credit to try our products over 60 days!

                Sign up

                Join the Tech Talk
                Success! Thank you! Please check your email for further details.

                Please complete your information!

                Become a contributor for community

                Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                DigitalOcean Documentation

                Full documentation for every DigitalOcean product.

                Resources for startups and SMBs

                The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                Get our newsletter

                Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                New accounts only. By submitting your email you agree to our Privacy Policy

                The developer cloud

                Scale up as you grow — whether you're running one virtual machine or ten thousand.

                Get started for free

                Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                *This promotional offer applies to new accounts only.