Apache服务器上提供的Django站点无法正常工作,日志没有错误

时间:2019-05-03 14:26:19

标签: django apache firewall

我在服务器中的/home/rohit/django-site/infer上有一个Django网站。 apache2wsgi已经安装并且可以正常工作。要看到这一点:

$ 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

这是/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'

看看,我知道一切都很好。

/var/log/apache2/access.log是空的,这意味着我对站点的所有请求甚至都没有到达服务器。似乎是防火墙问题。

防火墙

防火墙特别允许从外部访问端口8000。这意味着即使端口80http)也不允许从外部访问。

为了进行测试,我从服务器上运行了./manage.py runserver 0.0.0.0:8000,并通过访问<ip-of-server>:8000从笔记本电脑成功访问了该站点。这意味着可以从外部访问端口8000

现在,为了确保apache可以在该端口上运行,我编辑了文件/etc/apache2/sites-available/000-default.conf,现在看起来像这样:

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

因此,完成所有这些操作后,当我在浏览器中放置地址<ip-of-server>:8000时,出现“无法连接”错误。另外,空的access.log意味着请求从未到达服务器。但是端口8000是开放的,因此防火墙应该不是问题。那么,有什么可能会引起问题吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Daniel Roseman所评论,我在Listen 8000上添加了/etc/apache2/sites-available/000-default.conf

我还需要允许访问项目的主目录。因此,按照MaximeK

的建议,我在/etc/apache2/apache2.conf中添加了以下内容
<Directory /home/rohit/django-site/infer>
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order deny,allow
        Allow from all
</Directory>

然后,根据this答案的建议,我通过以下方式更改了/home/rohit/django-site/infer的权限:

sudo chmod -R 755 <site_top_folder>