Tuesday, 12 January 2010

Trying to get PHP working

This morning I was trying to get PHP running on Ubuntu. I installed spawn-fcgi, and then copied the CentOS init script I was using on the webserver for starting and stopping PHP.

The first thing I needed to know was where the source function library was in Ubuntu. The CentOS init script had '/etc/rc.d/init.d/functions', but this doesn't exist in Ubuntu. A quick google and I found the Ubuntu version was '/lib/lsb/init-functions'.

I tried starting PHP using the script, but got the error
$Starting fcgi: ./webapps/spawn-fcgi/php-fcgictl: 62: daemon: not found
Another quick google and I found that Ubuntu doesn't use daemon, but instead start-stop-daemon. Looking at the Lighttpd init script for Ubuntu, it seemed that I could actually use just the plain spawn-fcgi command without having to put daemon or start-stop-daemon in front of it.

I could now get PHP started okay, but when I tried to stop it, I would get the message
$$Stopping fcgi:
And the PHP processes wouldn't be stopped. I did some googling, but couldn't work out how to get it working, so I posted to the Ubuntu forums to try and get some help.

While I was waiting for a reply I thought I might as well make sure that PHP was working okay. I started up Nginx but got the following:
djeyewater@rusty-ubuntu:~$ ./webapps/nginx/nginxctl restart
* Stopping Nginx Server... [ OK ]
* Starting Nginx Server... [warn]: duplicate MIME type "text/html" in /home/djeyewater/webapps/nginx/conf/nginx.conf:21
[warn]: conflicting server name "www.photosite.com" on 0.0.0.0:7776, ignored
[warn]: conflicting server name "photosite.com" on 0.0.0.0:7776, ignored
[warn]: conflicting server name "static1.photosite.com" on 0.0.0.0:7776, ignored
[warn]: conflicting server name "static2.photosite.com" on 0.0.0.0:7776, ignored
Googling didn't come up with much useful, then I remembered that I'd had this problem before - the problem is that when you edit a file Ubuntu creates a backup of the file before you edited it with a tilde ~ on the end of the file name. So my sites folder that nginx was loading the site configs from had a file 'photosite' and a backup of this file called 'photosite~'. Obviously nginx loads both files, and so you get the conflicting server name error as both files contain the same server names.

So to fix it, you just need to make sure you delete the backup files so Nginx only loads one version of each site config.

After lunch I tried importing the mysql databases from the web server into mysql on my Ubuntu Virtual Machine. But I kept getting an error about foreign key constraints. I looked at the table in question in the database that had been imported, and indeed, it contained values that violated the foreign key constraints.

I looked at the same table on the server, and it had different values that were okay. So I looked in the sql I was trying to import, and that had the correct values. It seemed like MySQL was assigning the rows that it was importing an auto increment id rather than the actual id value that was specified in the SQL.

Then I realised that the problem was down to a trigger I had on the table - the trigger was setting NEW.id to the value returned by a SELECT statement. This works fine in normal use as you're not going to be inserting rows into this table with a specific id. But of course, when you're trying to import the table, the value returned by the SELECT statement is NULL, and since the auto increment on the table has already been set as being the same number on the full table, this means that your rows get inserted with the wrong ids, thus causing the foreign key constraint to fail when you try to apply it to the table.

e.g. On my table I had 10 rows, so on the server they would have ids of 1-10, but when I tried to import the rows, they would start off at the next auto increment number, giving me rows numbered 11-20 instead.

The solution was just to modify the trigger to only set NEW.id if NEW.id starts off as NULL.

Eventually I managed to get a local copy of my website working, so I tried uploading a large image to see why it was using so much memory on the webserver. But the PHP process processing the request died. I tried again, and got the same thing. So after googling with no success I posted to the Nginx forum to see if I could get some advice there.

In the evening I watched 3 episodes of Power Rangers with L, and 'Hell is a City' with Mauser.

The weather was overcast all day.

Food
Breakfast: Tangerine Marmalade Toast Sandwich; Cup o' Tea.
Lunch: Bowl of Vegetable Fake Cup a Soup; Slice of Toast; Banana; Pear; Slice of Home-made Flapjack; Chocolate Waver Bar; Cup o' Tea.
Dinner: Chilli con Carne; Pepper; Rice; Cheese flavour Salsa Dip; Grated Mature Cheddar Cheese; Sweet & Crunchy Salad. Pudding was a Jam & Cream Doughnut. Coffee; Cherry Liqueur; 2x pieces of Sainsbury's Truffle Chocolate.

No comments: