2

I'm having a hard time deploying a Bottle app. I've tried using some of the suggested answers in past questions but I can't seem to get this working. I end up with a 500 Internal Server Error

This is my set up.

  • Ubuntu 16.04
  • Apache
  • libapache2-mod-wsgi-py3
  • Python 3.5

My .wsgi and app.py file sit at:

/var/www/bottle_app/

  • app.wsgi
  • app.py

app.wsgi is as follows

import os
# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))

import bottle
# ... build or import your bottle application here ...
import app
application = bottle.default_app()

app.py is as follows

from bottle import route
@route('/')
def hello():
    return 'Hello world'

Apache .conf file:

<VirtualHost *:80>
ServerName example.com 

WSGIDaemonProcess bottle_app user=bottle group=www-data processes=1 threads=5
WSGIScriptAlias / /var/www/bottle_app/app.wsgi

<Directory /var/www/bottle_app>
    WSGIProcessGroup bottle_app
    WSGIApplicationGroup %{GLOBAL}
    Require all granted
</Directory>

When I run python3 app.py, nothing is returned (I'm assuming this is expected) When I run python3 app.wsgi I get:

Traceback (most recent call last):
File "app.wsgi", line 3, in <module>
  os.chdir(os.path.dirname(__file__))
FileNotFoundError: [Errno 2] No such file or directory: ''

My Apache error logs show the following errors.

Target WSGI script '/var/www/bottle_app/app.wsgi' cannot be loaded as Python module
Exception occurred processing WSGI script '/var/www/bottle_app/app.wsgi
Traceback (most recent call last):
File "/var/www/bottle_app/app.wsgi", line 7, in <module>
   import app
ImportError: No module named 'app'

I did this on a clean Ubuntu install under user bottle with sudo privileges. This is probably the 10th time I've rebuilt using different suggestions from other questions from users who had similar problems. I was trying to avoid having to post a question that would seem like duplicate. Any help would be greatly appreciated.

404error
  • 575
  • 2
  • 4
  • 16

1 Answers1

3

Before you import your app module in the app.wsgi file, try:

import sys
sys.path.insert(0, '/var/www/bottle_app')

A cleaner way might be to make use of the home or python-path parameters to the WSGIDaemonProcess entry in the Apache configuration.

WSGIDaemonProcess bottle_app user=bottle group=www-data processes=1 threads=5 python-path=/var/www/bottle_app

The __file__ isn't absolute, so if you need to get it's location for this type of purpose (where a controlling process like Apache might be doing funny things with paths) try:

os.chdir(os.path.dirname(os.path.abspath(__file__)))
systemjack
  • 2,815
  • 17
  • 26
  • Thanks for the reply. I made the change you suggested to my app.wsgi and now when I run python3 app.wsgi nothing is returned, I'm assuming all is well there now. My Apache log still show the same errors though. – 404error Apr 27 '16 at 22:37
  • 1
    Another post suggested explicitly including your app directory in the path: `sys.path.insert(0, '/var/www/bottle_app')` [link](http://stackoverflow.com/questions/17678037/running-apache-bottle-python). I'd guess something else wrong with the overall config though. Shouldn't need to be this complicated. – systemjack Apr 27 '16 at 22:47
  • `import sys sys.path.insert(0, '/path/to/application/app/folder')` something like this? – 404error Apr 27 '16 at 22:49
  • Man I cannot express how grateful I am. I added what you suggested to app.wsgi and it worked. Can you change your answer to include sys.path.insert so I can accept it? – 404error Apr 27 '16 at 22:54
  • Glad I was able to help :-) – systemjack Apr 27 '16 at 22:58
  • Originally i was trying to deploy with nginx and gunicorn, that was a whole other headache, I figured this route would be easier. Thanks again, the universe will find a way to thank you! – 404error Apr 27 '16 at 23:00
  • 3
    In general it is bad practice to rely on the current working directory being a specific location in web applications. You should always aim to construct absolute paths for anything you need. If it is in code base you can do that by anchoring off ``__file__`` as shown, but just don't use that to then change the working directory, use it to create absolute paths for the assets etc. – Graham Dumpleton Apr 28 '16 at 01:14
  • @GrahamDumpleton that just went over my head. Is there some suggested reading you think I should do on this topic? – 404error Apr 28 '16 at 16:15
  • It is documented in http://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#application-working-directory – Graham Dumpleton Apr 28 '16 at 16:49