LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Statically Compile PHP - Not possible then? (https://www.linuxquestions.org/questions/linux-newbie-8/statically-compile-php-not-possible-then-860677/)

helptonewbie 02-04-2011 07:23 AM

Statically Compile PHP - Not possible then?
 
Hi All,

Been trying to do this for a few hours looks like a lot of people have trouble doing this and so far i'm in the same boat as all of them.

So just trying to get this to work so I can ship PHP where I want and it will just work when I move about to different machines etc.

'./configure' '--prefix=/opt/php123' '--enable-cli' '--disable-all' '--enable-session' '--enable-static'
Code:

# file /opt/php123/bin/php
/opt/php123/bin/php: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), for GNU/Linux 2.6.4, not stripped
# ldd /opt/php123/bin/php
        linux-gate.so.1 =>  (0xffffe000)
        libcrypt.so.1 => /lib/libcrypt.so.1 (0xb7f7f000)
        librt.so.1 => /lib/librt.so.1 (0xb7f76000)
        libresolv.so.2 => /lib/libresolv.so.2 (0xb7f63000)
        libm.so.6 => /lib/libm.so.6 (0xb7f3e000)
        libdl.so.2 => /lib/libdl.so.2 (0xb7f39000)
        libnsl.so.1 => /lib/libnsl.so.1 (0xb7f24000)
        libc.so.6 => /lib/libc.so.6 (0xb7e04000)
        libpthread.so.0 => /lib/libpthread.so.0 (0xb7df0000)
        /lib/ld-linux.so.2 (0xb7fc3000)

This is when trying to compile php 5.2.17
I'm fully aware its EOL but in this case Needs MUST!

If i change or 'mv' one of the shared linked in libs then php does fail with unable to load shared library.... what gives!?

Thanks for any replies.
Regards,
M

business_kid 02-04-2011 09:30 AM

Those libs come from glibc, if I'm not mistaken. They handle everyday interfacing with the system. EVERYTHING is linked against ld-linux.so & libc.so.6. It's presumed you have access to /lib.

helptonewbie 02-04-2011 09:54 AM

Stupid PHP i think is buggy on this.

Can get it working by editing Makefile just before make && make install.

Change lines BUILD_CLI= blah where -export-dynamic
Replace with
-all-static

Then everything gets sorted and statically linked.

@php - does --enable-static actually do anything!? haha

Cheers

Valery Reznic 02-05-2011 08:23 AM

Quote:

Originally Posted by helptonewbie (Post 4248362)
Stupid PHP i think is buggy on this.

Can get it working by editing Makefile just before make && make install.

Change lines BUILD_CLI= blah where -export-dynamic
Replace with
-all-static

Then everything gets sorted and statically linked.

@php - does --enable-static actually do anything!? haha

Cheers

While you was able to statically link PHP, and it's definitely an accomplishment
are you sure it's enough?
Usually PHP tends to load a lot of modules and those modules use shared libraries - the very thing you are trying to avoid

For more details you can look here http://magicermine.com/docs/c_packaging.html#s_php

helptonewbie 02-05-2011 10:18 AM

Hi Valery,

Thanks for your comment.

As you can see from my compile choices i'm loading very very little and anything that is being loaded is PHP built-ins.
Thanks for the link though because I will have a look at that.

The 'file' command shows the php bin built as static and 'ldd' also notes that the binary is static and isn't starting any shared libs.

Only problem is - haha it doesn't seem to work. I am trying to load in a dynamic library however that's a .so file, and i get:
PHP Warning: PHP Startup: Unable to load dynamic library '/opt/php/lib/XX.so' - /opt/php/lib/XX.so: undefined symbol: zend_register_string_constant in Unknown on line 0

See i've got a version of php exactly like this but not statically compiled which works fine (same version and compile options - even 'php -i' between this version and other normal version everything is basically the same except this static version shows up the error above). But when running php -i on this statically compiled version i get the above error. The library is being pulled in by the php.ini file which is exactly as planed and the whole reason i'm trying to do this in the first place.

