LoginSignup
16
19

More than 1 year has passed since last update.

Ubuntu 20.04にPHPを複数インストールして切り替える方法(PPA・apt)

Last updated at Posted at 2021-09-16

はじめに

本書では、Ubuntu 20.04に、PHPの環境をインストールする方法をまとめます。

案件によってPHPのバージョンが違うこともあるため、複数のバージョンをインストールして、alternativesで切り替えられるようにします。

インストール手順

リポジトリ追加

sudo add-apt-repository ppa:ondrej/php 

のコマンドを実行します。

Press [ENTER] to continue or Ctrl-c to cancel adding it.

と表示されたら、Enterキーを押します。

apt update

sudo apt update

PHPをインストール

ここでは、本ページ執筆時点で最新の、8.0をインストールします。

sudo apt install -y php8.0

他に、複数バージョンの切り替えを確認するため、本ページ執筆時点で有効な、7.3、7.4もインストールします。

sudo apt install -y php7.3
sudo apt install -y php7.4

PHPモジュールをインストール

sudo apt search php8.0-*

を実行すると、

libapache2-mod-php8.0/focal 8.0.10-1+ubuntu20.04.1+deb.sury.org+1 amd64
  server-side, HTML-embedded scripting language (Apache 2 module)

libphp8.0-embed/focal 8.0.10-1+ubuntu20.04.1+deb.sury.org+1 amd64
  HTML-embedded scripting language (Embedded SAPI library)

php8.0/focal 8.0.10-1+ubuntu20.04.1+deb.sury.org+1 all
  server-side, HTML-embedded scripting language (metapackage)

php8.0-amqp/focal 1.10.2-8+ubuntu20.04.1+deb.sury.org+1 amd64
  AMQP extension for PHP

php8.0-apcu/focal 5.1.20+4.0.11-1+ubuntu20.04.1+deb.sury.org+1 amd64
  APC User Cache for PHP

php8.0-ast/focal 1.0.14-1+ubuntu20.04.1+deb.sury.org+1 amd64
  AST extension for PHP 7
・
・
・

のように、インストール可能なモジュールが表示されるので、必要なモジュールをインストールする。

例:MySQLを使う場合

sudo apt install -y php8.0-mysql

alternativesでバージョン切り替え

定義

sudo update-alternatives --install /usr/bin/php php /usr/bin/php7.3 100
sudo update-alternatives --install /usr/bin/php php /usr/bin/php7.4 110
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.0 120

切り替え方法

Priorityを選択して切り替える方法

sudo update-alternatives --config php
There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php8.0   120       auto mode
  1            /usr/bin/php7.3   100       manual mode
  2            /usr/bin/php7.4   110       manual mode
* 3            /usr/bin/php8.0   120       manual mode

Press <enter> to keep the current choice[*], or type selection number: 

のように表示されるので、Selectionの番号を入れてEnterキーを押すと、そのバージョンが有効になります。
※上記はPHP 8.0が有効になっている状態です。

php -v
PHP 8.0.10 (cli) (built: Aug 26 2021 15:50:07) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies

のように、選択したバージョンになっていればOKです。

コマンドで直接設定する方法

7.4を有効にする場合:

sudo update-alternatives --set php /usr/bin/php7.4
php -v
PHP 7.4.23 (cli) (built: Aug 26 2021 15:51:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.23, Copyright (c), by Zend Technologies

のように、選択したバージョンになっていればOKです。

WSL2 Ubuntu 20.04でのxdebug用の設定

WSL2 Ubuntu 20.04にPHPを入れ、XDebugを有効にしてデバッグしたので、備忘録のため、その手順をまとめます。

WSL2 Ubuntu 20.04での設定

phpX.X-xdebug(PHP8.0の場合、php8.0-xdebug)をaptでインストールします。

apt install -y php8.0-xdebug

次に、xdebugの設定ファイルを編集します。
php8.0の場合:/etc/php/8.0/mods-available/xdebug.ini

まず、

zend_extension=xdebug.so

の設定をコメントアウトします。

; zend_extension=xdebug.so

次に、末尾に下記を追加します。

zend_extension="/usr/lib/php/20200930/xdebug.so"

xdebug.log=/tmp/xdebug.log
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9003

xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_autostart = 1

なお、zend_extensionのパスはバージョンによって異なるので、xdebug.soのファイルを検索して、絶対パスを設定します。

Visual Studio Codeでのデバッグ設定

Visual Studio Code(以下、vscode)は、WSL2 Ubuntuで起動します。

cd {ソースのディレクトリ}
code ./

※初回は、色々とインストールされるので時間がかかりますので、しばらく待ちます。

次に、デバッグの設定 launch.jsonconfigurationの設定に、下記を追加します。

        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            // "stopOnEntry": true,        // 最初に制御が来たソースの先頭で必ずブレークする設定。(トレースが正しくできることが確認できたら、消して構わない。)
            "port": 9003
        },

なお、初めてデバッグの設定をするときは、stopOnEntryをtrueに設定し、デバッグの実行時にブレークすることを確認します。
正しくブレークしてデバッグできることが確認出来たら、stopOnEntryの設定をfalse(あるいはコメントアウト)にして大丈夫です。

16
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
19