Question

i cant get litespeed django venv to wrok .

Posted May 6, 2019 2.2k views
DjangoUbuntu 18.04

I am using the Django OpenLiteSpeed One-Click app. I can’t get OpenLiteSpeed beyond the example site.

I get 503 or it just keeps loading. In the log I get:
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named ‘encodings’

All I need to do to break it is set the example to use Mysql.

If you change back to sqllite3 it works fine. I know it work because if i use the django test server “python manage.py runserver 0.0.0.0:8000” so the venv is setup correctly.

I have tried adding to the context:
PYTHONHOME=/usr/local/lsws/Example/venv/
LS_PYTHONBIN=/usr/local/lsws/Example/venv/bin/python
Any Ideas?

1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
11 answers

HI

How about keep PYTHONHOME=/usr/local/lsws/Example/venv/ only in the Environment?

Best

Hi,

Yes that was the 1 st thing I did. I just tried again and I get the same result.

Understood, we will try to reproduce it locally first and update you soon.

The only way I could get things to work was to remove PYTHONHOME=/usr/local/lsws/Example/venv/ then copy all my modules to /usr/local/lib/python3.6/dist-packages.
This is a work around and not using venv.

Hi,

Could you please provide a detail step for how to trigger/reproduce it ?

All I need to do to break it is set the example to use Mysql.

I set it to mysql , but it works on my test server.

Best regards,

Below are the steps to break it. I just tried this whit the same results. Can you provide the context of you tested and the settings.py. I would like to see it.

Here are the steps.

cd /usr/local/lsws/Example/

virtualenv -p python3 venv

source venv/bin/activate

pip install django

django-admin startproject testapp

vi testapp/testapp/settings.py

change
ALLOWED_HOSTS = [’*’]

add
STATICURL = ’/testapp/static/’
STATIC
ROOT = ’/usr/local/lsws/Example/testapp/public/static’

cd testapp

mkdir -p public/static
python manage.py collectstatic

chown -R nobody:nogroup /usr/local/lsws/Example/testapp

Set up Context
Type = App Server

URI = /testapp/

Location = /usr/local/lsws/Example/testapp

Binary Path = /usr/local/lsws/fcgi-bin/lswsgi

Application Type = WSGI

save and recycle litespeed

goto http://xxxx/testapp/admin

test and it will work

edit Context add

Environment = PYTHONHOME=/usr/local/lsws/Example/venv/

save and recycle litespeed

test and it will work

Now for the fun.

vi testapp/testapp/settings.py

comment out the sqllite3.

add

DATABASES = {
‘default’: {
'NAME’: 'testappdb’,
'ENGINE’: 'mysql.connector.django’,
'USER’: 'testuser’,
'PASSWORD’: 'testuser’,
'HOST’: '10.10.10.10’,
'OPTIONS’: {
'autocommit’: True,
'charset’: 'utf8mb4’,
},
}
}

pip install django-mysql

pip install mysql-connector-python==8.0.5 (I use this version because the newer ones have too many problems)

test and it will give 503 or 500 or just spin.

python manage.py runserver 0.0.0.0:8000

goto http://xxxx:8000/testapp/admin

This will work.

Hi,

I followed your steps , however I noticed that you miss one conf

below context setting up “Application Type”, the line “startup file” , could you please add this ?

testapp/wsgi.py

this is my settings.py , the part about DB

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'django',
        'PASSWORD': '123456789',
        'HOST': 'localhost',
        'PORT': '',
        'OPTIONS': {
        'autocommit': True,
        'charset': 'utf8mb4',
    },
}

Did you run manage.py migrate?

no 500 or 503 error , however if I remove that startup file line , I do have 500 error

(venv) root@openlitespeed-django-s-1vcpu-1gb-ams3-01:/usr/local/lsws/Example/testapp# curl -I -XGET http://xxx.xxx.xxx/testapp/admin/
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: /testapp/admin/login/?next=/testapp/admin/
Expires: Thu, 09 May 2019 13:40:58 GMT
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
X-Frame-Options: SAMEORIGIN
Vary: Cookie
Content-Length: 0
Date: Thu, 09 May 2019 13:40:58 GMT
Server: LiteSpeed
Connection: Keep-Alive

Best regards,

I have the same problem please your support community

I have this exact same problem.

It specifically has to do with the PYTHON environment. When I remove that it works fine. When I keep it in, it doesn’t.

Any progress on a solution? It’s been over a year!

Hi,

It’s been a while so I tried to setup mysql with OLS Django again, and it still works.

Here’s my steps.

  1. Launch OpenLiteSpeed Django Image
  2. Active virtual environment by command source /usr/local/lsws/Example/html/bin/activate
  3. Follow DigitalOcean guide to
    • Install packages apt install mysql-server -y apt install python3-dev libmysqlclient-dev default-libmysqlclient-dev -y pip3 install mysqlclient
    • Create the Database
sudo mysql -u root
mysql>

CREATE DATABASE blog_data;
CREATE USER 'djangouser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON blog_data.* TO 'djangouser'@'%';
FLUSH PRIVILEGES;
  • Edit django DB settings /usr/local/lsws/Example/html/demo/demo/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'read_default_file': '/etc/mysql/my.cnf',
        },
    }
}
  • Create MySQL credentials nano /etc/mysql/my.cnf [client] database = blog_data user = djangouser password = password default-character-set = utf8
  • Reload DB systemctl daemon-reload systemctl restart mysql
  • Apply changes to Django python3 manage.py migrate

We can try to verify the mysql DB

  1. mv db.sqlite3 to other place

  2. Create superuser

    python3 manage.py createsuperuser
    

Now you should see an entry generated under blogdata.authuser

by Jeremy Morris
In this tutorial, you will learn how to set up the initial foundation for a blog website with connections to a MySQL database. This will involve creating the skeleton structure of the blog web application using django-admin, creating the MySQL database and then connecting the web application to the database.

Hi @haujo01 @healthzoneriyadh @bgv ,

I have updated the doc FAQ here in case you need more information.

https://docs.litespeedtech.com/cloud/images/django/#how-do-i-switch-sqlite-to-mysql

For some reason you got 500/503, also No module named ‘encodings’ cases, please try updating the environment variable via Web Admin > Virtual Hosts > Context like below:

  • Ubuntu 18: PYTHONPATH=/usr/local/lsws/Example/html/lib/python3.6:/usr/local/lsws/Example/html/demo LS_PYTHONBIN=/usr/local/lsws/Example/html/bin/python
  • Ubuntu 20: PYTHONPATH=/usr/local/lsws/Example/html/lib/python3.8:/usr/local/lsws/Example/html/demo LS_PYTHONBIN=/usr/local/lsws/Example/html/bin/python

Let us know if you need any help.

Best,
Eric

Submit an Answer