Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a server that hosts mydomain.com. Then I have a server that hosts subdomain1.mydomain.com.

On the subdomain1.mydomain.com server, how do I make subdomains? For instance I want to create subdomain2.subdomain1.mydomain.com

I have my subdomain1.mydomain.com site in /home/USER/public_html and it is working just fine. But When I try to create subdomain2.subdomain1.mydomain.com, it says Server not found in the browser. I have tried so many configurations in my /etc/hosts and /etc/apache2/sites-available/subdomain2.subdomain1.mydomain.com, without any luck

Can I get some detailed instructions maybe? I have looked everywhere and every place tells you different, with no luck.

Ubuntu Server 11.10 w/ the Lamp Stack option

share|improve this question
    
Something occured to me just now: Do you want the sudomains to point to the same or to a different website? –  Lukas Dec 7 '11 at 19:04
    
It is a Drupal Multisite install. They all just need to point to to this server. –  joe Dec 8 '11 at 3:21
    
In that case, a ServerAlias should do the trick for you. I edited my answer to include one. You can find the corresponding Apache Manual site here –  Lukas Dec 8 '11 at 6:41
add comment

2 Answers 2

Configuration of Apache

What you'll need is the Apache VirtualHost directive. See the Apache Documentation and some Examples.

Basically what you want to do in Ubuntu is to make sure, that the port you want to use (usually :80) is enabled in /etc/apache2/ports.conf like this:

NameVirtualHost *:80
Listen 80

Next you'll have to create a new conf-file in /etc/apache2/sites-available. I'd suggest to name it proj1.conf or proj1.mydomain.conf.

There you can configure the VirtualHost as follows:

<VirtualHost *:80>
  ServerName proj1.subdomain.domain.com
  DocumentRoot /var/www/proj1
  ServerAdmin name@domain.com

  # Write a seperate log per Virtualhost
  CustomLog /var/log/apache2/proj1.subdomain.access_log combined
  ErrorLog /var/log/apache2/proj1.subdomain.error_log

  # Maybe you want to put some restrictions on the directory
  <Directory /var/www/proj1>
    Options -Indexes +FollowSymLinks + Includes
    AllowOverride All
    # Restrict Access to certain IP's
    Order Deny,Allow
    Deny from All
    Allow from 127.0.0.1 IP IP IP
    Satisfy ALL
  </Directory>
</VirtualHost>

Consult the Apache Manual to see what you could do with the Directive.

To enable this Site, link it to /etc/apache2/sites-enabled

ln -s /etc/apache2/sites-available/proj1.conf /etc/apache2/sites-enabled/proj1

Now all you have to do is make sure your config is valid and then restart Apache:

apache2ctl configtest && /etc/init.d/apache2 restart

DNS

If you're running a virtual machine, DNS is probably provided by your hoster, so you could talk to him regarding setting the DNS up.

Naturally you will have to set up the subdomain in your DNS in a way that it points to this server. For this you can create an A- or CNAME-Record for each subdomain and point it to your server.

This is how your Bind Zone-file could look:

$TTL 2h

@       IN      SOA     dns1.example.com.      emailaddress.domain.com. (
                        2011120701  ; serial number YYMMDDNN
                        1h          ; Refresh
                        20m         ; Retry
                        2w          ; Expire
                        2h          ; Min TTL
            )

@               IN  NS      dns1.example.com. 
@               IN  NS      dns2.example.com. 

@               IN  MX      10 mx1.domain.com.
@               IN  MX      20 mx2.domain.com.
@               IN  A       999.999.999.999
subdomain       IN  A       666.666.666.666
proj1.subdomain IN  CNAME   subdomain
*.subdomain     IN  CNAME   subdomain

Obviously you'd have to replace domain.com by your domain, 999.999... and 666.666 by the proper IP's, change the NS and MX Records etc..

See the Bind manual for further details.

For testing purposes you can also edit the hosts-file on your local computer. That way only your computer will resolve the subdomain to this server, so you can check if everything works and only change the DNS if it does.


Edit: If you want to create "sub-subdomains" that point to the same directory as the subdomain, you could add a ServerAlias to your Apache2 config, next to the ServerName:

  ServerName subdomain.mydomain.ain
  ServerAlias proj.subdomain.mydomain.ain
  ServerAlias *.subdoamaind.mydomain.ain

The DNS configuration would be the same as pictured above.

share|improve this answer
    
my ssh port is 8022. So should I use that instead of 80? –  joe Dec 7 '11 at 15:24
1  
The ssh Port has nothing to do with the port your Webserver is running on. –  Lukas Dec 7 '11 at 17:07
    
@joe If you type an address like example.com in your browser it will connect to the IP to which the name example.com resolves on port 80. The Webserver waits for connections on this Port. Of course you can run a Webserver on a different Port (eg. Port 666), but then you'd have to connect to http:example.com:666. NOTE: you can only run one Webservice per Port, so if you'll config your Webserver on the same Port ssh is running on it won't work. Normally Ports have a defined purpose. See Wikipedia for details. –  Lukas Dec 7 '11 at 17:08
    
"I'd suggest to name it proj1.conf or proj1.mydomain.conf." For mydomain.conf....do I use the actual regular domain, or the subdomain server I am on? To be more clear, would I use proj1.mydomain.conf or proj1.subdomain1.conf, as per in my original post –  joe Dec 7 '11 at 17:19
    
Where you put "IP IP IP", do I use the servers ip? –  joe Dec 7 '11 at 17:26
show 2 more comments

Lukas is right but dealing with Vhosts this way can be a real pain.

Have you tried a free web admin software ? Even tho we're a team of developer at work, we use Webmin (http://www.webmin.com/) for our VHosts, Cron jobs and config management.

You can also grab a copy of CPanel &cie but those are paid and usually won't bring you much more than free web based panels.

share|improve this answer
    
CPanel does not support Ubuntu. –  joe Dec 7 '11 at 15:24
    
@joe Maybe you could have a look at VHCS then. Here is the link to the Ubunu Forums for a installation guide. –  Lukas Dec 7 '11 at 17:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.