勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ

Podmanコンテナ環境にCodeIgniter 4.7.4をインストールしHello Worldページ表示まで行う方法

  PHP CodeIgniter

以前LAMP環境にCodeIgniter 4系をインストールしたが、ディレクトリ構成については考えていなかった。そのため適切と思われるディレクトリ構成にてPodmanコンテナ環境(PHP 8.4.25)にCodeIgniter 4.7.4をインストールし、Hello Worldページ表示まで行う方法をメモ。

 

やりたいこと

  • 「/var/www/html/test.com/ci4_test」ディレクトリにCodeIgniter 4をインストール
  • ブラウザからは「test.com/admin/ci4_test」でアクセス

尚、通常のWeb公開用ディレクトリは「/var/www/html/podman/test.com/public」になる。

 

対応方法

CodeIgniterのインストール

「/var/www/html/test.com/ci4_test」ディレクトリを作成し、以下でインストール。

podman exec \
    -w /var/www/html/test.com/ci4_test \
    web-container \
    composer create-project codeigniter4/appstarter .

apache側設定

バーチャルホスト設定用ファイル(vhost.conf)に以下の通り追記する。

<VirtualHost *:80>
    ServerName test.com
    ServerAlias www.test.com

    DocumentRoot /var/www/html/test.com/public

    RemoteIPHeader X-Forwarded-For
    RemoteIPInternalProxy 127.0.0.1 10.0.2.2 172.17.0.1 10.89.0.0/16

    <Directory /var/www/html/test.com/public>
        AllowOverride All
        Require all granted
    </Directory>

    # 追記ここから
    Alias /admin/ci4_test "/var/www/html/test.com/ci4_test/public"
    <Directory "/var/www/html/test.com/ci4_test/public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    # 追記ここまで

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

ファイル権限設定

過去記事で作成したシェルスクリプトでファイル権限を設定する。

set_perms.sh

CodeIgniter側設定

/ci4_test/public/.htaccess

以下の通り2か所追記する。

# Disable directory browsing
Options -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

    # 追記個所1つ目
    RewriteBase /admin/ci4_test/

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # 追記個所2つ目
    # .envファイルとappディレクトリへのアクセスを禁止
    RewriteRule ^(\.env|app/?|app/.*)$ - [F,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
ServerSignature Off
# Disable server signature end

/ci4_test/.env

以下2か所を追記。

CI_ENVIRONMENT = development
app.baseURL = 'https://test.com/admin/ci4_test/'

/ci4_test/app/Controllers/HelloWorld.php

<?php
namespace App\Controllers;
class HelloWorld extends BaseController
{
    public function index(): string
    {
        return view('HelloWorld');
    }
}

/ci4_test/app/Views/HelloWorld.php

<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello world</title>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

/ci4_test/app/Config/Routes.php

以下を追記。

$routes->get('/HelloWorld', 'HelloWorld::index');

動作確認

ここまでで問題無ければ「test.com/admin/ci4_test/HelloWorld」にアクセスするとページが表示され、「test.com/admin/ci4_test/.env」「test.com/admin/ci4_test/app」にアクセスするとapache側で403になる筈。

 - PHP CodeIgniter

  関連記事

PHP8.4 & CodeIgniter4でDateTimeImmutableエラー
PHP8.4 & CodeIgniter4でDateTimeImmutableエラー

CodeIgniter4.4.4がPHP8.3だと正常に動作していたが、PHP8 ...

CodeIgniter3でCronを実行する方法
CodeIgniter3でCronを実行する方法

CodeIgniter3系で特定の処理をCronで自動実行したかった。以下に設定 ...

CodeIgniter4で簡易版ログインシステムの実装方法(管理画面向け)
CodeIgniter4で簡易版ログインシステムの実装方法(管理画面向け)

CodeIgniter4で管理画面向けの簡易版ログインシステムを作成したい。通常 ...

CodeIgniter4で独自の404ページを表示する方法
CodeIgniter4で独自の404ページを表示する方法

CodeIgniter4にて存在しないページをブラウザから開くと、デフォルトの状 ...

CodeIgniter3でログをファイルに出力する方法
CodeIgniter3でログをファイルに出力する方法

Codeigniter3でエラーログの設定を行い特定のファイルに出力させ当該ログ ...