Deployment as web application

Hi Folks,

I have a fair amount of experience with Apache and surrounds, but I am quite novice with Python and a complete fish out of water on the current project: Deploying Trytond as an Apache served Python application.

I’m running Fedora. I have added a user “Tryton” for this purpose and I have installed a virtual environment – /home/tryton/python. I have successfully run Trytond in this virtual environment manually:
trytond -c ~/.config/tryton/7.2/trytond.conf

I have the following trytond.conf, which I don’t suspect to be any problem:

WSGIDaemonProcess tryton user=tryton group=apache threads=5 home=/home/tryton/python
WSGIProcessGroup tryton
WSGIScriptAlias / /var/www/vhost/tryton.tryx.org/tryton_wsgi.py

<Directory /home/tryton/python/bin>
    Require all granted
</Directory>

As referenced above, I have /var/www/vhost/tryton.tryx.org/tryton_wsgi.py:

import sys
import os

# Virtual environment's site-packages to the Python path
sys.path.insert(0, '/home/tryton/python/lib/python3.12/site-packages')

# Tryton application directory to the Python path
sys.path.insert(0, '/home/tryton/python/bin/trytond')

from trytond import config
from trytond.wsgi import app as tryton_server

def application(environ, start_response):
    return tryton_server(environ, start_response)

Based on my research, I think this is all pretty vanilla stuff. When I request http://tryton.tryx.org, I get the disappointing:

**# Method Not Allowed**
**The method is not allowed for the requested URL.**

This is all new and exciting to me, so I’m looking for someone maybe a little less excitable, that has seen this before, and knows what it is and how to stop it.

Thanks for the help,

Chris.

I do not know Apache anymore and I have never deployed Tryton on it.

But you should use trytond.application.app as WSGI application as documented.
You should not import trytond.config but instead you must define the environment variable TRYTOND_CONFIG=~/.config/tryton/7.2/trytond.conf.

Hi Folks,

I have winnowed my errors to one – one persistent bastard! I am getting “Method not allowed” when I try to run under Apache and make a request with a browser. I am also getting “Method not allowed” when I run a test server.

Here is my wsgi.py:

import os
import trytond.application

# Set the configuration file environment variable for Tryton
os.environ['TRYTOND_CONFIG'] = '/home/tryton/.config/tryton/7.2/trytond.conf'

# Create the Tryton WSGI application
application = trytond.application.app

# Optional: For standalone testing
if __name__ == "__main__":
    from wsgiref.simple_server import make_server
    httpd = make_server('', 8000, application)
    print("Serving on port 8000...")
    httpd.serve_forever()

This is my ignorance of Python, pure and simple. I have no idea what I’m doing. I have spent the last few days systematically guessing and I have run out of guesses. (-:

Thanks for the help,

Chris.

This happens usually because the [web] root is empty or not set.

I think the problem is that you import the application before setting the TRYTOND_CONFIG environment.
So no configuration file is loaded.

This was true, but it was not the problem, or at least not the only problem. There was also an SELinux prohibition, which I’ve fixed. For the duration, I have “setenforce 0”, so there’ll be no more of that foolishness.

Regardless of the above corrections, I am still getting “Method not allowed”. I can see in the browser network analysis that a “GET” is performed, but only “POST, OPTION” is permitted. Either I am misconfigred when I “GET”, or the application is misconfigured by not expecting “GET”. Does anybody have any thoughts on this?

I’m pretty sure this has nothing to do with my Apache configuration, unless I am not running the correct application, which would be a first order configuration problem, but also obvious to anybody offering assistance, so I must conclude that the problem is much deeper.

For comparison, the following trivial test python WSGI application works, which uses nearly identical Apache configuration, the difference being the specific script Apache is configured to run in response to “GET /”:

test.wsgi.py

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

I think that this confirms that my httpd.conf (below) is not a problem, however the test is not diagnostic, since the application is trivial and completely visible, whereas Trytond is not my code, and I only barely understand it, although I’m getting there.

httpd.conf (in part)

LogLevel info

WSGIDaemonProcess tryton user=tryton group=apache threads=5 home=/home/tryton/python
WSGIProcessGroup tryton
WSGIScriptAlias / /var/www/vhost/tryton.tryx.org/trytond.wsgi.py
#WSGIScriptAlias / /var/www/vhost/tryton.tryx.org/test.wsgi.py

<Directory "/home/tryton/python">
    Require all granted
</Directory>

I include my trytond.wsgi.py script for completeness, so anybody reviewing this has the complete picture:

trytond.wsgi.py

import os
import sys

# The virtual environment's site-packages to the Python path
sys.path.insert(0, '/home/tryton/python/lib/python3.12/site-packages')

# The Tryton application directory to the Python path
sys.path.insert(0, '/home/tryton/python/bin/trytond')

os.environ['TRYTOND_CONFIG'] = '/home/tryton/.config/tryton/7.2/trytond.conf'
import trytond.application

# Create the Tryton WSGI application
application = trytond.application.app

Thanks for the help,

Chris.

For me this still looks like the [web] root path is wrong or the process has no read access to it.

Hi Ced,

Permission problems on /var/www/vhost/tryton.tryx.org/… will produce 403 – Forbidden, or 404 – Not Found. I have test.wsgi.py in the same place as trytond.wsgi.py, and test.wsgi.py exhibits no permissions problems. I am sure of very few things, but I am sure that I don’t have a permissions problem. Something else is causing this.

My browser network analysis shows that the trytond.wsgi.py script is executing and returning “Method not allowed”, which can only happen if the script is allowed to execute, which exonerates it of permissions problems.

Can I put a “trip wire” in Trytond/application.py to invoke a python debugger? We are getting into python specific responses, which is where I suspect I am going to have to go, but if I can step through the code, then I have a shot at solving this.

Thanks for the help,

Chris.

The werkzeug SharedDataMiddleware send the request to the app if it can not find the file and the werkzeug MapAdapter raises 405 Method Not Allowed if there is a match but not for the right method (there is a root / for POST and OPTION).

So I’m pretty sure that the [web] root path is either wrong or not accessible by the WSGI process (it could be that the configuration is not loaded and so the path fallback to the home of the user).