LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

①Django 環境構築

Last updated at Posted at 2020-03-24

はじめに

本記事では、Djangoの環境構築時のめもを備忘録として書いたものです。

前提条件

  • OS環境は、Virtualboxで稼働中のCentOS8.1です。以下、OS詳細情報となります。
# cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core)
  • Python3の環境が既に構築済みであること。 ※以下が、Pythonのバージョンである。
# python -V
Python 3.7.6
  • 今回は、DjangoのデフォルトのDBである「SQLite」は使用せずに、PostgreSQL12を使用する想定で環境構築をおこなう。

手順概要

手順は、以下の順におこなっていきます。

(1)PostgreSQL環境整備
(2)Djangoインストール

(1)PostgreSQL環境整備

(1.1)PostgreSQL12をインストール

▼既存環境のPostgreSQLが入っていないことを確認

# rpm -qa | grep postgres

▼PostgreSQL12のリポジトリをインストール

# dnf -y install https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-8-x86_64/pgdg-redhat-repo-42.0-6.noarch.rpm

▼リポジトリ情報を確認

# dnf info pgdg-redhat-repo

▼既存のPostgreSQLモジュールを削除

# dnf -y module disable postgresql

▼PostgreSQL12をインストール

# dnf -y install postgresql12-server postgresql12-devel

※postgresql-develが、Pythonの接続時に必要なようなので、併せてインストール

▼ログイン時の読み込みファイルにPostgreSQL12の設定を追記(末尾に追記)

/etc/profile

export PATH=/usr/pgsql-12/bin/:$PATH

▼設定ファイルの再読み込みを実施

# source /etc/profile

▼バージョン確認

# postgres --version
↓以下出力結果
postgres (PostgreSQL) 12.2

※PostgreSQLのバージョンが12であること

(1.2)PostgreSQLのセットアップ

▼データベースの初期化

# postgresql-12-setup initdb

▼PostgreSQL12のアクセス設定で、「md5」を指定

/var/lib/pgsql/12/data/pg_hba.conf
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
↓
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             192.168.56.0/24         md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

※Virtualboxでの検証なため、ローカルネットワーク(192.168.56,0/24)も許可しとく

▼PostgreSQL12の起動・自動起動

# systemctl start postgresql-12 && systemctl enable postgresql-12

▼PostgreSQLのログイン確認

# sudo -u postgres psql -U postgres

※ログインができればOK!!

▼Djangoにアクセスする用のユーザを作成

postgres=# CREATE USER django WITH PASSWORD 'django';

※今回は、Djangoにアクセスする用のユーザは、以下で設定

user:django
pass:django

▼Djangoプロジェクト用のデータベースを作成

postgres=# CREATE DATABASE djangodb OWNER django;

▼データベース作成後の確認

postgres=# \l djangodb

                              List of databases
   Name   | Owner  | Encoding |   Collate   |    Ctype    | Access privileges
----------+--------+----------+-------------+-------------+-------------------
 djangodb | django | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
(1 row)

▼PostgreSQLから抜ける

postgres=# \q

(2)Djangoインストール

(2.1)仮想環境を設定

今回は、Django専用の仮想環境を用意して、その中に構築。

▼virtualenvをインストール

# pip install virtualenv

▼仮想環境「django」を作成

# virtualenv django

▼仮想環境の有効化(仮想環境にはいる)

# source django/bin/activate

※ちなみに抜ける時は、「deactivate」コマンドを実施する。

(2.2)Djangoをインストール

▼Django関連のパッケージをインストール

# pip install django==2.2.5 psycopg2 psycopg2-binary django-bootstrap4

※今回使用するDjangoのバージョンは「2.2.5」でおこなう。

▼インストール済みパッケージの確認

# pip list
↓以下、出力結果
Package           Version
----------------- -------
beautifulsoup4    4.8.2
Django            2.2.5
django-bootstrap4 1.1.1
pip               20.0.2
psycopg2          2.8.4
psycopg2-binary   2.8.4
pytz              2019.3
setuptools        46.0.0
soupsieve         2.0
sqlparse          0.3.1
wheel             0.34.2

▼Djangoバージョン確認

# python -m django --version

※Djangoのバージョンが、「2.2.5」であればOK

正常にインストールができていればOK。
次項に続く( ^ω^)・・・
②Django 環境設定&管理者サイト表示

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up