python - 如何在 apache 上配置 django 并在 apache 中创建 https 以便 django url 应使用 https ://<ip>:80 port 打开

标签 python django apache

我想让 django 安全地在 https 服务器上运行,而不使用第三方包,如 runsslserver 或 sslserver,因为我就是这种方法

在 settings.py 文件中我配置了这些行。

SECURE_SSL_REDIRECT = True

所以当在浏览器中给出url时,url能够重定向到https://192.168.31.2/cp_vm/details但我收到安全 ssl 错误。

请建议我任何获取输出的方法,或者在 apache 上使用 https 运行 django 也可以,但我能够找到链接 https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 但在浏览器中输入ip 192.168.41.5/cp_vm/details后,它显示无法连接。

我附上了 apache 000.default.conf 文件,也请帮助我,如果上面有任何错误,请原谅我,请给我一些建议,以便

  1. 我可以先在 apache 服务器上运行 django 。
  2. 然后在同一个 apache 服务器上获取 https//。如果有任何帮助,请为我提供一些帮助。

我的配置文件如下:

<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.example.com
    ServerName 10.206.51.6
    ServerAdmin 10.206.51.6
    #ServerAdmin webmaster@localhost
    #DocumentRoot /var/www/html
    DocumentRoot /var/www/nfvs_portal
    ## imc urls
    ProxyPass /imc http://10.206.50.12:8080/imc
    ProxyPassReverse /imc http://10.206.50.12:8080/imc

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

    # 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 /opt/hpe_nfvs/nfvs_portal/static
    <Directory /opt/hpe_nfvs/nfvs_portal/static>
            Require all granted
    </Directory>

    <Directory /opt/hpe_nfvs/nfvs_portal/nfvs_portal>
            <Files wsgi.py>
                 Require all granted
            </Files>
    </Directory>

    WSGIDaemonProcess nfvs_portal python-path=//opt/hpe_nfvs/nfvs_portal:/opt/hpe_nfvs/nfvs_portal/nfvs_portal/lib/python2.7/site-packages
    WSGIScriptAlias / /opt/hpe_nfvs/nfvs_portal/nfvs_portal/wsgi.py

</VirtualHost>

最佳答案

我遇到了很多麻烦,做了很多功课,才在我的ubuntu服务器上完成了django站点+带有https配置的apache。最后,我能够成功配置它,并且我的网站现在可以按预期使用 https 运行。

下面是.py和apache配置文件。请更正您的文件,如果您仍然遇到任何问题,请告诉我。

wigs.py

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE","go_portal_site.settings")
os.environ['HTTPS'] = "on"
application = get_wsgi_application()

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

    <VirtualHost *:80>  
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    redirect permanent / https://site.site.com

    Alias /static /home/ubuntu/django_portal/ci/site_portal/portal_site/static
    <Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/static>
        Require all granted
    </Directory>

    <Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    #WSGIDaemonProcess go_portal_site python-path=/home/ubuntu/django_portal/ci/site_portal/portal_site python-home=/home/ubuntu/django_portal/ci/site_portal/portal_site/phase2_env
    WSGIProcessGroup go_portal_site
    WSGIScriptAlias / /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site/wsgi.py
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName site.site.com
    Alias /static /home/ubuntu/django_portal/ci/site_portal/portal_site/static
    <Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/static>
        Require all granted
    </Directory>

    <Directory /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>


    WSGIDaemonProcess go_portal_site python-path=/home/ubuntu/django_portal/ci/site_portal/portal_site python-home=/home/ubuntu/django_portal/ci/site_portal/portal_site/phase2_env
    WSGIProcessGroup go_portal_site
    WSGIScriptAlias / /home/ubuntu/django_portal/ci/site_portal/portal_site/go_portal_site/wsgi.py

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

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/site.site.com-cer.pem
    SSLCertificateKeyFile /etc/apache2/ssl/site.site.com-key.pem

</VirtualHost>
<小时/>

(phase2_env) ubuntu@ip-10-0-10-125:~/django_portal/ci/site_portal/portal_site$ sudo service apache2 restart * 重新启动Web服务器apache2 [确定] (phase2_env) ubuntu@ip-10-0-10-125:~/django_portal/ci/site_portal/portal_site$

关于python - 如何在 apache 上配置 django 并在 apache 中创建 https 以便 django url 应使用 https ://<ip>:80 port 打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35270336/

相关文章:

python - 获取 Django View 中的复选框列表

python - 接受并返回多个值的函数

python - 创建一个将列表作为输入并返回该列表中字符串 “bebo” 的计数的函数

css - 找不到或无法读取要导入的文件 : bootstrap-sass

javascript - 通过django模板将geojson添加到传单 map 中

php - 从 htaccess 中删除 php 扩展,但保持 PATH_INFO 功能

php - Laravel 5.2 多个 file_get_contents() 不适用于产品

apache - 在单个端口上运行两个 tomcat

python - 为什么 python int() 会这样工作?

python - GAE 数据存储缓存键与过滤器