Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, January 22, 2020

Runnig multipe versions of PHP on Ubuntu

Running multiple versions of PHP on Ubuntu is fairly trivial since all supported version can be found in the main PPA for supported PHP versions. The repository can be added with

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update

Look here1 for details. Then the relevant versions can then be installed

$ sudo apt install php7.4
$ sudo apt install php5.6

It is now possible to switch to the relevant version interactively with

$ sudo update-alternatives --config php

or alternatively directly with

$ sudo update-alternatives --set php /usr/bin/php5.6

This will change the version on the command line. Change version used by Apache with2

$ sudo a2dismod php7.4
$ sudo a2enmod php5.6
$ sudo systemctl restart apache2

Extensions

Most relevant extensions can be installed directly via apt. Fx.

$ sudo apt install php-ast

This will add the extension to all installed version of PHP the extension is supported for

Extensions via PECL3

Some extension is only available via PECL. PECL comes with the dev files for PHP, also available via apt

$ sudo apt install php7.4-dev

When using multiple versions of PHP, you need to set up the dev dependencies correctly, so extensions are installed correctly. php, phpize and php-config all need to be set to use the correct PHP version

$ sudo update-alternatives --config php
$ sudo update-alternatives --config phpize
$ sudo update-alternatives --config php-config

Extension installed via PECL is only installed for the current version of PHP. You need to manually add the extension to php.ini with (ast extension used as an example)

extension=ast.so

Manually installing extensions4

If you are compiling an extension yourself, you need to setup PHP the same way as when using PECL. After downloading the source code for the extension, build and install the extension with

$ cd extname 
$ phpize 
$ ./configure 
$ make

Remember to add the extension to php.ini

References

  1. https://launchpad.net/~ondrej/+archive/ubuntu/php
  2. https://opensenselabs.com/blog/tech/change-php-version-drupal-website-apache-nginx
  3. https://www.php.net/manual/en/install.pecl.pear.php
  4. https://www.php.net/manual/en/install.pecl.phpize.php

Wednesday, June 6, 2012

Installing PCNTL for PHP on OSX Lion

Today I needed to install the PCNTL extension for PHP for a tutorial session at the Dutch PHP Conference. And since I am working on OSX Lion, this of course gave some problems due to the rather odd installation of Apache and PHP on OSX.

Several sites on the web gave quick guides for this, but still, as always there are a few things you need to keep in mind for it all to work. I will try to walk through the process as detailed as possible:

First start by installing Xcode. You can get the latest version from the App Store. NOTE that the App Store only have the latest version of Xcode, so if you need a version for Snow Leopard or older, you need to dig deep on the web (https://connect.apple.com is a good place to start).

Next you need to install the Command Line Tools from Xcode. You do this by opening Xcode, go to Preferences (CMD + ,) -> Downloads -> Components and hit install on the Command Line Tools. You can get the Command Line Tools without installing Xcode via https://connect.apple.com

Now you need to find out what version of PHP is installed on OSX

$ php -v
PHP 5.3.10 with Suhosin-Patch (cli) (built: Feb 20 2012 22:55:53) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

and then get the source code for that version

$ curl -O http://us.php.net/distributions/php-5.3.10.tar.gz

When the download have finished, you should unpack, find the PCNTL extension, phpize, configure, make and then make install

$ tar -xzvf php-5.3.10.tar.gf
$ cd php-5.3.10/ext/pcntl
$ phpize
$ ./configure
$ make
$ sudo make install

If phpize is complaining, you are probably missing autoconf. The easiest way to install autoconf is via Homebrew. You install Homebrew by doing

$ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

The first thing you need to do afterwards is to run

$ brew doctor

to find out if any dependencies are missing. Homebrew will telle you what to do, if any is missing.

Lastly you need to enable the PCNTL extension. Do this by adding the following to your php.ini

extension=pcntl.so

if you do not know where your php.ini file are, you probably need to create on, by doing

sudo cp /private/etc/php.ini.default /private/etc/php.ini

Restart Apache and verify that the PCNTL extension are loaded correctly

$ sudo apachectl restart 
$ php -m | grep pcntl 
pcntl

If the PCNTL extension for some reason is not loaded, try and copy the pcntl.so from the compiled source code to the extensions directory

$ sudo cp php-5.3.10/ext/pcntl/modules/pcntl.so /usr/lib/php/extensions/no-debug-non-zts-10090626/

Restart apache and verify the the PCNTL extension is loaded.

Resources:

Wednesday, October 6, 2010

file_get_contents acting very slow

Lately i have been working on a simple REST-like service interface to an existing product at work. I admit, I found some code online that I modified. No need to do work already done, right? ;-)

