title: Apache throws ERROR 500: Internal Server Error when GET from localhost/internal network
body: <p>I have a production server with <strong>apache</strong> and <strong>django</strong> installed using <strong>mod_wsgi</strong>.</p>
  
  <p>The django application has a REST API that serves some info when a GET request is sent. </p>
  
  <p>This has always worked fine on the develop server, were we ran django using manage.py in a screen. Now we created a production server with apache running django but this API returns <em>Error 500</em> when running <strong><em>wget</em></strong> from <em>localhost</em> or other machines in the same network (using 192.168.X.X IP).</p>
  
  <p>Here's the output from wget:</p>
  
  <pre><code>~$ wget localhost:80/someinfo
  --2020-04-02 16:26:59--  http://localhost/someinfo
  Resolving localhost (localhost)... 127.0.0.1
  Connecting to localhost (localhost)|127.0.0.1|:80... connected.
  HTTP request sent, awaiting response... 500 Internal Server Error
  2020-04-02 16:26:59 ERROR 500: Internal Server Error.
  </code></pre>
  
  <p>It seems that the connection succeeds, so I guess it's not an apache problem. The error comes from the API response.</p>
  
  <p>The error in apache <strong>error.log</strong> looks like this:</p>
  
  <pre><code>127.0.0.1 - - [02/Apr/2020:14:24:36 +0000] "GET /someinfo HTTP/1.1" 500 799 "-" "Wget/1.19.4 (linux-gnu)"
  </code></pre>
  
  <blockquote>
    <p>question: what is the number after 500? Sometimes is 799 and other times is 803.</p>
  </blockquote>
  
  <p>But if the request is done using the public IP of the server from outside (i.e. from the browser) the API works fine and I see the correct information.</p>
  
  <p>I already checked django's allowed hosts and it was accepting localhost, and the 192.168.X.X IP of the other machine. In the end I left django's <strong>settings.py</strong> like this:</p>
  
  <pre><code>#ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.1.101']
  ALLOWED_HOSTS = ['*']
  </code></pre>
  
  <blockquote>
    <p>Note: 192.168.1.101 is the machine that tries to make the GET request.
    The final goal of all this is to be able to make a GET request from a python script running in that machine (which already works if django runs via manage.py).</p>
  </blockquote>
  
  <p>My <strong>apache.conf</strong>:</p>
  
  <pre><code>
      &lt;VirtualHost *:80&gt;
  
              ServerAdmin webmaster@localhost
              #DocumentRoot /var/www/html
  
              Alias /static /home/myuser/myproject/django/static_root
          &lt;Directory /home/myuser/myproject/django/static_root&gt;
              Require all granted
          &lt;/Directory&gt;
  
          &lt;Directory /home/myuser/myproject/django/myproject_django&gt;
              &lt;Files wsgi.py&gt;
                  Require all granted
              &lt;/Files&gt;
          &lt;/Directory&gt;
  
          WSGIDaemonProcess myproject python-home=/home/myuser/env python-path=/home/myuser/myproject/django
          WSGIProcessGroup myproject
          WSGIScriptAlias / /home/myuser/myproject/django/myproject_django/wsgi.py
      &lt;/VirtualHost&gt;
  
  </code></pre>
  
  <blockquote>
    <p>I tried running django via manage.py and the wget from localhost works just fine. <em>The problem only appears when django is ran by apache.</em></p>
  </blockquote>
  
  <p>I also tried the solution given in <a href="https://stackoverflow.com/questions/27093746/django-stops-working-with-runtimeerror-populate-isnt-reentrant">this post</a>, but changing the line does not fix the error.</p>
  
  <p>I have some doubts concerning this error:</p>
  
  <blockquote>
    <ol>
    <li>how does apache run django?</li>
    <li>does restarting apache2 service also restart django? (thus, reading again the settings.py)</li>
    <li>Is there any other django settings file rather than the one I'm editing?</li>
    <li>how can I see django logs? I don't have the console now so I can't see real time prints.</li>
    </ol>
  </blockquote>
  
  <p>I appreciate a lot any help.</p>