PHP Classes
PHP Classes
elePHPant
Icontem

Simplify Your PHP Applications Testing Using LXC in Your Development Environment

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Simplify Your PHP App...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)  

Author:

Posted on:

Categories: PHP Tutorials

LXC is a lightweight virtualization technology that runs on top of Linux to create application testing environments without having to install full virtual machines.

Read this article to learn how to setup LXC to test your PHP applications on your development environment without waste of resources of your machine and with great control of the software you use to test your PHP software with all other software packages and tools you need just like if you were running them on your production enviroment.




Why Virtualization?

Over time, each developer may need to install an increasing amount of software packages, libraries and services. For example, if you are a Web developer, for each project you are building, in addition to PHP you may need to install MySQL, PostgreSQL, MongoDB, Redis, RabbitMQ , Apache, nginx, memcached among others.

Furthermore, a lot of different libraries which used by the software described above will be installed, and this list is not full.

I do not know how about you, but I do not like such a mess. In fact, in my experience, I have to deal not only with the whole zoo of software, but also with lots of versions of this software.

As an example, today I am working on a project using php 5.4 and PostgreSQL 9.3. Tomorrow I will have to patch holes into the legacy project, which runs on top of PHP 5.2 and MySQL 5.1. On the next day I can work with app which used MySQL cluster.

Working on production server is a bad idea, so it is advisable to have not only a local version of your application, but also use an environment as close as possible as the native production environment.

Why LXC?

What is LXC? LXC (Linux Containers) is an operating system level virtualization environment for running multiple isolated Linux systems (containers) on a single Linux control host. The Linux kernel provides the cgroups functionality that provides limitation and prioritization of resources (CPU, memory, block I/O, network, etc.) without the need for starting any virtual machines, namespace isolation functionality that allows complete isolation of an applications' view of the operating environment, including process trees, networking, user IDs and mounted file systems.

I decided to use LXC because for me it is more than enough to have a container in which I will
be able to deploy the necessary version of operating system and install the necessary software.

Strategies

One container, one service

Main goal of this strategy is having clear blocks for your application. For example, we can create one container for exact version of PHP with Apache or nginx, one container for exact version MySQL or PostgreSQL, one container for memcached or Redis.

We can create many containers and combine them for our application needs. LXC allows clone containers, so we can create container for MySQL, clone it and quickly setup MySQL cluster.

One container for the whole environment

Main goal of this strategy is having full copy of production environment. Sometimes production server have very specific setup. We can reproduce it in our local container and this will simplify development process for exact project. Also this strategy is a good fit for support legacy code.

For both strategies we have comfortable abilities. Source code can be placed into directory on host. This directory can be mounted into container, so we can edit our code without accessing to container. Also you can decide which container must be started with the host system like an usual service, it allows you to work with container right after login.

Install LXC

In this example I am using Fedora as host system and Debian as guest. First of all we need to install LXC.

sudo dnf install lxc* libvirt libvirt-daemon debootstrap bridge-utils redir

sudo systemctl start libvirtd

Check bridge network interface.

brctl show
If we see something like that:
bridge name   bridge id           STP enabled    interfaces

virbr0        8000.52540049bd93   yes            vethK8WPM2

We can go further, or reboot you computer.

Create containers

We will create two containers – one for PHP and nginx (nginx can be installed into separate container, but we use the simplest method here), and the second for PostgreSQL. Before this, we change default LXC template.

sudo sed -i s/lxcbr0/virbr0/g /etc/lxc/default.conf

Create and setup the PostgreSQL container

sudo lxc-create -t debian -n postgres

sudo lxc-start -n postgres

sudo lxc-attach -n postgres

apt install postgresql postgresql-client postgresql-contrib postgresql-filedump

Configure PostgreSQL

# for example, our container ip is 192.168.122.6

# add this line to /etc/postgresql/9.4/main/pg_hba.conf

host    all    all    192.168.122.1/32    trust

# uncomment & change line into /etc/postgresql/9.4/main/postgresql.conf

listen_address = '*'

Restart service:

service postgresql start

That is all. Our container is ready, PostgreSQL is running.

Here is a short video of this step:

Create and setup PHP with nginx container

sudo lxc-create -t debian -n php

sudo lxc-start -n php

sudo lxc-attach -n php

apt install php5 php5-cli php5-fpm php5-pgsql php5-twig php5-curl php5-mcrypt php5-xdebug php5-xsl php5-xhprof nginx

Setup nginx

# for example, our container ip is 192.168.122.193

# /etc/nginx/sites-enabled/default

server {
    listen 192.168.122.193:80;
    root /srv
    if ( !-e $request_filename ) {
        rewrite ^ /index.php last;
    }
    location / {
        index index.php
        try_files $uri $uri/ /index.php;
    }
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

Restart nginx:

service nginx restart

Now we exit and configure the container for mounting host directory:

sudo lxc-stop -n php
sudo echo “lxc.mount.entry=/home/user/code/project /var/lib/lxc/php/rootfs/srv none bind,create=dir 0 0” >> /var/lib/lxc/php/config
sudo lxc-start -n php

That is all. Now we can edit you code using your host machine software and run it into container.

Pros and Cons

Pros

 - We can isolate our code and services
 - We can keep host system clean
 - We can run multiple software versions
 - We can run "distributed" systems on single host
 - We can "construct" an environment on demand with already created containers

Cons

 - It is a little harder than just installing a LAMP stack on host system

Conclusion

Using LXC to create and test multiple environments on your development machine is a very flexible solution that can save you time and headaches once you understand the whole process described in this article.

Post a comment if you liked this article or have questions on how to setup a LXC based enviroment to address you PHP project development needs.




You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

5. why not be docker - jam lee (2015-06-22 23:10)
docker... - 1 reply
Read the whole comment and replies

4. Great article - Anthony Amolochitis (2015-06-19 00:36)
Great topic man. I had to bookmark this one.... - 0 replies
Read the whole comment and replies

3. Good to know - Makhtar Diouf (2015-06-18 18:45)
VMs... - 0 replies
Read the whole comment and replies

2. thanks - tinashe zulu (2015-06-18 09:45)
thank you... - 0 replies
Read the whole comment and replies

1. Excellent tutorial - Yoni Lamri (2015-06-18 09:44)
Light light light... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Simplify Your PHP App...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)