Installing Apache 2.2 on FreeBSD 6.2

I. Install and Configure Apache 2.2

Login as root using the su command and lets get started.

 root@freebsd:/usr/ports/www/apache22# cd /usr/ports/www/apache22
root@freebsd:/usr/ports/www/apache22# make install
root@freebsd:/usr/ports/www/apache22#

Lets make the apache start with the system.

 root@freebsd:/usr/ports/www/apache22# vi /etc/rc.conf
	apache22_enable=”YES”

Now we need to edit 2 lines in the httpd.conf file. Take a note of these 2 lines and what you set them to. Later on when we create virtual hosts, the first entry must use the same settings as these according to the documentation.

 root@freebsd:/usr/ports/www/apache22# vi /usr/local/etc/apache22/httpd.conf
	ServerName www.aminonline.com:80 
	ServerAdmin amin@aminonline.com

We need to load this before the http daemon will run. We will also add it to load at boot time so that next time we reboot the system apache will be able to load.

:/usr/ports/www/apache22">root@freebsd:/usr/ports/www/apache22# kldload accf_http
root@freebsd:/usr/ports/www/apache22# vi /boot/loader.conf
	accf_http_load=”YES”

We have to rehash the tcsh shell to get the commands installed by the port available to the shell. Then we test the apache configuration and start the apache server.

 root@freebsd:/usr/ports/www/apache22# rehash
 root@freebsd:/usr/ports/www/apache22# apachectl configtest
	Syntax OK
root@freebsd:/usr/ports/www/apache22# apachectl start

Test the server from your browser like so for example http://10.25.2.5/
If all is well you should see a “It Works!” message. Now we’re going to move the apache22 web directory to a standard directory.


root@freebsd:/usr/ports/www/apache22# cd /usr/local/www/apache22
root@freebsd:/usr/local/www/apache22# mv * ../
root@freebsd:/usr/local/www/apache22# cd ..
root@freebsd:/usr/local/www# rmdir apache22/

Now we have to replace all occurences of
 “/usr/local/www/apache22″ with “/usr/local/www” in the “/usr/local/etc/apache22/httpd.conf” file.

I will use a confirmation when replacing, just in case. I got to replace it in 5 locations within the file.

root@freebsd:/usr/local/www# vi /usr/local/etc/apache22/httpd.conf
	:1,$s//usr/local/www/apache22//usr/local/www/gc
	Confirm change? [n]y
	Confirm change? [n]y
	Confirm change? [n]y
	Confirm change? [n]y
	Confirm change? [n]y

Lets test out configuration, and restart apache server.

 root@freebsd:/usr/local/etc/apache22# apachectl configtest
     Syntax OK
root@freebsd:/usr/local/etc/apache22# apachectl restart

At this point I get Access Denied when I try to browse the site.
Which will get fixed when I add virtual domains and set access restrictions for them.

I. Setting up Virtual Hosts

Now if that is working we’re going to add some virtual domains to it and alter the access setting so that everyone can view them. Uncomment the following Include statement. If you do not have it, then you might be using the older version of apache server, make sure you’re using 2.2.

 root@freebsd:/usr/local/www# cd /usr/local/etc/apache22
 root@freebsd:/usr/local/etc/apache22# vi httpd.conf
	# Virtual hosts
	Include etc/apache22/extra/httpd-vhosts.conf

The first VirtualHost section must contain the same ServerName as defined in the global httpd.conf earlier. This Virtualhost will catch any hosts that do not match the other virtual hosts. The directory section is necessary, since Apache2.2 restricts access to the virtual hosts so we can override in here for each virtual host which is actually nicer. Pay attention to the bold fields, which is what we set in the httpd.conf file earlier, make these the same as that.

 root@freebsd:/usr/local/etc/apache22# vi extra/httpd-vhosts.conf
     NameVirtualHost *:80       

     # First Virtual Host; Any unknown ServerName or ServerAlias will also get sent here
     <VirtualHost *:80>
     ServerAdmin amin@aminonline.com
     DocumentRoot /usr/local/www/www.noobtechdev.com
     ServerName www.aminonline.com
     CustomLog /usr/local/www/logs/www.aminonline.com.access.log combined
     ErrorLog /usr/local/www/logs/www.aminonline.com.error.log
     <Directory /usr/local/www/www.aminonline.com>
          Order Deny,Allow
          Allow from all
          DirectoryIndex index.php index.html index.htm
     </Directory>
     </VirtualHost>       

     # Second Virtual Host, will only process the specified ServerName
     <VirtualHost *:80>
     ServerAdmin amin@aminonline.com 
     DocumentRoot /usr/local/www/blog.aminonline.com
     ServerName blog.aminonline.com
     CustomLog /usr/local/www/logs/blog.aminonline.com.access.log combined
     ErrorLog /usr/local/www/logs/blog.aminonline.com.error.log
     <Directory /usr/local/www/blog.aminonline.com>
          Order Deny,Allow
          Allow from all
          DirectoryIndex index.php index.html index.htm
     </Directory>
     </VirtualHost>

