How to install PHP Data Structures (DS) extension on Ubuntu 16.04

First, you will need to install PEAR via apt-get to get the necessary package and distribution system that both PEAR and PECL use. From a shell prompt enter:

sudo apt-get install php-pear

You will be prompted to confirm the install. Just press “y” and enter. If all goes well you should see it download and install the php-pear package.

Now you will need to install the php-dev package to get the necessary PHP7 source files to compile additional modules. Enter the following from a shell prompt:

sudo apt-get install php-dev

If you do not install the php-dev package and try to install a PECL extension using “pear install”, you will get the following error:

sh: phpize: not found
ERROR: `phpize’ failed

The PECL_HTTP extension requires an additional dependency package to be installed. You can probably skip this for other extensions:

sudo apt-get install libcurl4-openssl-dev

Now we are finally ready to actually install the extension. From a shell prompt enter following:

sudo pecl install ds

The installer may ask you about some specific options for the extension you are installing.  Just accept the defaults and go ahead.

Once the install is complete, it’s time to enable the extension.
First, edit the following file (create it if it does not exist already):

sudo vi /etc/php/7.0/mods-available/ds.ini

and change it’s contents to:


; configuration for php ds module
; priority=30
extension=ds.so


Than check and remove any symbolic links to 20-ds.ini file, such as:

sudo rm /etc/php/7.0/fpm/conf.d/20-ds.ini
sudo rm /etc/php7.0/apache2/conf.d/20-ds.ini
sudo rm /etc/php7.0/cli/conf.d/20-ds.ini

You need to remove above listed symlinks because of bug: there is hard dependency on the json extension. DS extension shouldn’t try to implement JsonSerializable if the json extension is not loaded, but actually do it and it will complain with exception if it is not found:


PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php/20151012/ds.so'
 - /usr/lib/php/20151012/ds.so: undefined symbol: php_json_serializable_ce in Unknown on line 0

That’s why we removed 20-ds.ini symlinks and specified ds.so to load after json is already enabled.

Now, disable and then re-enable the extension:

sudo phpdismod ds
sudo phpenmod ds

You may need to restart your HTTP server:

# If you are on apache
sudo service apache2 restart
# if you are on nginx
sudo service nginx restart

My favorite programming quotes

“90% of coding is debugging. The other 10% is writing bugs”
Bram Cohen (the author of the peer-to-peer BitTorrent protocol)

“The programmer who refuses to keep exploring will surely stagnate, forget his joy, lose the will to program (and become a manager).”
Marijn Haverbeke

“The more I learn, the more I realize
how much I don’t know.”
Albert Einstein

“Never memorize something that
you can look up.”
Albert Einstein

How to parse a csv file in php when the delimiter is unknown

There are plenty of MS Excel alternatives out there – OpenOffice, LibreOffice, Kingsoft, Google Spreadsheets and many more. The problem is that CSV files created by different softwares may vary (, or ; to name a few options). If you need to process CSV files exported from different systems and software and handle the delimiter automatically, you can benefit from using SplFileObject::getCsvControl method.

Example usage:

<?php $csvFileObject = new SplFileObject($_FILES["csv"]["tmp_name"]); list($delimiter, $enclosure) = $csvFileObject->getCsvControl();

$lines = fopen($_FILES["csv"]["tmp_name"], 'r');
if($lines) {
while (($line = fgetcsv($lines, 4096, $delimiter, $enclosure)) !== false) {
//do something
}
}

Reference: http://php.net/manual/en/splfileobject.getcsvcontrol.php

How to setup Pure-FTPd server on OpenWRT enabled router

I am proud owner of TP-Link TL-WDR3500 router flashed with OpenWRT, which is really good custom firmware offering countless possibilities. Recently I’ve installed FTP server on it so I can access the attached external HDD drive remotely. This how-to is simple guide showing you to configure Pure-FTPd with TLS support on your OpenWRT enabled router, too.

Why Pure-FTPd

There are many reasons to prefer Pure-FTPd over other FTP servers available as OpenWRT packages:

  • It is a secure FTP server
  • It has FTPS support (offers optional TLS Encryption)
  • You can use both real user accounts and virtual ones
  • You can put every user in a chroot jail

Continue reading

GitBash: error setting certificate verify locations

I had tried to push changes to project using the https-URL, but it failed with an error:

error setting certificate verify locations: CAfile: C:/Program Files/Git/mingw64/libexec/ssl/certs/ca-bundle.crt

The problem occurred  on Windows 7 x64, with Git-2.15.0-32. You have to fix the path to ca-bundle.crt using absolute path with back-slashes. Run this command in GitBash to do it:

git config --global http.sslcainfo "C:/Program Files (x86)/Git/usr/ssl/certs/ca-bundle.crt

How to install mailcatcher on CentOS 7 and configure it for PHP

The problem

Handling outgoing emails in a web application can be hard, because it’s very easy things to get wrong and send unwanted test mails to real world customers while testing some functionality.
Ensuring sent emails are designed, parsed and formatted correctly is a painstaking problem, too.

… and the solution

Mailcatcher is a program you can use to test sending email. It gives you the ability to inspect sent emails and their headers. It is a simple SMTP server that can receive emails. It also gives you a nice web interface to preview the sent emails.

We’ll cover installing the dependencies for Mailcatcher. Then we’ll install and set it up for easy use in our development environment. This includes use with PHP. Finally, we will setup password protect subdomain for easier access using Nginx.

Continue reading

How to compact VirtualBox’s VDMK file size

This guide explains how to shrink (compact) the virtual disk files (files having extension .vmdk) of your VirtualBox virtual machines so they consume less disk space. It is also easier to upload and share them with other people.

Fill the free space with zeros
VirtualBox only knows that the space is really free if it’s been set to zero, and standard deletion won’t do this. Login into the virtual machine and run the following command:

cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill

Navigate to VirtualBox virtual machine folder
The virtual machines you have configured in VirtualBox are stored in folder “VirtualBox VMs” inside your home directory (or at least when Ubuntu is your host machine). Every machine has its own directory with name starting with the machine name. You have to navigate to the folder containing the files of the virtual machine having disk you want to compact and open terminal.

Get the UUID of the virtual disk
You have to obtain the UUID of the disk you want to shrink. I will explain you why later.
The command to do this is:

vboxmanage showhdinfo box-disk1.vmdk

This command assumes that your working directory is the directory that contains the vmdk file. Of course, you have to change the name of the vmdk file if it is different.
Write down the reported UUID because you will need it later.

Convert it to vdi
In order to compact the disk, you need to convert it to VDI format using this command

VBoxManage clonehd box-disk1.vmdk box-disk1.vdi --format vdi

Compact

VBoxManage modifyhd box-disk1.vdi --compact

Convert it back to vmdk

VBoxManage clonehd box-disk1.vdi box-disk1.vmdk --format vmdk
rm box-disk1.vdi

Set the original UUID
You converted the original vmdk disk file to vdi, compacted it and created new vmdk file from the compacted one. So, basically, you have created new virtual disk with new UUID which will not be recognized by VirtualBox unless you set the original disk’s UUID to the newly created one using the following command:

vboxmanage internalcommands sethduuid ./box-disk1.vmdk <original UUID here>

Voilà! The virtual disk size is successfully reduced!

Leave me a comment if you have any troubles or ideas how to improve this article. I am not VirtualBox expert and so I can’t guarantee that the above guide is the easiest way to get the job done.

How to restore a vagrant box using vmdk disk

The proper way to backup (and later restore) vagrant box is by using vagrant package command. This command creates a re-usable box file that can be easily transfered and used on other machines.

I have two Vagrant boxes I use on Ubuntu host machine. The last time I reinstalled my host machine OS I forgot to backup the boxes using vagrant package command, but saved the ~/VirtualBox VMs/ folder and all the files in it. This folder contains the virtual machines I amusing with VirtualBox (the virtual HDs to be more specific), so I used them to restore my boxes. Below are the steps I used to recover the boxes using the virtual HDs (vmdk files)

1. Import the machine
Go to ~/VirtualBox VMs/<name of the machine>/ and double click on the file with vbox extension. This should open VirtualBox GUI and import the machine.

2. Create base box file using the virtual machine you just imported
Use the following command to create a box file.

vagrant package --base <name of running virtual machine> --output ubuntu.box

3. Import the base box
First at all, you have to know the name of vagrant box you are restoring. Just open the Vagrantfile and look for line like this

config.vm.box = "ubuntu/trusty64"

As you can see, the box I am trying to restore is named “ubuntu/trusty64” and so I have to run the following command:

vagrant box add "ubuntu" ./ubuntu.box

Of course, you have to adjust the file path to the ubuntu.box file you have created in step 2.
To confirm that box is successfully added, you can run

vagrant box list

.

If everything is OK, you can now start your box in the usual way – using vagrant up command. Enjoy!

Add GIT branch information to Bash prompt

I’ve seen such as fancy Bash prompts on various tutorials and Linux examples over the Internet and I’ve always wondered how is achieved. I never really had a enough free time to learn more about it and explore the options. But being jobless for a month gave me opportunity to play with the thinks I like 🙂

My solution is pretty simple: when you navigate to git controlled folder, the bash prompt will show “@ branch” after the directory name. Nothing fancy.

Just open your ~/.bashrc configuration file with your favorite editor and add the following:

get_git_branch () {
git name-rev HEAD 2> /dev/null | sed "s/[a-zA-Z0-9]\+\ \(.*\)/ @ \1/"
}

than put this into your PS1 string:

$(get_git_branch)

so it become something like that

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w$(parse_git_branch) \n\$\[\033[00m\] '

Restart your terminal or type bash to start new bash session. Navigate to git controlled folder to test. It should look like this:

bash-git