Tag Archives: php

Installing OCI8 on Ubuntu 12.04 LTS Precise (with PHP 5.2.x)

Recently I wrote about installing PHP 5.2 on Ubuntu 10.04 LTS, then again about how I also got PHP 5.2 working on Ubuntu 12.04 LTS Precise in the same way. Well now I’m trying to get OCI8 working in this environment and there were a couple of obstacles.

Firstly, an updated package somewhere along the line broke my fix to libtool.m4. So that needed fixing with:


sudo mv /usr/share/aclocal/libtool.m4 /usr/share/aclocal/libtool.m4.bak
cat /usr/share/aclocal/lt~obsolete.m4 /usr/share/aclocal/ltoptions.m4 /usr/share/aclocal/ltsugar.m4 /usr/share/aclocal/ltversion.m4 /usr/share/aclocal/libtool.m4.bak | sudo tee -a /usr/share/aclocal/libtool.m4

Then you need to follow the manual OCI8 installation method the bit your after starts: “For a manual install, download the PECL OCI8 package”.

Once you get to the following command:


phpize

It’s time to fix things up. The libtool in the package just won’t work when you go to run make. So remove it and link to your local version.


sudo rm ./libtool
sudo ln -s `which libtool` ./libtool

Now you can carry on with the instructions. You may get a few warning messages, but everything should be fine.

Hope that helps,

Steve

PHP 5.2.x with APC on Ubuntu 10.04 LTS Lucid (via Hardy)

As part of a move to virtualise our last remaining physical servers and build more scaling capability and resilience into our web serving infrastructure we have a specific need to run a couple of legacy apps on PHP 5.2.x. In the future when we’ve managed to fit in testing a bug fixes we’ll have these apps running on PHP 5.3, or even 5.4, but for now this need exists.

We have templates for Ubuntu 10.04 LTS, and since this is the middle ground between when PHP 5.2 was last shipped with Ubuntu in 8.04 LTS, and the latest and greatest 12.04 LTS I figured it’d be more secure, efficient and easier to implement it there!

Thankfully most of the work has been done for me.. Randy Fay shows how to run Karmic’s PHP 5.2 on Lucid but Sergius14 suggested pinning PHP 5.2 to the Hardy versions and I tend to agree with the reason. I want the latest security patches bundled in!

So taking Randy’s /etc/apt/sources.list.d/karmic.list and /etc/apt/preferences.d/php file and changing karmic to hardy in the file name and file contents gets us up and running. See the attached files to see what we’re running, if you use these files remove the .txt suffix from the file name.

Because we’re now looking at double the amount of packages, apt is going to complain, we need to increase it’s cache. If you don’t you’ll get an error when you run sudo apt-get update so, following the fine instructions on how to increase your apt cache limit we’ll do this as a one liner:

echo 'APT::Cache-Limit "100000000";' | sudo tee -a /etc/apt/apt.conf.d/70debconf

Next let’s update and install libapache2-mod-php5 to pull in PHP5, Apache2 and the basics.

sudo apt-get update
sudo apt-get install libapache2-mod-php5

Check it’s working by visiting the ip of the server in a web browser.

http://youripaddress/

To check PHP’s working we’ll create a new file calling the phpinfo() function.

echo -e '' | sudo tee /var/www/phpinfo.php

Check http://youripaddress/phpinfo.php to ensure PHP is installed and correctly running. If your browser tries to download a file, you might need to restart Apache to enable PHP.

sudo /etc/init.d/apache2 restart

Now we need APC (There’s a good article on Wikipedia about PHP accelerators, of which APC is one). The php-apc package installable from APT has some unmet dependencies because we’re using Hardy’s packages. We can circumvent that by installing it via PECL instead.

This route isn’t without a few complications because we’re using Hardy’s PHP5.2 some files aren’t in the right places, or automatically included with dependencies.

I added to Randy’s /etc/apt/preferences.d/php the following to get the PHP5 dev files for PECL to run.

