Gentoo – Apache Web Server

image_pdfimage_print

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:

emerge -av apache

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

/etc/init.d/apache2 start

Then set it to the default runelvel:

rc-update add apache2 default

We also need to add the apache2 use flag to our /etc/make.conf file. As root type:

nano -w /etc/make.conf

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.

emerge -av --newuse world

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

nano -w /etc/apache2/httpd.conf

At the very end of this file add the line

ServerName localhost

to stop apache from spamming about the server name every time the service is started. Restart the apache service:

/etc/init.d/apache2 restart

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.

nano -w /etc/apache2/vhosts/00_default_vhost.conf

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:
  • cd /var/www
  • Create the directory for the new domain:
    mkdir somedomain
  • Copy the necessary directories from localhost
  • cp -a /var/www/localhost/* /var/www/somedomain

    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:

nano -w /etc/apach2/vhosts/00_default_vhosts.conf

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

######################### 
#Your_domain_name_here 
######################### 
<VirtualHost *:80> 
ServerName somedomain.com 
Serveralias somedomain.com 
DocumentRoot /var/www/somedomain/htdocs/
DirectoryIndex index.php index.html index.htm 
<Directory /var/www/somedomain/htdocs>
Options -Indexes FollowSymlinks 
AllowOverride All 
Order allow,deny
Allow from all
</Directory>
</VirtualHost> 

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

/etc/init.d/apache2 restart

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.

  1. No Comments