【问题标题】:Django site on Ubuntu 18.04 with Apache2 not working after installing SSL安装 SSL 后 Ubuntu 18.04 上的 Django 站点与 Apache2 无法正常工作
【发布时间】:2019-07-04 12:10:23
【问题描述】:

我正在尝试使用 mode_wsgi 在带有 Apache2 的 Ubuntu 18.04 上部署我的 Djang(1.10) 应用程序,该站点在设置 SSL 之前运行良好,但是当我使用 certbot 从 Let'sEncrypt 安装 SSL 证书时不再加载。

这是我的配置:

项目文件夹路径:

/home/abdul

Http配置:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName www.orderfetchers.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

    Alias /static /home/abdul/Fetchors/static
      <Directory /home/abdul/Fetchors/static>
        Require all granted
      </Directory>

      Alias /media /home/abdul/Fetchors/media
      <Directory /home/abdul/Fetchors/media>
        Require all granted
      </Directory>

      <Directory /home/abdul/Fetchors/Fetchors>
        <Files wsgi.py>
          Require all granted
        </Files>
      </Directory>
    #WSGIScriptAlias / /home/abdul/Fetchors/Fetchors/wsgi.py
    #WSGIDaemonProcess django_app python-path=/home/abdul/Fetchors python-home=/home/abdul/Fetchors/venv
    #WSGIProcessGroup django_app
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.orderfetchers.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

这里是Https 配置:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName www.orderfetchers.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

    Alias /static /home/abdul/Fetchors/static
      <Directory /home/abdul/Fetchors/static>
        Require all granted
      </Directory>

      Alias /media /home/abdul/Fetchors/media
      <Directory /home/abdul/Fetchors/media>
        Require all granted
      </Directory>

      <Directory /home/abdul/Fetchors/Fetchors>
        <Files wsgi.py>
          Require all granted
        </Files>
      </Directory>
    WSGIScriptAlias / /home/abdul/Fetchors/Fetchors/wsgi.py
    WSGIDaemonProcess django_app python-path=/home/abdul/Fetchors python-home=/home/abdul/Fetchors/venv
    WSGIProcessGroup django_app

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.orderfetchers.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.orderfetchers.com/privkey.pem
</VirtualHost>
</IfModule>

【问题讨论】:

  • 您遇到的当前错误是什么?您刚刚粘贴了一堆配置,但在不知道您遇到什么错误的情况下很难开始查找问题。
  • 其实我的浏览器并没有出现任何错误,当我加载站点时,它只是在加载....过了一会儿站点无法访问!
  • 您是否尝试过使用curl 访问该站点。尝试使用curl --insecure ...,以防证书配置错误但站点正常
  • 卷曲返回curl: (7) Failed to connect to www.orderfetchers.com port 443: Operation timed out
  • 但是使用--insecure它返回主页html。

标签: django ssl apache2 mod-wsgi certbot


【解决方案1】:

tl;dr:检查您的防火墙(端口 443),并使用有关配置的在线教程(如下)。


好的,所以我遇到了与您完全相同的错误 - 我的网站在 Ubuntu 机器上使用 Apache2 和 Django 1.10 在端口 80 上运行良好,当我按照 LetsEncrypt 的指南进行操作时,我的网站将永远加载在超时之前。我假设您帖子中的问题是如何阻止您的网站超时并实际加载。

就我而言,我很幸运,因为我只是一个白痴,忘记为我的 Ubuntu 机器打开 AWS 安全组上的 443 端口。但是,我有一些额外的配置可能会对您有所帮助:

settings.py

# SSL support
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# session expire at browser close
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

# wsgi scheme
os.environ['wsgi.url_scheme'] = 'https'

wsgi.py

os.environ['HTTPS'] = "on"

这些配置是在这些参考站点的帮助下找到并编译的(这也可能对您有所帮助):

https://simpleisbetterthancomplex.com/tutorial/2016/05/11/how-to-setup-ssl-certificate-on-nginx-for-django-application.html

https://www.pdxpixel.com/blog/2014/02/04/setting-up-django-site-ssl-apache-mod_wsgi-mod_ssl/

https://docs.djangoproject.com/en/dev/topics/security/#ssl-https

如果不进一步了解您的系统是什么,就很难更准确地提供帮助,但我发现这是有用的,所以希望它有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多