WindowsでPHP(Composer, Apache)

PHPを利用するために、PHPとApacheをインストールして使っていきます。

Windowsのネイティブ環境(WSLではない)で使います。アプリのインストールにはScoopをなるべく使うようにします。

PHPのインストール

Scoopに有るか確認

> scoop search php
Results from local buckets...

Name           Version              Source Binaries
----           -------              ------ --------
php-nts        8.3.10               main
php            8.3.10               main

> scoop info php

Name        : php
Description : A popular general-purpose scripting language that is especially suited to web development. (thread safe)
Version     : 8.3.10
Bucket      : main
Website     : https://windows.php.net
License     : PHP-3.01
Updated at  : 2024/07/31 1:29:27
Updated by  : github-actions[bot]
Binaries    : php.exe | php-cgi.exe | phpdbg.exe
Environment : PHP_INI_SCAN_DIR = <root>\cli;<root>\cli\conf.d;
Suggestions : extras/vcredist2022

versionsやその名もphpのバケットには歴代のバージョンがあるようだが、とりあえずmainのphpをいれてみる。php-ntsは(non-thread safe)でIISを使う時はコチラがいいようだ。

Scoopでインストール

> scoop install php
Installing 'php' (8.3.10) [64bit] from 'main' bucket
php-8.3.10-Win32-vs16-x64.zip (30.8 MB) [=====================================================================] 100%
Checking hash of php-8.3.10-Win32-vs16-x64.zip ... ok.
Extracting php-8.3.10-Win32-vs16-x64.zip ... done.
Running pre_install script...done.
Linking ~\scoop\apps\php\current => ~\scoop\apps\php\8.3.10
Creating shim for 'php'.
Creating shim for 'php-cgi'.
Creating shim for 'phpdbg'.
Persisting cli
Persisting php.ini-production
Running post_install script...done.
'php' (8.3.10) was installed successfully!
'php' suggests installing 'extras/vcredist2022'.

vcredist2022をインストール(一度インストールすれば削除してもよい)

**注意**
post_install script は管理者権限が必要でした。

> scoop search vcredist2022
Results from local buckets...

Name         Version       Source Binaries
----         -------       ------ --------
vcredist2022 14.40.33810.0 extras

> scoop info vcredist2022

Name        : vcredist2022
Description : Microsoft Visual C++ Redistributable for Visual Studio 2015-2022
Version     : 14.40.33810.0
Bucket      : extras
Website     : https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist
License     : Freeware
Updated at  : 2024/05/22 1:30:35
Updated by  : github-actions[bot]
Notes       : You can now remove this installer with 'scoop uninstall vcredist2022'

> scoop install vcredist2022
Installing 'vcredist2022' (14.40.33810.0) [64bit] from 'extras' bucket
vc_redist.x64.exe (24.2 MB) [=================================================================================] 100%
Checking hash of vc_redist.x64.exe ... ok.
vc_redist.x86.exe (13.2 MB) [=================================================================================] 100%
Checking hash of vc_redist.x86.exe ... ok.
Linking ~\scoop\apps\vcredist2022\current => ~\scoop\apps\vcredist2022\14.40.33810.0
Running post_install script...done.
'vcredist2022' (14.40.33810.0) was installed successfully!
Notes
-----
You can now remove this installer with 'scoop uninstall vcredist2022'

> scoop uninstall vcredist2022
Uninstalling 'vcredist2022' (14.40.33810.0).
Unlinking ~\scoop\apps\vcredist2022\current
'vcredist2022' was uninstalled.

PHPのバージョンを確認

> php -v
PHP 8.3.10 (cli) (built: Jul 30 2024 15:15:59) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.3.10, Copyright (c) Zend Technologies

Composerのインストール

Scoopに有るか確認

> scoop search composer
Results from local buckets...

Name     Version Source Binaries
----     ------- ------ --------
composer 2.7.7   main

> scoop info composer

Name        : composer
Description : Dependency Manager for PHP
Version     : 2.7.7
Bucket      : main
Website     : https://getcomposer.org
License     : MIT
Updated at  : 2024/06/12 2:05:12
Updated by  : github-actions[bot]
Binaries    : composer.ps1
Environment : COMPOSER_HOME = $persist_dir\home
Path Added  : <root>\home\vendor\bin
Suggestions : php | php-nts
Notes       : 'composer selfupdate' is aliased to 'scoop update composer'

Scoopでインストール

> scoop install composer
Installing 'composer' (2.7.7) [64bit] from 'main' bucket
composer.phar (2.9 MB) [======================================================================================] 100%
Checking hash of composer.phar ... ok.
Running pre_install script...done.
Linking ~\scoop\apps\composer\current => ~\scoop\apps\composer\2.7.7
Creating shim for 'composer'.
Adding ~\scoop\apps\composer\current\home\vendor\bin to your path.
Persisting home
'composer' (2.7.7) was installed successfully!
Notes
-----
'composer selfupdate' is aliased to 'scoop update composer'

Composerのバージョンを確認

> composer -V
Composer version 2.7.7 2024-06-10 22:11:12
PHP version 8.3.10 (C:\Users\tmacoto\scoop\apps\php\current\php.exe)
Run the "diagnose" command to get more detailed diagnostics output.

Apacheのインストール

