Skip to content

Installing on CentOS 7 x64 PHP 7.3

Thibault Barillet edited this page Aug 22, 2023 · 6 revisions

Compiling and installing the module might cause some headache if you run into errors. The installation below was tested on an CentOS Linux release 7.6.1810 (Core) 64-bits machine and the module installed in PHP 7.3 from Plesk and Remi. Let's get started.

Installing dependencies

# V8
sudo yum --enablerepo=epel install curl git python glib2-devel subversion gcc gcc-c++ make chrpath redhat-lsb-core
# PHP
sudo yum install php php-devel re2c

If you want your system PHP (defaults to PHP5) to be PHP 7.3 (or any other version), you need to add a repository that has provides it. I've tested here by using Remi's RPM PHP 7.3.

Recommended: Building a recent c++ compiler

You can check your current version with gcc -v. Old versions may not support the complete c++11 standard. When you run into GLIBCXX errors, incompatible libstdc++ versions, or Could not find libv8_libplatform library, this might be your answer. Note that compiling takes about an hour. A faster alternative might be using the pre-compiled devtoolset-7, but I've not tested this.

sudo yum install bzip2
cd /usr/local/src
wget https://www.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-8.3.0/gcc-8.3.0.tar.gz
tar zxf gcc-8.3.0.tar.gz
cd gcc-8.3.0/
./contrib/download_prerequisites
./configure --disable-multilib --enable-languages=c,c++
make
sudo make install
export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64

If you run gcc -v now it should tell 8.3.0. If not try sudo ldconfig to refresh the shared libraries, exec bash to restart your bash session, or just a sudo reboot for your server. Additional help can be found on the internet (here for example). In the last step we've set our LD_LIBRARY_PATH, this is where the GLIBCXX_3.4.xx libraries from GCC can be found.

Installing v8

This will build the latest stable version as of this moment used by Chromium and Chrome (7.5.288.23). You can find the currently latest stable versions here, found in the column v8_version.

/!\ DONT FORGET TO REPLACE THE V8 VERSION NUMBER, IT WON'T WORK IF YOU DON'T DO THAT

# Add depot_tools
cd /usr/local
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=`pwd`/depot_tools:"$PATH"

# Build our v8 version
cd /usr/local/src
fetch v8
cd v8
git checkout 11.8.31# v8_version here
gclient sync
./build/install-build-deps.sh
gn gen out.gn/library --args='use_custom_libcxx=false is_component_build=true is_debug=false target_cpu="x64" use_goma=false goma_dir="None" v8_enable_backtrace=true v8_enable_disassembler=true v8_enable_object_print=true v8_enable_verify_heap=true'
ninja -C out.gn/library libv8.so

# Move files we need to /opt/v8
sudo mkdir -p /opt/v8/{lib,include}
sudo cp -v out.gn/library/lib*.so out.gn/library/*_blob.bin out.gn/library/icudtl.dat /opt/v8/lib/
sudo cp -vR include/* /opt/v8/include/

# Update shared libraries when needed
sudo ldconfig

We're using gn gen for the configuration. Please refer to build documentation for more information about it or information about the available options.

Installing php-v8js

Now that v8 is installed, we can compile the module for PHP version(s). For the system PHP the workflow is as follows:

cd /usr/local/src
git clone https://github.com/phpv8/v8js.git
cd v8js
phpize
./configure --with-v8js=/opt/v8 LDFLAGS="-lstdc++"
make -B
make test
sudo make install

If your system has multiple PHP versions installed, the flow is a bit different. The example below applies to Plesk using PHP 7.3. Note the path /opt/plesk/php/7.3/bin/ as well as --with-php-config option. Repeat for any version you want to compile the module for.

# No need to repeat if done once
# cd /usr/local/src
# git clone https://github.com/phpv8/v8js.git
# cd v8js

/opt/plesk/php/7.3/bin/phpize
./configure --with-v8js=/opt/v8 --with-php-config=/opt/plesk/php/7.3/bin/php-config LDFLAGS="-lstdc++"
make -B
make test
sudo make install

After sudo make install the location of the v8js.so module will be shown, for example /opt/plesk/php/7.3/lib64/php/modules/.

Enable the module in your php.ini

Add extension=v8js.so to your php.ini file. You can check the location of you php.ini file via php --ini. You can also specify the full path, given in the last step. E.g. extension=/opt/plesk/php/7.3/lib64/php/modules/v8js.so.

Restart your webserver when needed and you should be good to go!

You can run echo "<?php phpinfo();" | php | grep -i v8 to test.

Result

Troubleshooting

  • PHP Warning: PHP Startup: Unable to load dynamic library 'v8js.so' (tried: /usr/lib64/php/modules/v8js.so (/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /opt/v8/lib/libv8_libplatform.so)), /usr/lib64/php/modules/v8js.so.so (/usr/lib64/php/modules/v8js.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

This means PHP could not find the (new) GCC version libraries, set via export before. The solution is to manually add it to the shared libraries. Run as root the following commands:

echo "/usr/local/lib64" > /etc/ld.so.conf.d/gcc-8.3.0.x86_64.conf
ldconfig

We're assuming the new gcc libraries are in /usr/local/lib64 (if you've followed the instructions above). The map /etc/ld.so.conf.d/ contains various files, each with a string of a map that should get included by ld. You can ignore a warning about /usr/local/lib64/libstdc++.so.6.0.25-gdb.py is not an ELF file - it has the wrong magic bytes at the start. It's a very old issue with gcc.

Restart your webserver (httpd) and the error should be gone and the extension loaded!