Gentoo – Apache Web Server

In a previous guide I discussed how to set up MySQL on your Gentoo Linux box. One great use for MySQL is to combine it with an Apache web server and PHP to create content rich, dynamic websites. In this guide I will discuss how to set up an Apache web server with vhosts support.

Installing Apache is pretty straight forward, there are a few configuration files that need to be edited but nothing to difficult. First lets install apache:

Once apache finishes compiling and installs star the service by typing:

Then set it to the default runelvel:

We also need to add the apache2 use flag to our /etc/make.conf file. As root type:
and add apache2 to your list of use flags. Then check to see if any packages need to be recompiled with the new use flag.

Now we need to make some changes to the config files. Still as root type:

At the very end of this file add the line to stop apache from spamming about the server name every time the service is started. Restart the apache service:

By default apache looks in /var/www/localhost/htdocs for your web files. You should now be able to open a web browser on another pc on your network and type: http://enter_your_server's_ip_here, if you see the apache test page you are good to go. You can now delete all of the files in the /var/www/localhost/htdocs folder and replace them with some .html or .htm files.

This is fine if you only want to host one website on your server, but apache is much more powerful than that. I prefer to use the default location for development and custom tools, and set up virtual hosts for all of my websites. Apache has a config file called 00_default_vhosts.conf which is where you can specify the locations of other websites you wish to host on your server.

At the end of this file you can add as many virtual hosts as you would like. Each one would correspond to a domain name that you own that points to your IP address. This way you can keep the localhost folder access limited to the internal network only (cause you don’t want everyone to have access to your web server stats and other tools), and specify virtual hosts that will be accessed from the web via port 80.

Here is how you do it:

  • First you will need to create the directory for your new virtual host in the /var/www folder. You will also want to include the same folders that are in the localhost folder such as cgi-bin, icons, htdocs, and error.
  • Log in as root and do the following:
  • Create the directory for the new domain:
  • Copy the necessary directories from localhost
  • If you had any files in htdocs that copied over just delete them in the new htdocs directory.

Now you need to edit the /etc/apache2/vhosts/00_default_vhosts.conf and add your new virtual host entry to the end of the file. So as root type:

Here is an example of what a virtual host entry would look like:

Once you finish adding your new vhost entry you need to restart apache:

Now, if you have your domain name registered and pointing to your public IP address, and port 80 is forwarded to your server in your router, you should be able to open a web browser and type http://somedomain.com and see the website that you put in that folder on your server.

Rinse and repeat to add more domains.