Run JS code inside PHP. Just... because
I struggled for a while with this installation, so leaving a trail here for anyone looking.
What you need to accomplish?
Goal: run JS code inside a PHP script running on a webserver.
Already accomplished: Macs come with a webserver (Apache) and PHP.
To get there:
1. Install v8js extension for PHP
2. Let Apache handle .php requests (simple config changes)
Step 1 is the tough one. You need to install the v8js PECL package. So first you need to install PECL. To install PECL, first you need to install PEAR. V8JS only works with v8 up to a certain version. So you cannot install the latest v8. Oh, and the easiest way to install v8 is to use Homebrew.
So the path ahead: Homebrew, v8, downgrade v8, pear, pecl, v8js, config php and apache, hello world!
Very common, you probably have it already, but for the sake of completeness...
Follow the instructions on http://brew.sh/, which basically ask you to do this:
$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
You might already have v8.
$ brew install v8
But it's probably the latest, you need to downgrade.
$ cd /usr/local $ brew versions v8
You'll see lines like so:
... 3.16.14 git checkout 0671779 /usr/local/Library/Formula/v8.rb 3.15.11 git checkout cb30f36 /usr/local/Library/Formula/v8.rb 3.9.24 git checkout bb7fcab /usr/local/Library/Formula/v8.rb 3.8.9 git checkout 0bce8c4 /usr/local/Library/Formula/v8.rb ...
$ git checkout bb7fcab /usr/local/Library/Formula/v8.rb
... because I found this. But feel free to experiment with versions newer than 3.9.24
$ brew unlink v8 $ brew install v8 $ brew switch v8 3.9.24
BTW, at this time you should have a new console, which is always fun:
$ which v8 /usr/local/bin/v8 $ v8 V8 version 3.9.24 [sample shell] > var a = 1; > 2 + a; 3
PEAR and PECL
$ cd /usr/lib/php $ sudo php install-pear-nozlib.phar # you need to let PHP know where PEAR is by updating the include_path $ sudo vi /etc/php.ini # include_path = ".:/usr/lib/php/pear" $ sudo pear channel-update pear.php.net $ sudo pecl channel-update pecl.php.net $ sudo pear upgrade-all
Now the planets are all aligned. This is the most important step that has been the ultimate goal:
$ sudo pecl install v8js-beta
Tell PHP about v8js:
$ vi /etc/php.ini
look for "extension=" and add somewhere:
Now v8js should be available on the php command line, maybe try:
$ php -i | grep v8js
Do as php.net says:
$ sudo vi /private/etc/apache2/httpd.conf
Uncomment this line:
LoadModule php5_module libexec/httpd/libphp5.so
Add these lines in the httpd.conf to teach apache to handle .php requests:
<IfModule mod_php5.c> # If php is turned on, we respect .php and .phps files. AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps # Since most users will want index.php to work we # also automatically enable index.php <IfModule mod_dir.c> DirectoryIndex index.html index.php </IfModule> </IfModule>
Save the file, restart Apache:
$ sudo apachectl graceful
or just start it if it isn't running:
$ sudo apachectl start
Test it all
$v8 = new V8Js(); echo $v8->executeString(' var hello = "Hellow, "; var world = "JS wrrrld"; hello + world; ');
Point your browser to http://localhost/test.php and admire: