how-to: build php with proper imap support

sloop

Verified User
Joined
Jun 13, 2004
Messages
22
Location
Raleigh, NC
problem: on centos, the libc-client package is compiled with a low "FD_SETSIZE" limit (I assume 256). when adding imap support to php, compiling against the default libc-client RPMs, this will cause Apache to segfault if Apache has over 256 open file descriptors. All active apache logs count against this limit so its easy to hit on a shared hosting server.

solution: download latest imap c-client and compile from scratch:

download and extract
Code:
cd /usr/local
wget ftp://ftp.cac.washington.edu/imap/c-client.tar.Z
tar xvfz c-client.tar.Z 
cd imap-2007f

edit source files

edit src/osdep/unix/os_art.c and src/osdep/unix/os_sv2.c in an editor,

find this:
Code:
#define	FD_SETSIZE	256

change it to:
Code:
#define	FD_SETSIZE	32768


openssl symlink

you should already have openssl and openssl-dev packages installed, but no /usr/local/ssl directory. run:

Code:
ln -s /usr/lib64/openssl/engines/ /usr/local/ssl
ln -s /usr/include/ /usr/local/ssl/include

compile imap c-client

Code:
make lnp SSLTYPE=unix EXTRACFLAGS=-fPIC

put files where php wants

Code:
mkdir lib
mkdir include
cp c-client/*.c lib/
cp c-client/*.h include/
cp c-client/c-client.a lib/libc-client.a

recompile php

Code:
cd /usr/local/directadmin/custombuild

if you don't already have a custom configure.php;

Code:
mkdir -p custom/ap2
cp configure/ap2/configure.php5 custom/ap2

add these lines to your custom/ap2/configure.php5 file:

Code:
	--with-imap=/usr/local/imap-2007f \
	--with-imap-ssl \


now run ./build php n

now your phpinfo() should show IMAP c-Client Version = 2007f
 
Last edited:
FYI, this guide has been around for a while as well:
http://help.directadmin.com/item.php?id=341

I was wondering why that guide has "--with-imap=/usr/lib/dovecot/imap" because I did not see a libc-client in /usr/lib/dovecot/imap, and the guide tells you to install the libc-client RPM anyway.

That guide doesn't address the FD_SETSIZE issue which was causing apache processes to segfault on a server with a few hundred sites when using php imap function.
 
Hi sloop, thank you for this thread. I am a FreeBSD user, so I would like to add a few comments for those looking to compile IMAP and PHP on a FreeBSD 7.x and 8.x system (64-bit ok too!).

FreeBSD users can install the c-client libraries from ports, and compile php in DirectAdmin/custombuild without any problems.

You might get some errors when recompiling CURL or PHP, such as the following:
Code:
configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.

This means you are missing the cclient libraries. Just do the following to install them.

Code:
# cd /usr/ports/mail/cclient
# make install clean

I compiled with the default options (SSL and IPV6 support). I haven't applied the FD_SETSIZE fixes as sloop outlined above. I can look into this further if problems arise.

Afterwards, you can modify your custom configure.php5 configuration file and apply the following two lines at the end. Don't forget the backslashes (\)!

Code:
--with-imap=/usr/lib/dovecot/imap \
--with-imap-ssl

I also tried the following with success, but I rather let Dovecot manage my IMAP (I don't know if this makes a difference to be honest).

Code:
--with-imap \
--with-imap-ssl

I got this working with suPHP on a 8.2 machine, along with the latest OpenSSL from ports (1.0.0e).

Another note: if you are having problems with kerberos support in PHP 5.x, make sure you reference the directory in your configure.php5 like this (not fully tested, I compiled without kerberos support due to some other problems).

Code:
"--with-kerberos=/usr/local/include"
 
I tried this manual steps coz CentOS 6.0 here... and an error shows up in the middle, when I try to compile...:

cat osdepbas.c osdepckp.c osdeplog.c osdepssl.c > osdep.c
make[1]: *** No rule to make target `mail.h', needed by `osdep.o'. Stop.
make[1]: Leaving directory `/usr/local/imap-2007f/src/osdep/unix'
make: *** [lnp] Error 2




compile imap c-client

Code:
make lnp SSLTYPE=unix EXTRACFLAGS=-fPIC

put files where php wants

Code:
mkdir lib
mkdir include
cp c-client/*.c lib/
cp c-client/*.h include/
cp c-client/c-client.a lib/libc-client.a
 
i am also trying to get a da centos 6 build working, as i also have imap ssl compiled in my previous centos 5 builds and i would assume this would be an easy task :(

anyone knows why c-client the /usr/lib/libc-client.a is not installed by default on centos 6?
i do see the /usr/lib/libc-client.so but this links on centos 5 to /usr/lib/libc-client.so.1 and the /usr/lib/libc-client.a links on centos 5 to c-client.a

i assume the modifications for the FD_SETSIZE seems logical but i never experienced any problems, who has changed this var in its custom builds?

Code:
(offtopic) for people that do want to automate this install you can use these sed commands to change the FD_SETSIZE values

/bin/sed -i 's/FD_SETSIZE\t256/FD_SETSIZE\t32768/' src/osdep/unix/os_art.c
/bin/sed -i 's/FD_SETSIZE\t256/FD_SETSIZE\t32768/' src/osdep/unix/os_sv2.c

i get a few warning messages when compiling

Code:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Building in PARTIAL compliance with RFC 3501 security
+ requirements:
+ Compliant:
++ TLS/SSL encryption is supported
+ Non-compliant:
++ Unencrypted plaintext passwords are permitted
+
+ In order to rectify this problem, you MUST build with:
++ SSLTYPE=unix.nopwd
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Do you want to continue this build anyway? Type y or n please:

so should
Code:
make lnp SSLTYPE=unix EXTRACFLAGS=-fPIC

not be

Code:
make lnp SSLTYPE=unix.nopwd EXTRACFLAGS=-fPIC

as for the .nopwd would only allow clear text logins in an encrypted session.

what would be the default setting in the normal build!?

when compiling i see a few warning messages, and these give me allways an uncertain feeling as i dont know how it would impact later in the use or compile chain.

Code:
mh.c: In function âmh_pingâ:
mh.c:865: warning: passing argument 3 of scandir from incompatible pointer type
/usr/include/dirent.h:252: note: expected âint (*)(const struct dirent *)â but argument is of type âint (*)(struct dirent *)â
mh.c:865: warning: passing argument 4 of scandir from incompatible pointer type
/usr/include/dirent.h:252: note: expected âint (*)(const struct dirent **, const struct dirent **)â but argument is of type âint (*)(const void *, const void *)â
mh.c: In function âmh_appendâ:
mh.c:1131: warning: passing argument 3 of âscandirâ from incompatible pointer type
/usr/include/dirent.h:252: note: expected âint (*)(const struct dirent *)â but argument is of type âint (*)(struct dirent *)â
mh.c:1131: warning: passing argument 4 of âscandirâ from incompatible pointer type
/usr/include/dirent.h:252: note: expected âint (*)(const struct dirent **, const struct dirent **)â but argument is of type âint (*)(const void *, const void *)â

etc. with these warnings i allways want to not implement this, unfortunatly for centos 6 it seems the only way. :(
but i do hope the rpm repo would be fixed to include the /usr/lib/libc-client.a -> c-client.a

i dont understand why in the top procedure in the source folder you would put the .c and the .h files in the folders libs and includes?

Code:
mkdir lib
mkdir include
cp c-client/*.c lib/
cp c-client/*.h include/
cp c-client/c-client.a lib/libc-client.a

to install this lib would this not be enough?

edit: i understand now i overlooked the need to change your custom/ap2/configure.php5 with the folder to /usr/src/imap.. etc ;)

Code:
# install as lib
cp c-client/c-client.a /usr/lib/c-client.a
#link as lib
ln -s /usr/lib/c-client.a /usr/lib/libc-client.a


warning and disclaimer: i include here below my own version to auto install this, as i test this on my first centos 6 auto builds, becarefull when you just copy and paste things!! all read and understand first. thx

Code:
# install custom libc-client.a/c-client.a on a clean centos 6
#
# auto install TEST version
#
#source:http://www.directadmin.com/forum/showthread.php?t=41879&page=1
#problem: on centos, the libc-client package is compiled with a low "FD_SETSIZE" limit (I assume 256). when adding imap support to php, compiling against the default libc-client RPMs, this will cause Apache to segfault if Apache has over 256 open file descriptors. All active apache logs count against this limit so its easy to hit on a shared hosting server.
#
# dependency:
## pam-devel is an dependency for imapssl on a clean centos 6 
#
yum install pam-devel -y 
#
# perform c-client.a install
wget -O /usr/src/c-client.tar.Z ftp://ftp.cac.washington.edu/imap/c-client.tar.Z
tar -C /usr/src -xzvf /usr/src/c-client.tar.Z
/bin/sed -i 's/FD_SETSIZE\t256/FD_SETSIZE\t32768/' /usr/src/imap-2007f/src/osdep/unix/os_art.c
/bin/sed -i 's/FD_SETSIZE\t256/FD_SETSIZE\t32768/' /usr/src/imap-2007f/src/osdep/unix/os_sv2.c
ln -s /usr/lib64/openssl/engines/ /usr/local/ssl
ln -s /usr/include/ /usr/local/ssl/include
#.nopwd will allow only clear text logins in encrypted session, and only unix would allow this but give a warning
#make lnp SSLTYPE=unix.nopwd EXTRACFLAGS=-fPIC
# echo y = to accept the warning for insecure channels. probaly not the best thing but as this is public and it seems default i keep it in this post. please change as desired. 
cd /usr/src/imap-2007f
echo y | make lnp SSLTYPE=unix EXTRACFLAGS=-fPIC
# install as lib
mkdir lib
mkdir include
cp c-client/*.c lib/
cp c-client/*.h include/
cp c-client/c-client.a lib/libc-client.a
# extra as systemwide lib
/bin/cp -f /usr/src/imap-2007f/c-client/c-client.a /usr/lib/c-client.a
#link as lib
ln -s /usr/lib/c-client.a /usr/lib/libc-client.a
#

Code:
# change custom apache for imapssl (and if you have allready the line --with-imap=/usr/lib/dovecot/imap \ in your custom configure.php5)
export getdatetimevar=`date +"%m%d%y%H%M%S"`
/bin/cp -f /usr/local/directadmin/custombuild/custom/ap2/configure.php5 /usr/local/directadmin/custombuild/custom/ap2/configure.php5-$getdatetimevar
sed -i 's/--with-imap=\/usr\/lib\/dovecot\/imap \\/--with-imap=\/usr\/src\/imap-2007f \\/' /usr/local/directadmin/custombuild/custom/ap2/configure.php5
 
Last edited:
I am also trying to compile PHP with IMAP on CentOS 6. I have followed instructions of every post here and some from other websites. Finally got it to compile to a point where it doesn't complain about libc-client.a, but the compile eventually halts with these errors:
/usr/local/directadmin/custombuild/php-5.2.17/ext/imap/php_imap.c: In function 'zm_startup_imap':

/usr/local/directadmin/custombuild/php-5.2.17/ext/imap/php_imap.c:481: error: 'auth_gss' undeclared (first use in this function)

/usr/local/directadmin/custombuild/php-5.2.17/ext/imap/php_imap.c:481: error: (Each undeclared identifier is reported only once

/usr/local/directadmin/custombuild/php-5.2.17/ext/imap/php_imap.c:481: error: for each function it appears in.)

make: *** [ext/imap/php_imap.lo] Error 1

make: *** Waiting for unfinished jobs....

and when restarting httpd I get this error: invalid command php_admin_flag; apache is complaining about that flag in any vhost httpd.conf file. Any ideas?


UPDATE:
I used sloop's instructions above. Even with setting --with-imap=/to-the-path-where-libc-client.a resides, I still had compile errors. It seems PHP was looking elsewhere.

I verified that libc-client.a existed in /usr/local/imap-2007f/c-client and in /usr/local/imap-2007f/lib.

So....I created a symbolic link under /usr/lib for libc.client.a , set configure.php5 to

--with-imap=/usr/lib \
--with-imap-ssl

and recompiled with no problems. Thought I'd post this in case anyone else has the issue under CentOS 6.
 
Last edited:
For debian6:

apt-get install libssl-dev
ln -s /usr/lib/ssl/engines/ /usr/local/ssl
ln -s /usr/include/openssl/ /usr/local/ssl/include
make slx SSLTYPE=unix EXTRACFLAGS=-fPIC
 
I've gotten through most of it but on the php build it seens that it can't find the imap-2007f directory, and the directory is there- any ideas? I used soulshephards directions to a T. the imap-2007f directory I have had all files chown'd to root.


Thank you for using PHP.

/usr/local/directadmin/custombuild/custom/ap2/configure.php5: line 33: --with-imap=/usr/local/imap-2007f: No such file or directory

*** There was an error while trying to configure php. Check the custom/ap2/configure.php5 file
 
I've gotten through most of it but on the php build it seens that it can't find the imap-2007f directory, and the directory is there- any ideas? I used soulshephards directions to a T. the imap-2007f directory I have had all files chown'd to root.


Thank you for using PHP.

/usr/local/directadmin/custombuild/custom/ap2/configure.php5: line 33: --with-imap=/usr/local/imap-2007f: No such file or directory

*** There was an error while trying to configure php. Check the custom/ap2/configure.php5 file

what os/distro are you using? the instructions i originally posted were for centos 5. I can do a version for centos 6 if there is interest.
 
what os/distro are you using? the instructions i originally posted were for centos 5. I can do a version for centos 6 if there is interest.

Centos 5, I'm going to step through it again.

When I compile it's as if it can't see the directory which is just odd, I wonder if it is a permissions issue and that maybe I should chown admin:admin for it
 
Imap in php

what os/distro are you using? the instructions i originally posted were for centos 5. I can do a version for centos 6 if there is interest.

Hello, I'm in the middle of configuring a new server, and I'm a bit lost with the part after the error "configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.". I get the error, but the next part of the instructions on http://www.directadmin.com/imap.txt seem not-CentOS-like...

It would be greatly appreciated if you could update the instructions for CentOS 6.
Kind regards,
 
I don't understand. Why should these instructions not be for Centos 6?

and cannot find libc-client.a anywhere on your box:
updatedb
locate libc-client.a

Then type:
wget http://dl.atrpms.net/src/el6-i386/atrpms/stable/uw-imap-2007e-8.src.rpm
rpmbuild --rebuild uw-imap-2007e-8.src.rpm
cd /usr/src/redhat/RPMS/x86_64/
rpm -ivh uw-imap-static-2007e-8.el6.x86_64.rpm uw-imap-devel-2007e-8.el6.x86_64.rpm libc-client-2007e-8.el6.x86_64.rpm

Note, you may need to remove the newer version of libc-client and libc-client-devel, if the above conflict.
Also, link the .a file:
ln -s /usr/lib64/libc-client.a /usr/lib/libc-client.a
 
I don't understand. Why should these instructions not be for Centos 6?

Hello Richard,
rpmbuild --rebuild uw-imap-2007e-8.src.rpm gets me on my CentOS 6.3
-bash: rpmbuild: command not found.

I do have to admit, I'm not an expert. I can help myself a bit, with the support of a forum and a friend.
Kind regards,
Hendrik
 
This seems to work, I think

Have you tried to see if yum can install rpmbuild?
Code:
# yum install rpmbuild
Note I haven't taken the time to test; this may not work, but it's worth trying.

Jeff

Hello Jeff,
I failed to mention this in my post, but yes, I did try this.
Actually, I tried #yum search rpmbuild, with no real result (it found pacemaker, but that's not it).
It was rpm-build, so it seems. I followed this guideline: http://wiki.centos.org/HowTos/SetupRpmBuildEnvironment.

# yum install rpm-build
then
# yum install redhat-rpm-config
Then I tried to continue with http://www.directadmin.com/imap.txt.
# wget http://dl.atrpms.net/src/el6-i386/atrpms/stable/uw-imap-2007e-8.src.rpm
# rpmbuild --rebuild uw-imap-2007e-8.src.rpm
However, there was a PAM-something missing.
# yum install pam-devel
# rpmbuild --rebuild uw-imap-2007e-8.src.rpm
If you had installed the more recent libc-client, remove it first (conflict).
# yum remove libc-client
Find the location the rpmbuild has put the files.
# rpm -ivh uw-imap-static-2007e-8.el6.x86_64.rpm uw-imap-devel-2007e-8.el6.x86_64.rpm libc-client-2007e-8.el6.x86_64.rpm
# ln -s /usr/lib64/libc-client.a /usr/lib/libc-client.a

Added in /usr/local/directadmin/custombuild/custom/ap2/configure.php5 the 2 lines
--with-imap-ssl \
--with-imap
# ./build php n

Ok, this seemed to work for me, phpinfo() shows me imap-support.
On a CentOS 6.3.
Note, I just gathered small pieces from anywhere.
If this should not work for you, I'm not an expert (far from).
 
Hi Sloop,

Thanks for this guide, it worked great for me on CentOS 6.3 x86_64 after pulling lots of my hair out.

Quick question though...

I can see the "imap" module loaded but no reference to "imap-ssl". Is it included within the one module or should I be seeing a reference to "imap-ssl" within the modules list?

I've compiled with:
"--with-imap=/usr/local/imap-2007f" \
"--with-imap-ssl=/usr/local/imap-2007f" \ (added this line to see if it added imap-ssl to the list but didn't)
"--with-imap-ssl"

This builds fine but as said only see the one "imap" module in the list.

Thanks for the help.
 
Hello,

phpinfo() should show something as following:

Code:
imap
IMAP c-Client Version     2004 
SSL Support               enabled 
Kerberos Support          enabled

Do you see that differently?
 
Hi,

I see:
IMAP c-Client Version => 2007f
SSL Support => enabled

But not "Kerberos Support"

I've definately compiled with it though:

#!/bin/sh
"./configure" \
"--prefix=/usr/local/php5" \
"--enable-force-cgi-redirect" \
"--enable-fastcgi" \
"--with-config-file-path=/usr/local/etc/php5/cgi" \
"--with-curl=/usr/local/lib" \
"--with-gd" \
"--with-gettext" \
"--with-jpeg-dir=/usr/local/lib" \
"--with-freetype-dir=/usr/local/lib" \
"--with-kerberos" \
"--with-mcrypt" \
"--with-mhash" \
"--with-mysql=/usr" \
"--with-mysqli=/usr/bin/mysql_config" \
"--with-pcre-regex=/usr/local" \
"--with-pdo-mysql=/usr" \
"--with-pear=/usr/local/lib/php" \
"--with-png-dir=/usr/local/lib" \
"--with-xsl" \
"--with-zlib" \
"--with-zlib-dir=/usr/local/lib" \
"--with-openssl" \
"--with-iconv=/usr/local" \
"--enable-bcmath" \
"--enable-calendar" \
"--enable-exif" \
"--enable-ftp" \
"--enable-gd-native-ttf" \
"--enable-magic-quotes" \
"--enable-soap" \
"--enable-sockets" \
"--enable-mbstring" \
"--enable-zip" \
"--enable-wddx" \
"--with-imap=/usr/local/imap-2007f" \
"--with-imap-ssl"
 
Back
Top