The problem arose when i tried to call my brand new web service. I got extremely long waiting time (about 15 secs to precise), before the resource was returned. The code I used look like this:

$result = file_get_contents('http://example.com/service/');

As simple as it gets. When I tried calling the service directly from my browser, the result came back instantly.

I turned to my old friend Google to look for an answer. Most of the answers i got was that the DNS lookup was slow. So i tried my script from all the different servers I have access to, but with the same result.

The solution was quite simple, when I finally found it. In my REST-like service I set the follow header:

header('HTTP/1.1 200 OK');

By doing this I force the connection to use HTTP 1.1. This is pretty standard, but apparently by doing this I use the default behaviour of HTTP 1.1 and that is to keep the connection alive. The same as setting:

header('Connection: keep-alive');

So the connection started by file_get_contents() is keept open, until it times out. Timeout is set to 15 secs on the server!! So the solution was to put the following header in my service:

header('Connection: close');

A w00p w00p the result was returned instantly.

A quick tip if you run into this problem, and the service is not your own, is to set the header yourself. You can do this by giving a context to the file_get_contents. This can be done by doing the following:

$opts = array(

'http'=>array(
header' => 'Connection: close'
)
);
$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/service/', false, $context);

This will force the connection to close immediately after the resource have been retrived. Al option that can be set in a context (for HTTP) can be found here: http://php.net/manual/en/context.http.php

Monday, March 8, 2010

POST array in PHP decoded wrong??

I just stubled upon an error? in the way PHP decode the keys in the $_POST array. If you define an input field in a form with a name containing a . (period) f.x. 'test.field', then the corresponding key in the $_POST array, when posted to a PHP script, will be 'test_field'. PHP substitute the . with a _ (underscore). According the HTML spec the . is a legal character in the name as long as it starts with [a-zA-Z].
So why do PHP make this substitution? I tried to find the answer online, but without any luck. Please post a comment if you know the answer or where to find it.

Update
I found this comment on php.net http://www.php.net/manual/en/language.variables.external.php#81080 Apparently this is done for backwards compatibility with register globals.

Friday, November 27, 2009

php, LDAP and the whole tree

After using several hours on trying to search the entire AD tree on Windows 2003, I finally found the solution. Copied directly from php.net:

allie at lsu dot edu
06-Mar-2007 10:23
I sure do wish there was some way I could get this information out to all programmers in the world about binding and searching MS AD. This is the second time I was bit by the "I need to search the entire tree" problem.

For php (and apache auth_ldap ) you need to specify port 3268 when you want to search the entire tree. Otherwise it will spit out the partial results error.

ldap_connect($server,3268);

I'm just fortunate enough to have won this same battle with apache searching the whole directory. When I noticed our php application failing auth's for users, I was immediately able to fix the problem by adding this port specification (and the ldap_set_option($ldapserver, LDAP_OPT_REFERRALS, 0) option).

I really hope this helps someone else before they pull all their hair out. I know I miss mine.

Wednesday, February 18, 2009

Code readability

Faldt lige over et indlæg om code readability. Meget af det er sikkert logik for burhøns, men det skader aldrig lige at få pointerne igen. Nogen gange har man en tendens til at bare at spytte kode ud, uden at tænke på at der måske er en der skal læse koden i fremtiden. Indlæget er det første i en serie. Indtil videre er der to i serien, men der burde komme en eller to mere.

PHP for begyndere - Screencast

Hvis man ikke har brugt PHP får og gerne vil lære det, så er denne screencast ideel. Den starter helt fra bunden og gennemgår alt hvad man har brug for at komme godt i gang med PHP. Har man erfaring med andre sprog, kan serien godt virke lidt lang, men tiden er godt givet ud.

Monday, February 16, 2009

Memcache

Faldt lige over en glimende start artikel om memcache og hvordan den bruges i PHP. Har ikke selv brugt det, men kan ikek forestille mig størrer sites kan overleve uden en eller anden form for caching.

Friday, December 5, 2008

PHP reading list

Okay, så kommer der heller ikke mere i dag. IBM lå også lige inde med en reading list for PHP. Der er både noget for nybegyndere og noget for erfarende brugere.

Reading list

PHP patterns

Måske jeg bare skulle have smidt alle de her link i en post, men fandt lige lidt mere interesant.
5 PHP patterns
5 more PHP patterns

Gode PHP vaner - Part 2

Syndes også lige jeg ville smide denne her.

Gode PHP vaner

Faldt lige over denne her artikel på IBMs hjemmeside.