I needed a version php.5.2.x to run this particular library and thats all i needed, hence the compile options being so minimal.

If you've got any other ideas they'l be greatly recieved and i'll look into that link you sent over now as well.

Many Thanks,
M

business_kid 02-06-2011 03:34 AM

Have you simply thought of setting LD_LIBRARY_PATH, running ldconfig or at a push, even running
cd /opt/lib
lndir /usr/lib (which is awful) and then try the version you have?

helptonewbie 02-06-2011 08:14 AM

HI Business_kid,

I have already added the path of any shared libs to ld.so.conf and run ldconfig.

using the following i can see that both versions of php static or shared are picking up any shared libraries that they still require without problem

strace -e open [php binary]

But if you have any other ideas i'm happy to hear them?

Best,
M

EDIT:-
Using the following strace against each binary...
static compile version - strace -F -tt -o static.txt -e open php-5.2.17/bin/php -i
dynamic compile version - strace -F -tt -o dynamic.txt -e open php-5.2.17_safe/bin/php -i
So i know both versions are getting access to the libraries they ask for as well.

So what i find interesting is the order in which these are being run. I think I read only a few min's ago that the ordering is important...???

Code:

# cat static.txt | grep 'O_RDONLY) = 3'
14:29:14.333706 open("/usr/share/locale/locale.alias", O_RDONLY) = 3
14:29:14.334542 open("/usr/lib/locale/en_GB.utf8/LC_CTYPE", O_RDONLY) = 3
14:29:14.334915 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
14:29:14.335301 open("/etc/localtime", O_RDONLY) = 3
14:29:14.336840 open("/opt/php-5.2.17/lib/php.ini", O_RDONLY) = 3
14:29:14.339825 open("/opt/php/lib/php.ini", O_RDONLY) = 3
14:29:14.343026 open("/opt/php/lib/php520_cybersource.so", O_RDONLY) = 3
14:29:14.344088 open("/etc/ld.so.cache", O_RDONLY) = 3
14:29:14.344419 open("/usr/local/src/simapi-php-5.0.1/lib/libspapache.so", O_RDONLY) = 3
14:29:14.345171 open("/usr/local/src/simapi-php-5.0.1/lib/libxerces-c.so.21", O_RDONLY) = 3
14:29:14.345978 open("/usr/local/src/simapi-php-5.0.1/lib/libxalan-c1_5_0.so", O_RDONLY) = 3
14:29:14.346749 open("/lib/libc.so.6", O_RDONLY) = 3
14:29:14.347599 open("/lib/libdl.so.2", O_RDONLY) = 3
14:29:14.348311 open("/lib/libgcc_s.so.1", O_RDONLY) = 3
14:29:14.349024 open("/lib/libpthread.so.0", O_RDONLY) = 3
14:29:14.349854 open("/usr/local/src/simapi-php-5.0.1/lib/libstdc++.so.4", O_RDONLY) = 3
14:29:14.350662 open("/lib/libm.so.6", O_RDONLY) = 3
14:29:14.351514 open("/lib/ld-linux.so.2", O_RDONLY) = 3
14:29:14.371984 open("/opt/php/lib/php520_cybersource.so", O_RDONLY) = 3
14:29:14.372864 open("/etc/ld.so.cache", O_RDONLY) = 3
14:29:14.373186 open("/usr/local/src/simapi-php-5.0.1/lib/libspapache.so", O_RDONLY) = 3
14:29:14.373869 open("/usr/local/src/simapi-php-5.0.1/lib/libxerces-c.so.21", O_RDONLY) = 3
14:29:14.374640 open("/usr/local/src/simapi-php-5.0.1/lib/libxalan-c1_5_0.so", O_RDONLY) = 3
14:29:14.375557 open("/lib/libdl.so.2", O_RDONLY) = 3
14:29:14.376363 open("/lib/libgcc_s.so.1", O_RDONLY) = 3
14:29:14.377061 open("/usr/local/src/simapi-php-5.0.1/lib/libstdc++.so.4", O_RDONLY) = 3
14:29:14.377819 open("/lib/libm.so.6", O_RDONLY) = 3
# cat dynamic.txt | grep 'O_RDONLY) = 3'
14:29:04.833939 open("/etc/ld.so.cache", O_RDONLY) = 3
14:29:04.834744 open("/lib/libcrypt.so.1", O_RDONLY) = 3
14:29:04.835466 open("/lib/librt.so.1", O_RDONLY) = 3
14:29:04.836087 open("/lib/libresolv.so.2", O_RDONLY) = 3
14:29:04.836764 open("/lib/libm.so.6", O_RDONLY) = 3
14:29:04.837356 open("/lib/libdl.so.2", O_RDONLY) = 3
14:29:04.838030 open("/lib/libnsl.so.1", O_RDONLY) = 3
14:29:04.838695 open("/lib/libc.so.6", O_RDONLY) = 3
14:29:04.839423 open("/lib/libpthread.so.0", O_RDONLY) = 3
14:29:04.843643 open("/usr/share/locale/locale.alias", O_RDONLY) = 3
14:29:04.844545 open("/usr/lib/locale/en_GB.utf8/LC_CTYPE", O_RDONLY) = 3
14:29:04.844936 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
14:29:04.845398 open("/etc/localtime", O_RDONLY) = 3
14:29:04.846989 open("/opt/php-5.2.17/lib/php.ini", O_RDONLY) = 3
14:29:04.850856 open("/opt/php/lib/php520_cybersource.so", O_RDONLY) = 3
14:29:04.852898 open("/etc/ld.so.cache", O_RDONLY) = 3
14:29:04.853250 open("/usr/local/src/simapi-php-5.0.1/lib/libspapache.so", O_RDONLY) = 3
14:29:04.855012 open("/usr/local/src/simapi-php-5.0.1/lib/libxerces-c.so.21", O_RDONLY) = 3
14:29:04.857039 open("/usr/local/src/simapi-php-5.0.1/lib/libxalan-c1_5_0.so", O_RDONLY) = 3
14:29:04.858883 open("/lib/libgcc_s.so.1", O_RDONLY) = 3
14:29:04.860656 open("/usr/local/src/simapi-php-5.0.1/lib/libstdc++.so.4", O_RDONLY) = 3

