# Howto build statically linked PHP CGI (without shared libs)

- Thread: https://externals.io/message/40436
- Original archive: https://news-web.php.net/php.internals/40436
- Started by santi@usansolo.net on 2008-09-10T14:54:08+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #40436 — santi@usansolo.net — 2008-09-10T14:54:08+00:00

URL: https://externals.io/message/40436#40436

Dear Srs,

I'm trying to compile a statically linked, without shared libs, CGI version
of PHP, I've already tested with 5.2.6 and 5.2.7-dev.

Is there any option in ./configure to compile PHP with this feature?

Also I have tried passing CC and CFLAGS environment variables with
"-static" flags.. but the CGI builds using shared libs, example:

# file /tmp/php-cgi /tmp/php-cgi: ELF 32-bit LSB executable, Intel 80386,
version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared
libs), stripped

# ldd php-cgi | head
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x00335000)
        libc-client.so.0 => /usr/lib/libc-client.so.0 (0xb7f38000)
        libexslt.so.0 => /usr/lib/libexslt.so.0 (0x00367000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00323000)
        libm.so.6 => /lib/tls/libm.so.6 (0x002fe000)
        libaspell.so.15 => /usr/lib/libaspell.so.15 (0x00767000)
        libpspell.so.15 => /usr/lib/libpspell.so.15 (0xb7f36000)
        librt.so.1 => /lib/tls/librt.so.1 (0x006b2000)
        libmysqlclient.so.14 => /usr/lib/mysql/libmysqlclient.so.14
(0xb7dff000)
        libmcrypt.so.4 => /usr/lib/libmcrypt.so.4 (0xb7dce000)

How can I compile statically the CGI version of PHP? Thanks!! 

Regards,

-- 
Santi Saez
http://woop.es

---

## #40437 — crrodriguez@suse.de — 2008-09-10T15:06:32+00:00

URL: https://externals.io/message/40436#40437 · In reply to: #40436

santi@usansolo.net escribió:
> Dear Srs,
> 
> I'm trying to compile a statically linked, without shared libs, 

Why you want such thing ?


> Is there any option in ./configure to compile PHP with this feature?

No.

> How can I compile statically the CGI version of PHP? Thanks!! 

even if you manage to link all libraries statically, you will end with a
huge, memory hungry and hard to debug php binary that anyways require
shared libc at runtime ;-P


-- 
"A computer is like an Old Testament god, with a lot of rules and no
mercy. "

Cristian Rodríguez R.
Platform/OpenSUSE - Core Services
SUSE LINUX Products GmbH
Research & Development
http://www.opensuse.org/

---

## #40438 — santi@usansolo.net — 2008-09-10T15:31:04+00:00

URL: https://externals.io/message/40436#40438 · In reply to: #40437

On Wed, 10 Sep 2008 11:06:32 -0400, Cristian Rodríguez
<crrodriguez@suse.de> wrote:

Dear Cristian,

> > […]
> 
> Why you want such thing ?

We want to provide a method to "customize" PHP version to our clients; for
example, all our servers run with PHP 5.1.6, but if one client needs PHP
v5.2.6 to run X CMS.. so we provide a statically linked version of the CGI
for 5.2.6.

In the future, if we upgrade from CentOS 4 to CentOS 5 (for example).. all
clients running with the default PHP version in the server will not have
problem. But clients with "personalized" PHP version running as CGI, will
have problems: there will be no available the shared libs wich is
dynamically linked to..

So, we want to provide this version of the CGI.., we know that the version
of the CGI will be bigger, and that's not a good idea, but we need to do it
;-(


> > […]
> 
> even if you manage to link all libraries statically, you will end with a
> huge, memory hungry and hard to debug php binary that anyways require
> shared libc at runtime ;-P

Yes, it maybe a problem.. 

Now we're using statifier [1] to convert dynamically linked executables and
all it's libraries in one file:

# file php-cgi
php-cgi: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for
GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped

# ldd php-cgi | head -10
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x00335000)
        libc-client.so.0 => /usr/lib/libc-client.so.0 (0x00397000)
        libexslt.so.0 => /usr/lib/libexslt.so.0 (0x00367000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00323000)
        libm.so.6 => /lib/tls/libm.so.6 (0x002fe000)
        libaspell.so.15 => /usr/lib/libaspell.so.15 (0x00767000)
        libpspell.so.15 => /usr/lib/libpspell.so.15 (0x00111000)
        librt.so.1 => /lib/tls/librt.so.1 (0x006b2000)
        libmysqlclient.so.14 => /usr/lib/mysql/libmysqlclient.so.14
(0x001c8000)
        libmcrypt.so.4 => /usr/lib/libmcrypt.so.4 (0x00f67000)

# statifier php-cgi php-cgi.statifier

# file php-cgi.statifier
php-cgi.statifier: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked, not stripped

# ldd php-cgi.statifier
        not a dynamic executable

statifier'd version of the CGI works fine.. but we're looking a better
method to directly built the CGI.

# ls -lh php-cgi php-cgi.statifier
-rwxr-xr-x  1 root root 7.0M Sep 10 15:10 php-cgi
-rwxr-xr-x  1 root root  19M Sep 10 17:14 php-cgi.statifier

Regards,

[1] http://statifier.sourceforge.net/

--
Santi Saez
http://woop.es

---

## #40440 — Nuno Lopes — 2008-09-10T15:48:30+00:00

URL: https://externals.io/message/40436#40440 · In reply to: #40438

> statifier'd version of the CGI works fine.. but we're looking a better
> method to directly built the CGI.

