[英]how to configure django on apache and make https in the apache so that django url should open with https://<ip>:80 port
我想讓django安全地在https服務器上運行,而無需使用第三方軟件包(例如,runsslserver或sslserver),因為我就是這種方法
在settings.py文件中,我已經配置了這些行。
SECURE_SSL_REDIRECT = True
所以當在瀏覽器中提供URL時,URL能夠重定向到https://192.168.31.2/cp_vm/details,但是我遇到了安全的SSL錯誤。
請建議我任何方式來獲得輸出,或者用https在apache上運行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顯示該IP無法連接。
我附上了apache 000.default.conf文件,請也幫我解決這個問題,如果上述任何錯誤都請您指點一下,請原諒我
我的配置文件如下:
<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服務器上使用https配置完成django網站+ apache 。 最后,我可以成功配置它,並且我的站點現在可以通過https正常運行。
以下是.py和apache配置文件 。 請更正您的文件,如果您仍然遇到任何問題,請告知我們。
假發
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服務apache2重新啟動*重新啟動Web服務器apache2 [確定](phase2_env)ubuntu @ ip-10-0-10- 125:〜/ django_portal / ci / site_portal / portal_site $
在HTTP服務器之上, 您需要使用Apache2 運行SSL服務器 ,即配置<VirtualHost *:443>
。 另外,我會讓Apache2處理重定向,而不是Django。
<VirtualHost *:80>
# Virtual host for the redirection
ServerName 10.206.51.6
ServerAdmin 10.206.51.6 # <- This should be an email!
DocumentRoot /dev/null/
Redirect permanent / https://10.206.51.6/ # Redirects also what is after the "/"
</VirtualHost>
<VirtualHost *:443>
# SSL site
ServerName 10.206.51.6
ServerAdmin 10.206.51.6 # <- This should be an email!
DocumentRoot /var/www/nfvs_portal/
SSLCertificateFile /etc/apache2/certs/your-server-certificate.pem
SSLCertificateKeyFile /etc/apache2/certs/your-server-certificate.key.pem
# ... [The rest of your site configuration]
</VirtualHost>
如果即使使用此配置也遇到麻煩,我將把問題分解為(1)使Apache2能夠重定向並在文檔根目錄中提供靜態SSL頁面(不使用WSGI),以及(2)修改該配置以使用您的Django網站。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.