Building a PHP 5 web server with MySQL 5 support isn’t as easy as it seems. We know this by heart: we spend literally a whole day last week trying to do this. The main problem is that PHP 5 doesn’t come with embedded support for MySQL and MySQL 5 does not provide a separated stand-alone client module ready for downloading and installing. In this step-by-step tutorial we will show you how to compile and install Apache 2, PHP 5 with MySQL 5 support and PHP optimization and caching system (Xcache) on Linux, so you will be able to build your own web server in a matter of minutes – and not hours like happened to us.
We got several error messages along the way, and we are going to publish the most common ones, so if you get the same messages you will know what to do.
The most important thing is that you need to upgrade your Linux version to its latest version, or at least check if libstdc++.so.6 is installed. It should be at /usr/lib/ directory (you can also try locate libstdc++.so.6 command). If you cannot find this file, you will get the following error message along the way:
error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
This error happens because your system has an old version of C++ library, probably libstdc++.so.5. The solution is to update the C++ library. However, since almost everything on Linux depends on C++, you will have to upgrade a lot of stuff as well. On Debian, the Linux flavor we were using, it was easier to simply upgrade de whole system with the following commands:
This will upgrade not only the C++ library but also update your Debian to its latest version. We recommend you performing these commands before starting the installation and configuration process.
Another trick for Debian users is to edit /etc/apt/sources.list and check from which server Debian will download files using apt commands like apt-get. By default you will see https://ftp.debian.org there, but this server is usually overloaded. You can comment the lines that point to debian.org and add new lines pointing to a faster mirror, for example:
deb https://ftp.uwsg.indiana.edu/linux/debian stable main
deb-src https://ftp.uwsg.indiana.edu/linux/debian stable main
A full list of mirrors is available here: https://www.debian.org/mirror/list. Scroll down and prefer using a secondary mirror, because they are usually less loaded, thus making all your installing and upgrading process a lot easier – especially for upgrading the whole operating system like we recommended.
After these preliminary explanations, let’s go to our step-by-step tutorial. Please notice that the following steps must be done on the same order we wrote: you cannot try to compile and install PHP 5 before MySQL 5 – it won’t work.
[nextpage title=”Configuring and Installing Apache 2″]
The first step is to download, configure and install Apache 2, which is the web server software. This is the easiest part of our tutorial. Download the latest version from https://httpd.apache.org/download.cgi. Choose the mirror nearest to the server you are building. You have an option to choose between 2.2.x series and 2.0.x series, this is up to you. The version you need to download is the “unix source”. Then on command line type something like this, depending on the link you got on this webpage:
tar -xvzf httpd-2.0.59.tar.gz
./configure –prefix=/usr/local/apache2 –enable-deflate –enable-ssl –enable-suexec –with-suexec-caller=apache –with-suexec-docroot=/ –with-suexec-datadir=/www/ –enable-mods-shared=most
Of course you may add another configuration parameters you may need for your particular installation. The trick is to make sure to use –enable-deflated to install mod_deflate, which is a plug-in for Apache that enables HTML compression, making your web server to present webpages faster and also to save bandwidth.
Also notice where we installed Apache: /usr/local/apache2
Edit /etc/profile and add :/usr/local/apache2 at the end of the PATH statement.
You will also need to edit /usr/local/apache2/conf/httpd.conf to configure your webserver. This configuration procedure, however, isn’t the scope of the present tutorial.
That’s it. Now we need to install and configure MySQL 5. Like we said, this must be done before configuring and installing PHP 5.
[nextpage title=”Configuring and Installing MySQL 5″]
This step is required because PHP 5 does not bring embedded MySQL support anymore. Even if you are not installing a MySQL 5 server on the same server as your web server, this step is required in order to install MySQL 5 client, in order to allow PHP 5 to connect to MySQL 5 servers.
The first thing you need to know is that MySQL 5 does not bring separated packages for client and server configurations, like MySQL 4 – everything is inside the same package. Also, the procedure for installing a MySQL client is different from installing a MySQL server.
If you don’t want a MySQL 5 server running on the same server as your web server – which is desirable for large websites, i.e., you will have one web server running Apache + PHP and another server running exclusively MySQL – you will need to install only a “client” version of MySQL 5.
If you want a MySQL 5 server running on the same server as your web server, then you will need to install the whole MySQL 5 server.
The procedure for these two scenarios is quite different, so you need to know which path to take.
Anyway, for both scenarios the first thing you need to do is to uninstall any MySQL version you may already have installed on your server.
On Debian you can do this by running (assuming that the old version was 4.1, of course):
apt-get remove mysql-server-4.1
apt-get remove mysql-client-4.1
On Fedora you can do this by running:
rpm -e <module_name>
With rpm -qa | grep MySQL you can find out the MySQL modules you have installed and that you will need to remove.
You can also, just in case, move the old MySQL libraries that may be left in the system to a backup location, with:
mv /usr/lib/libmysql* /root
So if anything goes wrong you still have these files.
Here comes the tricky part. If you want to install just MySQL 5 client, you will need to download the source code. The problem is that the source code is the last thing on MySQL downloads page, and a lot of people download the binaries instead, which contains MySQL server, not the client.
If you want to install MySQL 5 server on the same server as your web server, you will need to download both the source code and the binaries. Of course you can compile the source code to get the binaries done, however here is a important trick: according to MySQL website, if your server is based on an Intel CPU, the binaries compiled with Intel compiler will make your MySQL 5 server to run 20% faster than installing the standard binaries compiled with gcc. Since our server was a dual-Xeon, we preferred to download the Intel-compiled version rather than compiling MySQL 5 by ourselves.
So, for both scenarios you will need to create MySQL 5 client. As we mentioned, this is done by downloading the MySQL 5 source code at https://dev.mysql.com/downloads/mysql/5.0.html – it is the very bottom of this page. Download the “Compressed GNU TAR archive (tar.gz)” version.
The downloading, configuration and installation sequence would be the following:
tar -xvzf mysql-5.0.37.tar.gz
You can check if your installation was successfully done by checking if the new mysql libraries were installed at /usr/lib:
The output should be something like this:
If you can’t find these files or if they are using a version lower than 15, you must redo the above procedures, making sure to run make clean before starting over.
Now your MySQL 5 client is installed. The next step is installing MySQL 5 server, if you want to have a MySQL 5 server on the same server as your web server. MySQL 5 server installation is quite easy.
Download the compiled binaries from https://dev.mysql.com/downloads/mysql/5.0.html and install them. As we said before, if your server is based on an Intel CPU, you should download the “Linux (non rpm, Intel C/C++ compiled, glibc-2.3) downloads” version. That is the version we downloaded, so the step-by-step of installing this package would be the following:
tar -xvzf mysql-5.0.37-linux-i686-icc-glibc23.tar.gz
mv mysql-5.0.37-linux-i686-icc-glibc23 mysql
useradd -g mysql mysql
mv mysql /usr/local
chown -R root .
chown -R mysql data
chgrp -R mysql .
bin/mysqld_safe –user=mysql &
Note the path where MySQL will be installed: /usr/local/mysql.
Edit /etc/profile and add :/usr/local/mysql/bin at the end of the PATH statement.
[nextpage title=”Configuring and Installing PHP 5″]
Now it is time for downloading, configuring and installing PHP 5. You must install MySQL 5 client BEFORE configuring PHP 5, otherwise you will get a series of error messages. So if you came directly to this page, please go back to the previous page and follow the steps described there.
Before compiling PHP 5, you need to install several dependencies, if they aren’t already installed. They are Zlib, Jpeg and Png. They are required if you want to run GD, which is a module for editing images on the fly. We recommend enabling GD support, thus we will install these three items:
For zlib, download its latest version at https://www.zlib.net/ and do:
tar -xvzf zlib-1.2.3.tar.gz
For png support, download Libpng at https://www.libpng.org/pub/png/libpng.html and do:
tar -xvzf libpng-1.2.16.tar.gz
For Jpeg support:
tar -xvzf jpegsrc.v6b.tar.gz
These are the support libraries for GD. On our installation on Debian, we needed to install the following libraries to solve dependency errors while compiling PHP 5:
Apt-get install libxml2
Apt-get install libxml2-dev
Apt-get install libxml2-utils
apt-get install autoconf
dpkg -s autoconf
Now it is time for downloading, configuring and compiling PHP. PHP can be downloaded from https://www.php.net/downloads.php. The step-by-step would be:
tar -xvzf php-5.2.1.tar.gz
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=/usr/local/bin/mysql_config –with-mysqli=/usr/local/bin/mysql_config –prefix=/usr/local/apache2/php –with-config-file-path=/usr/local/apache2/php –with-zlib-dir=/usr/lib –with-gd –with-imap=/usr/local/imap-2004a –with-xml –enable-shmop –with-zlib –with-png –with-jpeg-dir=/usr/lib –enable-gd-native-ttf –enable-exif –with-kerberos –with-imap-ssl
cp php.ini-dist /usr/local/apache2/php/php.ini
Of course this configuration is the one we needed based on the PHP scripts we were going to run; your configure line can be different.
The major trick here are the –with-mysql and –with-mysqli parameters. They must tell configure script the location of mysql_config file: it can be at /usr/local/bin/ or at /usr/local/mysql/bin, depending wether you installed only MySQL 5 client or both client and server, respectively. The major problem with PHP 5 configuration with MySQL 5 support resides on these two parameters. If they are misconfigured, you will get errors such as:
configure: error: Cannot find MySQL header files under /usr/bin/mysql.
Note that the MySQL client library is not bundled anymore.
configure: error: mysql configure failed. Please check config.log for
Notice that php was installed at /usr/local/apache2/php.
Now you need to configure httpd.conf file to add PHP 5 support. Run vi /usr/local/apache2/conf/httpd.conf and look for the line below:
LoadModule php4_module modules/libphp4.so
If it exists, please add a # in front of it. Now check if the line below exists and if it is uncommented. If it doesn’t exist, please add it:
LoadModule php5_module modules/libphp5.so
Also look for the following line:
AddHandler php-script php
If it exists, add a # in front of it. Now check if the line below exists and if it is uncommented. If it doesn’t exist, please add it:
AddHandler php5-script php
Now look for the following line and if it is uncommented. If it doesn’t exist, add it:
AddType text/html php
Exit vi (Esc wq <Enter>) and restart Apache with the following command:
Now you need to check if you installation was successful by creating a file called phpinfo.php with the following contents:
Put it at the root directory of your webserver (e.g., /www/your-website) and call it with your web browser (https://www.your-website.com/phpinfo.php). Of course you must have configured httpd.conf first in order to make Apache to know which directory it should use for your website. Even if you don’t have an URL pointing to your server IP address yet or if the server you are building is a local server on your own network, you can configure Apache (at its httpd.conf file) to answer requests based on the server IP address, thus you can use https://192.168.0.2/phpinfo.php (where 192.168.0.2 is obviously our web server IP address).
You should get a detailed description of how PHP is configured on your server. If you get this screen, congratulations, Apache and PHP are up and running. You should look if the following sessions exist on this page (below “Configuration”) and if they are enabled: mysql (“MySQL Support: Enabled” and if “Client API version” is the same as you installed, 5.0.21 on the example we used on our tutorial), mysqli (“MySQLi Support: Enabled” and if “Client library version” is the same as you installed, 5.0.37 on the example we used on our tutorial) and GD (“GD Support: Enabled”). If you can’t find them, check the configuration and installation process.
If everything is fine with your Apache and PHP 5 with MySQL 5 support installation, you can go ahead to the final step, which is installing a PHP cache and optimizer, which is a module that will save CPU resources from your web server, improving its performance.
[nextpage title=”Configuring and Installing Xcache”]
There are several PHP cache modules around – they help saving CPU resources from your server –, some also bringing PHP optimization, which improves PHP performance: PHP-Accelerator, Mmcache, APC and Xcache. We will show you how to configure and install Xcache, as it brings native support for PHP 5, so you won’t face any compatibility issue.
Look at https://18.104.22.168/pub/XCache/Releases/ for the latest Xcache release on the 1.2 series (1.2.0 at the time of this writing) on the tar.gz format.
The downloading, configuration and compilation sequence is the following:
tar –xvzf xcache-1.2.0.tar.gz
./configure –enable-xcache –enable-xcache-optimizer –with-php-config=/usr/local/apache2/php/bin/php-config
Please pay attention at the output to see where Xcache module was installed. In our case, it was installed under /usr/local/apache2/php/lib/php/extensions/no-debug-non-zts-20060613/.
Now edit php.ini (vi /usr/local/bin/apache2/php/php.ini) and add the following lines. Change the first one according to where xcache module was installed.
zend_extension = /usr/local/apache2/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
; required for >=php5.1 if you turn XCache on
auto_globals_jit = Off
xcache.admin.user = “admin”
; paste the output of md5(”password”) below
xcache.admin.pass = “”
xcache.size = 32M
xcache.count = 1
xcache.slots = 8K
xcache.mmap_path = “/dev/zero”
xcache.cacher = On
xcache.optimizer = On
You can install a web admin interface that comes with Xcache. To do that, you need to fill the xcache.admin.pass with a password in MD5 format (click here to convert a text password into MD5) and then copy admin folder from the Xcache installation directory to a directory on your web server, for example:
mv admin /www/your-website/xcache
With this Xcache admin panel will be accessible through https://www.your-website.com/xcache
Restart Apache with /usr/local/apache2/bin/apachectl restart and check again your phpinfo output, this time looking for a Xcache session (“Xcache session: Enabled”). If it is there, you will have your webserver up and running with PHP 5, MySQL 5 support and PHP cache and optimization module. If not, repeat again the procedures described on this page.