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.
- Update our repositories
- (***Optional***) Upgrade installed packages
- Install our tools to enable us to compile the source code.
- Make yourself a folder just for source files in your home dir and move inside of it.
- 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).
- Now get the source files for zlib from zlib.net
- Uncompress the tar.gz files we just downloaded
- We should now have two tar.gz files and two folders like shown below.
- Navigate into the zlib folder
- 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).
- 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.
- Make sure there weren’t any errors, and navigate into the apache source folder now.
- 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…
- 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.
- 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.
- Single Core Processor (Athlon XP, Celeron etc)
- 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.
- Great, we’re done. Error’s? Google is your best friend! If not lets start up apache and test it out.
- 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.
- Copy the provided startup script to /etc/init.d/
- Modify it so that it is executable with chmod
- Open the startup script and lets make it startup nicely.
- Modify the very top of the script from…
- Save and exit, and lets add our apache startup script to the start-up manager.
- Let’s not give ourselves a bit of extra security by running apache as a system user instead of a daemon.
- Open the apache config file and change the owner
- Find the section that looks like below. It should be on or around line 116.
- Save and Exit, And lets test the newly modifed startup script.
- 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
- Looks good! Reboot it once and make sure it comes up all by itself!
$ sudo aptitude update
$ sudo aptitude safe-upgrade
$ sudo aptitude install build-essential
$ mkdir ~/src && cd src/
$ wget http://www.takeyellow.com/apachemirror/httpd/httpd-2.2.15.tar.gz
$ wget http://zlib.net/zlib-1.2.5.tar.gz
$ tar zxvf httpd-*.tar.gz && tar zxvf zlib-*.tar.gz
$ 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
$ cd zlib-1.2.4/
$ ./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.
$ make && sudo make install
$ cd ../httpd-2.2.15/
$ ./configure --help
$ ./configure --prefix=/usr/local/apache2 --enable-so
$ sudo make -j3 install
$ sudo /usr/local/apache2/bin/apachectl start
$ sudo /usr/local/apache2/bin/apachectl stop
$ sudo cp /usr/local/apache2/bin/apachectl /etc/init.d/apachectl
$ sudo chmod +x /etc/init.d/apachectl
$ sudo nano /etc/init.d/apachectl
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 |
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 |
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' ... |
$ sudo nano /usr/local/apache2/conf/httpd.conf
116 117 | User daemon Group daemon |
Change it to…
116 117 | User apache Group nogroup |
$ sudo /etc/init.d/apachectl start
$ 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
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!






















2 Comments
Could you explain about RHEL 5.4. Becasue I’m using Redhat 5.4 and from step #22 I’m having problem.
I have ubuntu 10.04. I installed apache 2.2.15 per your instructions (no errors) and lost functionality on my desktop. I got startup errors when the panel launches. I un-installed the whole OS and started fresh. After doing this the second time, I think there is something oddly wrong here as it happened once again. I think your instructions are great (not counting step #9 “$ cd zlib-1.2.4/” should be “$ cd zlib-1.2.5/”). I think something in apache maybe? Anyway… I am saving this how-to for future use. I am gonna report this to ubuntu and apache and see why the install causes issues with ubuntu 10.04.