Ubuntu – editing apache vhosts and ubuntu hosts file

11.04Apache2hostsvirtualhost

I have just installed ubuntu (11.04).

I have installed apache/mysql/php etc.

I have a few sites sitting in

/var/www/site1  
/var/www/site2  
/var/www/site3  

I want to be able to access these at

http://site1.local  
http://site2.local  
http://site3.local

So in my hosts file I have

127.0.0.1   site1.local  
127.0.0.1   site2.local  
127.0.0.1   site3.local  

Then I have copied /etc/apache2/sites-available/default 3 times.

so now I have:

/etc/apache2/sites-available/site1  
/etc/apache2/sites-available/site2  
/etc/apache2/sites-available/site3  

These all look like (with appropriate sitex names)

<VirtualHost site1.local>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/site1
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/site1/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

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

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

In the terminal I then call

sudo a2ensite site1 site2 site3 && sudo /etc/init.d/apache2 reload

Which seems to work,
except only one of the vhosts works at the same time (including default),
What am I doing wrong?


When I run the reload command I get

  • Reloading web server config apache2
    apache2: Could not reliably determine the server's fully qualified domain name,
    using 127.0.1.1 for ServerName
    [Fri Apr 15 10:45:27 2011] [warn]
    NameVirtualHost *:80 has no
    VirtualHosts

Best Answer

File: /etc/hosts

127.0.0.1 site1.local
127.0.0.1 site2.local
127.0.0.1 site3.local

File: /etc/apache2/apache2.conf

[...]
NameVirtualHost *:80
[...]

Files: /etc/apache2/sites-available/site{i}.local.conf (instead of {i} insert number: 1 or 2 or 3)

<VirtualHost *:80>
    ServerName site{i}.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/site{i}
    [...]
</VirtualHost>

In directory /etc/apache2/sites-enabled/ create symbolic links:

@site{i}.local.conf

Restart apache and have fun :)

Related Question