Installing Apache and php from source is a time consuming process in comparison to installing it via the repositories. Usually the only reason you want to do it this way is because you want full control. Not to say you don’t have control over installing with aptitude, but this way you have a fine grain control of where it’s installed, what version, and what options. Sometimes also granting you a slight performance increase depending on your architecture and hardware configuration.

If you’re just looking to install Apache & PHP for general use, you are better off using the packaged binaries via aptitude. But if you are setting up for a production environment accessible via the web. Installing from source is the best option in my opinion.

Not to mention the fact that we as human’s are lazy.  It’s too easy to take the speed and ease of package managers like aptitude, emerge, and yum.  They too can allow you to control many of these aspects when it comes to controlling the software being installed.  But yet, many don’t use them.  So if you’re up to learn something new today, give this a shot!  Never be afraid to try.

So, now that you’ve been warned. Lets get into it, it really doesn’t take all that long. But in comparison to the prepackaged binaries there’s a nominal difference.

  1. Update our repositories
  2. $ sudo aptitude update
  3. (***Optional***) Upgrade installed packages
  4. $ sudo aptitude safe-upgrade
  5. Install our tools to enable us to compile the source code.
  6. $ sudo aptitude install build-essential
  7. Make yourself a folder just for source files in your home dir and move inside of it.
  8. $ mkdir ~/src  && cd src/
  9. We’ll go ahead and download the stuff we need such as the apache & zlib source files. You should actually go to the Apache Download’s page to find the closest mirror. The apache source files can be found in the httpd folder on any of their mirrors (ie. http://www.takeyellow.com/apachemirror/httpd/httpd-2.2.15.tar.gz is where I’ll be downloading mine from).
  10. $ wget http://www.takeyellow.com/apachemirror/httpd/httpd-2.2.15.tar.gz
  11. Now get the source files for zlib from zlib.net
  12. $ wget http://zlib.net/zlib-1.2.5.tar.gz
  13. Uncompress the tar.gz files we just downloaded
  14. $ tar zxvf httpd-*.tar.gz && tar zxvf zlib-*.tar.gz
  15. We should now have two tar.gz files and two folders like shown below.
  16. $ ls -lah
       total 6.9M
       drwxr-xr-x  4 scott scott 4.0K 2010-04-19 18:03 .
       drwxr-xr-x  4 scott scott 4.0K 2010-04-19 17:56 ..
       drwxr-xr-x 11 scott scott 4.0K 2010-03-01 21:34 httpd-2.2.15
       -rw-r--r--  1 scott scott 6.3M 2010-03-05 13:13 httpd-2.2.15.tar.gz
       drwxr-xr-x 13 scott scott 4.0K 2010-03-14 18:09 zlib-1.2.5
       -rw-r--r--  1 scott scott 529K 2010-03-14 18:09 zlib-1.2.5.tar.gz
  17. Navigate into the zlib folder
  18. $ cd zlib-1.2.4/
  19. Lets run configure to make sure we have all the dependencies we need to make zlib with the prefix /usr/local (where zlib will be installed).
  20. $ ./configure --prefix=/usr/local
       Checking for gcc...
       Checking for shared library support...
       Building shared library libz.so.1.2.5 with gcc.
       Checking for off64_t... Yes.
       Checking for fseeko... Yes.
       Checking for unistd.h... Yes.
       Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf().
       Checking for vsnprintf() in stdio.h... Yes.
       Checking for return value of vsnprintf()... Yes.
       Checking for errno.h... Yes.
  21. If you got any errors, make a comment below or search it out on Google. If no errors, then lets make it. Pay attention here, “make” is run as a regular user. But “make install” is run through sudo. Half way through it will prompt for the password. Enter it and continue.
  22. $ make && sudo make install
  23. Make sure there weren’t any errors, and navigate into the apache source folder now.
  24. $ cd ../httpd-2.2.15/
  25. Now, this is where you have some decisions to make. We are about to configure apache and we must specify how we want apache to work, what its going to do etc. If you are actually setting up in a production environment, you probably already know. But if you would like to have a look at what options can be configured here enter the following…
  26. $ ./configure --help
  27. I’ll demonstrate with a fairly general configuration, but you should take the time to research what you need and what you do not need. This is where you can achieve performance benefits over the packaged binaries by streamlining apache with only the things it needs and nothing it doesn’t need.This part will actually take a minute or two depending on your hardware. Watch for errors.
  28. $ ./configure --prefix=/usr/local/apache2 --enable-so
  29. If you receive any errors you must resolve those and run the configure script again. Google is great for figuring out what you need if you get an error.If no errors came up it’s time to make!

    Manually setting your CFLAGS to match your cpu’s architecture can help with your make compile time’s and help configure Apache to perform better. It “can” also degrade server performance if done improperly. This is all really dependent on your hardware specifications and what you set them too.

    I won’t be going into detail about this, but it is definitely worth looking into. I’ve included some links talking more about this. But for now the only optimization I am going to recommend that will help specifically with the compile time is the “-j” flag when running make.

    • Single Core Processor (Athlon XP, Celeron etc)

      • Example – $ make -j2
    • Dual Core Processor (Athlon X2, Core2Duo etc)

      • Example – $ make -j3
    • Triple Core Processor (AMD Phenom X3)

      • Example – $ make -j4
    • Quad Core Processor (AMD Opteron, Intel Xeon etc)

      • Example – $ make -j5

    A general rule of thumb would be (CPU Cores) + 1 = (-jx) This will tell the make command to run in parallel doubling your compiling speed, cutting that time nearly in half!

    Sometimes this does cause compilers to crash. If yours does crash, either drop the -j option down and increment and try again. Still crashes? Omit the -j option all together.

    CFLAGS Optimizations

  30. This part is the longest of all the steps, you should see a ton of jibberish text flying across your screen. Possibly even hear your CPU fan kick up to full speed. It’s pretty much cranking away full til using every bit of CPU power you have to turn the human readable code for apache into machine language.Go grab a cookie, have a smoke. It should only be 5 or 10 minutes depending on your computer/server’s hardware. Once again, resolve any errors before proceeding. When it’s done we’ll run make install, this must be done as root or with sudo.
  31. $ sudo make -j3 install
  32. Great, we’re done. Error’s? Google is your best friend! If not lets start up apache and test it out.
  33. $ sudo /usr/local/apache2/bin/apachectl start
  34. Point your browser to http://localhost or http://<YOUR IP ADDRESS> and you should get a really basic page that says “It Worked!”. Alright its all good. Lets set it up so that it starts apache when the machine boots up. But first stop the server.
  35. $ sudo /usr/local/apache2/bin/apachectl stop
  36. Copy the provided startup script to /etc/init.d/
  37. $ sudo cp /usr/local/apache2/bin/apachectl /etc/init.d/apachectl
  38. Modify it so that it is executable with chmod
  39. $ sudo chmod +x /etc/init.d/apachectl
  40. Open the startup script and lets make it startup nicely.
  41. $ sudo nano /etc/init.d/apachectl
  42. Modify the very top of the script from…
  43. 1
    2
    3
    4
    
     #!/bin/sh
     #
     # Licensed to the Apache Software Foundation (ASF) under one or more
     # contributor license agreements.  See the NOTICE file distributed with

    to this…

    1
    2
    3
    4
    5
    6
    7
    
     #!/bin/sh
     #
     # chkconfig: - 85 15
     # description: Apache 2.2.15 Webserver
     #
     # Licensed to the Apache Software Foundation (ASF) under one or more
     # contributor license agreements.  See the NOTICE file distributed with
  44. Save and exit, and lets add our apache startup script to the start-up manager.
  45. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    $ sudo /usr/sbin/update-rc.d apachectl defaults
        update-rc.d: warning: /etc/init.d/apachectl missing LSB information
        update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
        Adding system startup for /etc/init.d/apachectl ...
        /etc/rc0.d/K20apachectl -> ../init.d/apachectl
        /etc/rc1.d/K20apachectl -> ../init.d/apachectl
        /etc/rc6.d/K20apachectl -> ../init.d/apachectl
        /etc/rc2.d/S20apachectl -> ../init.d/apachectl
        /etc/rc3.d/S20apachectl -> ../init.d/apachectl
        /etc/rc4.d/S20apachectl -> ../init.d/apachectl
        /etc/rc5.d/S20apachectl -> ../init.d/apachectl
  46. Let’s not give ourselves a bit of extra security by running apache as a system user instead of a daemon.
  47. 1
    2
    3
    4
    
    $ sudo adduser --system apache
        Adding system user `apache' (UID 103) ...
        Adding new user `apache' (UID 103) with group `nogroup' ...
        Creating home directory `/home/apache' ...
  48. Open the apache config file and change the owner
  49. $ sudo nano /usr/local/apache2/conf/httpd.conf
  50. Find the section that looks like below. It should be on or around line 116.
  51. 116
    117
    
    User daemon
    Group daemon

    Change it to…

    116
    117
    
    User apache
    Group nogroup
  52. Save and Exit, And lets test the newly modifed startup script.
  53. $ sudo /etc/init.d/apachectl start
  54. If no errors came up lets look to see the process is running or by checking in our webbrowser again for the “It Works!” m
  55. $ ps aux | grep http
        root      1632  0.6  0.5 139076  2660 ?        Ss   18:55   0:00 /usr/local/apache2/bin/httpd -k start
        apache    1633  0.0  0.3 139076  1992 ?        S    18:55   0:00 /usr/local/apache2/bin/httpd -k start
        apache    1634  0.0  0.3 139076  1992 ?        S    18:55   0:00 /usr/local/apache2/bin/httpd -k start
        apache    1635  0.2  0.3 139076  1992 ?        S    18:55   0:00 /usr/local/apache2/bin/httpd -k start
        apache    1636  0.0  0.3 139076  1992 ?        S    18:55   0:00 /usr/local/apache2/bin/httpd -k start
        apache    1639  0.0  0.3 139076  1992 ?        S    18:55   0:00 /usr/local/apache2/bin/httpd -k start
  56. Looks good! Reboot it once and make sure it comes up all by itself!

So now you got your webserver.  I would suggest going and reading up about configuring the server for extra security.  Research is key here, like I’ve said before.  Easy to install, difficult to master.  Start with going to http://www.apache.org and reading through their documentation.  Go read your favorite tech forums, look for more tutorials etc.  Any kind of server exposed to the internet is worth knowing inside and out.

Learn where your logs are.  Especially the error log, when running things like mod_security it’ll log those attempted attacks.  Attempt your own simulated attacks in a test environment, see if you can break in.  And then fix it, it’s a learning process.  Don’t be afraid to toy with it, because the script kiddies sure aren’t.

Come back soon, I’ll be doing much more with apache, mysql, php and more!  Good luck!

Tags: