LoginSignup
2
1

More than 1 year has passed since last update.

php のバージョンが混在する環境で特定バージョンのphp-fpmを使用する Homestead 編

Posted at

Docker 優勢の現代に、ローカル環境で Homestead を使用している人は少ないかもしれませんが、メモです。

環境(前提)

Homestead で php7 .2の環境を構築し、Laravel6 系を動かしていました

起こったエラー

ある日突然、下記エラーを吐いてWebページが502で開けなくなりました。

[crit] 861#861: *2 connect() to unix:/var/run/php/php7.2-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.10.1, server: hoge.local, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "hoge.local"

原因

エラーにある通り /var/run/php/ の配下を見てみると以下の通り、php8.0 用の fpm しか存在していませんでした。

$ ll /var/run/php/
total 4
drwxr-xr-x  2 www-data www-data  100 Mar 17 09:55 ./
drwxr-xr-x 37 root     root     1240 Mar 17 10:02 ../
-rw-r--r--  1 root     root        3 Mar 17 09:55 php8.0-fpm.pid
srw-rw-rw-  1 vagrant  vagrant     0 Mar 17 09:55 php8.0-fpm.sock=
lrwxrwxrwx  1 root     root       30 Mar 17 09:55 php-fpm.sock -> /etc/alternatives/php-fpm.sock=

なんで今まで動いていたの?という疑問を抱きつつ、まぁ動くようにしていきます。

確認

私が動かしたいのは php7.2 用の fpm です。存在するか確認します。

$ systemctl list-unit-files | grep php
php5.6-fpm.service                            disabled        enabled      
php7.0-fpm.service                            disabled        enabled      
php7.1-fpm.service                            disabled        enabled      
php7.2-fpm.service                            disabled        enabled      
php7.3-fpm.service                            disabled        enabled      
php7.4-fpm.service                            disabled        enabled      
php8.0-fpm.service                            enabled         enabled      
phpsessionclean.service                       static          enabled      
phpsessionclean.timer                         enabled         enabled      

居ますね。居ますが disabled です。8.0を disabled にして 7.2 を enabled にしたいです。今日のゴールは多分ここ。

やったこと

まず、 php8.0-fpm.service これを止めます

$ sudo systemctl stop php8.0-fpm.service

この時点ではまだ 8.0 が enabled です。
この通り。

$ systemctl list-unit-files | grep php
php5.6-fpm.service                            disabled        enabled      
php7.0-fpm.service                            disabled        enabled      
php7.1-fpm.service                            disabled        enabled      
php7.2-fpm.service                            disabled        enabled      
php7.3-fpm.service                            disabled        enabled      
php7.4-fpm.service                            disabled        enabled      
php8.0-fpm.service                            enabled         enabled      
phpsessionclean.service                       static          enabled      
phpsessionclean.timer                         enabled         enabled  

ちなみに、止めたのでここはこんな感じになります。

ll /run/php/
total 0
drwxr-xr-x  2 www-data www-data   60 Mar 17 10:55 ./
drwxr-xr-x 37 root     root     1240 Mar 17 10:45 ../
lrwxrwxrwx  1 root     root       30 Mar 17 09:55 php-fpm.sock -> /etc/alternatives/php-fpm.sock

8.0 の fpm が消えてます。

やったこと

$ sudo systemctl disable php8.0-fpm.service
Synchronizing state of php8.0-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable php8.0-fpm
Removed /etc/systemd/system/multi-user.target.wants/php8.0-fpm.service.

確認します

$ systemctl list-unit-files | grep php
php5.6-fpm.service                            disabled        enabled      
php7.0-fpm.service                            disabled        enabled      
php7.1-fpm.service                            disabled        enabled      
php7.2-fpm.service                            disabled        enabled      
php7.3-fpm.service                            disabled        enabled      
php7.4-fpm.service                            disabled        enabled      
php8.0-fpm.service                            disabled        enabled      
phpsessionclean.service                       static          enabled      
phpsessionclean.timer                         enabled         enabled 

disabled になってますね。では、7.2 を 起動 します。

$ sudo systemctl start php7.2-fpm.service
$ sudo systemctl status php7.2-fpm.service
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-03-17 10:59:00 UTC; 15s ago
       Docs: man:php-fpm7.2(8)
    Process: 4289 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.2/fpm/pool.d/www.conf 72 (code=exited, status=0/SUCCESS)
   Main PID: 4274 (php-fpm7.2)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2279)
     Memory: 11.3M
     CGroup: /system.slice/php7.2-fpm.service
             ├─4274 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
             ├─4287 php-fpm: pool www
             └─4288 php-fpm: pool www

active になってますね。
ちなみに、この時点ではまだ disabled です。

$ systemctl list-unit-files | grep php
php5.6-fpm.service                            disabled        enabled      
php7.0-fpm.service                            disabled        enabled      
php7.1-fpm.service                            disabled        enabled      
php7.2-fpm.service                            disabled        enabled      
php7.3-fpm.service                            disabled        enabled      
php7.4-fpm.service                            disabled        enabled      
php8.0-fpm.service                            disabled        enabled      
phpsessionclean.service                       static          enabled      
phpsessionclean.timer                         enabled         enabled  

再起動しても 7.2 を使用し続けるために、enabled にします。

$ sudo systemctl enable  php7.2-fpm.service
Synchronizing state of php7.2-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php7.2-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php7.2-fpm.service → /lib/systemd/system/php7.2-fpm.service.

確認します。

$ systemctl list-unit-files | grep php
php5.6-fpm.service                            disabled        enabled      
php7.0-fpm.service                            disabled        enabled      
php7.1-fpm.service                            disabled        enabled      
php7.2-fpm.service                            enabled         enabled      
php7.3-fpm.service                            disabled        enabled      
php7.4-fpm.service                            disabled        enabled      
php8.0-fpm.service                            disabled        enabled      
phpsessionclean.service                       static          enabled      
phpsessionclean.timer                         enabled         enabled 

ドン!これでもう大丈夫。

2
1
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
2
1