Looks to me like dynamic PHP pulls in a few different shared lib's right at the start of loading PHP. I can't understand why statically built php seems to still have dependencies on other libraries within /lib? And i know the static version appears to try to run twice through or something? Well that explains why I get the same error several times over.

business_kid 02-07-2011 02:39 AM

It could be in the way you configure it, or in the file itself. Static may not just be a supported mode, and you may have to hack the Makefile. I presume you are doing something like ./configure --enable-static --disable-shared?

Why do you need/want a totally static php? It's normally a poor way to go.

Valery Reznic 02-07-2011 03:30 AM

Quote:

Originally Posted by helptonewbie (Post 4249911)
HI Business_kid,

Looks to me like dynamic PHP pulls in a few different shared lib's right at the start of loading PHP. I can't understand why statically built php seems to still have dependencies on other libraries within /lib? And i know the static version appears to try to run twice through or something? Well that explains why I get the same error several times over.

Your perfectly static PHP read file php.ini, and load (via dlopen) modules from /usr/local/src/simapi-php-5.0.1/lib/
and those modules (shared libraries) depend on other shared libraries and dlopen load them all.

Linking PHP static is not meant it will be portable - link I posted before explains why in details.

helptonewbie 02-08-2011 03:27 AM

@business_kid:-
Latest set of configure command is
Code:

'./configure'  '--prefix=/opt/php-5.2.17' '--enable-cli' '--disable-all' '--enable-session' '--enable-static' '--with-config-file-scan-dir=/opt/php/lib'
I'm already hacking with the Makefile as explained in my OP. The reason i'm trying to build a static PHP is so that I can do exactly what its meant for and make it portable between systems and just pick-up and move it, for it to work out of the box.

