5

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
6
  • @Hailwood: Are there any error messages or message output when you do sudo /etc/init.d/apache2 reload ?
    – Thomas Ward
    Apr 14, 2011 at 22:33
  • @EvilPhoenix: Check the update :)
    – Hailwood
    Apr 14, 2011 at 22:47
  • Was curious, because that matches the same error(s) I get, which explains my interest in this question you asked.
    – Thomas Ward
    Apr 14, 2011 at 22:48
  • Weird, I am surprised there is not an apache.stackexchange.com
    – Hailwood
    Apr 14, 2011 at 23:12
  • btw, just found serverfault so I have asked serverfault.com/questions/259890/… as well.
    – Hailwood
    Apr 14, 2011 at 23:20

2 Answers 2

8

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 :)

1
  • 2
    One note is that the line suggested for /etc/apache2/apache2.conf was found in /etc/apache2/ports.conf which is imported by apache2.conf
    – Hailwood
    Apr 18, 2011 at 23:51
0

There is a script that makes this simple for you - https://github.com/RoverWire/virtualhost

This eventually does the same thins as explained by the answers given but does it in just one command eg. -

sudo virtualhost create mysite.local my_site

It also allows you to delete a host you created

sudo virtualhost delete mysite.local my_site

Just note that the "my_dir" assumes that it starts after /var/www

So if your site folder is in /var/www/my_site

You should run this (pass directory path without /var/www) -

sudo virtualhost create mysite.local my_site

You can also modify the script file & remove the default path "/var/www" so that you can pass an absolute path to the site directory

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .