title: django apache2 settings on ubuntu 20 for intranet access
body: <p>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.</p>
  <p>here is what I have</p>
  <ol>
  <li>ubuntu 20.04 installed in a virtual machine (an openstack instance)</li>
  <li>this is on an intranet that I can access at work or through a vpn</li>
  <li>I installed apache2</li>
  <li>created a virtual host conf file</li>
  <li>tested it with a dummy page -- it worked</li>
  <li>installed djano and a few other items that most tutorials mention</li>
  <li>made my demo default django app</li>
  <li>collected static files to myproject/static</li>
  <li>enabled mod_wsgi</li>
  <li>modified my virtual host conf file like this (using the real server name instead of &quot;example.com&quot;):
  (my project is located at /home/.../myproject)</li>
  </ol>
  <pre><code>&lt;VirtualHost *:80&gt; 
      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
      &lt;Directory /home/.../myproject/static&gt;
           Require all granted
      &lt;/Directory&gt;
      
      Alias /media /home/.../myproject/media
      &lt;Directory /home/.../myproject/media&gt;
           Require all granted
      &lt;/Directory&gt;
      
      &lt;Directory /home/.../myproject/myproject&gt;
           &lt;Files wsgi.py&gt;
               Require all granted
           &lt;/Files&gt;
      &lt;/Directory&gt;
         
      WsGIPassAuthorization On
      WSGIDaemonProcess myproject python-home=/home/.../myproject/myprojectenv python-path=/home/.../myproject&gt;
      WSGIScriptAlias / /home/.../myproject/myproject/wsgi.py
      WSGIProcessGroup myproject
      
  &lt;/VirtualHost&gt;
  </code></pre>
  <p>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:</p>
  <p><a href="https://i.stack.imgur.com/PIRDl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PIRDl.png" alt="enter image description here" /></a></p>
  <p>in my settings.py file, I have added the correct ip address</p>
  <pre><code>ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'my ip address', 'example.com']
  </code></pre>
  <p>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, <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a> still correctly shows my dummy virtual host success page).</p>
  <p>Most of what i put in the file was the same as i found everywhere. a few places added things like:
  <code>LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so</code>
  and
  <code>WsGIPassAuthorization On</code></p>
  <p>I am a total novice, so please explain things nicely if you can.</p>
  <p>EDIT: I've just updated mod_wsgi and run this hello world tutorial. it worked fine. <a href="https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html" rel="nofollow noreferrer">https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html</a></p>
  <p>EDIT2: looking at the apache error log I see
  <code>ModuleNotFoundError: No module named 'myproject'</code>
  So, I guess that is the problem! But how do I fix it?</p>
  <p>error in apache log</p>
  <pre><code>[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 &quot;/home/web/myproject/myproject/wsgi.py&quot;, line 16, in &lt;module&gt;
  [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 &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/core/wsgi.py&quot;, 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 &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/__init__.py&quot;, 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 &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py&quot;, 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 &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py&quot;, 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 &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py&quot;, 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 &quot;/usr/lib/python3.8/importlib/__init__.py&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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 &quot;&lt;frozen importlib._bootstrap&gt;&quot;, 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'```
  
  </code></pre>
  <p>EDIT 3:</p>
  <p>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</p>
  <pre><code>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 &quot;myproject/wsgi.py&quot;, line 22, in &lt;module&gt;
      application = get_wsgi_application()
    File &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/core/wsgi.py&quot;, line 12, in get_wsgi_application
      django.setup(set_prefix=False)
    File &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/__init__.py&quot;, line 19, in setup
      configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    File &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py&quot;, line 82, in __getattr__
      self._setup(name)
    File &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py&quot;, line 69, in _setup
      self._wrapped = Settings(settings_module)
    File &quot;/home/web/myproject/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py&quot;, line 170, in __init__
      mod = importlib.import_module(self.SETTINGS_MODULE)
    File &quot;/usr/lib/python3.8/importlib/__init__.py&quot;, line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1014, in _gcd_import
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 991, in _find_and_load
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 961, in _find_and_load_unlocked
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 219, in _call_with_frames_removed
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1014, in _gcd_import
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 991, in _find_and_load
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 973, in _find_and_load_unlocked
  ModuleNotFoundError: No module named 'myproject'
  </code></pre>