Peter's Blog

Redefining the Impossible

Ubuntu Apache2 Virtual Server


I needed to set up another drupal site on my ubuntu linode. I had a domain name, I wanted to make it an independent site. I decided to keep it seperate from my existing site by putting in a fresh Drupal 4.6.1 installation and not to use Drupals virtual server facility.

I knew Apache2 supported virtual hosting and I decided to use that. I tried creating a new virtual host by creating a file in /etc/apache2/sites-available as follows:

<VirtualHost *>
        ServerName www.site2.com
        ServerAlias site2.com
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/site2
        <Directory /var/www/site2/>
                Options Indexes FollowSymLinks MultiViews
                # pcw AllowOverride None
                AllowOverride All
                Order allow,deny
                allow from all
                # This directive allows us to have apache2's default start page
                # in /apache2-default/, but still have / go to the right place
                # Commented out for Ubuntu
                #RedirectMatch ^/$ /apache2-default/
        </Directory>

        ErrorLog /var/log/apache2/site2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/site2/access.log combined
        ServerSignature On

</VirtualHost>

where site2 is the name of the new site. Note that I created /var/log/apache2/site2 so that the site would get it's own access logs.

I used the command

a2ensite site2

to enable the site. I restarted apache2 and, bang, both this site and the new site showed the new site, I had broken this site.

After faffing around and googling, I tried a simple experiment. I removed the symbolic link to site2 from /etc/apache2/sites-enabled created by a2ensite and I just appended the above file to /etc/apache2/sites-available/default. I restarted apache2 and this worked, I had two sites. This is probably not the right way to do it but it works and any time I spend fixing it will bring this site down which bothers me so I'll leave it as it is unless I come across the correct way to do it.

Update: On my oneandone server running debian this is working fine as a seperate file, enabled with a2ensite:

<VirtualHost *>
    ServerName petersblog.org
    ServerAlias petersblog.org *.petersblog.org
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/petersblog.org
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/petersblog.org>
        # pcw No directory listsings
        # Options Indexes FollowSymLinks MultiViews
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog /var/log/apache2/petersblog.org/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/petersblog.org/access.log combined
    ServerSignature On

</VirtualHost>

I have four sites set up like this and all are working.


Filed under: apache debian drupal ubuntu

10 Comments

Norm Says:

over 3 years ago

Peter, I don't really know what a2ensite is or does but I do know that if you use multiple sites (virtual hosts) then ALL sites have to be in virtual hosts... even the main default site.

Norm

Peter Says:

over 3 years ago

Debian/ubuntu allows you to set up a number of files defining sets of virtual hosts. a2ensite is used to define whether each file is included amongst all the configuration files for your apache server.

All my sites are in virtual hosts. I don't find this a problem.

Peter

mbradlcu Says:

over 2 years ago

Hi and thanks for posting your travels on this subject! I noticed when following your path that I got this error in the

/var/log/apache2/error.log: error (2)No such file or directory: could not open transfer log file /var/log/apache2/site2/access.log. Unable to open logs

I removed the entries in the new site (sites-available) for logging and was able to do a a2ensite and it worked. Do you have to touch a coupe log files so they exist first? anyway, thanks again Matt

Peter Says:

over 2 years ago

The log files were all created automatically. Try making sure apache has write permission to the log directory.

Peter

Anonymous Says:

over 2 years ago

I ended up removing the directories and just naming my logs after the site. then it all worked. hurrah

Jose luis Says:

over 2 years ago

Hi,

I had a similar problem, when a new virtual host was added to my apache 2 the new site was ignored and it always served the "first" virtual host. In my case, the solution was to make the server able to know the IP of the server name. I did it by modifying the /etc/hosts file and added the name resolution here.

Anonymous Says:

over 2 years ago

Instead of putting all that stuff in your default, just rename the symlink in sites-enabled from default to 0000-default. this makes it run first (you need the virtual host stuff in there, and this should be the default for *) now, any sites you add with a2ensite will work as expected.

VLD Says:

about 1 year ago

I've done exactly what you've done, but it doesn't work... What should i do?

a visitor Says:

about 1 year ago

nice blog, thanks.

bluelab Says:

8 months ago

Concerning permissinons on log dir : chmod root:adm

and configure as usual

cheers

Sorry but comments on this post are now closed.