Jan 17, 2007 Meeting Report: Part 1
For the meeting on the 17th Kevin Carter talked about the installation and configuration of Apache and PHP from source. What follows is a detailed howto from that meeting. First off though is some links that were presented in the meeting.
VirtualBox goesopen source http://virtualbox.org/
Linux Kernel in a Nutshell book available for free download http://www.kroah.com/lkn/
Description of Linux filesystem hierarchy http://www.pathname.com/fhs/
Now onto the howto….
LAMP stands for Linux, Apache, MySQL, and PHP/Python/Perl. A couple of easy ways to get a LAMP server running is the SLAMPP LiveCD or Ubuntu Server. Those are good if you are ok running the default configurations. I am a web developer and I have a need to have things running with special configurations. So instead of using apt-get or yum or whatever package manager to install 20 packages to get the functionality I desire, I would rather just install them from source. So in this article I will talk about configuring and installing Apache and PHP. In the future I may go over MySQL as well.
To get started I am using Ubuntu 6.06 as the base. But this will work with any linux if you have the prerequisites. For starters Ubuntu does not come with the tool necessary to compile software. The easiest way to get these is to do
sudo apt-get install gcc g++ make flex
PHP will also require you to have libxml2 installed you can do this manually or with apt. The package you need with apt is libxml2-dev. That gets you the necessary header files that PHP uses to compile XML support. Also if you want MySQL integrated with this you will need to have it pre-installed. The Ubuntu packages are mysql-server and libmysqlclient15-dev.
OK now we have some of the dependencies out of the way. If your unsure how to compile for source please read this.
Lets start with apache. You can obtain the Apache server from here. This guide will go over version 2.0 but 2.2 should work just as well. The standard ./configure, make, make install will get you the default instillation of apache. This means that it will put everything under /usr/local/apache2 The folder it contains are as follows.
bin –Where the binaries are
build –Tools for build modules
cgi-bin –Location of PERL CGI scripts
conf –The conf files
error –Pages that are displayed when an error happens. eg 404
htdocs –Where all your website files go
icons –Icons used by apache when it create directory listing pages
include –Apache's header files
lib –Library files
logs –Where it keeps its logs
man –The manual pages
manual –The apache 2.0 manual
modules –where any modules are kept
Now I don’t like this layout. I think that your site files should be kept in either /var or /srv and the conf files should be in /etc and the logs should be in /var/log
One way to get a better layout is by some configure flags.
./configure --prefix=/usr/local/apache --datadir=/srv/www –sysconfdir=/etc/httpd --mandir=/usr/local/man
This is good but you would still have to move the location of the log files in the httpd.conf file. Apache makes use of a file called config.layout This file contains several layout variations to choose from or you could create your own. If you wanted you could also choose a layout that is close to what you want and then use some other flags to make a few changes. I figure if I’m going to compile Apache every time a new version comes out I might as well just write a layout out for myself. My layout looks as follows.
<Layout Kevin>
prefix: /usr/local/apache
exec_prefix: ${prefix}
bindir: ${exec_prefix}/bin
sbindir: ${exec_prefix}/bin
libdir: ${exec_prefix}/lib
libexecdir: ${exec_prefix}/modules
mandir: /usr/local/man
sysconfdir: /etc/httpd
datadir: /srv/www
installbuilddir: ${datadir}/build
errordir: ${datadir}/error
iconsdir: ${datadir}/icons
htdocsdir: ${datadir}/htdocs
manualdir: ${datadir}/manual
cgidir: ${datadir}/cgi-bin
includedir: ${prefix}/include
localstatedir: /var/log
runtimedir: /var/run
logfiledir: ${localstatedir}/apache
proxycachedir: ${localstatedir}/proxy
</Layout>
The choice is really up to you on where you put everything. Thats the great part about compiling from source. To use a layout you would use the flag –enable-layout=[NAME]
One last note before I finish up talking about configuration, Apache has a few different MPMs (Multi-Processing Modules). These control the way in which Apache handles its connections. You can read about the different ones here. But PHP says that it runs its best with the prefork MPM. So to enable this we use –with-mpm=prefork
At this point you should have decided what configure flags you would like to use. There are other that I haven’t talked about that you may want, read the provided documentation to see if there is anything else you want. And then we can do the make and make install to install Apache.
Configuration
Now I will talk about some settings in the httpd.conf and setting up things like log rotation and init scripts.
In the httpd.conf file is where you set your various configuration on how apache should run. The default should be fine but if you want to make some changes you can. Some config options of note are:
Listen 80 --This sets the port on which Apache operates. Another popular choice is 8080
User/Group --You can set the user and group Apache runs as with the User and Group options, by default its User nobody and Group -1
ServerAdmin --This is the email address that will be shown when errors are generated.
DocumentRoot sets the path to the website files. “/srv/www/html” in my setup, “/usr/local/apache2/htdocs” in the defaul setup.
ServerTokens and ServerSignature specify how much in will be given out about Apache. Such things as what version Apache is and what modules you are running.
The other configuration options are well documented in the httpd.conf file so give it a read if your curious about other things.
So now you have Apache installed how do you start it? As root run “apachectl start” depending on where you put the apache bin files this may or may not be in your path. apachectl takes the following commands: start,stop,restart,graceful,startssl,and configtest. This is nice but we want to have Apache start itself when the system starts. For this we use an init script. Depending on what Linux systemyou use, your init scripts may be different. But Ubuntu 6.06 uses the SysV init style. The apachectl program is made to act like a init script, so you can like directly to it for your init. So in /etc/init.d create a link to the apachectl program.
cd /etc/init.d
ln -s /usr/local/apache/bin/apachectl httpd
Then put the link to start it on system bot in the proper runlevel folder. By default Ubuntu boots to runlevel 2.
cd /etc/rc2.d
ln -s ../init.d/httpd S80httpd
Change the number depending on when in the sequence you want it to start. Also add a link in the rc0.d and rc6.d folders with a K instead of an S to stop Apache on system shutdowns and reboots.
Finally for Apache we’ll talk about log rotation. There are two ways to do log rotation with Apache. The first is the rotatelog script that comes with Apache. To use it you put something like the following in your httpd.conf file.
TransferLog “| /path/to/rotatelogs logfile rotation_time_in_seconds”
The other option is to use logrotate. Logrotate is a fairly common system utility designed to handle the log rotation for many program. It is run once a day by cron. To use it all you have to do is drop a config file for the logs you want to rotate into /etc/logrotate.d An example config is as follows
/var/log/apache/access_log {
missingok
compress
rotate 5
#mail webmaster@yourdomain.com
size 10M
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
The script says to take the log file found at /var/log/apache/access_log.
missingok says if its not there don’t issue an error.
compress compresses the old files using gzip
rotate 5 says to rotate the logs 5 time before deleting. So you end up with access_log,access_log.1,access_log.2,etc
mail email@address makes it so logrotate will email you the log files instead of deleting them
size 10M will cause the log to be rotated only when it has become larger than 10 megs
And that part after postrotate causes Apache to restart so it will start writing to a new log file.
Read the man page for logrotate to see some more of its options.
Thats it for Apache. Stay tuned for part 2 where I’ll go over the installation of PHP.
Installing software from source
Installing software from source is a fairly simply process once you learn the proper commands. Most software is compiled with a simple
./configure
make
make install
Only the last one needs to be run as root (provided your installing it in the normal system location). A lot of sofware can take flags on the
./configure
command to change the way the software is configured. A standard one is
--prefix=[PATH]
to change the location the software is installed into. To find out what flags are available for a particular piece of software you can run
./configure --help
Also always read the INSATLL and README files that come with the software and any documentation that is available.