Package: php5-dev
Pin: release a=hardy
Pin-Priority: 991

Now we’ll get php5-cli, php5-dev and php-pear (which includes PECL), we’ll update the PECL channel whilst we’re there and then try to install APC with PECL.

sudo apt-get install php5-cli php5-dev php-pear
sudo pecl channel-update pecl.php.net
sudo pecl install apc

AGH!

Errors! As I said before, some files like libtool.m4 and ltmain.sh are in the wrong places and need to be symlinked.

sudo ln -s /usr/share/aclocal/libtool.m4 /usr/share/libtool/libtool.m4
sudo ln -s /usr/share/libtool/config/ltmain.sh /usr/share/libtool/ltmain.sh

libtool.m4 is also missing a few bits since libtool.m4 has been broken down into multiple files so we need to concatenate them back together and append to libtool.m4:

cat /usr/share/aclocal/lt~obsolete.m4 /usr/share/aclocal/ltoptions.m4 /usr/share/aclocal/ltsugar.m4 /usr/share/aclocal/ltversion.m4 | sudo tee -a /usr/share/aclocal/libtool.m4

We also have a missing pcre.h header file. To get that we need libpcre3-dev.

sudo apt-get install libpcre3-dev

This should be it, so lets use PECL to download, compile and install APC (accept all the defaults), enable it in the php.ini and restart Apache.

sudo pecl install apc
echo -e '\n\nextension=apc.so' | sudo tee -a /etc/php5/apache2/php.ini
sudo /etc/init.d/apache2 restart

Now it’s time go back and check http://youripaddress/phpinfo.php to see if APC is enabled!

Congratulations if you’ve read this far and if it helped!

Steve Daniels

Slightly more Regular Expressions

IMG_8221For me regular expressions are like magic. I can appreciate the wonderful things they can do but I’ve never really understood how the trick works. As I develop primarily in PHP there are plenty of PHP string functions which when combined, can get me the desired result. However, yesterday I had to extract two values from a single string, so I thought I’d give Regular Expressions another go. Over the years of sniffing at them I realised that I’d picked up a few things.

  • Word Boundaries – These look different depending what flavour of regex you’re using. I’m using PHP’s PCRE functions so my word boudaries are forward slashes
  • Characters – I knew about Character Classes and I know what some do, not all of them. For example I know that [A-Za-z] will find an alpahbetical character and \d will find a single digit.
  • Repetition – I knew about greediness ( the + character). This character will try to continue matching your token after its found the first occurence.

So when I was confronted with the following string: item_newsArticle1138, I decided that it would just extend my knowledge enough to investigate using regex to extract “newsArticle” and “1138”

Getting the number off the end of the string was easy enough. I’m using the preg_match() function in php to pass the found string into a variable:
preg_match('/\d+/', 'item_newsArticle1138', $modelid);

The forward slashes act as regex delimters, so all we need is \d+ which finds the first digit in the string and the plus sign (+) continues finding subsequent digits: 1138.

Then came the tough one. If I use /[A-Za-z]+ as my regex;
preg_match('/[A-Za-z]+/', 'item_newsArticle1138, $model);

The regex engine reports finding “item” in the string and stops at the underscore, ignoring the part I needed. What I needed the regex to do was find the underscore and then get all the alphabetic charaters after it. To do this I needed to learn how to use positive and negative lookahead and lookbehind.

So what I needed to add to my string was a positive lookbehind: (?<=_). The brackets and the question mark provide the code for the lookahead/lookbehind. The inclusion of a less than makes it a lookbehind (N.B. omit the less than symbol for lookahead - not a greater than symbol). The equals sign tells the regex "to look for" and then I pass in the underscore character, as that's what I'm looking for in the string.

That gives us the following regex:
preg_match(‘/(?<=_)[A-Za-z]+/', 'item_newsArticle1138, $model);

Be careful the php function preg_match returns the results in the variable declared as the last arguement ($model) as an array, so to use the value you need $model[0] to return “newsArticle”.

Hey presto, the magic is revealed. I’m no expert, so somebody’s bound to tell me a better way in the comments, hello?

Arrays in dBs and YAML Config

I’m working on some new mega-menus for GO. I’ll post about that when we go live.

Following a pre-Christmas code review of the user-defined munu choices, we identified that we needed a way to provide a default set of menu items for everybody prior to them making and saving their personal choices. Mike remembered that we had an Option table capable of storing an array of values per person.

As this table existed in the GO symfony framework it already had methods for getting and setting values. Reading those methods introduced me to the php serialize function. This function takes an array and generates a storable representation of its value. For example this
Array
(
   [0] => 1
   [1] => 2
   [2] => 3
   [3] => 4
   [4] => 9
)

is turned into this:a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:9;}

