概要
DjangoをEC2インスタンス上で動かす環境を構築するまでにやったことを書いていく。
- その1:ユーザー作成
- その2:必要なソフトウェアのインストール
- その3:ソースコードの配置
- その4:「SQLite 3.8.3 or later is required」エラー対応
- その5:GunicornでDjangoアプリを起動する。
- その6:アプリケーションログを出力する。
- その7:Gunicornの前段にNginxを配置する。
- その8:静的コンテンツを配信できるようにする。
- その9:EC2をALBに組み込んでみる。
参考文献
- 『Pythonプロフェッショナルプログラミング 第3版』の「Chapter 11 環境構築とデプロイの自動化」
- 『現場で使える Django の教科書《実践編》』の「第7章 デプロイ」
その4:「SQLite 3.8.3 or later is required」エラー対応
概要
前回、ソースコードの配置まで完了したので、python3 manage.py runserver
で起動できるかどうか試してみたところ、以下のエラーが発生した。
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
今回はこのエラーに対応する。
エラーの内容
エラーメッセージの内容より、DjangoはSqlite3.8.3以上を要求しているが、Amazon Linux2にインストールされているSqliteのバージョンは3.7.17であることが原因であると理解した。
バージョンを確認してみると確かに3.7.17である。
$ sqlite3 -version 3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668 $
手順
・前提
adminユーザーで作業する。
・DjangoからMariaDBを使用する準備
・MariaDBをインストールする。
$ sudo yum install mariadb-server
・mysqlclientのインストールに必要な各種モジュールをインストールする。
→ 以前に検証した際に、mysqlclientをインストールする前に、事前にインストールしておかなければならないモジュールがあることが分かっているので、以下をまとめてインストールする。
$ sudo yum install python3-devel mysql mysql-devel gcc
・mysqlclientをインストールする。
$ sudo pip3 install mysqlclient
・mariadbを起動する。
$ sudo systemctl start mariadb $ ps aux | grep "mariadb" | grep -v "grep" mysql 21953 0.4 8.4 967876 85048 ? Sl 01:39 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock $
・mariadbにログインする。
→ 初期パスワードは空文字
$ mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.64-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
・DATABASEを作成する。
> create database mysite; > show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysite | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) >
・Djangoの設定
・settings.pyを以下のように変更する。
[変更前]
DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(BASE_DIR, "db.sqlite3"), } }
[変更後]
DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", "NAME": "mysite", "USER": "root", "PASSWORD": "", "HOST": "localhost", "PORT": "3306" } }
・動作確認
・サーバーを起動する。
$ python3 manage.py runserver
・ターミナルをもう一つ開いてcurlしてみる。
$ curl -I "http://localhost:8000/admin/" HTTP/1.1 302 Found Date: Mon, 25 May 2020 02:06:11 GMT Server: WSGIServer/0.2 CPython/3.7.6 Content-Type: text/html; charset=utf-8 Location: /admin/login/?next=/admin/ Expires: Mon, 25 May 2020 02:06:11 GMT Cache-Control: max-age=0, no-cache, no-store, must-revalidate, private X-Frame-Options: DENY Content-Length: 0 Vary: Cookie X-Content-Type-Options: nosniff $
→ 302
が返ってくるので、サーバーは正常に起動しており、疎通も取れていることが確認できた。
以上で「その4:「SQLite 3.8.3 or later is required」エラー対応」は完了。
その5に続く。