动机?(Motivation?)
Run JS code inside PHP. Just... because 🙂
在PHP中运行JS代码。 只是...因为🙂
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.
目标:在Web服务器上运行PHP脚本中运行JS代码。
Already accomplished: Macs come with a webserver (Apache) and PHP.
已经完成:Mac随附了一个Web服务器(Apache)和PHP。
To get there: 1. Install v8js extension for PHP 2. Let Apache handle .php requests (simple config changes)
到达那里:1.安装PHP的v8js扩展2.让Apache处理.php请求(简单的配置更改)
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.
步骤1是艰难的一步。 您需要安装v8js PECL软件包。 因此,首先您需要安装PECL。 要安装PECL,首先需要安装PEAR。 V8JS仅适用于特定版本的v8。 因此,您无法安装最新的v8。 哦,安装v8的最简单方法是使用Homebrew。
So the path ahead: Homebrew, v8, downgrade v8, pear, pecl, v8js, config php and apache, hello world!
所以前进的道路是:自制软件,v8,降级v8,pear,pecl,v8js,config php和apache,hello world!
家酿 (Homebrew)
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:
请遵循http://brew.sh/上的说明,该说明基本上要求您执行以下操作:
$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
UPDATE Nov 10, 2014: this is now
2014年11月10日更新:现在
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
v8 (v8)
You might already have v8.
您可能已经拥有v8。
$ brew install v8
But it's probably the latest, you need to downgrade.
但这可能是最新的,您需要降级。
$ cd /usr/local
$ brew versions v8
UPDATE: Nov 10, 2014: brew versions
is no more. But no bother, skip the versions command and use the git checkout...
below.
更新:2014年11月10日:不再提供brew versions
。 但不要打扰,跳过版本命令并使用下面的git checkout...
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
...
So do:
这样:
$ 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
...因为我找到了这个。 但是请随意试用3.9.24以上的版本
Then
然后
$ 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
梨和PECL (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
UPDATE Nov 10, 2014: Apache config is is now located in /private/etc/. Go there, copy php.ini.default to php.ini and move on
2014年11月10日更新: Apache config现在位于/ private / etc /中。 去那里,将php.ini.default复制到php.ini并继续
V8JS(V8JS)
Now the planets are all aligned. This is the most important step that has been the ultimate goal:
现在,所有行星都对齐了。 这是已达到最终目标的最重要步骤:
$ sudo pecl install v8js-beta
UPDATE Nov 10, 2014: this failed. To fix:
2014年11月10日更新:此操作失败。 修理:
$ brew install autoconf
Tell PHP about v8js:
告诉PHP有关v8js的信息:
$ vi /etc/php.ini
look for "extension=" and add somewhere:
查找“ extension =“,然后添加到某处:
extension=v8js.so
Now v8js should be available on the php command line, maybe try:
现在,v8js应该可以在php命令行上使用,也许可以尝试:
$ php -i | grep v8js
Apache配置 (Apache config)
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:
将这些行添加到httpd.conf中,以教apache处理.php请求:
<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:
保存文件,重新启动Apache:
$ sudo apachectl graceful
or just start it if it isn't running:
或者如果它没有运行就将其启动:
$ sudo apachectl start
全部测试 (Test it all)
Go to /Library/WebServer/Documents/
Create file test.php
Paste:
转到/Library/WebServer/Documents/
创建文件test.php
粘贴:
<?php
$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:
将浏览器指向http://localhost/test.php并欣赏:
![v8js](https://i-blog.csdnimg.cn/blog_migrate/d4a2138d2e553f01cb7d21de7047fecc.png)
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
翻译自: https://www.phpied.com/installing-v8js-for-php-on-a-mac/