Django site served on Apache server doesn't work, logs have no error

不问归期 提交于 2020-05-17 09:33:06

问题


I've a Django website located at /home/rohit/django-site/infer, in a server. apache2 and wsgi are already installed and working correctly. To see that:

$ systemctl status apache2.service 
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Fri 2019-05-03 19:12:34 IST; 31min ago
  Process: 9354 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 9364 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 9368 (apache2)
    Tasks: 24 (limit: 4915)
   CGroup: /system.slice/apache2.service
           ├─9368 /usr/sbin/apache2 -k start
           ├─9370 /usr/sbin/apache2 -k start
           ├─9371 /usr/sbin/apache2 -k start
           ├─9372 /usr/sbin/apache2 -k start
           ├─9373 /usr/sbin/apache2 -k start
           ├─9374 /usr/sbin/apache2 -k start
           └─9375 /usr/sbin/apache2 -k start

Here is the content of /var/log/apache2/error.log:

[Fri May 03 17:04:03.885793 2019] [mpm_prefork:notice] [pid 24349] AH00163: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Fri May 03 17:04:03.885868 2019] [core:notice] [pid 24349] AH00094: Command line: '/usr/sbin/apache2'
[Fri May 03 19:12:34.361607 2019] [mpm_prefork:notice] [pid 24349] AH00169: caught SIGTERM, shutting down
[Fri May 03 19:12:34.480584 2019] [mpm_prefork:notice] [pid 9368] AH00163: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Fri May 03 19:12:34.480660 2019] [core:notice] [pid 9368] AH00094: Command line: '/usr/sbin/apache2'

Looking at which, I understand that everything is okay.

The /var/log/apache2/access.log is empty though, which means that all the requests made by me for the site didn't even reach the server. That seems like a firewall issue.

Firewall

The firewall has specifically allowed port 8000 to be accessed from outside world. That means that even port 80 (http) isn't allowed to be accessed from the outside world.

To test that, I ran ./manage.py runserver 0.0.0.0:8000 from the server, and successfully accessed the site from my laptop, by visiting <ip-of-server>:8000. That means that port 8000 is accessible from outside.

Now, to make sure that apache serves on this port, I edited the file /etc/apache2/sites-available/000-default.conf, which now looked like:

<VirtualHost *:8000>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias /static /home/rohit/django-site/infer/static
        <Directory /home/rohit/django-site/infer/static>
                Require all granted
        </Directory>
        <Directory /home/rohit/django-site/infer/infer>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
        WSGIDaemonProcess infer python-path=/home/rohit/django-site/infer python-home=/home/rohit/django-site/inferenv
        WSGIProcessGroup infer
        WSGIScriptAlias / /home/rohit/django-site/infer/infer/wsgi.py
</VirtualHost>

So, after doing all this, when I put the address <ip-of-server>:8000 on my browser, I get an "unable to connect" error. Plus, the empty access.log means that the request never reached the server. But port 8000 is open, so the firewall shouldn't be the problem. So, is there anything that might be causing the problem?

Any help is appreciated!


回答1:


As commented by Daniel Roseman, I added Listen 8000 to the /etc/apache2/sites-available/000-default.conf.

I also needed to allow access to my project's home directory. So I added the following to /etc/apache2/apache2.conf, as suggested by MaximeK

<Directory /home/rohit/django-site/infer>
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order deny,allow
        Allow from all
</Directory>

Then I changed the permissions for /home/rohit/django-site/infer, as suggested by this answer, by:

sudo chmod -R 755 <site_top_folder>


来源:https://stackoverflow.com/questions/55971841/django-site-served-on-apache-server-doesnt-work-logs-have-no-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!