How can I host two (or more) Python Flask apps on the same OLS server

#1
Hi everyone.

I have 2 Flask applications hosted on different servers and I would like to concentrate them on the same VPS running OpenLiteSpeed. I tried to follow the only tutorial I could find (https://openlitespeed.org/kb/python-wsgi-applications/) to set it up. I created a virtual host and its root is located inside my /home directory. It is structured like this:

/home
|-- /$VH_NAME
|------ wsgi.py
|------ /virtualenv
|------ /app
|------------ __ini__.py

On WebAdmin, I have created for this virtual host the following context:

URI /
Location /home/$VH_NAME/
Binary Path /usr/local/lsws/fcgi-bin/lswsgi
Application Type WSGI
Startup File wsgi.py
Environment:
PYTHONPATH=/home/$VH_NAME/virtualenv/lib/python3.6:/home/$VH_NAME
LS_PYTHONBIN=/home/$VH_NAME/virtualenv/bin/python

It worked just fine. So I created another virtual host and did exactly the same thing. I configured it the same way I did with the first one. Now none of them work. Both return error 500. The browser says they fail because of multiple redirections (I don't understand why).

The stderr.log files of both applications show this error on wsgi.py:

from app import app as application

ModuleNotFoundError: No module named 'app'
They also show multiple consecutive lines like this:

2022-02-23 21:05:15.949401 [101426] packetLen < 0
None of these errors occurred when there was only one virtual host.

Is there anyone who can help me? Thanks a lot!
 

Cold-Egg

Administrator
#2
I set up multiple Django Apps on the same VPS without issue.
May I know if you have set up different domain names for these two virtual hosts?
Before creating the app context in the second virtual host, does the site is still working normally?
 
#3
I set up multiple Django Apps on the same VPS without issue.
May I know if you have set up different domain names for these two virtual hosts?
Before creating the app context in the second virtual host, does the site is still working normally?
Hey, thanks for answering.

I didn't try to use Django because, honestly, I know nothing about it. I skipped the tutorial straight to the Flask part. Either way, yes, before creating the app context in the second virtual host, the first site was working normally.

To be more precise, they both were working normally while the context in the second virtual host was as follows:

URI /
Location /home/$VH_NAME/
Binary Path /usr/local/lsws/fcgi-bin/lswsgi
Application Type WSGI

and the wsgi.py file was:

Python:
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    message = 'It works!\n'
    version = 'Python %s\n' % sys.version.split()[0]
    response = '\n'.join([message, version])
    return [response.encode()]
After that, changed the context, created the __init__.py file inside the /app directory and changed the wsgi.py file to:

Python:
#!/usr/bin/env python
import sys
sys.path.insert(0, os.path.dirname(__file__))

from app import app as application
That's when both stopped working.

Answering your other question: yes, I set up two different domain names for these virtual hosts.

Thank you once again!
 

Cold-Egg

Administrator
#4
What if you try to restart the lswsgi process?
Code:
killall lswsgi
Does any error show in logs?
Code:
tail -f /usr/local/lsws/logs/*
 
#5
What if you try to restart the lswsgi process?
Code:
killall lswsgi
Does any error show in logs?
Code:
tail -f /usr/local/lsws/logs/*
I did restart the process. Now I've reset everything back and did it all over again, with one single difference.

Instead of creating one virtualenv inside each of the virtual hosts directories, ending up with two, I created it on my /home directory, outside of the virtual hosts folders.

So instead of...

/home
|-- /$VH_NAME [1]
|------ wsgi.py
|------ /virtualenv
|------ /app
|------------ __ini__.py
|-- /$VH_NAME [2]
|------ wsgi.py
|------ /virtualenv
|------ /app
|------------ __ini__.py

...it is now:

/home
|-- /virtualenv
|-- /$VH_NAME [1]
|------ wsgi.py
|------ /app
|------------ __ini__.py
|-- /$VH_NAME [2]
|------ wsgi.py
|------ /app
|------------ __ini__.py

On the context of both hosts, I've used the following environment variables:

PYTHONPATH=/home/virtualenv/lib/python3.6:/home/$VH_NAME
LS_PYTHONBIN=/home/virtualenv/bin/python

So far so good. It's working. And now it seems like I've made a stupid mistake.

If anything changes, I'll let you know.

Thanks!
 
#7
Well... here I am again :rolleyes:

For many days, everything has been fine. Now it was time to finally set the DNS record to the new ip.

Surprisingly, once again: error 500!

The log said:

Traceback (most recent call last):
File "/home/crm.polenfranchising.com.br/wsgi.py", line 6, in <module>
from app import app as application
ModuleNotFoundError: No module named 'app'

The only thing I did before was changing the permissions to the VH directory in order to connect to it through FTP to upload the files. Nothing else. So I used chown -R nobody:nobody on it, like is told by the end of the tutorial (https://openlitespeed.org/kb/python-wsgi-applications/).

Coincidence or not, it worked. But how can I upload my files if I can't access the directory by FTP? Would you please give a hint?

Thanks!
 
#9
The website only works when the ownership of the folder is nobody. So I followed the steps on the link and created a new user, changed the ownership of the folder to its group and then changed permissions to allow the group to read and write on the folder. I expected to be able to access the folder by loging on FTP using the new user that belongs to the same group, but it didn't work. When I open it on my FTP, the files inside the folder don't even appear.
 

Cold-Egg

Administrator
#10
Oops, what if you run,
Code:
chown -R sftpuser:wordpress /var/www/html
Just tested it, seems to work for me. It means to change file/folder owner to sftpuser. I am not sure if it's the best method, at least I have no other better idea at the moment.
 
Top