5

I am trying to deploy a Flask project on Ubuntu 16 server (Apache2). In this project, I am using Python3 libraries. So I want to set Flask to use Python 3 on the server. But I am really having a terrible time. Here is what I am doing:

sudo apt-get install apache2
sudo apt-get update
sudo apt-get install libapache2-mod-wsgi-py3 # I think that is how you install wsgi for python3
sudo apt-get install python-flask
sudo apt-get upgrade

my project conf: /etc/apache2/sites-available/project.conf

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

and finally: /var/www/FlaskApps/FlaskApps.wsgi

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

# home points to the home.py file
from home import app as application
application.secret_key = "somesecretsessionkey"

NOTE: When I install wsgi like this:

sudo apt-get install libapache2-mod-wsgi

it works, but it uses python2. and When I install wsgi 3. It does not work and it says no module called flask. So how can I set flask to use python3 by default?

I read this question: Getting Flask to use Python3 (Apache/mod_wsgi) but it didn't help me. It is not clear for me because they are using virtualenv.

codeDojo
  • 359
  • 3
  • 16
  • Check if replacing #! /usr/bin/python with #! /usr/bin/python3 helps ? – Sumit S Chawla Mar 05 '18 at 05:42
  • Changing the ``#!`` line does nothing when using mod_wsgi. What version of Python is used is dictated by what mod_wsgi is compiled for. – Graham Dumpleton Mar 05 '18 at 09:04
  • 2
    If you get that ``flask`` can't be imported, it means that the ``flask`` package wasn't installed into the Python 3 installation. If you are using a virtual environment you need to tell mod_wsgi to use it. – Graham Dumpleton Mar 05 '18 at 09:05
  • 2
    You shouldn't install Flask or other python dependencies via `apt-get` at all. Instead, use a virtualenv and use `pip` to install them int here. Using your distro's package manager will give you ancient versions. – ThiefMaster Mar 05 '18 at 13:23

2 Answers2

12

Setting Python 3 as default is what you're looking for I believe.

Running which python shows that 2.7 is the default run ln -sf /usr/bin/python3 /usr/bin/python to make Python 3 the default. That's the step I always take when setting up Flask on a fresh install of Ubuntu.

BrettJ
  • 1,176
  • 2
  • 17
  • 33
  • Glad to assist! – BrettJ Mar 05 '18 at 18:37
  • 1
    Note that doing this will not change what version of Python mod_wsgi uses. It is always necessary for mod_wsgi to be compiled against a specific Python version. – Graham Dumpleton Mar 05 '18 at 21:33
  • 1
    @BrettJ, just noting that the flask_and_apache instructions site you linked is down :( ( I was pretty keen to see them ) – Jesse Reza Khorasanee Feb 12 '19 at 22:42
  • @JesseRezaKhorasanee sorry mate, that site doesn't exist any longer. Long story, but my apologies. – BrettJ May 27 '19 at 01:26
  • 1
    @JesseRezaKhorasanee here's that site from the [wayback](https://web.archive.org/web/20180731174818/http://www.brettjouwstra.me/flask_and_apache/) machine. That okay with you BrettJ? – Bob Stein Jun 03 '19 at 12:06
  • @BobStein Yep, if it's helpful to someone I'm good with it. No promises it still works/works as expected. – BrettJ Jun 03 '19 at 23:31
0

I would recommend you use a fresh virtual env for each project, so if you want to create a virtual environment with python3.7 as your default (it's better to leave python3 defaulting to python3.5 on ubuntu16.04 at least as other programs rely on it),

virtualenv -p python3.7 name_of_your_venv 

after that you can choose between activating your venv before installing your python dependencies ( source ./name_of_your_venv/bin/activate), or you can do it without activating it:

./name_of_your_venv/bin/pip install -r requirements.txt

you also have to change the first line of your .py files from

#! /usr/bin/python 

to

#! /path/to/name_of_your_venv/bin/python 

This will allow you to not have different python packages mixed up potentially causing headaches

Ernest Dujo
  • 61
  • 3
  • 11