Categories
PHP Workflow

Switching local PHP versions with Homebrew

At the time of writing PHP 8.1 is the default, and I have 7.4 and 8.0 installed.

To swap them around link/unlink and then stop and start the appropriate Homebrew service.

Linking changes CLI version; starting the service affects the version Nginx connects to.

You may also want to change the binary that Tinkerwell loads.

Odd Bits

The syntax to link unlink PHP 8.1 (default) are different to the other two. I would not have a clue why.

When you stop a PHP service, sometimes a “fallback” one automatically starts. It might not be the one you want, so you have to stop it and start the one you need.

PHP 8.1

brew unlink php && brew link php

Other versions

brew link --overwrite --force php@7.4
// or 
brew link --overwrite --force php@8.0

Starting the Homebrew service

brew services list
brew services stop php@8.0 
brew services start php
brew services start php@7.4    etc...
Categories
PHP Workflow

Upgrading Local PHP with PHP-FPM

Updated and linked new version with Homebrew. Update path in terminal manager may help third party programs too.

PHP comes with its own PHP-FPM. For the new version I modified its config to run on a new port.

/usr/local/etc/php/8.0/php-fpm.d

e.g. ran this version at 127.0.0.1:9080

brew services restart php

Then in nginxcfg for a virtual host I updated its config to call php-fpm at that port. That site runs PHP 8.0. Others using old port seem to still be working with PHP 7.4.6. Unsure if that will survive a restart, or if only one version of php-fpm will be running (?)

Categories
PHP Workflow

Xdebug Profiling

Installed xdebug with pecl install xdebug.

Enabled in extra .ini config file in:

/usr/local/etc/php/7.2/conf.d

Restart PHP with:

brew services restart php72

The webcachegrind script does not seem to work. It lists only a few of the cache files, and they often do not match their description. For example the filename will appear to be webcachegrinds own PHP script, but all the data relates to a different WordPress script. Out of sync with the requests.

Might need a different tool like QCachegrind.

Currently cachegrind files are in /private/var/tmp

Categories
PHP Workflow

Upgrading to PHP 7.2 locally

Tried for brew install php@7.3 – produced errors. No go 🙁

Went for 7.2 instead. Installed ok, but brew link php@7.2 didn’t work (errors).

Manually edited ~/.bash_profile to add new PHP to PATH

export PATH="/usr/local/Cellar/php@7.2/7.2.15/bin:PATH"

That got command line PHP up to date. Fast CGI config needed to be updated to listen to port 9072. Unfortunately I’ve broken the multi-version setup. Now PHP-FCGI only listens to 9072 for PHP 7.2 (no access to 5.6 or 7.1 at the moment).

/usr/local/etc/php/7.1/php-fpm.d
/usr/local/etc/php/7.2/php-fpm.d (updated this to :9072)

So Apache config (httpd-vhosts.conf) needs to point to :9072 to work.

This article at GetGrav looks very thorough, but takes a different approach of using a PHP switcher script, rather than FPM listening on different ports for different versions.

See: restarting local PHP

Categories
PHP

PHP Testing Databases

class ModelTest extends TestCase {
  use DatabaseTransactions;

  /** @test */
  function its_radical() {
    // Given, When, Then
    
    //Faker class for Article to generate fake data
    factory(Article::class, 3)->create();

    //variation
    factory(Article::class, 3)->create(['reads' => 10]); 
    
    $popular = factory(Article::class, 3)->create(['reads' => 22]);
    $articles = Article::trending()->get();

    $this->assertEquals($popular->id, $articles->first()->id);
  }
}

class Article {
  public function scopeTrending($q, $take) {
    return $q->orderBy('reads', 'DESC')->take($take);
  }
}

Categories
PHP

Xdebug Profiling with Qcachegrind

Enable xdebug profiling temporarily while you profile. You’ll kill a ton of disk space if you leave it on.

Install qcachegrind from homebrew (brew install qcachegrind)

By default cachegrind output files go to /var/tmp (I think).

Move somewhere useful. Open qcachegrind to read this file. It’s in:
/usr/local/Cellar/qcachegrind/18.04.1

Spotlight didn’t find it, so maybe create a symlink or something elsewhere.

 

Categories
PHP Workflow

Local Mac PHP issues

Update March 2019

brew services restart php@7.2

I was trying to change PHP settings, to be able to experiment with Xdebug profiling.

phpinfo() shows that it loads /usr/local/etc/php/7.1/php.ini , which is true, but didn’t seem the case because changes were not taking affect after restarting PHP and Apache.

I was restarting the wrong PHP with brew services restart php71. Even stopping it has no effect.
So that’s not the version that Apache is pointing to. It uses some other instance of the service that I don’t know how to restart without doing the whole computer!

Do this

sudo brew services restart php@7.1
brew services list can also be useful, or even brew services list | grep php

Categories
PHP Servers

Restart PHP7.3 FPM

Restarting PHP FPM on Ubuntu 16.04

Probably
service php7.3-fpm restart

Maybe
systemctl restart php-fpm.service

Homebrew Locally

sudo brew services restart php74

Categories
PHP

Installing PHPUnit

Download latest version

  1. Saved .phar in c:\server\bin
  2. Add c:\server\bin to PATH
  3. Follow their instructions for creating  a .cmd wrapper
  4. Test by calling phpunit –version from cmd

Not sure how Laravel connects its tests/ to phpunit yet. Also not sure if version mismatch between Laravel and latest matters.

Categories
PHP

PHP7 Windows OpenSSL

I just upgraded from PHP 5.6.20 to 7.0.13 on Windows 10.

The existing Apache 2.4 installation uses Open SSL DLLs incompatible with PHP7.

I copied these two files from php7 to apache/bin

  • libeay32.dll
  • ssleay32.dll

Seems to be working now