PECL PHP Trader Module: HOWTO

Example image

Introduction

The PECL PHP Trader module is a very powerful PHP extension that allows you to implement various trading functions in your PHP application. In my opinion though the documentation is a bit thin on the ground and it can be pretty tricky to find simple setup guides to help you through compiling and installing this module.

Hopefully this guide will get you up and running quickly so you can start building your own kickass trading scripts.

You can install the module using the pecl install command on the command line, but I personally prefer to compile things myself. Mainly because I find it a lot less complicated but also because it helps me cut down on deploying additional bloat to a production server. I simply pre-compile the module on a staging server then migrate it to the production server. No tools required on the production server. Not only does this cut down on the number of installed packages on your production server, it also helps cut down on potential attack surface. If your server gets compromised, the last thing you want is to provide a full toolset to your attackers to make compiling and deploying their payloads easier. I won’t be going through migrating the module in this guide, but I will be outlining the steps to compile the module and enable it. Assuming your production server and your staging server use the same processor architecture, you should be able to just copy the module into the PHP extensions folder on your production server and enable it (as per instructions below). It’s a pretty straight forward task.  

Preparation

Before you can begin compiling the module, we need to get your environment set up with all the correct libraries and tools. For this howto I will be assuming you’re deploying the module to a Linux based environment (Ubuntu or Arch). However, the steps are probably very similar for other platforms. For those interested, this module will compile on a Raspberry Pi. I have successfully compiled it on a Raspberry Pi Model B+, Raspberry Pi 2 and a Raspberry Pi 3 it’s worth noting that the module runs very well on these devices although compile time is reasonably (but not annoyingly) high. It feels like ages if you stay and watch it compile, but I assure you it’s not so long as to be excrutiating.

The three basic packages you need to install are the Build Essential package, the PHP package (which you probably already have installed) and the PHP Development tools. On Arch Linux you only require the PHP package as this package also includes the development tools and libraries.

To do this simply execute the following commands in a terminal:

Ubuntu / Debian
sudo apt-get install build-essential
sudo apt-get install php
sudo apt-get install php-dev
Arch
pacman -S php

Once installed, check the packages installed correctly by running the following commands:

php --version
phpize --version

You should see the version numbers of both tools printed to the terminal window. For example:

[[email protected] ~]$ php --version
PHP 7.0.12 (cli) (built: Oct 14 2016 05:48:15) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
[[email protected] ~]$ phpize --version
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
[[email protected] ~]$ 

Download and Extract Source Code

The link to the source code you need can be found here:

https://pecl.php.net/package/trader

Scroll down the page slightly and you will find a table with the heading available releases.

Right click on the link to the latest tgz file and copy the link address. For your convenience, at the time of writing the link to the latest file is:

https://pecl.php.net/get/trader-0.4.0.tgz

You’re now ready to open a new terminal window! Once you have your nice fresh terminal window, create a new directory by typing the following:

[[email protected] ~]$ mkdir phptrader

Then move into that directory by typing:

[[email protected] ~]$ cd phptrader

We’re almost to the good stuff, I promise! The next thing we need to do is download the file from the URL we got earlier. To do this, enter the following into your terminal window:

[[email protected] phptrader]$ wget https://pecl.php.net/get/trader-0.4.0.tgz

Wait a few moments until the file has finished downloading, once it has we need to extract everything from that file, simply type:

[[email protected] phptrader]$ tar -xvf trader-0.4.0.tgz

You will now see all the files being extracted to a subfolder within the folder you are currently in, it should have the same name as the tgz file. Move into that folder:

[[email protected] phptrader]$ cd trader-0.4.0

We’re now ready to proceed with compiling the module! Well pretty much…

Compiling the Module

Alright, we’re ready to start kicking off the interesting stuff. First off, we need to run phpize, this command prepares the development environment ready for us to finally kick off compiling this module. To do this, simply type phpize:

[[email protected] trader-0.4.0]$ phpize

