HOWTO Apache VirtualHost by IP Address

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

[edit] Introduction

Setup VirtualHost - Simple instructions for us real (AKA dumb) people.

NOTE: The filenames and directory structure of gentoo's apache2 install has changed significantly since the writing of this document.

[edit] Assumptions

This document assumes that you have emerged / apache2 already. It also assumes that you are using IP Aliasing. But if you are not and you use name based virtual host see Name based virtual hosting tab

[edit] Edit your Configuration Files

[edit] /etc/apache2/httpd.conf

Lets start with /etc/apache2/httpd.conf

File: /etc/apache2/httpd.conf
### Main Configuration Section
### You really shouldn't change these settings unless you're a guru
###
ServerRoot /usr/lib/apache2
ServerName yourserver.yourdomain.com
#LockFile /etc/apache2/apache2.lock
PidFile /var/run/apache2.pid
ErrorLog logs/error_log
LogLevel warn
DocumentRoot /var/www/localhost/htdocs 

###
### Virtual Hosts
###
# We include different templates for Virtual Hosting. Have a look in the
# vhosts directory and modify to suit your needs.
Include conf/vhosts/vhosts.conf
#Include conf/vhosts/dynamic-vhosts.conf
#Include conf/vhosts/virtual-homepages.conf 

Uncomment the ServerName to get rid of the silly ServerName error. Also make sure to uncomment the 'Include conf/vhosts/vhost.conf' line.

[edit] /etc/conf.d/apache2

File: /etc/conf.d/apache2
# Config file for /etc/init.d/apache2

# An example from /etc/apache2/conf/modules.d/40_mod_ssl.conf:
#
# <IfDefine SSL>
#  <IfModule !mod_ssl.c>
#    LoadModule ssl_module    extramodules/mod_ssl.so
#  </IfModule>
# </IfDefine>
#
# This means that the mod_ssl.so DSO module is only loaded
# into the server when you pass "-D SSL" at startup.  To
# enable WebDAV, add "-D DAV -D DAV_FS".  If you installed
# mod_php then add "-D PHP4" or "-D PHP5".  For more options, please
# read the files in the /etc/apache2/conf/modules.d directory.

APACHE2_OPTS="-D PHP4 -D SSL"
# or this for PHP5
APACHE2_OPTS="-D PHP5 -D SSL"

If you are using PHP (who isn't?) then simply add -D PHP4 or -D PHP5 as necessary to the APACHE2_OPTS command.

[edit] /etc/apache2/conf/vhosts/vhosts.conf

Ok now for the really important part. Edit /etc/apache2/conf/vhosts/vhosts.conf

File: /etc/apache2/conf/vhosts/vhosts.conf
 #### LocalHost aka Domain 1 ####
 <VirtualHost my.ip.add.ress>
       ServerName www.mydomain1.com
       ServerPath /mydomain1.com
       DocumentRoot /var/www/localhost/htdocs
 </VirtualHost>

 #### Domain 2 ####
 <VirtualHost my.ip.add.ress>
        ServerName www.mydomain2.com
        DocumentRoot /var/www/mydomain2.com/htdocs
        <Directory "/var/www/mydomain2.com/htdocs">
          Options None
          AllowOverride None
          Order allow,deny
          Allow from all
        </Directory>
 </VirtualHost>

 #### Replicate Domain 2 for further Domains ####
                                ""
                               "" 

The secret here is adding this part;

<Directory "/var/www/mydomain2.com/htdocs">
         Options None
         AllowOverride None
         Order allow,deny
         Allow from all
</Directory>

Now if your directory structure looks like this;

/var/www/
         localhost/htdocs
         domain1.com/htdocs
         domain2.com/htdocs
         domain3.com/htdocs

[edit] Name based virtual hosting tab

just add this in the top of the vhosts.conf before <VirtualHosts ...>

NameVirtualHost _your_ip_here


Then everything should work fine for you.

Dont forget to restart apache:

# /etc/init.d/apache2 restart

[edit] Discussion

You can discuss this in the Gentoo forums thread here; http://forums.gentoo.org/viewtopic-p-2686304.html#2686304

Personal tools