ゼロからLinux(Fedora)でpython djangoの実行・開発環境を作る(1 of 2)

この記事の目的は以下。

  • hyper-vを利用し、まっさらなFedora 35の仮想環境を作成する
  • その上にデスクトップ環境を作り、python djangoの実行環境、Visual Studio CodeのLinux版をインストールして開発環境も作る
  • 家のどこのPCからでも接続して作業ができるようにする
  • djangoで作るアプリのコードはgithubで管理できるようにする

Fedoraをインストール

https://getfedora.org/ja/server/download/

netinstallのISOイメージを落としてくる。そのあとhyper-vでクイック作成で新規マシンを作成

メモリのサイズを4096MBに調整して起動。しばらく待つとインストール画面に来る。構成を選ぶことができるけど、おそらくGNOMEが入ってくるFedora Workstationを選んでLibre Officeだけは追加しておく。ほかのを選んでもあとでいまいちなー、ってなりそうですし。

「インストール開始」を押したら、他のことをしながらしばらく放置。やがてデスクトップ環境にログインできました。

ルートボリューム(ディスク)サイズの調整

デフォルトのインストーラがあまり賢くないのか、ちゃんとやらなかったから悪いのか、なぜかルートにマウントされているパーティションが125Gのうち15Gしか使ってない。とりあえず30GBくらいは割り当ててる状態にしたいところ。ネットで調べて、以下のコマンドで拡張できた。結果の記載は省略。

$ df -h
$ pvscan --cache
$ sudo vgdisplay
$ sudo lvdisplay
$ sudo lvextend -L +20G /dev/fedora/root
$ sudo xfs_growfs /dev/fedora/root
$ df -h

SELinux無効化

特に不要なので無効化。以下の設定ファイル変更後に再起動。

$ sudo vim /etc/selinux/

SElinux=disabled

リモートデスクトップ接続

TigerVNCとXRDPを使って、Windows PCからリモートデスクトップ接続を可能にする。

$ sudo dnf insatll xrdp tigervnc-server
$ sudo systemctl start xrdp.services
$ sudo systemctl enabled xrdp.services
$ sudo firewall-cmd --permanent --add-port=3389/tcp
$ sudo firewall-cmd --reload

Visual Studio Code のインストール

$ sudo vim /etc/yum.repos.d/vscode.repo

[vscode]
name=Visual Studio Code
baseurl=https://packages.microsoft.com/yumrepos/vscode
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc

$ sudo dnf update
$ sudo dnf install code

MySQLサーバーのインストール

dnf でインストールしたあとにデフォルトでできているデータベースを確認して、新規にユーザを作成して、新規にテストアプリ用のデータベースも作成して権限も付与する。DB名、ユーザ名、パスワードなどは適宜書き換えて使う。

$ sudo dnf install community-mysql-server community-mysql-libs community-mysql-devel

$ mysql --version
mysql  Ver 8.0.27 for Linux on x86_64 (Source distribution)

$ sudo systemctl start mysqld
$ sudo systemctl enable mysqld

$ sudo mysql

> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

> CREATE DATABASE testapp;
> CREATE USER '<user>'@'localhost' IDENTIFIED BY '<password>';
> GRANT ALL PRIVILEGES ON testapp.* TO '<user>'@'localhost';
> quit



Python djangoのインストール

pipでpython djangoをインストールする最短手順。最初にgccとpython3-develをインストールするのはpipでmysqlクライアントをインストールするときになぜか必要になるため。

$ sudo dnf install gcc python3-devel
$ cd ~
$ sudo dnf install python3-pip python3-mysqlclient
$ mkdir py
$ cd py

#venvする
$ python -m venv testappenv
$ source testappenv/bin/activate

#pipを最新バージョンにする
$ pip install -U pip

$ pip install mysqlclient

$ django-admin startproject testapp

$ cd testapp
$ vim ./testapp/settings.py
DATABASES = {
    'default': {
       'ENGINE': 'django.db.backends.mysql',
       'NAME': 'testapp',
       'USER': '<USER>',
       'PASSWORD': '<PASSWORD>',
       'HOST': 'localhost',
       'PORT': '3306',
    }
}

$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py runserver 0.0.0.0:8001

ここまで来たら、一旦Djangoが動いていることを確認できる。

何やかや使うのがDjangoの標準のAdmin画面。これを使えるようにする。runserverはいったんCtrl+Cで閉じる。

$ python manage.py createsuperuser
Username (leave blank to use xxxxx: admin
Email address:  
Password: 
Password (again): 
Superuser created successfully.

もう一度 runserver したらここに来れる。

いったんここまで。

Leave a Comment

Your email address will not be published. Required fields are marked *