you can always use llvm and compile all libraries you need to bitcode
and then link them with PHP. that way will enable more optimizations
and will also reduce the binary size, since llvm will remove the
library functions that PHP doesn't use. (details on how to do this are
too much off-topic for this list)
Still, upgrading a library only represents a problem if its ABI
changes (usually the filename changes as well). If not, there isn't
any trouble.

Nuno

---

## #40441 — Jani Taskinen — 2008-09-10T16:19:28+00:00

URL: https://externals.io/message/40436#40441 · In reply to: #40437

Cristian Rodríguez kirjoitti:
> santi@usansolo.net escribió:
> > […]
> 
> Why you want such thing ?
> 
> 
> > […]
> 
> No.

Wrong: --disable-shared --enable-static

Might or might not work, no guarantees.

--Jani

---

## #40450 — Santi Saez — 2008-09-10T21:39:47+00:00

URL: https://externals.io/message/40436#40450 · In reply to: #40441

Jani Taskinen escribió:
> Wrong: --disable-shared --enable-static
>
> Might or might not work, no guarantees.

Dear Jani,

Thanks for your reply, I have tried with a very simple:

# ./configure --disable-all --disable-shared --enable-static

But, generated binary uses shared libs ;-(

# file sapi/cgi/php-cgi
sapi/cgi/php-cgi: ELF 32-bit LSB executable, Intel 80386, version 1 
(SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not 
stripped

# ldd sapi/cgi/php-cgi
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x00335000)
        libresolv.so.2 => /lib/libresolv.so.2 (0x00375000)
        libm.so.6 => /lib/tls/libm.so.6 (0x002fe000)
        libdl.so.2 => /lib/libdl.so.2 (0x002f8000)
        libnsl.so.1 => /lib/libnsl.so.1 (0x0039e000)
        libc.so.6 => /lib/tls/libc.so.6 (0x001ca000)
        /lib/ld-linux.so.2 (0x001b0000)

Thanks anyway! I will try other method..

--
Santi Saez
http://woop.es

---

## #40451 — Paul Biggar — 2008-09-10T22:23:52+00:00

URL: https://externals.io/message/40436#40451 · In reply to: #40450

Hi Santi,


> Jani Taskinen escribió:
> > […]

As you say, this doesn't work.

I needed something like this to for profiling. I believe I got the
static archive linked, but it was a while ago, so I'm not positive
(sadly, the profiling never worked this way).

Here is the script I used. I don't think the script itself works, but
if you have a look through the last 5 or 6 lines of it, it should be
easy enough.

http://code.google.com/p/phc/source/browse/trunk/misc/old/get_libphp5_archive


Those long lines were created by looking at the make output used for
building the shared archive. I think I had to manually remove .o files
every time the linker complained, which was a lot, a problem that you
don't have with shared libs.

Let me know how you get on,
Paul



> Jani Taskinen escribió:


-- 
Paul Biggar
paul.biggar@gmail.com

---

## #40458 — Jani Taskinen — 2008-09-11T07:57:55+00:00

URL: https://externals.io/message/40436#40458 · In reply to: #40450

Santi Saez wrote:
> Jani Taskinen escribió:
> > […]
> 
> Dear Jani,
> 
> Thanks for your reply, I have tried with a very simple:
> 
> # ./configure --disable-all --disable-shared --enable-static
> 
> But, generated binary uses shared libs ;-(

Yes, that wasn't really the thing. Libtool is nasty but it is possible.
With this small Makefile modification you'll get what you want:

BUILD_CLI = $(LIBTOOL) --mode=link $(CC) -all-static
                                          ^^^^^^^^^^^

Just add the '-all-static' option there after $(CC) and before any other 
stuff on the line. And same for BUILD_CGI line too if you want that as 
all static too.

I tested this and it worked, with some nasty notices but those can be 
ignored. :)

BEWARE: You need ALL the libraries that PHP links to as static libs for 
this to work!

--Jani

---

## #40468 — crrodriguez@suse.de — 2008-09-11T14:35:45+00:00

URL: https://externals.io/message/40436#40468 · In reply to: #40458

Jani Taskinen escribió:

> BEWARE: You need ALL the libraries that PHP links to as static libs for
> this to work!

And that is exactly what may not work ;) static libraries are being
retired from linux distributions, this is at least the case of openSUSE,
Fedora and probably others.



-- 
"A computer is like an Old Testament god, with a lot of rules and no
mercy. "

Cristian Rodríguez R.
Platform/OpenSUSE - Core Services
SUSE LINUX Products GmbH
Research & Development
http://www.opensuse.org/

---

## #40469 — Antony Dovgal — 2008-09-11T14:42:55+00:00

URL: https://externals.io/message/40436#40469 · In reply to: #40468

> Jani Taskinen escribió:
> 
> > […]
> 
> And that is exactly what may not work ;) static libraries are being
> retired from linux distributions, this is at least the case of openSUSE,
> Fedora and probably others.

Are you sure?
I can see them in -devel packages all right.

# rpm -qf /usr/lib64/libbz2.a
libbz2-devel-1.0.5-13.1

-- 
Wbr, 
Antony Dovgal

---

## #40470 — Jani Taskinen — 2008-09-11T14:44:55+00:00

URL: https://externals.io/message/40436#40470 · In reply to: #40469

Antony Dovgal wrote:
> 
> > […]
> 
> Are you sure?
> I can see them in -devel packages all right.
> 
> # rpm -qf /usr/lib64/libbz2.a
> libbz2-devel-1.0.5-13.1
> 

..and you can always build your libs yourself too.. ;)

--Jani
