Lluís Air-Fi
Lluís Air-Fi

Reputation: 53

Apache throws ERROR 500: Internal Server Error when GET from localhost/internal network

I have a production server with apache and django installed using mod_wsgi.

The django application has a REST API that serves some info when a GET request is sent.

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 Error 500 when running wget from localhost or other machines in the same network (using 192.168.X.X IP).

Here's the output from wget:

~$ 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.

It seems that the connection succeeds, so I guess it's not an apache problem. The error comes from the API response.

The error in apache error.log looks like this:

127.0.0.1 - - [02/Apr/2020:14:24:36 +0000] "GET /someinfo HTTP/1.1" 500 799 "-" "Wget/1.19.4 (linux-gnu)"

question: what is the number after 500? Sometimes is 799 and other times is 803.

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.

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 settings.py like this:

#ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.1.101']
ALLOWED_HOSTS = ['*']

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).

My apache.conf:


    <VirtualHost *:80>

            ServerAdmin webmaster@localhost
            #DocumentRoot /var/www/html

            Alias /static /home/myuser/myproject/django/static_root
        <Directory /home/myuser/myproject/django/static_root>
            Require all granted
        </Directory>

        <Directory /home/myuser/myproject/django/myproject_django>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>

        WSGIDaemonProcess myproject python-home=/home/myuser/env python-path=/home/myuser/myproject/django
        WSGIProcessGroup myproject
        WSGIScriptAlias / /home/myuser/myproject/django/myproject_django/wsgi.py
    </VirtualHost>

I tried running django via manage.py and the wget from localhost works just fine. The problem only appears when django is ran by apache.

I also tried the solution given in this post, but changing the line does not fix the error.

I have some doubts concerning this error:

  1. how does apache run django?
  2. does restarting apache2 service also restart django? (thus, reading again the settings.py)
  3. Is there any other django settings file rather than the one I'm editing?
  4. how can I see django logs? I don't have the console now so I can't see real time prints.

I appreciate a lot any help.

Upvotes: 2

Views: 5666

Answers (1)

Llu&#237;s Air-Fi
Llu&#237;s Air-Fi

Reputation: 53

I finally managed to solve it myself.

It turns out wsgi handles requests from localhost or external IPs as different instance groups. So all I had to do is put

WSGIApplicationGroup %{GLOBAL}

in

/etc/apache2/sites-available/000-default.conf

Upvotes: 2

Related Questions