PHP(ピー・エイチ・ピー)は、 "The PHP Group" によってコミュニティベースで開発されているオープンソースの汎用プログラミング言語およびその公式の処理系であり、特にサーバーサイドで動的なウェブページを作成するための機能を多く備えていることを特徴とする。
名称の PHP は再帰的頭字語として、 "PHP: Hypertext Preprocessor" を意味するとされており、「PHPはHTMLのプリプロセッサである」とPHP自身を再帰的に説明している。
ややこしいネーミングですね?
そんなことにこだわらなくてもいいのにと思いました。
Ubuntu 24.04 LTSにPHPをインストールするメモ。
Ubuntu 24.04 の公式リポジトリからインストールできる PHP のバージョンを確認するには、以下のコマンドを実行します。
apt-cache policy php
このコマンドを実行すると、利用可能な PHP のバージョンとインストール候補が表示されます。
出力例
php: Installed: (none) Candidate: 2:8.3+93ubuntu2 Version table: 2:8.3+93ubuntu2 500 500 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 Packages
apt list -a php
このコマンドを実行すると、リポジトリ内の PHP のバージョン一覧が表示されます。
出力例
Listing... Done php/noble 2:8.3+93ubuntu2 all
apt search php | grep -E '^php/'
これにより、リポジトリにある PHP のバージョンを含むパッケージ一覧を表示できます。
出力例
WARNING: apt does not have a stable CLI interface. Use with caution in scripts. php/noble 2:8.3+93ubuntu2 all
apt show php
このコマンドを実行すると、パッケージの詳細情報が表示され、現在のデフォルトのバージョンが確認できます。
出力例
Package: php Version: 2:8.3+93ubuntu2 Priority: optional Section: php Source: php-defaults (93ubuntu2) Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 11.3 kB Depends: php8.3 Download-Size: 4,076 B APT-Sources: http://jp.archive.ubuntu.com/ubuntu noble/main amd64 Packages Description: server-side, HTML-embedded scripting language (default) PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. . This package is a dependency package, which depends on latest stable PHP version (currently 8.3).
Ubuntu の公式リポジトリでは、通常 LTS リリースに対応した安定版の PHP が提供されますが、より新しいバージョンを使用したい場合は ondrej/php PPA を追加することも可能です。
sudo add-apt-repository ppa:ondrej/php sudo apt update
その後、上記のコマンドを再度実行すると、新しいバージョンの PHP がインストール可能かどうかを確認できます。
Ubuntu 24.04 に apt コマンドを使用して PHP をインストールする方法を説明します。
まずは、パッケージリストを最新の状態に更新します。
sudo apt update
Ubuntu 24.04 の公式リポジトリでは、PHP 8.3 が提供されているため、以下のコマンドでインストールできます。
sudo apt install php
このコマンドを実行すると、php-cli, php-common などの基本的なパッケージも一緒にインストールされます。
出力例
Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.3 libapr1t64 libaprutil1-dbd-sqlite3 libaprutil1-ldap libaprutil1t64 liblua5.4-0 php-common php8.3 php8.3-cli php8.3-common php8.3-opcache php8.3-readline ssl-cert Suggested packages: apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser php-pear The following NEW packages will be installed: apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.3 libapr1t64 libaprutil1-dbd-sqlite3 libaprutil1-ldap libaprutil1t64 liblua5.4-0 php php-common php8.3 php8.3-cli php8.3-common php8.3-opcache php8.3-readline ssl-cert 0 upgraded, 18 newly installed, 0 to remove and 1 not upgraded. Need to get 6,998 kB of archives. After this operation, 30.5 MB of additional disk space will be used. Do you want to continue? [Y/n] Y ←Yと入力する
インストールが完了したら、以下のコマンドで PHP のバージョンを確認できます。
php -v
出力例(PHP 8.3 の場合)
PHP 8.3.6 (cli) (built: Dec 2 2024 12:36:18) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.6, Copyright (c) Zend Technologies with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
必要に応じて、以下のような追加モジュールをインストールできます。
モジュール | 説明 | インストールコマンド |
----------- | ------ | --------------------- |
php-cli | コマンドラインで PHP を実行 | sudo apt install php-cli |
php-fpm | Web サーバー(Nginx など)で使用する PHP-FPM | sudo apt install php-fpm |
php-mysql | MySQL/MariaDB 用の PHP モジュール | sudo apt install php-mysql |
php-curl | cURL ライブラリを利用する | sudo apt install php-curl |
php-xml | XML 処理機能 | sudo apt install php-xml |
php-mbstring | マルチバイト文字列(日本語対応) | sudo apt install php-mbstring |
php-zip | ZIP 圧縮機能 | sudo apt install php-zip |
php-gd | 画像処理用 GD ライブラリ | sudo apt install php-gd |
例: すべての追加モジュールをインストールする場合
sudo apt install php-cli php-fpm php-mysql php-curl php-xml php-mbstring php-zip php-gd
出力例
Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libzip4t64 php8.3-curl php8.3-fpm php8.3-gd php8.3-mbstring php8.3-mysql php8.3-xml php8.3-zip Suggested packages: php-pear The following NEW packages will be installed: libzip4t64 php-cli php-curl php-fpm php-gd php-mbstring php-mysql php-xml php-zip php8.3-curl php8.3-fpm php8.3-gd php8.3-mbstring php8.3-mysql php8.3-xml php8.3-zip 0 upgraded, 16 newly installed, 0 to remove and 1 not upgraded. Need to get 2,863 kB of archives. After this operation, 8,758 kB of additional disk space will be used. Do you want to continue? [Y/n] Y ←Yと入力する
sudo apt install libapache2-mod-php sudo systemctl restart apache2
これで、Apache で PHP が動作するようになります。
sudo apt install php-fpm sudo systemctl restart php8.3-fpm
Nginx の設定ファイルで fastcgi_pass を設定し、PHP を動作させます。
php -v
1. /var/www/html に info.php を作成
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
2. Web ブラウザで http://<サーバーIP>/info.php にアクセス
Ubuntu の公式リポジトリでは最新の安定版(現在は PHP 8.3)が提供されますが、異なるバージョンをインストールしたい場合は ondrej/php PPA を利用するのが一般的です。
sudo add-apt-repository ppa:ondrej/php sudo apt update
その後、特定のバージョン(例: PHP 8.2)をインストール
sudo apt install php8.2 php8.2-fpm php8.2-mysql
特定のバージョンをデフォルトにする場合は update-alternatives を使用します。
sudo update-alternatives --config php