Rocky Linux 9 + Django + VirtualBox + Apache + pyenv + mod_wsgi

Django のデプロイ

ユーザー名の「 user 」のディレクトリ内にデプロイするための手順です。

Linux カーネル Linux version 5.14.0-362.24.1.el9_3.0.1.x86_64
OS Rocky Linux release 9.4 (Blue Onyx)
Web サーバー Apache/2.4.57 (Rocky Linux)
プログラミング言語 python 3.11.9
フレームワーク Django 5.0.6
Apache 用のモジュール mode_wsgi 5.0.0

Rocky Linux 9 のアップグレード

sudo dnf upgrade

SELinux を無効にします。無効の方法は、以下のリンク先を参照します。

Apache のインストール

Apache のインストール

sudo dnf install -y httpd

Apache の自動起動

sudo systemctl enable httpd

Apache の起動

sudo systemctl start httpd

ファイヤーウォールのポートの解放設定

sudo firewall-cmd --add-service=http --zone=public --permanent
sudo firewall-cmd --add-service=https --zone=public --permanent
sudo firewall-cmd --reload

Pyenv をインストールする

Python のバージョン管理は、Pyenv を使います。

Pyenv に必要なパッケージのインストール

sudo dnf -y install gcc bzip2 bzip2-devel openssl openssl-devel readline readline-devel sqlite-devel tk-devel git

ホームディレクトリに pyenv のリポジトリをクローンします。

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

.bash_profile に pyenv のパスを追加します。

echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

pyenv がインストールされたか確認します。

pyenv --version

Python のインストール

インストール可能な Python のバージョンを調べます。

pyenv install --list

Python 3.11.9 をインストールします。

pyenv install 3.11.9

インストールしたバージョンの 3.11.9 に変更します。

pyenv global 3.11.9
pyenv rehash

Python のバージョンが切り替わったか確認します。

python -V

Django のインストール

Django をインストールします。

pip install django

mod_wsgi のインストール

mod_wsgi をインストールします。

mod_wsgi とは、WSGI (Web Server Gateway Interface) インターフェースに準拠した Python のプログラムを Apache で動作させるためのモジュールです。

pip install mod_wsgi

次のようなエラーが出た場合には、「 httpd-devel 」と「 wheel 」もインストールします。

Collecting mod_wsgi
  Downloading mod_wsgi-5.0.0.tar.gz (497 kB)
     qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 497.7/497.7 kB 6.0 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  x Getting requirements to build wheel did not run successfully.
  x exit code: 1
  mq> [20 lines of output]
      Traceback (most recent call last):
        File "/home/user/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/home/user/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/user/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-lizjcr2m/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-lizjcr2m/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-lizjcr2m/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 487, in run_setup
          super().run_setup(setup_script=setup_script)
        File "/tmp/pip-build-env-lizjcr2m/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 88, in <module>
      RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in the documentation for this package and install any missing Apache httpd server packages.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

x Getting requirements to build wheel did not run successfully.
x exit code: 1
mq> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

「 httpd-devel 」と「 wheel 」をインストールするには次のようにします。

sudo dnf install httpd-devel
pip install wheel

Django アプリケーションの作成

アプリケーションを作成するには次のようにします。

django-admin startproject mysite

アプリケーションのディレクトリに移動します。

cd mysite

アプリケーションを起動します。

python manage.py runserver

Apache の設定

「 /etc/httpd/conf/httpd.conf 」ファイルを次のように変更します。

VirtualBox で設定した際、割り当てられていた IP アドレスが 「 192.168.11.6 」のため、次のように変更します。

/etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80
Listen 192.168.11.6:8000

「 8000番のポート 」もアクセスできるように、ファイヤーウォールの設定を変更します。

sudo firewall-cmd --permanent --zone=public --add-port=8000/tcp
sudo firewall-cmd --reload

Apache の mod_wsgi の設定

user ディレクトリに pyenv をインストールし、Django アプリケーションの mysite を作った場合の設定です。

.so ファイルの場所を探します。.so ファイルとは 「 shared object 」の略で、共有ライブラリのファイルの一つです。

find /home/user/.pyenv -name *-linux-gnu.so

ファイルのパスがわかれば、Apache の modules ディレクトリにコピーします。

sudo cp -p /home/user/.pyenv/versions/3.11.9/lib/python3.11/site-packages/mod_wsgi/server/mod_wsgi-py311.cpython-311-x86_64-linux-gnu.so /etc/httpd/modules

Apache の追加の設定については、「 django.conf 」というファイルを作成し、そのファイルに設定をします。

「 /home/httpd/conf.d 」ディレクトリに移動し、「 django.conf 」ファイルを開きます。

cd /home/httpd/conf.d
sudo vi django.conf

django.conf ファイルの設定内容は以下です。

/home/httpd/conf.d/django.conf

LoadModule wsgi_module modules/mod_wsgi-py311.cpython-311-x86_64-linux-gnu.so

WSGIScriptAlias / /home/user/mysite/mysite/wsgi.py
WSGIDaemonProcess wsgi_app python-home=/home/user/.pyenv python-path=/home/user/mysite/mysite
WSGIPythonHome /home/user/.pyenv/versions/3.11.9
WSGIPythonPath /home/user/mysite

<Directory /home/user/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Alias /static/ /home/user/mysite/mysite/static/
<Directory /home/user/mysite/mysite/static>
  Require all granted
</Directory>

WSGIScriptAlias は、Apache に来たリクエストをどこにつなぐかを指定します。ここでは 「 /(ルート)」に来たリクエストを Django でつくったプロジェクトに飛ばしています。

WSGIDaemonProcess は、Linux ではデーモン(=サービス)として動かすのが推奨されています。

WSGIPythonHome は、mod_wsgi に使わせたい Python のパスを記述します。ディレクトリを指定します。

WSGIPythonPath は、プロジェクトのパスを指定します。

設定変更後、シンタックスエラーがないか確認し、Apache を再起動します。

sudo apachectl configtest
sudo systemctl restart httpd

settings.py の変更

IP アドレスを指定します。

/home/user/mysite/mysite/settings.py

ALLOWED_HOSTS = ['192.168.11.6']

指定しておかないと次のようなエラーになります。

ブラウザでアクセスする

ブラウザで「 192.168.11.6:8000 」を開いて次の画面が表示されれば成功です。

IP アドレスは、それぞれの環境のものを指定します。

ALLOWED_HOSTS = []