Wednesday, July 9, 2014


If you have ever have a problem with php forking, upstart will be your good friend.

What is upstart?
In short: service management daemon

"Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running."

Let's make it clear (from our perspective):
You can start any of your (php) scripts as a daemon (even with arguments) with a handy toolset: status / start / stop / restart

Config your job (e.g. a script)
Pretend that you have a my-awesome-app.php, which you want to start as daemon on every system boot. Your script can easily become fragile, so you also want to monitor it with a respawn mechanism.

First, you have to create an upstart config and place it in the /etc/init directory. In this case it will be this one: /etc/init/my-awesome-app.conf

# /etc/init/my-awesome-app.conf
description "daemonized awesome app"
version "1.0"
author "adammbalogh"

# restart my app on a crash
# stop restarting my app if it crashes more than 5 times in 30 sec
respawn limit 5 30

# start my script after nginx started
start on started nginx
# stop it on system shutdown
stop on runlevel S

# run as user and group
setuid www-data
setgid www-data

  exec php /home/user/public_html/my-awesome-app.php
end script

After you've created this conf file, just start your app with upstart: sudo start my-awesome-app.conf

You can also create pre or post scripts with a simple statement.

pre-start script
  exec touch /tmp/myapp.log
end script

If you want to use configuration variable you can do it too.

env BYE=goodbye
post-start script
  exec echo $BYE >> /tmp/myapp.log
end script

Finally, a useful cmd which lists all jobs and their states: initctl list

Monday, July 7, 2014

Web and Cli Debugging with PhpStorm, XDebug in Vagrant

We've a Vagrant box in a development environment and want to debug our application. Take a quick review of the two main ways, so it can be either a web app or a cli based app.

Web app debugging
First, we have to create a Server configuration:

File -> Settings -> Php -> Servers

Server configuration

Add a new server with its ip address (or hostname) and set the desired port. Check "Use path mappings" and map the vagrant's paths to the local ones.

The next step is creating a Run/Debug configuration:

Run -> Edit Configurations

Run/Debug configuration

Add a "PHP Web Application" config. Give it a name and choose the previously configured server from the select box.

To run our app in debug mode just go to:

Run -> Debug

and the web app will appear on a new tab in debug mode! If you want to restart the debug session again (after the actual one is disconnected) just refresh the page in the browser.

Cli app debugging:
At the start we should able to reach the host machine from our Vagrant box. So, how can we find out the host's ip?
"The IP address of the host is always the same IP address but with the final octet as a 1."
Okay, e.g.: if the vbox's ip is we can ping as our host machine from our guest.

We need the Server configurations as well (the same as the Web app debugging).

On the server side (in Vagrant) we've to create some environment variables:

export PHP_IDE_CONFIG="serverName=phpsaltbox"

export XDEBUG_CONFIG="remote_host= idekey=phpsaltbox"

serverName is the name what we typed in the Server configurations.
idekey comes from the xdebug config xdebug.idekey (it can be anything).

To run our cli app in debug mode we'll use the "Start Listen PHP Debug Connections" function in PhpStorm.

"Start Listen PHP Debug Connections" function

As the final step, just run your cli app in Vagrant and you'll see the debug window in PhpStorm in a couple of seconds!


Friday, July 4, 2014

Swap File for low end dev servers

I have a Digital Ocean Vps, but only for hobby projects. So it has merely 512Mb memory. If you want
to run multiple applications on a small development server you can operate with more memory, just use swap files.

"Swap space will allow the operating system to temporarily move data from memory onto the disk when the amount of information in memory exceeds the physical memory space available"

Of course it will be much slower. Fortunately D.Ocean has only Ssd hard drives, so Ssd improves the situation.

Here are the bash lines from a Digital Ocean article:

sudo fallocate -l 1G /swapfile; sudo mkswap /swapfile; sudo swapon /swapfile

Create an empty 1Gb file; Format to swap space; Enable it
This swap file remains until the next reboot!