0

I can't seem to get my django site to be accessible outside of development. I have looked at countless tutorials and they all say the same thing, which I have tried. nevertheless, it doesn't work.

here is what I have

  1. ubuntu 20.04 installed in a virtual machine (an openstack instance)
  2. this is on an intranet that I can access at work or through a vpn
  3. I installed apache2
  4. created a virtual host conf file
  5. tested it with a dummy page -- it worked
  6. installed djano and a few other items that most tutorials mention
  7. made my demo default django app
  8. collected static files to myproject/static
  9. enabled mod_wsgi
  10. modified my virtual host conf file like this (using the real server name instead of "example.com"): (my project is located at /home/.../myproject)
<VirtualHost *:80> 
    ServerAdmin [email]   
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    DocumentRoot /home/.../myproject
    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
    
    LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
    Alias /static /home/.../myproject/static
    <Directory /home/.../myproject/static>
         Require all granted
    </Directory>
    
    Alias /media /home/.../myproject/media
    <Directory /home/.../myproject/media>
         Require all granted
    </Directory>
    
    <Directory /home/.../myproject/myproject>
         <Files wsgi.py>
             Require all granted
         </Files>
    </Directory>
       
    WsGIPassAuthorization On
    WSGIDaemonProcess myproject python-home=/home/.../myproject/myprojectenv python-path=/home/.../myproject>
    WSGIScriptAlias / /home/.../myproject/myproject/wsgi.py
    WSGIProcessGroup myproject
    
</VirtualHost>

I know there are two documentroots there. I tried both (but not at the same time). The error message I get when I try to access example.com is:

enter image description here

in my settings.py file, I have added the correct ip address

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'my ip address', 'example.com']

What am I missing? It feels like there is a simple step that I can't figure out. Currently, I am just testing that I can see something from the outside (another computer on our intranet), so I have not turned debug off or done any of the other recommended security steps for actual production. As my dummy apache2 page was viewable from the outside (again, another computer on our intranet -- not from anywhere), it seems like it should have worked (I did not modify my virtual host at 443 for django. but it has the settings for the certificate and keys I got. So, https://example.com still correctly shows my dummy virtual host success page).

Most of what i put in the file was the same as i found everywhere. a few places added things like: LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so and WsGIPassAuthorization On

I am a total novice, so please explain things nicely if you can.

EDIT: I've just updated mod_wsgi and run this hello world tutorial. it worked fine. https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html

EDIT2: looking at the apache error log I see ModuleNotFoundError: No module named 'myproject' So, I guess that is the problem! But how do I fix it?

error in apache log

[Thu May 06 06:45:32.981147 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] mod_wsgi (pid=546592): Failed to exec Python script file '/home/web/myproject/myproject/wsgi.py'.
[Thu May 06 06:45:32.981185 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] mod_wsgi (pid=546592): Exception occurred processing WSGI script '/home/web/myproject/myproject/wsgi.py'.
[Thu May 06 06:45:32.981302 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] Traceback (most recent call last):
[Thu May 06 06:45:32.981349 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/home/web/myproject/myproject/wsgi.py", line 16, in <module>
[Thu May 06 06:45:32.981352 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     application = get_wsgi_application()
[Thu May 06 06:45:32.981358 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu May 06 06:45:32.981361 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     django.setup(set_prefix=False)
[Thu May 06 06:45:32.981367 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
[Thu May 06 06:45:32.981370 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu May 06 06:45:32.981375 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Thu May 06 06:45:32.981378 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     self._setup(name)
[Thu May 06 06:45:32.981383 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
[Thu May 06 06:45:32.981386 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     self._wrapped = Settings(settings_module)
[Thu May 06 06:45:32.981391 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
[Thu May 06 06:45:32.981394 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu May 06 06:45:32.981400 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Thu May 06 06:45:32.981403 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]     return _bootstrap._gcd_import(name[level:], package, level)
[Thu May 06 06:45:32.981408 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Thu May 06 06:45:32.981413 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Thu May 06 06:45:32.981419 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
[Thu May 06 06:45:32.981424 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Thu May 06 06:45:32.981429 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Thu May 06 06:45:32.981435 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Thu May 06 06:45:32.981440 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535]   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Thu May 06 06:45:32.981452 2021] [wsgi:error] [pid 546592:tid 140672347686656] [remote 172.31.6.96:57535] ModuleNotFoundError: No module named 'myproject'```

EDIT 3:

actually, simply running the wsgi.py file from the terminal gives the same error. now im totally confused as this is the default tutorial app. and no error occurs when the same wsgi file is run from the development server

sys.version = '3.8.5 (default, Jan 27 2021, 15:41:15) \n[GCC 9.3.0]'
sys.prefix = '/home/web/myproject/myprojectenv'

sys.path = ['/home/web/myproject/myproject', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/web/myproject/myprojectenv/lib/python3.8/site-packages']
Traceback (most recent call last):
  File "myproject/wsgi.py", line 22, in <module>
    application = get_wsgi_application()
  File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'myproject'
anp925
  • 59
  • 4
  • Loadmodul should not be inside the virtual host definition. Please post the complete error message that you get at server restart. While testing do not start apache as a service because some usefull error messages might be hidden then. – Razenstein May 06 '21 at 05:12
  • thank you. i removed the line, but no difference (actually adding that line was something i did trying to troubleshoot -- it wasn't in any of the other examples i saw searching. I've editing the post with a screenshot of the error message. – anp925 May 06 '21 at 06:06
  • As i said in the 2nd edit, the apache error logs indicate that 'myproject' is not a module. But, it's right there. Not sure why it works on the development server but not here. – anp925 May 06 '21 at 06:38
  • this indicates that your Apache is running and calling your Python/Django App via wsgi ... so the former configuration problem of Apache is solved – Razenstein May 06 '21 at 12:02
  • the error traces shows your sys.path='/home/web/myproject/myproject' .... this should contain the folder that holds your apps (I guess in your structure it should be '/home/web/myproject/' which you add using python-path definition in your virtual host. It is not clear to me if python-path is correct as you replace the path with /.../ – Razenstein May 06 '21 at 12:54
  • hmm. the actual lines in the virtual host are: WSGIDaemonProcess myproject python-home=/home/web/myproject/myprojectenv python-path=/home/web/myproject> WSGIScriptAlias / /home/web/myproject/myproject/wsgi.py WSGIProcessGroup myproject. it seems correct, but maybe i've just been staring at it all too long – anp925 May 06 '21 at 16:25
  • maybe restart apache because python-path=/home/web/myproject is definitly not in your sys.path - or add a line in wsgi.py: sys.path.append('/home/web/myproject') which does the same. Its a little difficult to debug the project config here because one should have the complete picture including file tree here. – Razenstein May 06 '21 at 19:00
  • yes! adding the line to the wsgi.py file worked! I wonder why it wasn't getting added automatically, but i'm satisfied. – anp925 May 11 '21 at 03:05

0 Answers0