@Valery:-
Yes that's great, but ermine is a pay for product. It looks great and I know you get a few days trial but thats not useful enough for me. I'd love to be using it and would be if it was free.

I still can't understand the problem then, ok yes the other libraries that are being pulled in are pulling in more libraries for themselves, fair enough. But ive got the dynamic and static versions on the same box and thus as both can get to all the libraries they need plus shared libraries then the only thing I can think of why there's still a problem with the static version is something to do with the nss part of the link you sent over @valery?

Any ideas how I can force it to include these other things without the need for ermire? Or that that a crazy mine field?

Many Thanks,
M

business_kid 02-09-2011 03:35 AM

you need --enable-static and --disable-shared together on the same config line. Perhaps also email the authors of testdisk. They do an executable that is totally portable. Ask them for tips.
dec@genius:~$ ldd /oldslack/usr/sbin/testdisk
not a dynamic executable

Valery Reznic 02-09-2011 04:26 AM

Quote:

Originally Posted by helptonewbie (Post 4251633)
I still can't understand the problem then, ok yes the other libraries that are being pulled in are pulling in more libraries for themselves, fair enough. But ive got the dynamic and static versions on the same box and thus as both can get to all the libraries they need plus shared libraries then the only thing I can think of why there's still a problem with the static version is something to do with the nss part of the link you sent over @valery?

Any ideas how I can force it to include these other things without the need for ermire? Or that that a crazy mine field?

Many Thanks,
M

When you dlopen shared libraries, their dependencies includes /lib/ld-linux and libc.so as you can see in your
Code:

cat static.txt | grep 'O_RDONLY) = 3'
But you already have libc linked statically in your executable!
Having two libc in one process likely to be a bit of problem.
And ld-linux.so is a very special thing too. Loading (and starting) it in the middle of the program
execution may result in very interesting effects (and unlikely to make your program happy)

NSS libraries, judging by strace you posted not in use, but they can be used when your PHP will do actual works.

And it's not a crazy mine field. It's much, much worse :(

helptonewbie 02-10-2011 08:43 AM

Thanks for the continued responses guys its much appreciated!

@business_kid
I was under the assumption that --disable-shared would stop me be able to load the library that I am trying to call in from the php.ini?
But thanks, for the testdisk info i'll check them out and see if they can help at all.

@Valery Reznic
Thanks, i was looking to try and make the static version start the libraries in the same order by compiling with LD_PRELOAD and then the order that I wanted the libraries to be started in and to also make sure the libraries were pulled in but this didn't seem to make any difference at all. I'm so glad to hear its worse than a mine field and no wonder there's not many search results around for actually being able to sort this kind of thing out.

I also note that my changing of the Makefile that actually gets PHP to build statically might be the same thing thats causing the library extension i've got PHP.ini trying to load also not work. -all-static according to this means (http://www.gnu.org/software/libtool/...html#Link-mode) means not to include shared library files to me during compile? Or even to not include any on program execution.

Valery Reznic 02-10-2011 11:06 AM

Quote:

Originally Posted by helptonewbie (Post 4254143)
@Valery Reznic
Thanks, i was looking to try and make the static version start the libraries in the same order by compiling with LD_PRELOAD and then the order that I wanted the libraries to be started in and to also make sure the libraries were pulled in but this didn't seem to make any difference at all. I'm so glad to hear its worse than a mine field and no wonder there's not many search results around for actually being able to sort this kind of thing out.

I also note that my changing of the Makefile that actually gets PHP to build statically might be the same thing thats causing the library extension i've got PHP.ini trying to load also not work. -all-static according to this means (http://www.gnu.org/software/libtool/...html#Link-mode) means not to include shared library files to me during compile? Or even to not include any on program execution.

dlopen happened not during compilation, but at run time. So I don't think -all-static will affect it.
But I am no expert with libtool./


All times are GMT -5. The time now is 10:00 AM.