Tag Archives: PHP

Bug or feature?

 

wildebeest

 

 

 


Creating your PHP Onboarding for Windows machines

There are many many ways to have a full LAMP stack on a Windows machine and use it for PHP development like WAMP, for example. This is ok if you are developing just for fun, but in corporate level an on-boarding environment is much preferred. Having a pre-defined server image with all that is necessary to almost replicate your production server on a developer machine helps on preventing errors and, most importantly, quickly have new developers up-to-speed with the corporation development environment.

Consider that you would need:

  • Apache
  • PHP
  • MySQL
  • Memcache
  • APC
  • some extra libs
  • GIT repository

Creating this under windows can be such a hassle and in cases like this bundled solutions does not work.

To create a full LAMP stack on your windows machine and have it 100% stand-alone I have used Vagrant. On the prior version of Vagrant (V1) you had to use Putty in order to ssh to the VM and that was a painful process, with the newer version (V2) you can use built in Vagrant SSH as long you install an SSH support library like the one from GIT. There is a trick on the GIT installation, you must choose the option with the Linux / Unix commands (it has a red warning message about overwriting windows libs).

Prior installing Vagrant you will need to install Virtual Box. It is a Oracle virtual box machine. Very useful if you are on OSX or Linux and need to run tests on Windows IE9 (sig). This part of the process is very well explained on the Vagrant documentation.

Now comes to Vagrant install. It is fairly easy. Install Vagrant, then open command line and run “vagrant init”. It will create a default vagrant file with most of the basic things defined. Here comes a trick. The default vagrant file tries to download the precise VM.  I have tried to contact Vagrant a couple times on Twitter and with no luck. There is an issue with the download of this box, the server resets the connection prior the download to be finished.

There is a way out of this: VagrantBox.es – Select the box that you best consider that matches your environment, add it on vagrant (vagrant box add) and finally update the vagrant file to use the new added box.

After this, everything is as simple as setting up the environment. Unless you have an specific need to add the GIT repository files under another folder you can set it all under the vagrant folder (same folder where the vagrant file is located).

I could use the GIT command line on windows to clone / commit my repositories, but using the GIT windows tool from GitHub is way too fun :D

I know that most of PHP developers use either Linux or OSX for development. It is much easier to setup and use, but if you are stuck with Windows, you might as well benefit of this.

Just a note, off course the main environment mentioned here was LAMP, but this same process can be used for Ruby, Phyton and may other hipster languages available =D

 

Updates:

I had a networking issue with the current box that I’m using. It seems that it is a common issue with some boxes as mentioned here – https://groups.google.com/forum/?fromgroups=#!topic/vagrant-up/Yeu6UF-GJO8

To fix run this: sudo /etc/init.d/networking restart

If you never installed a LAMP stack before, this article here will give you all the steps to install it correctly: http://www.howtoforge.com/installing-apache2-with-php5-and-mysql-support-on-ubuntu-12.04-lts-lamp


Simple MySQL Heartbeat monitoring

I had an interesting request today: create a script that would return a page header 200 or 500 for a heartbeat monitoring on a MySQL server for a site. The reason behind it was that the server was returning way too many MySQL connection errors during a small period of time for it to considered a simple app error.

This is what I have came for to help with the issue:


<!--?php
$dsn = "mysql:dbname={$dbName};host={$host}";
$user = 'mysql_username';
$pass = 'mysql_user_password';
try {
$dbh = new PDO($dsn, $user, $pass);
$dbh = null;
header("HTTP/1.1 200 OK");
} catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
}

With this simple code we can monitor the page status and if it returns 500 set an alert to tell us that MySQL server is down.

There are, off course, better ways to do this, but if you don’t have too many resources or is using a shared server, this might be a good solution meanwhile.


My overview on MongoDB

Recently I have done a online MongoDB certification developer course with 10Gen. Prior this certification I haven’t never tried or really used a NoSQL database and after it I can say that I would really think twice if I would only use a relational database on a big project.

There is a big change of concept here, differently from a relational database with MongoDB you are willing to give up on relationship and see data in a object way. Let me see if I can explain this better.

In a relational database you would have a set of tables like:

Users 1 – n Addresses 1 – n AddressTypes

In MongoDB you would have a Json Object that would have all data from the user including (or not) the addresses and it’s types. What you store for a given object is completely up to you and if you really want to keep a relation between objects (split the user from the address object for example) you can, but you would have to do it programatically.