When the value is queried we can simply unserialize it to return it to its php readable form.

Default Values in YAML Config Files

In the Option table, if a record isn’t returned for a user (no option has been set) a default value (or array) can be returned when passed into the querying method. As I needed to query the data in more than one place in the application, I needed to set the default value in one place. Symfony uses YAML files for configuration so I set it in the application config file app.yml.
all:
  sf_guard_plugin:
    default_dashbar:      [1, 2, 3, 4, 9]

Spaces are all important in YAML. Ensure you don’t indent each line and include spaces between array elements.

To pull YAML config variables into your code enter:sfConfig::get('param_name', $default_value);. The ‘param_name’ takes the file name and the parent child structure of the YAML file, separated by underscores and can be found in the Configuring Symfony chapter of the manual.

Heavy duty stuff for the first post of 2011.

PHPNW09 Round Up

Last weekend Janeth, Andy and Simon (from Business Systems Solutions) headed over to Manchester Conference Centre for the second Annual PHP North West Conference. Organised by volunteers from the PHPNW user group it has a great community feel to it yet has a great reputation.

A few thoughts about some of the sessions I attended…

The Uncertainty Principle – Kelvin Henney

Nice start to the conference and a well presented talk.  Main thing I picked up was that when presented with a choice you may not have to make a decision immediately.

Passing the Joel Test in the PHP world – Lorna Mitchell

Lorna Mitchell’s talk looked at how relevant the Joel Test is to PHP development.  We have some way to go before we pass completely and it’s something I’ll be looking at over the next few weeks.

Tools and Talent – Rowan Merewood

Plusnet’s Rowan Merewood gave a really good presentation about how they go about developing and deploying new tools.  I was a little preoccupied finishing my own talk so I’ll be interested to take another look at the video.

Making your life easier: Xdebug – Derick Rethans

I’ve been aware of Xdebug for a long time, and I may have even tried it out but this talk showed some of the nice ways it can be used.  Probably worth us having another look at deploying it on our development server.

Building an Anti-CMS – Michael Nolan

That’ll be me!  Think it went okay – a few suggestions for improvements on joind.in but it could have gone much worse!  You can see a slidecast of the talk on another post.

Integrating Zend Framework and symfony – Stefan Koopmanschap

Skoop’s talk covered how Zend and Symfony can be used together.  We actually already do this – our search engine is powered by Zend Lucene – but there’s probably more components we can use, and some of the new Symfony components look like they have potential.

Everything you wanted to know about UTF-8 – Juliette Reinders Folmer

Maybe a little too detailed for 10am on a Sunday morning, but interesting to see how difficult this problem is to solve.

Intro to OOP with PHP – Rick Ogden

Pretty basic introduction to OOP but we often forget that not everyone learns this stuff so it was good to see.

PHP 5.3 – Hot or Not? – Sara Golemon

If PHP 4’s unwillingness to die is anything to go by then 5.3 may take a while to adopt widely. There’s some nice features though and if they’re required for a future version of symfony then it’s well worth us starting to make use of them.

jQuery – Michael Heap

