.   .   .
We're always looking for great talent‼️ 🚀😄 And we're passionate about driving innovation and improving lives. Join us as we help social impact organizations and enterprise customers build their web apps. Apply today!
.   .   .
# Override system PHP with MAMP's PHP. Depdending on your version of PHP
# you may need to replace 7.0.10 with your version
export PATH=/Applications/MAMP/bin/php/php7.0.10/bin:$PATH

Currently, I’m on a journey to see if I can render some React components server-side on a PHP server.

First step, we need to get the V8 engine running within PHP. I got this working on my Mac with MAMP, PHP 7.0.10, and V8JS. With that being said, I suspect this would be easy to reason about to deploy to a production environment.

Overall it’s an easy process, but the documentation was pretty scarce, so here’s a blog post.

1. Install V8 with Homebrew

Install Homebrew if you haven’t already. It will make your life easier. Once installed, install the V8 library.

brew install v8

2. Update your system’s PHP path

If you’re using MAMP, you want to make sure that when you run php in the terminal, you’re hitting the MAMP version of PHP.

You can check which path your computer is using to load PHP by running which php in the terminal.

But ultimately, you need to figure out where the PHP binary is located within your MAMP folder and then use that location to override your system’s default installation of PHP. You can do so by adding the following to the bottom of your ~/.bash_profile file.


3. Install PHP V8JS

To install PHP V8JS, Run the following in your terminal. Restart your terminal when you’re done.

cd /tmp
git clone https://github.com/phpv8/v8js.git
cd v8js
phpize
./configure CXXFLAGS="-Wno-c++11-narrowing"
make
make test
make install

4. Activate PHP V8JS

Now, you should have the V8JS extension installed but it’s still not activated. Open up your PHP configuration file and add the following after the other extension lines.

# My php.ini file was located at /Applications/MAMP/bin/php/php7.0.10/conf/php.ini
extension=v8js.so

5. Test things out

Restart your terminal again if you haven’t already then add a file named test.php to your Desktop. Add the following contents

<?php

$v8 = new V8Js();

/* basic.js */
$JS = <<< EOT
// 'print' is part of the v8js javascript API
print(Math.abs(-100));
EOT;

try {
  var_dump($v8->executeString($JS, 'basic.js'));
} catch (V8JsException $e) {
  var_dump($e);
}

?>

Then  run the following command in your terminal to execute the file.

php test.php

If you get the following output 100int(3), it’s working!

Conclusion

I’m still not sure whether or not this will prove useful but if it does, I’ll be sure to post my findings in another blog post. If I can get React components loading server-side on a PHP server, that’s going to open up some doors.

Let me know if I missed anything!

.   .   .

We're Hiring‼️ ?? Looking to join our team of web developers? We're passionate about innovation, family, and community. Apply today!