CentOS Stream 9にDjango 4をインストールする方法

環境
OSバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/redhat-release
CentOS Stream release 9
# cat /etc/redhat-release CentOS Stream release 9
# cat /etc/redhat-release
CentOS Stream release 9

pythonバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python --version
Python 3.9.10
# python --version Python 3.9.10
# python --version
Python 3.9.10

Djangoのインストール手順
1.Python venv 環境にアクセスします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python3 -m venv --system-site-packages ~/django
[root@localhost ~]# source ~/django/bin/activate
(django) [root@localhost ~]#
# python3 -m venv --system-site-packages ~/django [root@localhost ~]# source ~/django/bin/activate (django) [root@localhost ~]#
# python3 -m venv --system-site-packages ~/django
[root@localhost ~]# source ~/django/bin/activate
(django) [root@localhost ~]#

2. Django をインストールします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(django) [root@localhost ~]# pip3 install django==4
(django) [root@localhost ~]# pip3 install django==4
(django) [root@localhost ~]# pip3 install django==4

3.バージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(django) [root@localhost ~]# django-admin --version
4.0
(django) [root@localhost ~]# django-admin --version 4.0
(django) [root@localhost ~]# django-admin --version
4.0

4.venv環境 から exitします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(django) [root@localhost ~]# deactivate
(django) [root@localhost ~]# deactivate
(django) [root@localhost ~]# deactivate

5.テストプロジェクトを作成します
[root@localhost ~]# source ~/django/bin/activate
testprojectを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(django) [root@localhost ~]# django-admin startproject testproject
(django) [root@localhost ~]# cd testproject
(django) [root@localhost testproject]#
(django) [root@localhost ~]# django-admin startproject testproject (django) [root@localhost ~]# cd testproject (django) [root@localhost testproject]#
(django) [root@localhost ~]# django-admin startproject testproject
(django) [root@localhost ~]# cd testproject
(django) [root@localhost testproject]#

6.データベースを設定します
(django) [root@localhost testproject]# python manage.py migrate

7.管理者ユーザーを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: test@arkgame.com
Password: #パスワード入力
Password (again): #パスワード確認
Superuser created successfully.
# python manage.py createsuperuser Username (leave blank to use 'root'): root Email address: test@arkgame.com Password: #パスワード入力 Password (again): #パスワード確認 Superuser created successfully.
# python manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: test@arkgame.com
Password: #パスワード入力
Password (again): #パスワード確認
Superuser created successfully.

8.他ホストからアクセスを許可します
(django) [root@localhost testproject]# vi testproject/settings.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
28行目
修正前
ALLOWED_HOSTS = ['']
修正後
ALLOWED_HOSTS = ['*']
28行目 修正前 ALLOWED_HOSTS = [''] 修正後 ALLOWED_HOSTS = ['*']
28行目
修正前
ALLOWED_HOSTS = ['']
修正後
ALLOWED_HOSTS = ['*']

9.ポート番号8000を開放します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# firewall-cmd --zone=public --add-port=8000/tcp --permanent
success
# firewall-cmd --reload
success
# firewall-cmd --zone=public --add-port=8000/tcp --permanent success # firewall-cmd --reload success
# firewall-cmd --zone=public --add-port=8000/tcp --permanent
success
# firewall-cmd --reload
success

10.サーバーを起動します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
May 18, 2022 - 15:27:17
Django version 4.0, using settings 'testproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
# python manage.py runserver 0.0.0.0:8000 Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). May 18, 2022 - 15:27:17 Django version 4.0, using settings 'testproject.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.
# python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 18, 2022 - 15:27:17
Django version 4.0, using settings 'testproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

11.動作確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
http://192.168.71.152:8000/にアクセスします
「The install worked successfully! Congratulations!」が表示されます。
http://192.168.71.152:8000/にアクセスします 「The install worked successfully! Congratulations!」が表示されます。
http://192.168.71.152:8000/にアクセスします
「The install worked successfully! Congratulations!」が表示されます。

 

CentOS Stream 9

Posted by arkgame