Moving to Amazon AWS EC2 and RDS, and PHP 7

Moving a large web application from a dedicated host running PHP 5.5 to Amazon AWS EC2 with RHEL and PHP 7.1.2 presented much less in the way of challenges than I had expected. Maybe I’m a pessimist but I expected it to be a long drawn out affair.

As with any software change, we created a branch from our master source in Git, and worked on this. We would merge that back into master once the AWS EC2 instance was finally in production.

PHP had to be configured, which is expected as we had installed  PHP 7.1.2 and not yet touched its configuration. Having the old server as an example of what we needed made it very easy.

The application itself needed reconfiguration, as is normal when you move to new host. The database credentials needed to point to our database instance at AWS RDS, and other server specific changes like file locations needed to be made.

One issue I had was testing the application. We had to leave the current domain pointing to the old hosting as it was still in active use. How then to test the new server, while making sure that requests from it went back to the old server, without being noticed. If this happened, it wouldn’t be a complete test.

I  added an entry to the hosts file (normally /etc/hosts) on both the EC2 hosting and my test client (my MAC). If ever there was a stray link to the old domain, it would always be directed to the the EC2 host.

Obviously these host file changes were removed when the new host was in production.

There were a number of coding issued to be resolved, mostly to do with functions that had long been deprecated, but now finally removed (like split()). Beyond that a few file permission issues, but they are normal.

Before going live I had to transfer newly created files from the old web server. I used rsync as this is the most reliable way to ensure all new and amended files have been transferred. And it is very quick and easy to use.

Form a user’s point of view the new configuration seems about 20% faster, but we feel comfortable that we can increase the EC2 instance to a more powerful option should this ever be needed.

 

Debugging Web Services

Debugging normal  PHP web applications is pretty straight forward. I use Eclipse but almost all IDEs have some means of setting break points and seeing the value of variables. I know some people put print or log statements in their code but this is much more time consuming than just setting a breakpoint and looking at the variables there, and maybe steping forward to see how the script runs.

I would be interested to hear if others usually use IDEs like Eclipse and debug in this way. I would hate to work without it. I even use it to check a newly written script or one that I have taken over responsibility for. Stepping through the processing can help you spot problems very easily.

Now all that is ok for a simple script or web application, but what about the server end of a web service? Normally the server runs in response to a client call. The client request may be quite complex, so how do you debug the server side?

Well my answer is to log the client request as it is received by the server as follows. Add this code to the very top of your server entry point, then have the client make its request. The server will log the request then carry on to produce a result (or not if there is some bug you want to fix).

To debug the server side, replace the above code with the following.

That’s it!. Now you can debug the server side without needing a client to make a request. The server entry point thinks it is getting a request from a client, but really it is just coming from the log file.

Now all this might seem trivial or obvious, but it took me a while to work this out when I had my first web service server to debug. Suddenly I’m just debugging a stand alone script rather than a server than depends on an external client request.

I would be very interested to see if this is useful to anyone.

 

 

 

 

Strange PHP

This is post I thought I would start as I come across weird PHP code from time to time and I thought I would like to record it. When we are new at a language we struggle to find ways to achieve results and sometimes this leads to some quirky code. I’m sure others could find examples in my code.

1. Iterating though an array

instead of

 

 

Posted in PHP

Quality of Coding

Sometimes I think I’m on my own in believing that code should be tidy and consistent, and generally easy for other programmers to pick up and work with. I come across so much code that does the job it was intended to do but is very untidy and has only the barest of documentation.

There are lots of practical reasons for writing quality code but I would like to leave those for now and mention something more personal.

In Zend and The Art of Motorcycle Maintenance by Robert Persig, the concept of quality for its own sake is introduced. Early in the book he mentions one example from his own experience. It has always always stuck in my mind for some reason, and have nothing to do with programming.

Robert had a workshop with a big draw full of heavy tools. Every time he had to get a tool he had the dragging the draw out, which always involved lots of physical effort. One day it got him down and he decided to do something about it.

He went off and bought roller bearings and fitted them to the draw. From that time on he could open and close the draw with a single figure. It gave him great joy every time he used it.

Now the joy didn’t come from the saving of all that physical effort; it came from the pure quality he had built in. Every time he used the draw he felt good about it and himself.

He goes on to talk about how we can all build quality into everything we do. He mentioned how even very mundane jobs can be turned into something really fulfilling by concentrating on doing the job as well as it can possibly be done. In other words building in quality and getting a good feeling from it.

The best way to build in quality is to write it that way from the start. Make sure every line of code is something you are proud of. If you think you will come back later and tidy it up, if you are like me  it probably won’t happen.

 

Posted in PHP