We use jQuery pretty extensively as part of GO and our corporate website so I understood most of the code demonstrated but it was nice to see how to create plugins.

That’s all for my quick round up of PHP North West 2009.  Overall a very good event.  Thanks must go to Jeremy, his team at Solution Perspective Media and Lorna Mitchell, without whom the conference wouldn’t happen.

PHP North West Conference 2009

PHPNW 2009Last year, Andy and I attended the first Annual PHP North West Conference.  Well it’s back for PHPNW2009 next month – Saturday 10th October with extra stuff happening Friday evening and Sunday morning – at the Manchester Conference Centre.  This year there will be a few of us attending from Web Services allowing us to cover more of the conference sessions in more detail.

I also found out a couple of weeks ago that my session proposal was accepted so I’ll be speaking about “Building an Anti-CMS (and how it’s changed our web team)”.  What exactly this consists of will be decided on a two hour train journey to London next week but the abstract obviously sounded good!

As with last year, they’re offering concession tickets to students so I recommend anyone studying Web Systems Development or other Computing courses try to head over to Manchester and check it out – there’s a load of well known speakers (and me!) covering everything from PHP frameworks (Zend, Yii and Symfony all covered) to tools to programming best practices.

If you’re at Edge Hill and are going let me know and I’ll catch you in Manchester!

h ttp://conference.phpnw.org.uk/phpnw09/

PHP North West Conference Review

Dangerously close to going the whole of November without a blog post so I thought I’d catch up with a quick review of the PHP North West conference last weekend.

London has had a conference for a number of years so it was about time there was one up here so myself and Andy made the trip into Manchester to take part. The conference was organised by a few people from the PHPNW user group but it was better run than most “commercial” conferences I’ve been to.

Welcome Keynote: KISS (Keep It Simple, Stupid) – Derick Rethans

I must admit I wasn’t too impressed by this. At times it came across as a bit of a rant towards certain technologies without going into details about when they might be of use. For example sIFR was shown with a buggy version of AdBlock where it didn’t show any headings – yes, potentially a problem but for some it may be worth the risk.

MySQL EXPLAIN Explained – Adrian Hardy

Very good session giving an overview of how to use the EXPLAIN command to find out what MySQL is doing and help optimise your queries.

Regular Expression Basics – Ciarán Walsh

Basic introduction to regexps. I’m still looking for a text editor that supports perl style regular expressions in a logical way – Notepad++ does very odd things with line endings.

Index and Search, options for PHP programmers – Zoe Slattery

I think some people were expecting more of an introduction on how to use Lucene to add a search engine into your site but I found it really interesting.  My two take-home points:

  • Lucene on Java + JIT is 50 times faster than Zend Lucene!
  • Java Lucene has a cool standard analyser – picks out things like email addresses and company names like “AT&T”

The Power of Refactoring – Stefan Koopmanschap

We should do more test.  Really.

Twittex: From idea to live in 7 days – Stuart Herbert

Following Twitter’s announcement that they’d be stopping SMS delivery of updates to UK users, Gradwell spent a week working on a system to fill the gap.  The result maybe wasn’t as popular as they’d hoped but the experience seemed invaluble.  It’s a lot like Bath Web Services’ Get Creative Week.

Panel Discussion: State of the community – Ivo Jansch, Steph Fox, Scott MacVicar and Felix De Vliegher

The conference ended with a panel discussion.  Maybe not of immediate relevant to the work we do but everything talked about with trickle through.

I’ll leave it at that.  Congratulations to the organisers on a great event.

Image by skoop.

PHP North West Conference

phpnw08 PHP Conference 22/11/2008London has had a PHP conference running for a number of years – Andy and I attended the last one in February this year – but there’s not been anything up North so far. That’s all about to change with the creation of PHPNW and the announcement of the first (of many?!) PHP North West Conference to be held in the conference-centre-formally-known-as-GMEX (Manchester Central) on Saturday 22nd November.

Lorna Mitchell does a much better job of explaining it than I could:


