By paveltashev
I tried to configure a python application (based on Flask) on Ubuntu sever 16.04. I used Nginx, uWSGI and I followed this link: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04 but I get error 502.
The uwsgi.log file gives me this error:
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /var/www/dev/develop/api/socket.sock fd 3
Python version: 3.6.7 (default, Oct 21 2018, 04:56:05) [GCC 5.4.0 20160609]
Set PythonHome to /var/www/dev/develop/api/my-api
Python main interpreter initialized at 0x1432e60
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 1313856 bytes (1283 KB) for 64 cores
*** Operational MODE: preforking+threaded ***
added /var/www/dev/develop/api/ to pythonpath.
ModuleNotFoundError: No module named '/var/www/dev/develop/api/app'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 12701)
spawned uWSGI worker 1 (pid: 12705, cores: 8)
spawned uWSGI worker 2 (pid: 12706, cores: 8)
spawned uWSGI worker 3 (pid: 12707, cores: 8)
spawned uWSGI worker 4 (pid: 12708, cores: 8)
spawned uWSGI worker 5 (pid: 12709, cores: 8)
spawned uWSGI worker 6 (pid: 12710, cores: 8)
spawned uWSGI worker 7 (pid: 12711, cores: 8)
spawned uWSGI worker 8 (pid: 12729, cores: 8)
--- no python application found, check your startup logs for errors ---
[pid: 12711|app: -1|req: -1/1] 162.158.210.49 () {58 vars in 1117 bytes} [Sun Nov 25 14:36:42 2018] POST //auth/refresh => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 byte$
announcing my loyalty to the Emperor...
--- no python application found, check your startup logs for errors ---
[pid: 12711|app: -1|req: -1/2] 162.158.210.109 () {56 vars in 1016 bytes} [Sun Nov 25 16:48:38 2018] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches$
--- no python application found, check your startup logs for errors ---
[pid: 12706|app: -1|req: -1/3] 162.158.210.73 () {58 vars in 1021 bytes} [Sun Nov 25 16:48:39 2018] GET /favicon.ico => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes ($
announcing my loyalty to the Emperor...
The content of the /var/www/dev/develop/api/run.py is:
from app import app
if __name__ == "__main__":
app.run()
app.py contains the Flask app.
The content of /var/www/dev/develop/api/uwsgi.ini is:
[uwsgi]
base = /var/www/dev/develop/api
app = run
module = %(base)/app
master = true
home = %(base)/my-api
pythonpath = %(base)
socket = %(base)/socket.sock
chmod-socket = 777
processes = 8
threads = 8
harakiri = 15
callable = app
#vacuum = true
logto = /var/www/dev/develop/api/log/%n.log
The content of /etc/systemd/system/uwsgi_dev_api.service is:
[Unit]
Description=uWSGI dev api
[Service]
ExecStart=/var/www/dev/develop/api/my-api/bin/uwsgi --master --emperor /var/www/dev/develop/api/uwsgi.ini --die-on-term --uid www-data --gid www-data --logto /var/www/dev/develop/api/log/emperor.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
The content of /etc/nginx/sites-available/my-app.conf is:
server {
listen 80;
#listen [::]:80;
real_ip_header X-Forwarded-For;
set_real_ip_from 127.0.0.1;
server_name dev-api.my-app.co;
# Error pages
error_page 401 /custom_401.html;
error_page 403 /custom_403.html;
error_page 404 /custom_404.html;
error_page 400 402 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 426 428 429 431 440 444 449 450 451 494 495 496 497 498 /custom_ERROR.html;
error_page 500 501 502 503 504 505 506 507 508 509 510 511 520 522 598 599 /custom_50x.html;
location = /custom_401.html {
root /usr/share/nginx/html;
internal;
}
location = /custom_403.html {
root /usr/share/nginx/html;
internal;
}
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
location = /custom_50x.html {
root /usr/share/nginx/html;
internal;
}
location = /custom_ERROR.html {
root /usr/share/nginx/html;
internal;
}
# Service
location / {
include uwsgi_params;
uwsgi_pass unix:///var/www/dev/develop/api/socket.sock;
uwsgi_modifier1 30;
}
}
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!
Hi there,
The error message “ModuleNotFoundError: No module named ‘/var/www/dev/develop/api/app’” suggests that the app module cannot be found by uWSGI.
In your uwsgi.ini configuration, the module directive is set to %(base)/app. This directive is telling uWSGI to import the app module from the /var/www/dev/develop/api/app directory, but Python’s import system doesn’t recognize path-like syntax in this context.
There are two possible solutions:
Change the module Directive: Replace module = %(base)/app with module = app. This tells uWSGI to import the app module. Your uwsgi.ini should look like this:
[uwsgi]
base = /var/www/dev/develop/api
app = run
module = app
master = true
home = %(base)/my-api
pythonpath = %(base)
socket = %(base)/socket.sock
chmod-socket = 777
processes = 8
threads = 8
harakiri = 15
callable = app
#vacuum = true
logto = /var/www/dev/develop/api/log/%n.log
Fix the File Structure: Make sure your app.py file is in the directory specified by pythonpath (which is /var/www/dev/develop/api in your case) and that it’s importable as a Python module. This usually means it should be in a directory that contains an __init__.py file.
After making these changes, restart the uWSGI service and check if the issue has been resolved. If it persists, I could suggest checking out this guide here as well:
https://www.digitalocean.com/community/questions/502-bad-gateway-nginx-2
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.