Excellent, we can now get into the classic compile process. The initial command in this process is configure. This command checks your system to ensure you have all the requisite dependencies necessary to compile this module. Pay close attention to any error messages it may produce as they are likely to highlight any dependencies you are missing. To start this process simply type ./configure making sure you don’t forget the “./” before the command:

[[email protected] trader-0.4.0]$ ./configure

This process can take a moment, so relax for a moment. Once complete we can finally build the module itself. To do this, simply type make into the terminal window:

[[email protected] trader-0.4.0]$ make

Depending on the performance of your machine, this can take anywhere from a minute upto half an hour. To give you an idea, it takes about 15 minutes on a Raspberry Pi 3, and about 20 seconds on a 6th Generation Intel Core i7.

When the build completes you should see the following message:

Build complete.
Don't forget to run 'make test'.

You can run make test if you want. I generally don’t bother unless I run into problems further down the line, it’s a pretty reliable module to compile and I can’t think of any issues I have run into at this stage. Effectively, make test runs a bunch of tests against the module to ensure it has compiled correctly and to make sure it functions as expected. Totally optional, but that said, if you’re new to this process you should probably run it to get a feel for what it’s all about.

Install the Module

Alright, we’re on the closing stretch, we can take a deep breath now, the hardest part is over now, probably, all that is left is to install the module and switch it on!

Lets just get straight to it, to install the module type sudo make install into the terminal:

[[email protected] trader-0.4.0]$ sudo make install

When prompted, enter your password.

We need to use sudo for this step because the module needs to be installed into a directory that requires root priviliges to access and symlinks will automatically be created in a couple of locations.

If you don’t have the appropriate root permissions you will probably see something like this:

Installing shared extensions:     /usr/lib/php/modules/
cp: cannot create regular file '/usr/lib/php/modules/...': Permission denied
make: *** [Makefile:90: install-modules] Error 1

To rectify this, you probably just need to add your user account the /etc/sudoers file. I’d imagine most people reading this know how to do this, if you don’t know how to do this, I wouldn’t recommend tinkering with that file unless you know exactly what you’re doing. I may write a short article on this in the near future. If you want to see that, let me know by leaving a comment / tweet / email!

Note: Everything beyond this point requires sudo so stop now if you don’t have sudo available to you and spend some time reading the documentation for sudo here sudo readme

Enable the Module

Ok, so if you got past the previous step the module is now installed. Congratulations! We have one last step before you can start writing your multi-million dollar life changing trading robot. We need to add the module to your PHP config file. Fortunately, this is easy.

We need to first locate your php.ini file. To do this, we need to type yet another command into the terminal window, type in the following:

[[email protected] trader-0.4.0]$ ls /etc/ | grep php

What you should be presented with is all the folders within the etc folder containing php. If, like me, you only have one version of PHP installed, you will probably only see one result. In my case, my PHP folder is simply called php. Depending on the version of PHP you have installed the folder name can vary. For example your folder may be called php5 or php7.0 etc.

Lets move into our PHP folder by typing the following command:

[[email protected] trader-0.4.0]$ cd /etc/php

Of course, substituting the the path above with the path to your PHP folder.

Once there, we need to add the trader module to our extensions list. To do this type:

[[email protected] php]$ sudo nano php.ini

Enter your password and scroll down until you find your list of extensions. Each item on the list should start with “extension=”, the first in the list is usually bcmath.so.

Simple add “extension=trader.so” hit CTRL+O and hit enter. If you intend to use this module from the CLI that’s it, you’re finished. If you wish to use this module from a web application, simply restart your web server (Apache, Nginx etc).

For Apache:

[[email protected] php]$ sudo systemctl restart httpd

For Nginx:

[[email protected] php]$ sudo systemctl restart nginx

Congratulations you’re ready to start writing your trading functions! If you would like me to write a guide on how to use the trader module, send me a tweet @pg3tech!