投稿日:
Djnago+Apacheサーバにhttpsを設定する
ConoHaVPSにセットアップしたDjangoをhttps化します。
httpsにするには?
Webサイトを信頼できるものにするためには、常時https化する必要があります。前回は、Let’s Encryptサービスを利用して「SSL/TLSサーバ証明書」を無料で発行しました。今回は、サーバのURLを「https」にするため、Apacheの設定ファイルに証明書を設定します。
バーチャルホスト設定
バーチャルホストとは、1台のサーバを使って複数のドメイン名のWebサーバを提供する機能です。ドメインは1つのみ使用する予定ですが、クライアントが「http」にアクセスしてきたら「https」に自動でリダイレクトするようにせっていするため、バーチャルホスト機能を利用します。
作業の流れ
- Apacheのバーチャルホスト設定
- https化の設定
前提条件
今回の作業を行うにあたり、以下の前提条件を満たしているとします。
- すでにDjango サイトが動いている状態
- すでにDjangoプロジェクトの静的ファイル設置場所を指定している
- Web サーバに Apache を使っていること
- Let’s Encryptで証明書が発行できていること
準備
以下の準備を行います。
ConoHaVPSの準備
①ConoHaをレンタルします。
②ConoHaにroot以外のユーザを作ります。
③ConoHaにSSH接続するための設定を行います。
④ConoHaへのrootログインを禁止します。
⑤ConoHaのportを22から変更します。
⑥ConoHaVPSへApacheをインストールしてスタートページを表示します。
Python・Djangoの準備
①ConoHaVPSへPython3をインストールします。
②ConoHaVPSへPython3をインストールします。
③ConoHaVPSへWSGIをインストールします。
→ConoHaのVPSにApacheでPythonWebアプリを作る(WSGI)
④ConoHaVPSへDjangoをインストールします。
⑤Djangoのデータベース接続・初期化します。
⑥Djangoのプロジェクトにアプリケーションを作成します。
Apache・Webサーバサイドの準備
①Let’s Encryptで証明書を発行します。
→Djnago+ApacheサーバにLet’s Encryptで証明書を発行する
必要なもの
レンタルサーバ | ConoHaのVPSサーバ |
---|---|
独自ドメイン | ムームードメインで、独自ドメインのみ取得。2年契約で(2,000〜3,000円)くらい? |
ConoHaVPSの環境
アプリケーションサーバ OS | CentOS7.4 |
---|---|
Apache | 2.4.6 (CentOS) |
Python | 3.6.4 |
mod-wsgi | 4.5.24 |
Django | 2.0.2 |
mysqlclient | 1.3.12 |
PyMySQL | 0.8.0 |
クライアントPC
操作開発用のクライアントPCを用意します。windowsでもmacでも可能ですが、今回はubuntuを利用します。
OS | Ubuntu 16.04.1 LTS 64bit |
---|
Apacheのバーチャルホスト設定
すでにtest_djangoプロジェクトが作成済みとします。今回はtest_djangoプロジェクトに、pollsアプリケーションを作成します。
プロジェクト名 | test_django |
---|---|
アプリケーション名 | polls |
前回は、cerbotでLet’s Encryptの証明書を取得するため既にバーチャルホストの設定行いました。httpd.confの設定は削除し、Djangoの設定をwsgi.conf」ファイルへ記述します。以下は同じ内容ですが、https化する前に必要な設定です。
①VPSにログインします。
②前回作った仮想環境を起動します。
cd . test_django/bin/activate
変更前
- /etc/httpd/conf/
- httpd.conf
- magic
- /etc/httpd/conf.d/
- autoindex.conf
- ssl.conf
- userdir.conf
- welcome.conf
変更後
- /etc/httpd/conf/
- httpd.conf
- magic
- /etc/httpd/conf.d/
- autoindex.conf
- ssl.conf
- userdir.conf
- welcome.conf
- wsgi.conf
ssl.confは、cartbotのインストルが完了すると「/etc/httpd/conf.d」へ自動で作成されます。
vi /etc/httpd/conf/httpd.conf
以下のようにコメントアウトします。
#DocumentRoot "/var/www/cgi-bin/test_django" 〜省略〜 #ServerName 独自に取得したドメイン名.com:80
以下のようにDjangoサイトの設定を削除します。
#以下すべて削除 LoadModule wsgi_module /home/username/test_django/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so WSGIPythonPath /var/www/cgi-bin/test_django WSGIScriptAlias /test_django /var/www/cgi-bin/test_django/test_django/wsgi.py <Directory /var/www/cgi-bin/test_django/test_django1> <Files wsgi.py> Require all granted </Files> </Directory> Alias /static/ /var/www/cgi-bin/test_django/static/ <Directory /var/www/cgi-bin/test_django/static/> Require all granted </Directory>
④新たに、wsgi.confを作成します。そこに、Djangoサイトの設定を記述します。
vi /etc/httpd/conf/wsgi.conf
以下のように編集します。
NmeVirtualHost *:80 LoadModule wsgi_module /home/username/test_django/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so WSGIPythonPath /var/www/cgi-bin/test_django WSGIScriptAlias / /var/www/cgi-bin/test_django/test_django/wsgi.py <VirtualHost *:80> DocumentRoot /var/www/cgi-bin/test_django ServerName www.独自に取得したドメイン名.com ServerAlias 独自に取得したドメイン名.com <Directory /var/www/cgi-bin/test_django> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost> Alias /static/ /var/www/cgi-bin/test_django/static/ <Directory /var/www/cgi-bin/test_django/static/> Require all granted </Directory>
⑤Apache設定ファイルの文法が正しいか確認します。
apachectl configtest
⑥Apacheを再起動します。
systemctl restart httpd
⑤前回まで作ったページが開けるか確認します。以下のURLでアクセスします。
http://VPSのグローバルIPアドレス
OR
http://VPSに設定した独自ドメイン.com/
以下のように、ページが表示できれば成功です。
https化の設定
ベストでは無いと思いますが、「http://独自ドメイン名.com」が「https://独自ドメイン名.com」にリダイレクトする場合の例です。
①wsgi.confを編集します。
vi /etc/httpd/conf/wsgi.conf
以下のように編集します。
NameVirtualHost *:80 NameVirtualHost *:443 LoadModule wsgi_module /home/username/test_django/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so WSGIPythonPath /var/www/cgi-bin/test_django WSGIScriptAlias / /var/www/cgi-bin/test_django/test_django/wsgi.py <virtualhost *:443> DocumentRoot /var/www/cgi-bin/test_django ServerName 独自に取得したドメイン名.com ServerAlias 独自に取得したドメイン名.com SSLEngine On SSLCertificateFile /etc/letsencrypt/live/独自に取得したドメイン名/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/独自に取得したドメイン名/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/独自に取得したドメイン名/chain.pem <Directory /var/www/cgi-bin/test_django> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost> Alias /static/ /var/www/cgi-bin/test_django/static/ <Directory /var/www/cgi-bin/test_django/static/> Require all granted </Directory> <virtualhost *:80> ServerName 独自に取得したドメイン名.com RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] </virtualhost>
[R=301,L]
HTTPステータスコード 301指定することで、検索エンジンにページの移転を教えてあげるための設定です。
②Apacheを再起動します。
systemctl restart httpd
③前回まで作ったページが開けるか確認します。以下のURLでアクセスします。
http://VPSのグローバルIPアドレス
OR
http://VPSに設定した独自ドメイン.com/
以下のように、httpsページが表示できれば成功です。
- Python 114
- 制作 54
- RaspberryPi 41
- Django 40
- WordPress 40
- Linux 27
- VPS 22
- JavaScript 21
- PHP 20
- HTML・CSS 19
- AWS 16
- 仮想環境 15
- レスポンシブデザイン 13
- マイコン 11
- WEB全般 11
- 動画製作 9
- Webサービス 8
- 統合開発環境 8
- 機械学習 8
- PyCharm 7
- jQuery 7
- AfterEffects 7
- 起業・設立 7
- Django REST framework 6
- C# 6
- デザイン 6
- SEO 6
- pydata 6
- Visual Studio 5
- 数学 5
- 携帯サイト 5
- heroku 5
- Mac 5
- illustrator 5
- node.js 5
- Anaconda 5
- Nginx 4
- Jupyter Notebook 4
- インフラ 4
- Google Colaboratory 4
- symfony 4
- Webスクレイピング 3
- photoshop 3
- Go言語 3
- PC 3
- ツール 3
- Docker 3
- facebook 3
- 作業効率化 3
- データベース 3
- Cloud9 3
- コマンド 2
- micro:bit 2
- Kali Linux 2
- Webサーバー 2
- MariaDB 2
- ドローン 2
- コンテナ 2
- DaVinci Resolve 2
- ネットワーク 2
- Java 2
- movie 2
- PCDJ 2
- 音楽 2
- XSERVER 2
- Ansible 1
- Vue.js 1
- JSON 1
- Bootstrap 1
- バージョン管理システム 1
- SSL 1
- S3 1
- ムームードメイン 1
- ネットワーク 1
- アニメーション 1
- D3.js 1
- Rhino 1
- アニメ 1
- git 1
- windows 1
- アクセス解析 1
- スマートフォン 1
- アフィリエイトノウハウ 1
- 知識 1
- TypeScript 1
- 役立つ本・書籍 1
- データサイエンス 1
- ESP32 1
- AI 1
- ownCloud 1
- API 1