Building a web server
From SoftwarePractice.org
This short (I hope) article documents the process of building and configuring a basic web server using Apache, PHP, and MySQL. While you can get prebuilt binaries for a lot of platforms, including the complete suite as XAMPP, you might also enjoy the challenge of building it from scratch yourself.
Note: see also
The following assume that you know how to use a shell, and have root access to the machine on which you are building.
Contents |
Preliminaries
Before starting, it's worth creating the users and corresponding groups that execute the Apache and MySQL servers. In FreeBSD, this is done as follows:
pw groupadd mysql pw useradd -n mysql -g mysql pw groupadd apache pw useradd -n apache -g apache
We also need to decide where to place the web directories and the MySQL data directories. Personally I prefer to have them out of the distribution directories, to make future upgrades easier. To see where the disk space is, use the df command. I get
Filesystem 1K-blocks Used Avail Capacity Mounted on /dev/da0s1a 507630 56146 410874 12% / devfs 1 1 0 100% /dev /dev/da0s1e 507630 12 467008 0% /tmp /dev/da0s1f 26646234 1509652 23004884 6% /usr /dev/da0s1d 3023502 2296 2779326 0% /var
Since the /usr partition has the space, I am going to use /usr/www for the web directories and /usr/var for the MySQL data directories. Let's set them up now:
cd /usr mkdir www var chown apache:apache www chown mysql:mysql var
Download distributions
Get the source distributions of Apache, PHP, and MySQL:
I download source distributions into /usr/local/src, and untar/gzip them there for compiling.
Tip: if you are building on a remote server, use lynx to get the downloads. Simply locate the URL of the download file, and in lynx, type the letter 'g' and then paste in the URL and press return. Press 'd' to download the file, and select the Save to Disk option afterwards.
Untar the files:
tar -zxf httpd-2.2.2.tar.gz
and so on.
Build MySQL
Since we downloaded source distributions, we now need to compile them. We need to do MySQL first, since the Apache/PHP compile depends on it. This is actually the most complicated build on this page, if you get stuck with it, you might prefer to download a binary distribution instead.
In general, we can compile source distributions with a sequence of commands like this:
./configure make make install
(You have to be root to do this.) The configure script typically takes a bunch of extra arguments that can be used to turn various options on and off. In the case of MySQL, a good number of options are recommended in the MySQL documentation. Here is what I used, note the --localstatedir option is used to specify where the MySQL dta directories go.
CFLAGS="-O3" CXX=gcc CXXFLAGS="-O3 -felide-constructors -fno-exceptions -fno-rtti"
./configure \
--prefix=/usr/local/mysql --localstatedir=/usr/var --enable-assembler \
--with-mysqld-ldflags=-all-static
make
make install
cp support-files/my-large.cnf /etc/my.cnf
You will see the install in /usr/local/mysql.
cd /usr/local/mysql bin/mysql_install_db --user=mysql bin/mysqld_safe --user=mysql & bin/mysqlshow mysql
This last command shows the tables in the database named 'mysql.' There are 17 of them. This just shows that MySQL is running and the initial set of tables installed.
Build Apache with PHP
This is a straightforward build that works with commonly-available PHP software. Once you have done this, you can decide on whether you need to build Apache with additional modules. I did this on FreeBSD6.0. but it's pretty much the same on any Unix variant. The best documentation for this process is the INSTALL document in the PHP download. What follows is a simplified version (with a few extra comments thrown in).
- First, make sure that you have Perl installed.If not, better go get it and install it! The easiest way to install it is to untar it, then:
cd perl-5.8.8 sh Configure -d make make test make install
- Build Apache. Note that we are using version 2 of Apache:
cd httpd-2.2.2 ./configure --enable-so --enable-rewrite make make install
This compiles Apache with shared-library support, and with the rewrite module enabled (used by many applications these days). By default, Apache creates the directory /usr/local/apache2 and installs itself under that. If this is not a good location for some reason, you can change it with the --prefix=path option to the configure script.
- Build PHP:
cd php-5.1.4 ./configure --with-apxs2=/usr/local/apache2/bin/apxs \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config make make installThis builds PHP with support for the Apache2 interface and for MySQL.
Update 20/08/06: Note that we are compiling in support for both the old and the new-style interfaces to MySQL. Personally, I don't much like this... but it seemed to work. More info here.
- If configure fails complaining about lack of xml support, you will want to install libxml. Go get it and then:
cd libxml-2.6.24 ./configure make make install
(You will see the libxml libs in /usr/local/lib.)
- If configure complains about iconv, go get it and install it as for libxml.
Then redo the PHP configure and install. If you still get errors in configure, you will want to disable additional options or to install the needed libraries. (Use Google to find out what it's talking about when it complains about something missing!)
- Create the PHP initialization file (not sure why the install doesn't do this...)
cp php.ini-recommended /usr/local/lib/php.ini
For reasons unknown to me, you might want to find the include_path directive in this file and change it to:
include_path = ".:/usr/local/lib/php:/usr/local/include/php"
Configure Apache
There are a few small configuration things we need to set up now to get things going. The Apache configuration file is located at /usr/local/apache2/conf/httpd.conf. There are also some includes files located in the extra directory, I prefer to keep everything in the one config file.
- Open http.conf in an editor. Inside the <IfModule mime_module> section, add the line
AddType application/x-httpd-php .php
-
Locate the lines starting with User and Group and change them to
User apache Group apache
This tells the startup process to run all child processes as the 'apache' user.
-
Locate the line containing the DirectoryIndex directive and change it to
DirectoryIndex index.php index.html
This tells Apache to load index.php if a directory is accessed, or index.html if there is not index.php.
Try it Out
- Start the web server:
/usr/local/apache2/bin/apachectl start
See if your server works by connecting to it with a browser. If you installed it on your local machine, use localhost as the URL, otherwise use the name or IP address of the server. You should get a page that says "It works!" This is loaded from the file index.html in the directory /usr/local/apache2/htdocs. This directory is called the "document root," by the way.
- To test PHP, create an info.php file in the document root, and insert in it the line:
<?php phpinfo(); ?>
Load it into your browser eg http://www.mysite.com/info.php. You should get a page with loads of information on your web server and PHP install!
- To check the MySQL connection, create a file called say test.php in the document root, with this code in it
<?php mysql_connect("localhost", "root"); $result = mysql_list_tables("mysql"); $num_rows = mysql_num_rows($result); for ($i = 0; $i < $num_rows; $i++) { echo "Table: ", mysql_tablename($result, $i), "<br/>\n"; } ?>Load it into the browser using eg http://www.mysite.com/test.php -- you should see the same list of tables as when you first tried out MySQL above.
And that's it for this installment! You have a basic webserver running, although the database has no security whatsoever and the server configuration still needs a good amount of work.