Now we create directories for the Virtual Hosts we created as well as for their logs. Also I’m going to add a simple Hello World page to each Virtual Host for testing purposes.

 root@freebsd:/usr/local/etc/apache22# mkdir /usr/local/www/www.aminonline.com
root@freebsd:/usr/local/etc/apache22#
echo Hello World from www.aminonline.com > /usr/local/www/www.aminonline.com/index.html
root@freebsd:/usr/local/etc/apache22# mkdir /usr/local/www/blog.aminonline.com
root@freebsd:/usr/local/etc/apache22#
echo Hello World from blog.aminonline.com > /usr/local/www/blog.aminonline.com/index.html
 root@freebsd:/usr/local/etc/apache22# mkdir /usr/local/www/logs
root@freebsd:/usr/local/etc/apache22# apachectl configtest
	Syntax OK root@freebsd:/usr/local/etc/apache22# apachectl restart

Before the virtual hosts will work, you need to have the defined in your nameserver. My DNS setup for bind looks like this. I will bold the fields to pay attention to. If you want you can make your freebsd a local ip for testing locally as to bypass your firewall till you have everything working property. When changing DNS settings, remember to change the serial number entry (Data+revision) so that the nameservers will know it’s been updated. Also set the machine you are using the web browser on to test the apache to point to your name server directly. Especially when you’re testing it locally, so that every time you make a change it will be instant after you reload the DNS server.


     $TTL 6h
     @    IN SOA    ns1.aminonline.com. hostmaster.aminonline.com. (
			     2007041505
			     10800
			     3600
			     604800
			     86400 )       

     @           IN NS     ns1.aminonline.com.
     @           IN NS     ns2.aminonline.com.       

     ns1         IN A      72.140.6.193
     ns2         IN A      72.140.6.193       

     localhost   IN A      127.0.0.1       

     @           IN MX 10  mx-routes01.editdns.net.       

     mail        IN A      72.140.6.193       

     @           IN A      72.140.6.193
     celeron     IN A      72.140.6.193
     freebsd     IN A      72.140.6.193       

     ftp         IN CNAME  celeron
     smtp        IN CNAME  celeron
     pop3        IN CNAME  celeron       

     ; HTTP Apache server on freebsd, public IP virtual hosts
     www         IN CNAME  freebsd
     blog        IN CNAME  freebsd
     phpmyadmin  IN CNAME  freebsd
     awstats     IN CNAME  freebsd       

After you make changed to your nameserver, update the serial number and reload the nameserver. On my windows machine I reload bind like so: C:\WINDOWS\system32\dns\bin\rndc reload I installed BIND9 because the windows name server was starting to drive me a little bit nutty, since it’s been adding things in magically to point to local IPs instead of the public ones.
Now test your virtual hosts by pointing them to http://www.noobtechdev.com/ and then to http://blog.noobtechdev.com/ and see whether the proper pages display. Also try pointing your browser to your IP like so http://10.25.2.1/ and see if that takes you to the same page as http://www.aminonline.com which is sposed to catch all unknown hosts requests.

You should now have a working copy of the Apache with the ability to add virtual hosts to it. Restart your server and make sure that everything loaded ok on it’s own.

I. Problems I’ve Encountered

My DNS entries were not resolving to the proper hosts.

My computer was using cached DNS entried from my IPCop’s DNS server, so the subdomains I added to my domain were either pointing to their old IP or were not resolving at all. So I pointed my test machine directly to my DNS server and that fixed it. From then on whenever I made a change to DNS and reloaded it, it took affect immediately. Also a few time I forgot to update the serial number in the DNS zone file for my domain, which resulted in the data not getting reloaded by the DNS server.

1 Comment

  1. pedro gaca said,

    June 19, 2009 at 6:47 pm

    i liked this documentation, but a can´t star my apache22

    is there some one to halp me?

    Luanda-Angola


Post a Comment