Scoopに有るか確認

> scoop search apache
Results from local buckets...

Name                             Version             Source Binaries
----                             -------             ------ --------
apache                           2.4.62              main

> scoop info apache

Name        : apache
Description : The Apache HTTP Server ('httpd')
Version     : 2.4.62
Bucket      : main
Website     : https://www.apachelounge.com
License     : Apache-2.0
Updated at  : 2024/07/18 21:31:25
Updated by  : github-actions[bot]
Binaries    : bin\ab.exe | bin\abs.exe | bin\htcacheclean.exe | bin\htdbm.exe | bin\htdigest.exe | bin\htpasswd.exe | b
              in\httpd.exe | bin\httxt2dbm.exe | bin\logresolve.exe | bin\rotatelogs.exe

Scoopでインストール

> scoop install apache
Installing 'apache' (2.4.62) [64bit] from 'main' bucket
httpd-2.4.62-240718-win64-VS17.zip (11.2 MB) [================================================================] 100%
Checking hash of httpd-2.4.62-240718-win64-VS17.zip ... ok.
Extracting httpd-2.4.62-240718-win64-VS17.zip ... done.
Linking ~\scoop\apps\apache\current => ~\scoop\apps\apache\2.4.62
Creating shim for 'ab'.
Creating shim for 'abs'.
Creating shim for 'htcacheclean'.
Creating shim for 'htdbm'.
Creating shim for 'htdigest'.
Creating shim for 'htpasswd'.
Creating shim for 'httpd'.
Creating shim for 'httxt2dbm'.
Creating shim for 'logresolve'.
Creating shim for 'rotatelogs'.
Persisting htdocs
Persisting conf
Persisting logs
Running post_install script...done.
'apache' (2.4.62) was installed successfully!

Apacheのバージョンを確認

> httpd -v
Server version: Apache/2.4.62 (Win64)
Apache Lounge VS17 Server built:   Jul 16 2024 12:23:10

Apacheの設定

ScoopのWikiに書かれているコマンドで設定を追加する Apache with PHP

> iex (new-object net.webclient).downloadstring('https://gist.githubusercontent.com/nilkesede/c98a275b80b6d373131df82eaba96c63/raw/apache-php-init.ps1')
checking...
enabling PHP handler...
error: couldn't find C:\Users\tmacoto\scoop\apps\php\current\php7apache2_4.dll

エラーになる。
php7が固定で指定されていますね。。スクリプトを参考に手動で httpd.conf に設定を追加します。

C:\Users\tmacoto\scoop\persist\apache\conf\httpd.conf

# PHP setup
    LoadModule php_module 'C:/Users/tmacoto/scoop/apps/php/current/php8apache2_4.dll'
    AddHandler application/x-httpd-php .php
    PHPIniDir "C:\Users\tmacoto\scoop\apps\php\current"

Apacheの起動と確認

> httpd

ブラウザで http://localhost にアクセスしてみる。

PHPの確認
ドキュメントルートにテストプログラムを作成

> pushd "$(scoop which httpd | split-path)\..\htdocs"
> ls

    Directory: C:\Users\tmacoto\scoop\apps\apache\current\htdocs

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2007/06/12     3:53             46 index.html

> vi info.php

<?php phpinfo(); ?>

ブラウザで http://localhost/info.php にアクセスしてみる。

いけたっぽいね。

Apacheのサービス化(しない)

まず sudo をインストールする。

> scoop search sudo
Results from local buckets...

Name    Version      Source Binaries
----    -------      ------ --------
gsudo   2.5.1        main
psutils 0.2023.06.28 main   sudo.ps1
sudo    0.2020.01.26 main

> scoop info sudo

Name        : sudo
Description : An approximation of the Unix sudo command. Shows a UAC popup window unfortunately.
Version     : 0.2020.01.26
Bucket      : main
Website     : https://github.com/lukesampson/psutils
License     : MIT
Updated at  : 2021/01/07 21:58:34
Updated by  : Jakub ト古。bera
Binaries    : sudo.ps1

> scoop install sudo
Installing 'sudo' (0.2020.01.26) [64bit] from 'main' bucket
sudo.ps1 (2.2 KB) [============================================================================================] 100%
Checking hash of sudo.ps1 ... ok.
Linking ~\scoop\apps\sudo\current => ~\scoop\apps\sudo\0.2020.01.26
Creating shim for 'sudo'.
'sudo' (0.2020.01.26) was installed successfully!

Apacheをサービスに登録する

> sudo httpd -k install -n apache
Installing the 'apache' service
The 'apache' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
> sudo net start apache
apache サービスを開始します.
apache サービスは正常に開始されました。

再度、ブラウザでアクセスして動作するのを確認

サービスから削除する方法も書いておきます。

sudo net stop apache
sudo httpd -k uninstall -n apache

サービス化するとPHPのモジュールがうまく読み込めない(現状)
Scoopを使う理由の一つとして管理者権限が要らないということもあるので、サービスは使わないように
します
なので、サービスから一旦解除します。

ApacheでPHPを利用するための設定(追記)

PHPへのPathを設定しないとうまくモジュールを読み込めないようだ。

環境変数を編集

ユーザの環境変数
Path
新規
C:\Users\tmacoto\scoop\apps\php\current

コメント