Lorna Mitchell – PHP North West from oreillygmt on Vimeo.

Something that might be of interest to our students is that there will be a number of concessionary tickets available so keep an eye out on the conference website for when tickets are released.

I also popped into Manchester on Tuesday to go to the PHPNW meetup – a chance for PHP developers from across the region to get together and chat.  This time as well as a quick update about the conference, there was a short presentation about version control systems.  This is something we already do using subversion but it was interesting to see nontheless.

Anyway, it’s good to see the North West tech community continue to grow and I’m looking forward to going to PHP North West Conference 2008.

Use our data

I mentioned previously about some of the feeds we’re providing for news and events and said at the end of the post that we’d be doing some stuff for the Institutional Web Management Workshop. Well it’s finally time that we have to firm up what that stuff is!

Edge Hill – along with the Universities of Aberdeen and Bath – are sponsoring the workshop’s Innovation Competition. For this we’re letting people know what data we have available for people to use in the creation of innovate, lightweight demonstrations of web technologies.

For the Big Brief we’ve restructured several of our systems to allow information to be extracted in different ways. Now as well as viewing web pages, you can use our data the way you want to or access in more machine-readable forms.

Several of these feeds we use internally – for example, the JSON feed of events is used to populate the calendar in the sidebar to show what days events are on.

It’s all work in progress so we’ll be adding more formats in the coming months but I’m really looking forward to seeing the ways people can use information from the website as part of the innovation competition.

PHP London 2008

A belated writeup on last week’s PHP London Conference. Andy’s already written a post so I don’t feel too bad!

As it turned out we split the sessions so I’ll just cover those Andy’s not mentioned. First up was Stefan Esser‘s PHP Binary Analysis. It was looking at using complied PHP bytecode to debug and audit your code. Probably of more use to people doing detailed security audits but some interesting ideas that I’d like to look into when I get a bit more time.

After lunch Marcus Bointon presented Mail(); & Life after Mail(). He started early on by quoting a blog post from Hacked:

I Knew How To Validate An Email Address Until I Read The RFC

Anyone’s who’s ever tried to send email using PHP’s mail() function will know the lengths you go to to get things working. Even then you’re probably doing it wrong. The solution is to use a library to handle all the standards compliance for you, something that symfony provides through the PHPMailer library.

Marcus went through a bunch more libraries and compared some of the features they provide so it will be interesting to look into what’s best for our needs.

More interesting for me was finding out about return paths. This is what happens when an email bounces and with a bit of server side magic it is possible to handle errors better. It’s quite a complex task to do properly so I’m interested in a good hosted service which can be used for both one shot emails like user registrations for batch mailshots. Apparently there’s a few services out there but I’ve not seen any with a really good API.

Final session I went to alone was My Framework Is Better Than Yours? presented by Rob Allen, Toby Beresford and Ian P. Christian. Each gave a short presentation on their framework of choice – Zend, Code Igniter and symfony – followed by a panel discussion. It was clear that each has its advantages and disadvantages:

  • Zend is good for components to pick and choose which aspects of a framework you need. It can often be used with other frameworks too. This can also be a downside is they’re maybe not quite as integrated as other systems.
  • Code Igniter is lightweight and some might like that it runs under PHP4. Personally I think this is a disadvantage. Someone in the audience suggested there was a way of turning on HP5 mode but I can’t believe this does more than activates a few extra features. Coding for PHP5 is an attitude shift and I don’t see how they’ve done this while retaining compatibility.
  • symfony, well I knew a bit about that already ;) Pookey did a pretty good job of presenting it.

During the panel discussion there was a comment about the criminal use of the term MVC to describe the frameworks. It got the attention of the room and there’s quite a lot of talk about this on the interweb. My view is that it doesn’t really matter whether a framework sticks rigidly to some design pattern if it provides the features that you need. I’m interested in getting things done, not in the theory of system design.

That’s all from me – check out Andy’s summary of the other sessions.