It’s a bit hard of a concept to understand at first glance, another reason why I did really enjoy the online course that 10Gen gave, but once you understand it is only natural that you start reconsidering a lot of the decisions you had made with a relational database. It is not only a case of performance, it can also be a case of how you scope your objects and data on the database in a way that you can not only gain performance but also keep the necessary data in a single object.

Consider a real state agency website for example, A lot of the data that relates to a house can be hold into one single object. It not only could be held but also it can shrink the size of data since not every single house does offer the same standards or assets.

Aside of the concept that MongoDB brings it also brings 5 great features that can bring advantages depending of the project that you are dealing with:

  • Aggregation Framework (or it’s complex queries method) that use a concept similar to the Linux pipeline
  • 2D / 3D Geo-find – instead of doing insane big queries with math in it to find a geo-location, MongoDB already comes with this feature
  • Data connectors. It already have a ton of good connectors that works with some of the big languages (like PHP) and frameworks
  • GRIDFS or Blob Sharding  – splits big chunks of images in several collections making it easier to save and gather the image
  • Server Sharding – It is almost ridiculous how  easy is to setup several servers with voting capabilities to handle your MongoDB data, replication, etc

At the moment I’m not entirely sure if I could re-do a whole project only using MongoDB, but I would, given the time and goals, really consider it. The big reason is that I love the concept of storing an object instead of data parts.

Consider the following, if you need to store a whole session on the database which would be the best way to do it: a simple JSON object. With json_encode and json_decode from PHP, for example, you can easily create an array with the whole data.  If you can consider do this with a relational database, why not use a NoSQL database like MongoDB.

If you still have your doubts about MongoDB or even NoSQL databases I would give a check on the online training and their documentation. You probably will find it really fun to work with it.

Today I can say that I understand why MongoDB is becoming more and more popular by the day.


Phil Sturgeon • Why some people hate PHP | Blog

A worth reading post: Phil Sturgeon • Why some people hate PHP | Blog.


OWASP top 10


Improve performance: Save the Request and Use Signed Cookies

These are 2 ideas that I have just heard on a presentation. To gain performance with your application, save the requests (dont worry much with pageload) and use a signed cookie to autenticate uses instead of checking the sessions.

The save the requests concept is pretty simple: If you browser already cached something, unless you really need to reload that file, save that request and let it cached. For example, don’t change an image name or add an identifier to a css file unless strictly needed. That will save a request, therefore gaining performance.

The signed cookie is an interesting concept. If you take a look, the read time to the database (or cache mechanism) to check if the use is authenticated is greater than your application checking for a signed cookie. Read the cookie and if is there, authenticate the user.

I really did like these 2 ideas and probably will start using them on my apps. Anyway, sharing the tip.


Who tested this sh***

One of my colleagues was testing something and found out that a cron that was consuming 100% of memory and never ended. He is not a PHP Developer so he called me to take a look.

After less than a minute looking on the code I found the issue but I was perplexed to see how a code like that went to production.

In short this was the main execute code of the cron:


while(!empty($all)) {
$row = array_shift($all);
}

As you can imagine, that array will never be empty, so the loop will never end.

Fixing it was very easy, a simple change from while to foreach took care of the issue, but a question remains for me: who tested this sh***

Obviously it wasn’t tested, otherwise this would have been caught way prior getting to production.

Sorry about my rant but I can’t avoid to get mad when I see a developer that did not even run the code prior commit it.


PHP Test URL for SSL under a Load Balancer

Most likely your application will be under a load balancer when in production. If you need to test if the url is HTTPS with PHP you will probably use env(‘HTTPS’), but under a load balancer that wont work.

You will need to check for HTTP_X_FORWARDED_PROTO server variable.

Off course testing this under your local environment can be painful, but here is a small tip that works wonders. Change your http-ssl.conf and on your virtual host add:

RequestHeader set X-Forwarded-Proto "https"

This will automatically add the HTTP_X_FORWARDED_PROTO on the response header from Apache to your app.

The same logic can be used for other Apache header responses.


Installing Composer Russian Roulette

I have seen the rant that was happening on Twitter that generated this article. At first I was a bit confused because I couldn’t figure out why there was an issue with the Composer Install. It is such a great way to install it and, truly, for me, at 1st sight, I haven’t seen nothing wrong.

Truth is that the current way that you install Composer can bring some problems and there is a solution for it.

Great read: http://www.adayinthelifeof.nl/2012/10/15/installing-composer-russian-roulette/comment-page-1/


Follow

Get every new post delivered to your Inbox.

Join 293 other followers

%d bloggers like this: