Issue in NginX/Passenger/Flask app/Ubuntu 16.04 online Tutorial: setup not working
Issue report
Question 1: What is the problem?:
I want to install Nginx and Passenger on Ubuntu 16.04 to run python apps e.g. using Flask and I run into issues. When I visit my server http://hXXXXXXX.stratoserver.net/ in the browser I am getting:
We're sorry, but something went wrong.
Question 2: Passenger version and integration mode: Passenger open source 6.0.4 + Nginx
Question 3: OS or Linux distro, platform (including version): Ubuntu 16.04 LTS
$ uname -a
Linux hXXXXXXX.stratoserver.net 4.4.0-042stab141.3 phusion/passenger#1 SMP Fri Nov 15 22:45:34 MSK 2019 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
Question 4: Passenger installation method: Nginx + Phusion APT repo
Question 5: Your app's programming language (including any version managers) and framework (including versions): Python 3.7.6 + Flask 1.1.1
Question 6: Are you using a PaaS and/or containerization? If so which one? NA
Question 7: Anything else about your setup that we should know? I have a VPS at strato.nl with Ubuntu 16.04 installed, and the host address is: http://hXXXXXXX.stratoserver.net/ and I was following "Deploying a Python app with Passenger to production" tutorial with the following infrastructure:
Linux/Unix
Nginx
Passenger open source
Python installed via LinuxBrew
Passenger installed on Ubuntu 16.04 LTS
Demo Flask app from github
So the demo Flask app was cloned like this:
git clone https://github.com/phusion/passenger-python-flask-demo.git
Running passenger-memory-stats gives:
$ sudo /usr/sbin/passenger-memory-stats
Version: 6.0.4
Date : 2020-01-29 13:12:15 +0100
------------- Apache processes -------------
*** WARNING: The Apache executable cannot be found.
Please set the APXS2 environment variable to your 'apxs2' executable's filename, or set the HTTPD environment variable to your 'httpd' or 'apache2' executable's filename.
---------- Nginx processes -----------
PID PPID VMSize Private Name
--------------------------------------
23320 1 174.9 MB 0.8 MB nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
23325 23320 174.9 MB 0.8 MB nginx: worker process
### Processes: 2
### Total private dirty RSS: 1.54 MB
----- Passenger processes -----
PID VMSize Private Name
-------------------------------
23309 445.7 MB 2.5 MB Passenger watchdog
23312 672.3 MB 7.5 MB Passenger core
### Processes: 2
### Total private dirty RSS: 9.98 MB
When I run the app locally on the server everything works as expected:
$ python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Then in another terminal:
$ curl localhost:5000
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<style>
html, body {
font-family: sans-serif;
background: #f0f0f0;
margin: 4em;
}
.main {
background: white;
border: solid 1px #c0c0c0;
border-radius: 8px;
padding: 2em;
}
</style>
</head>
<body>
<section class="main">
<h1>Hello world!</h1>
<p>Welcome to the Passenger Flask example app.</p>
</section>
</body>
</html>
So it looks like locally all works fine. However when I visit http://hXXXXXXX.stratoserver.net/ in the browser I got an error page like described above.
The nginx error /var/log/nginx/error.log:
App 29730 output: Traceback (most recent call last):
App 29730 output: File "/usr/share/passenger/helper-scripts/wsgi-loader.py", line 369, in <module>
App 29730 output:
App 29730 output: app_module = load_app()
App 29730 output: File "/usr/share/passenger/helper-scripts/wsgi-loader.py", line 76, in load_app
App 29730 output:
App 29730 output: return imp.load_source('passenger_wsgi', startup_file)
App 29730 output: File "/var/www/demoapp/code/passenger_wsgi.py", line 3, in <module>
App 29730 output:
App 29730 output: from app import MyApp as application
App 29730 output: File "/var/www/demoapp/code/app.py", line 1, in <module>
App 29730 output:
App 29730 output: from flask import Flask, render_template
App 29730 output: ImportError
App 29730 output: :
App 29730 output: No module named flask
It seems the server uses Python2 by default. I need it to use Python3 where I installed Flask. How can I setup the Python version and Python libraries in Nginx/Passenger?
My demoapp.conf is:
$ vi /etc/nginx/sites-enabled/demoapp.conf
server {
listen 80;
server_name hXXXXXXX.stratoserver.net;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/demoapp/code/public;
# Turn on Passenger
passenger_enabled on;
passenger_app_env development;
passenger_friendly_error_pages off;
passenger_user demoapp;
}
What are the passenger / Nginx settings that I need to set to successfully run a Flask application? How can I set Passenger/Nginx to use Python3 and Python3 libraries?
The solution for me was to add passenger_python /home/linuxbrew/.linuxbrew/bin/python3; to the demoapp.conf. So it should look like:
server {
listen 80;
server_name hXXXXXXX.stratoserver.net;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/demoapp/code/public;
passenger_python /home/linuxbrew/.linuxbrew/bin/python3;
# Turn on Passenger
passenger_enabled on;
passenger_user demoapp;
}
This way Passenger is using Python3 and this is where I installed Flask. Maybe you could adit your tutorial to include this information?
Also the Passenger installation for Nginx should not include Apache support.
You should use a python virtual environment and, as you note, the path tug o the python executable can be supplied. However, your comment confuses the issue somewhat. Flask is not a production level tool and you need Passenger and the wsgi application server to replace flask.
Yes, using the virtual environment was the next step but online tutorial does not specify it is required for the Flask demo app as far as I recall. I know Flask is an application framework and I am running a Flask app on Nginx/Phusion Passenger server.