#34439: mod_wsgi for django with conda environment in Windows doesn't work
-------------------------------------+-------------------------------------
     Reporter:  Lorenzo Lo Giudice   |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  HTTP handling        |                  Version:  3.2
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  mod_wsgi windows     |             Triage Stage:
  apache conda                       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

 * status:  new => closed
 * resolution:   => invalid


Old description:

> This is my first post in stackoverflow.
> I'll try to explain to you what I've done with steps and what I've done
> to try to solve the problems.
>
> # Problem
> I'm struggling with the configuration of mod_wsgi for my django project.
>
> When i try to connect to localhost:8001 (port listened by mod_wsgi) I get
> an infinite loading or a "This site can’t be reached" page.
>
> # What i've done
>
> I have **installed apache2.4** with apachelounge
> (https://www.apachelounge.com/download/) in my C:\\ drive.
>
> I have **installed anaconda** (I haven't set conda in path) and I created
> my environment for my django project (called *production*).
>
> It runs correctly if I run the command `python manage.py runserver`
> (if i go to http://localhost:7000/ (port set to manage.py) it works.
>
> I installed mod_wsgi in my conda environment with `pip install mod-wsgi`.
>
> # Files
>
> ***httpd.conf*** (C:\Apache24\conf\httpd.conf)
> ```
> ServerName localhost
> Listen 8001
>
> ...
>
> LoadFile "C:/Users/my_name/anaconda3/envs/production/python39.dll"
> LoadModule wsgi_module "C:/Users/my_name/anaconda3/envs/production/lib
> /site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
> WSGIPythonHome "C:/Users/my_name/anaconda3/envs/production"
> WSGIScriptAlias / "C:/Users/my_name/my_path/Analyser/wsgi.py"
>
> <Directory "C:/Users/my_name/my_path/Analyser">
>     <Files wsgi.py>
>         Require all granted
>     </Files>
> </Directory>
>
> LogLevel info
> WSGIApplicationGroup %{GLOBAL}
> ```
>
> <br>
>
> ***wsgi.py*** (C:/Users/my_name/my_path/Analyser/wsgi.py)
>
> ``` python
> import os
> import sys
> from django.core.wsgi import get_wsgi_application
>
> # add the project folder to the path (otherwise it says (core:
> ModuleNotFound)
> _BASE_DIR = r'C:\Users\my_name\my_path\Analyser'
> if _BASE_DIR not in sys.path:
>     sys.path.append(_BASE_DIR)
>
> os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
>
> # set CRYPTOGRAPHY_OPENSSL_NO_LEGACY to True otherwise it says OpenSSL...
> os.environ.setdefault("CRYPTOGRAPHY_OPENSSL_NO_LEGACY", "1")
>
> print(f'checkpoint 1')
> application = get_wsgi_application()
> print(f'checkpoint 2')
> ```
>
> ***settings.py*** (C:/Users/my_name/my_path/Analyser/core/settings.py)
> ``` python
> WSGI_APPLICATION = 'wsgi.application'
>
> # other stuff
> from split_settings.tools import include
> include('config/*.py',)
> ```
>
> # Issue 1: No module named 'encodings'
> This site can't be reached when going to localhost:8001
> [Issue1: This site can't be reached](https://i.stack.imgur.com/i0p9r.png)
>
> ***error.log***
> ```
> Python path configuration:
>   PYTHONHOME = (not set)
>   PYTHONPATH = (not set)
>   program name = 'python'
>   isolated = 0
>   environment = 1
>   user site = 1
>   import site = 1
>   sys._base_executable = 'C:\\Apache24\\bin\\httpd.exe'
>   sys.base_prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
>   sys.base_exec_prefix =
> 'C:\\Users\\my_name\\anaconda3\\envs\\production'
>   sys.platlibdir = 'lib'
>   sys.executable = 'C:\\Apache24\\bin\\httpd.exe'
>   sys.prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
>   sys.exec_prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
>   sys.path = [
>     'C:\\Users\\my_name\\anaconda3\\envs\\production\\python39.zip',
>     '.\\DLLs',
>     '.\\lib',
>     'C:\\Apache24\\bin',
>   ]
> Fatal Python error: init_fs_encoding: failed to get the Python codec of
> the filesystem encoding
> Python runtime state: core initialized
> ModuleNotFoundError: No module named 'encodings'
>
> Current thread 0x00007520 (most recent call first):
> <no Python frame>
> ```
>
> tried with SetEnv in httpd.conf but it didn't work
> ```
> # did not work
> SetEnv PYTHONHOME "C:/Users/my_name/anaconda3/envs/production"
> SetEnv PYTHONPATH "C:/Users/my_name/anaconda3/envs/production/Lib/site-
> packages"
> ```
>
> I **added to my environment variables**
> - PYTHONHOME >> C:\Users\my_name\anaconda3\envs\production
> - PYTHONHOME >> C:\Users\my_name\anaconda3\envs\production\Lib\site-
> packages
>
> Now when i go to localhost:8001 i don't get the same error.
>
> # Issue 2: infinite loading
> Infinite loading when going to localhost:8001
>
> ***error.log***
> ```
> [Thu Mar 23 14:01:14.062920 2023] [wsgi:info] [pid 31308:tid 1076]
> mod_wsgi (pid=31308): Create interpreter 'localhost:8001|'.
> [Thu Mar 23 14:01:14.065559 2023] [wsgi:info] [pid 31308:tid 1076]
> mod_wsgi (pid=31308): Adding
> 'C:/Users/my_name/anaconda3/envs/production/Lib/site-packages' to path.
> [Thu Mar 23 14:01:14.068564 2023] [wsgi:info] [pid 31308:tid 1076]
> mod_wsgi (pid=31308): Adding 'C:/Users/my_name/my_path/Analyser' to path.
> [Thu Mar 23 14:01:14.072611 2023] [wsgi:info] [pid 31308:tid 1076]
> [client ::1:49573] mod_wsgi (pid=31308, process='',
> application='localhost:8001|'): Loading Python script file
> 'C:/Users/my_name/my_path/Analyser/wsgi.py'.
> [Thu Mar 23 14:01:14.298046 2023] [wsgi:error] [pid 31308:tid 1076]
> checkpoint 1
> [Thu Mar 23 14:01:14.298046 2023] [wsgi:error] [pid 31308:tid 1076] \r
> ```
>
> **I tried to set `WSGIApplicationGroup %{GLOBAL}` in httpd.conf**, now i
> get an Internal Server Error page.
>
> # Issue 3: internal server error (continue of Issue 2)
>
> Now the in the ***error.log*** appears:
> ```
> raise RuntimeError(\r
> [Thu Mar 23 14:09:06.159643 2023] [wsgi:error] [pid 22156:tid 1056]
> [client ::1:49632] RuntimeError: OpenSSL 3.0's legacy provider failed to
> load. This is a fatal error by default, but cryptography supports running
> without legacy algorithms by setting the environment variable
> CRYPTOGRAPHY_OPENSSL_NO_LEGACY. If you did not expect this error, you
> have likely made a mistake with your OpenSSL configuration.\r
> ```
>
> I tried to **set CRYPTOGRAPHY_OPENSSL_NO_LEGACY to True**
>
> I tried writing `SetEnv CRYPTOGRAPHY_OPENSSL_NO_LEGACY 1` in httpd.conf
> but it did not work, so
> I added `os.environ.setdefault("CRYPTOGRAPHY_OPENSSL_NO_LEGACY", "1")` in
> wsgi.py.
>
> # Issue 4: infinite loading (again...)
> When i visit localhost:8001 i get an infinite loading.
>
> ***error.log***
> ```
> [Thu Mar 23 14:23:33.150762 2023] [wsgi:info] [pid 3540:tid 1044] [client
> ::1:49785] mod_wsgi (pid=3540, process='', application=''): Loading
> Python script file 'C:/Users/my_name/my_path/Analyser/wsgi.py'.
> [Thu Mar 23 14:23:33.411332 2023] [wsgi:error] [pid 3540:tid 1044]
> checkpoint 1
> [Thu Mar 23 14:23:33.411332 2023] [wsgi:error] [pid 3540:tid 1044] \r
> ```
>
> And now I'm stuck here.
>
> Stackoverflow issue: https://stackoverflow.com/posts/75823890

New description:

 This is my first post in stackoverflow.
 I'll try to explain to you what I've done with steps and what I've done to
 try to solve the problems.

 # Problem
 I'm struggling with the configuration of mod_wsgi for my django project.

 When i try to connect to localhost:8001 (port listened by mod_wsgi) I get
 an infinite loading or a "This site can’t be reached" page.

 # What i've done

 I have **installed apache2.4** with apachelounge
 (https://www.apachelounge.com/download/) in my C:\\ drive.

 I have **installed anaconda** (I haven't set conda in path) and I created
 my environment for my django project (called *production*).

 It runs correctly if I run the command `python manage.py runserver`
 (if i go to http://localhost:7000/ (port set to manage.py) it works.

 I installed mod_wsgi in my conda environment with `pip install mod-wsgi`.

 # Files

 ***httpd.conf*** (C:\Apache24\conf\httpd.conf)
 {{{
 ServerName localhost
 Listen 8001

 ...

 LoadFile "C:/Users/my_name/anaconda3/envs/production/python39.dll"
 LoadModule wsgi_module "C:/Users/my_name/anaconda3/envs/production/lib
 /site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
 WSGIPythonHome "C:/Users/my_name/anaconda3/envs/production"
 WSGIScriptAlias / "C:/Users/my_name/my_path/Analyser/wsgi.py"

 <Directory "C:/Users/my_name/my_path/Analyser">
     <Files wsgi.py>
         Require all granted
     </Files>
 </Directory>

 LogLevel info
 WSGIApplicationGroup %{GLOBAL}
 }}}

 ***wsgi.py*** (C:/Users/my_name/my_path/Analyser/wsgi.py)

 {{{
 import os
 import sys
 from django.core.wsgi import get_wsgi_application

 # add the project folder to the path (otherwise it says (core:
 ModuleNotFound)
 _BASE_DIR = r'C:\Users\my_name\my_path\Analyser'
 if _BASE_DIR not in sys.path:
     sys.path.append(_BASE_DIR)

 os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")

 # set CRYPTOGRAPHY_OPENSSL_NO_LEGACY to True otherwise it says OpenSSL...
 os.environ.setdefault("CRYPTOGRAPHY_OPENSSL_NO_LEGACY", "1")

 print(f'checkpoint 1')
 application = get_wsgi_application()
 print(f'checkpoint 2')
 }}}

 ***settings.py*** (C:/Users/my_name/my_path/Analyser/core/settings.py)
 {{{
 WSGI_APPLICATION = 'wsgi.application'

 # other stuff
 from split_settings.tools import include
 include('config/*.py',)
 }}}

 # Issue 1: No module named 'encodings'
 This site can't be reached when going to localhost:8001
 [Issue1: This site can't be reached](https://i.stack.imgur.com/i0p9r.png)

 ***error.log***
 {{{
 Python path configuration:
   PYTHONHOME = (not set)
   PYTHONPATH = (not set)
   program name = 'python'
   isolated = 0
   environment = 1
   user site = 1
   import site = 1
   sys._base_executable = 'C:\\Apache24\\bin\\httpd.exe'
   sys.base_prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
   sys.base_exec_prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
   sys.platlibdir = 'lib'
   sys.executable = 'C:\\Apache24\\bin\\httpd.exe'
   sys.prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
   sys.exec_prefix = 'C:\\Users\\my_name\\anaconda3\\envs\\production'
   sys.path = [
     'C:\\Users\\my_name\\anaconda3\\envs\\production\\python39.zip',
     '.\\DLLs',
     '.\\lib',
     'C:\\Apache24\\bin',
   ]
 Fatal Python error: init_fs_encoding: failed to get the Python codec of
 the filesystem encoding
 Python runtime state: core initialized
 ModuleNotFoundError: No module named 'encodings'

 Current thread 0x00007520 (most recent call first):
 <no Python frame>
 }}}

 tried with SetEnv in httpd.conf but it didn't work
 {{{
 # did not work
 SetEnv PYTHONHOME "C:/Users/my_name/anaconda3/envs/production"
 SetEnv PYTHONPATH "C:/Users/my_name/anaconda3/envs/production/Lib/site-
 packages"
 }}}

 I **added to my environment variables**
 - PYTHONHOME >> C:\Users\my_name\anaconda3\envs\production
 - PYTHONHOME >> C:\Users\my_name\anaconda3\envs\production\Lib\site-
 packages

 Now when i go to localhost:8001 i don't get the same error.

 # Issue 2: infinite loading
 Infinite loading when going to localhost:8001

 ***error.log***
 {{{
 [Thu Mar 23 14:01:14.062920 2023] [wsgi:info] [pid 31308:tid 1076]
 mod_wsgi (pid=31308): Create interpreter 'localhost:8001|'.
 [Thu Mar 23 14:01:14.065559 2023] [wsgi:info] [pid 31308:tid 1076]
 mod_wsgi (pid=31308): Adding
 'C:/Users/my_name/anaconda3/envs/production/Lib/site-packages' to path.
 [Thu Mar 23 14:01:14.068564 2023] [wsgi:info] [pid 31308:tid 1076]
 mod_wsgi (pid=31308): Adding 'C:/Users/my_name/my_path/Analyser' to path.
 [Thu Mar 23 14:01:14.072611 2023] [wsgi:info] [pid 31308:tid 1076] [client
 ::1:49573] mod_wsgi (pid=31308, process='',
 application='localhost:8001|'): Loading Python script file
 'C:/Users/my_name/my_path/Analyser/wsgi.py'.
 [Thu Mar 23 14:01:14.298046 2023] [wsgi:error] [pid 31308:tid 1076]
 checkpoint 1
 [Thu Mar 23 14:01:14.298046 2023] [wsgi:error] [pid 31308:tid 1076] \r
 }}}

 **I tried to set `WSGIApplicationGroup %{GLOBAL}` in httpd.conf**, now i
 get an Internal Server Error page.

 # Issue 3: internal server error (continue of Issue 2)

 Now the in the ***error.log*** appears:
 {{{
 raise RuntimeError(\r
 [Thu Mar 23 14:09:06.159643 2023] [wsgi:error] [pid 22156:tid 1056]
 [client ::1:49632] RuntimeError: OpenSSL 3.0's legacy provider failed to
 load. This is a fatal error by default, but cryptography supports running
 without legacy algorithms by setting the environment variable
 CRYPTOGRAPHY_OPENSSL_NO_LEGACY. If you did not expect this error, you have
 likely made a mistake with your OpenSSL configuration.\r
 }}}

 I tried to **set CRYPTOGRAPHY_OPENSSL_NO_LEGACY to True**

 I tried writing `SetEnv CRYPTOGRAPHY_OPENSSL_NO_LEGACY 1` in httpd.conf
 but it did not work, so
 I added `os.environ.setdefault("CRYPTOGRAPHY_OPENSSL_NO_LEGACY", "1")` in
 wsgi.py.

 # Issue 4: infinite loading (again...)
 When i visit localhost:8001 i get an infinite loading.

 ***error.log***
 {{{
 [Thu Mar 23 14:23:33.150762 2023] [wsgi:info] [pid 3540:tid 1044] [client
 ::1:49785] mod_wsgi (pid=3540, process='', application=''): Loading Python
 script file 'C:/Users/my_name/my_path/Analyser/wsgi.py'.
 [Thu Mar 23 14:23:33.411332 2023] [wsgi:error] [pid 3540:tid 1044]
 checkpoint 1
 [Thu Mar 23 14:23:33.411332 2023] [wsgi:error] [pid 3540:tid 1044] \r
 }}}

 And now I'm stuck here.

 Stackoverflow issue: https://stackoverflow.com/posts/75823890

--

Comment:

 Please don't use Trac as a support channel. Closing per
 TicketClosingReasons/UseSupportChannels.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34439#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018723abedc9-dbdce78c-b956-40d4-adc4-c3b3be743200-000000%40eu-central-1.amazonses.com.

Reply via email to