Wednesday, March 16, 2011

Compile Apache2 + PHP5 + MySQL5 + xDebug + SSL + SVN + Python + Trac on Ubuntu Hardy (8.04) 64 bits

SkyHi @ Wednesday, March 16, 2011

After setting up Ubuntu Hardy 64 bits (also known as AMD64) on my PC I had to do what every developer needs to do every now and then: set up my working environment. This is not a hard thing to do on Ubuntu, particularly if you choose to go with precompiled packages, installing them through the friendly Synaptic Package Manager. However, I do like to keep a tight control on what is the base of my day to day work, so instead of using the built-in packages, I chose to build them from source. Once again, this is not hard, but it can bring some complications for 64 bits environments. As I may probably need to do this once again in the future, I’ve decided to blog this guide on how to install the following packages on Ubuntu 8.04 64 bits: Apache 2 (with SSL support), PHP5 with xDebug, MySQL 5, Python through mod_python, SVN (client and server), and Trac integrated with SVN.

Setting up the Basics
As I said, we will install each package by building them from source. However, in order to do so we have lots of dependencies. The most important ones will also be built from source, but others we can safely get from the package manager. So without any further explanation, let’s go ahead and install those basic dependencies (I did mean to actually break it down so you could see which software needed which package, but its very late in the AM and I’m too tired to do that now):
1$ sudo apt-get install build-essential
2$ sudo apt-get install libncurses5-dev
3$ sudo apt-get install libxml2-dev
4$ sudo apt-get install libcurl4-openssl-dev
5$ sudo apt-get install libpng12-dev
6$ sudo apt-get install autoconf
7$ sudo apt-get install libtool
zLib
This is a pre-requisite for MySQL and PHP, so let’s build it from source.
1$ cd /usr/local/src/
2$ sudo wget http://www.zlib.net/zlib-1.2.3.tar.gz
3$ sudo tar xzvf zlib-1.2.3.tar.gz
4$ sudo chown -R $USER:$USER zlib-1.2.3
5$ cd zlib-1.2.3/
6$ ./configure --prefix=/usr/local/zlib
Before proceeding with the build, we need to slightly modify the Makefile so we don’t get compile errors when trying to compile on a 64 bit environment. So go ahead and open the make file:
1$ gksudo gedit Makefile
and change the CFLAGS line to:
1CFLAGS=-O3 -DUSE_MMAP -fPIC -m64
Now let’s build and install:
1$ make
2$ sudo make install
MySQL
DEPENDS ON: zLib
01$ cd /usr/local/src/
03$ chown -R $USER:$USER mysql-5.0.51b
04$ sudo groupadd mysql
05$ sudo useradd -g mysql mysql
06$ ./configure \
07    --prefix=/usr/local/mysql \
08    --with-mysqld-user=mysql \
09    --without-debug \
10    --with-client-ldflags=-all-static \
11    --with-mysqld-ldflags=-all-static \
12    --disable-shared \
13    --localstatedir=/usr/local/mysql/data \
14    --with-extra-charsets=none \
15    --enable-assembler \
16    --with-zlib-dir=/usr/local/zlib \
17    --with-unix-socket-path=/tmp/mysql.socket \
18    CXXFLAGS=-fPIC CFLAGS=-fPIC CPPFLAGS=-fPIC
19$ make
20$ sudo make install
21$ sudo rm -f /usr/local/mysql/lib/mysql/libz.a
22$ sudo cp /usr/local/zlib/lib/libz.a /usr/local/mysql/lib/mysql/libz.a
The following is only needed the first time you build MySQL, so avoid doing it when you are actually re-building:
01$ sudo cp support-files/my-medium.cnf /etc/my.cnf
02$ sudo /usr/local/mysql/bin/mysql_install_db --user=mysql
03$ sudo chown -R root /usr/local/mysql/
04$ sudo chown -R mysql /usr/local/mysql/data
05$ sudo chgrp -R mysql /usr/local/mysql/
06$ sudo cp support-files/mysql.server /etc/init.d/mysql
07$ sudo chmod +x /etc/init.d/mysql
08$ sudo update-rc.d mysql defaults
09$ sudo /etc/init.d/mysql stop
10$ sudo /etc/init.d/mysql start
11$ /usr/local/mysql/bin/mysqladmin -u root password password
Now let’s default the MySQL server to use utf8 encoding. Open the MySQL configuration file:
1$ gksudo gedit /etc/my.cnf
And right after the [mysqld] line place the following:
1character_set_server=utf8
2skip-character-set-client-handshake
Now look for the line [mysql] and right after it add:
1default-character-set=utf8
Finish up by restarting the MySQL server:
1$ sudo /etc/init.d/mysql stop
2$ sudo /etc/init.d/mysql start
Apache 2
DEPENDS ON: zLib
01$ cd /usr/local/src/
03$ sudo tar xzvf httpd-2.2.8.tar.gz
04$ sudo chown -R $USER:$USER httpd-2.2.8
05$ cd /usr/local/src/httpd-2.2.8/srclib/apr
06$ ./configure --prefix=/usr/local
07$ make
08$ sudo make install
09$ cd /usr/local/src/httpd-2.2.8/srclib/apr-util
10$ ./configure --prefix=/usr/local --with-apr=/usr/local
11$ make
12$ sudo make install
13$ cd /usr/local/src/httpd-2.2.8
14$ ./configure \
15    --prefix=/usr/local/apache2 \
16    --with-apr=/usr/local \
17    --with-apr-util=/usr/local \
18    --enable-dav \
19    --enable-dav-lock \
20    --enable-rewrite \
21    --enable-ssl \
22    --enable-mods-shared=all \
23    --enable-deflate
24$ make
25$ sudo make install
The following instructions (everything until the next software package install) is only needed the first time you build Apache, so avoid doing it when you are actually re-building.
1$ sudo cp /usr/local/apache2/bin/apachectl /etc/init.d/apachectl
2$ sudo chmod +x /etc/init.d/apachectl
3$ sudo /usr/sbin/update-rc.d apachectl defaults
4$ sudo adduser --system apache
5$ gksudo gedit /usr/local/apache2/conf/httpd.conf
Once the editor has opened the file /usr/local/apache2/conf/httpd.conf, change the following lines:
1User daemon
2Group daemon
to:
1User apache
2Group nogroup
Now let’s enable SSL support. Once again this is only during your first build of Apache and it is not needed during re-builds. Let’s edit the main Apache configuration file:
1$ gksudo gedit /usr/local/apache2/conf/httpd.conf
Uncomment the following line (if it’s commented out):
1Include conf/extra/httpd-ssl.conf
And make sure the following line is not commented out (if it is commented out, uncomment it to enable; if the line doesn’t exist, add it):
1LoadModule ssl_module modules/mod_ssl.so
Let’s create the certificate for our server. When you are asked for information do not forget to specify the CommonName.
1$ cd /usr/local/apache2/conf
2$ sudo openssl genrsa -des3 -out server.key 1024
3$ sudo openssl req -new -key server.key -out server.csr
4$ sudo openssl x509 -req -days 730 -in server.csr -signkey server.key -out server.crt
5$ sudo cp server.key server.key.cryp
6$ sudo openssl rsa -in server.key.cryp -out server.key
7$ sudo chmod 400 server.key
Now edit the SSL configuration file in Apache:
1$ gksudo gedit /usr/local/apache2/conf/extra/httpd-ssl.conf
And make sure the ServerName line reflects your server name. You can now reload apache:
1$ sudo /etc/init.d/apachectl stop
2$ sudo /etc/init.d/apachectl start
PHP5
DEPENDS ON: Apache 2
01$ cd /usr/local/src
02$ sudo wget http://ar2.php.net/get/php-5.2.6.tar.gz/from/ar.php.net/mirror
03$ sudo tar xzvf php-5.2.6.tar.gz
04$ sudo chown -R $USER:$USER php-5.2.6/
05$ cd php-5.2.6
06$ ./configure \
07    --prefix=/usr/local/php \
08    --with-config-file-path=/usr/local/php \
09    --with-apxs2=/usr/local/apache2/bin/apxs \
10    --with-libxml-dir=/usr/include/libxml2 \
11    --with-mysql=/usr/local/mysql \
12    --with-zlib=/usr/local/zlib \
13    --enable-force-cgi-redirect \
14    --disable-cgi \
15    --with-curl \
16    --with-gd \
17    --with-gettext \
18    --enable-mbstring \
19    --enable-soap
20$ make
21$ sudo make install
22$ sudo /sbin/ldconfig
23$ sudo cp /usr/local/src/php-5.2.6/php.ini-recommended /usr/local/php/php.ini
24$ sudo /etc/init.d/apachectl stop
25$ sudo /etc/init.d/apachectl start
xDebug
DEPENDS ON: PHP5
01$ cd /usr/local/src
03$ sudo tar xzvf xdebug-2.0.3.tgz
04$ sudo chown -R $USER:$USER xdebug-2.0.3/
05$ cd xdebug-2.0.3
06$ /usr/local/php/bin/phpize
07$ ./configure --enable-xdebug \
08    --with-php-config=/usr/local/php/bin/php-config
09$ make
10$ cp modules/xdebug.so /usr/local/php/lib/php/
The following is only needed the first time you build xDebug, so avoid doing it when you are actually re-building. Let’s now enable xDebug on our php.ini file. Open the file for editing:
1$ gksudo gedit /usr/local/php/php.ini
And add the following lines just before the [Date] line
01zend_extension=/usr/local/php/lib/php/xdebug.so
02
03[debug]
04; Remote settings
05xdebug.remote_autostart=off
06xdebug.remote_enable=on
07xdebug.remote_handler=dbgp
08xdebug.remote_mode=req
09xdebug.remote_host=localhost
10xdebug.remote_port=9000
11
12; General
13xdebug.auto_trace=off
14xdebug.collect_includes=on
15xdebug.collect_params=off
16xdebug.collect_return=off
17xdebug.default_enable=on
18xdebug.extended_info=1
19xdebug.manual_url=http://www.php.net
20xdebug.show_local_vars=0
21xdebug.show_mem_delta=0
22xdebug.max_nesting_level=250
23;xdebug.idekey=
24
25; Trace options
26xdebug.trace_format=0
27xdebug.trace_output_dir=/tmp
28xdebug.trace_options=0
29xdebug.trace_output_name=crc32
30
31; Profiling
32xdebug.profiler_append=0
33xdebug.profiler_enable=0
34xdebug.profiler_enable_trigger=0
35xdebug.profiler_output_dir=/tmp
36xdebug.profiler_output_name=crc32
Python (with mod_python, SWIG bindings, and mysql_python)
DEPENDS ON: Apache 2, MySQL
01$ cd /usr/local/src
02$ sudo wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tgz
03$ sudo tar xzvf Python-2.5.2.tgz
04$ sudo chown -R $USER:$USER Python-2.5.2/
05$ cd Python-2.5.2.tgz
06$ ./configure --prefix=/usr/local/python \
07    CFLAGS=-fPIC CPPFLAGS=-fPIC
08$ make
09$ sudo make install
10$ cd /usr/local/src
12$ sudo tar xzvf mod_python-3.3.1.tgz
13$ sudo chown -R $USER:$USER mod_python-3.3.1/
14$ cd mod_python-3.3.1
15$ ./configure \
16    --with-apxs=/usr/local/apache2/bin/apxs \
17    --with-python=/usr/local/python/bin/python \
18    CFLAGS=-fPIC CPPFLAGS=-fPIC
19$ make
20$ sudo make install
21$ cd /usr/local/src
23$ sudo tar xzvf swig-1.3.35.tar.gz
24$ sudo chown -R $USER:$USER swig-1.3.35/
25$ cd swig-1.3.35
26$ ./configure \
27    --prefix=/usr/local/swig \
28    --with-python=/usr/local/python/bin/python \
29    CFLAGS=-fPIC CPPFLAGS=-fPIC
30$ make
31$ sudo make install
32$ cd /usr/local/src
33$ sudo wget http://downloads.sourceforge.net/mysql-python/MySQL-python-1.2.2.tar.gz?modtime=1172959928&big_mirror=0
34$ sudo tar xzvf MySQL-python-1.2.2.tar.gz
35$ sudo chown -R $USER:$USER MySQL-python-1.2.2/
36$ cd MySQL-python-1.2.2
Since we also have built in MySQL support for PHP5 on our Apache, I needed to build MySQL-python with embedded libraries, or otherwise mod_python would complain about it. This probably means that whenever you re-build MySQL you’ll also need to rebuild its python support. So let’s open the MySQL-python configuration file:
1$ gksudo gedit /usr/local/src/MySQL-python-1.2.2/site.cfg
And change lines:
1embedded = False
2mysql_config = /usr/local/bin/mysql_config
to:
1embedded = True
2mysql_config = /usr/local/mysql/bin/mysql_config
Let’s now build and install:
1$ sudo /usr/local/python/bin/python setup.py install
Since you probably have already installed (whithout even knowing) python on your Ubuntu box (through a software package, for example, AWN Manager), we need to tell Apache to use our custom Python build. So let’s open the Apache launcher for editing:
1$ gksudo gedit /etc/init.d/apachectl
And let’s add the following lines right after the ULIMIT_MAX_FILES line:
1#
2# Set Python PATH
3#
4export PATH="/usr/local/python/bin:$PATH"
Let’s finish by restarting Apache:
1$ sudo /etc/init.d/apachectl stop
2$ sudo /etc/init.d/apachectl start
Neon
This is a pre-requisite for SVN, so let’s build it from source. Eventhough there’s a newer version (0.28.2 at the time of this writing), I found that there are known issues with SVN and Neon that only let us install the 0.25.5 version. You are welcome to try a newer version, but if it doesn’t work go back to 0.25.5.
02$ sudo tar xzvf neon-0.25.5.tar.gz
03$ sudo chown -R $USER:$USER neon-0.25.5/
04$ cd neon-0.25.5/
05$ ./configure \
06    --prefix=/usr/local/neon \
07    --with-ssl \
08    CXXFLAGS=-fPIC CFLAGS=-fPIC CPPFLAGS=-fPIC
09$ make
10$ sudo make install
Subversion (with Python bindings)
DEPENDS ON: Neon, Swig, Apache 2, Python
01$ cd /usr/local/src
03$ sudo tar xzvf subversion-1.4.6.tar.gz
04$ sudo chown -R $USER:$USER subversion-1.4.6/
05$ cd subversion-1.4.6/
06$ sh autogen.sh
07$ ./configure \
08    --prefix=/usr/local \
09    --with-apxs=/usr/local/apache2/bin/apxs \
10    --with-httpd \
11    --with-apr=/usr/local \
12    --with-apr-util=/usr/local \
13    --with-neon=/usr/local/neon \
14    --with-swig=/usr/local/swig/bin/swig \
15    PYTHON=/usr/local/python/bin/python
16$ make
17$ sudo make install
18$ make swig-py
19$ sudo make install-swig-py
20$ sudo cp -R /usr/local/lib/svn-python/* /usr/local/python/lib/python2.5/site-packages
21$ sudo ldconfig
22$ sudo mkdir /var/lib/svn
23$ sudo mkdir /var/lib/svn/repository
24$ sudo chown -R apache:root /var/lib/svn/repository
25$ sudo chmod -R 770 /var/lib/svn/repository
26$ sudo chmod -R g+ws /var/lib/svn
Let’s create our first SVN user (replace $USER with your desired SVN user name):
1$ sudo /usr/local/apache2/bin/htpasswd -c /var/lib/svn/dav_svn.passwd $USER
Now let’s enable SVN over http with SSL. Open the main Apache configuration file:
1$ gksudo gedit /usr/local/apache2/conf/httpd.conf
Look for the line “#Include conf/extra/httpd-dav.conf” and right after it add the following:
1Include conf/extra/dav_svn.conf
Now let’s create that file:
1$ gksudo gedit /usr/local/apache2/conf/extra/dav_svn.conf
And place the following contents on it:
1
2    DAV svn
3    SSLRequireSSL
4    SVNParentPath /var/lib/svn/repository
5    AuthType Basic
6    AuthName "SVN repository"
7    AuthUserFile /var/lib/svn/dav_svn.passwd
8    Require valid-user
9
Restart Apache:
1$ sudo /etc/init.d/apachectl stop
2$ sudo /etc/init.d/apachectl start
As an example, let’s create an SVN project called test:
1$ sudo svnadmin create /var/lib/svn/repository/test
2$ sudo chown -R apache:root /var/lib/svn/repository/test
You will now have SVN access to that project using the URL https://SERVER/svn/test
Trac (with SVN integration)
DEPENDS ON: Subversion, MySQL, Python
1$ cd /usr/local/src
2$ sudo wget http://ftp.edgewall.com/pub/trac/Trac-0.11rc1.tar.gz
3$ sudo tar xzvf Trac-0.11rc1.tar.gz
4$ sudo chown -R $USER:$USER Trac-0.11rc1/
5$ cd Trac-0.11rc1
6$ sudo /usr/local/python/bin/python ./setup.py install --prefix=/usr/local/python
7$ sudo mkdir /var/lib/trac
Now let’s edit the main Apache configuration file:
1$ gksudo gedit /usr/local/apache2/conf/httpd.conf
And add the following at the end:
1
2  SetHandler mod_python
3  PythonInterpreter main_interpreter
4  PythonHandler trac.web.modpython_frontend
5  PythonOption TracEnvParentDir /var/lib/trac
6  PythonOption TracUriRoot /trac
7  SetEnv PYTHON_EGG_CACHE /tmp
8
Restart Apache:
1$ sudo /etc/init.d/apachectl stop
2$ sudo /etc/init.d/apachectl start
As an example, let’s add Trac support to our SVN project called test. We then first create the MySQL database, and then run the tracd program to set up the environment. When you are asked for the database URL specify: “mysql://root:password@localhost/trac_test”:
1$ mysqladmin -uroot -ppassword create trac_test
2$ sudo /usr/local/python/bin/trac-admin /var/lib/trac/test initenv
You can now access your trac installation for the test project using the URL: http://server/trac/test

REFERENCES
http://marianoiglesias.com.ar/ubuntu/apache2-php5-mysql-xdebug-ssl-svn-python-trac-on-ubuntu-hardy-8-04-64-bits/