7

I have a Ubuntu LAMP site hosted at vps.net.

I have a directory called john that is accessible via mysite.com/john I would like it instead to be accessed via john.mysite.com

My site root is /var/www and this directory is at /var/www/john

What do I need to do to make this happen?

3
  • Clarifications, plz: 1) is it ok if both mysite.com/john and john.mysite.com work? 2) Should any subdirectory automatically be converted into a subdomain or only those you configure?
    – Bittrance
    Mar 10, 2011 at 18:44
  • @Bittrance, I would prefer only john.domain work and not domain/john but its not a big deal either way. Also only for ones I configure, although I would also be interested in knowing how to make it auto for every directory.
    – JD Isaacks
    Mar 10, 2011 at 18:47
  • Making new dirs become subdomains needs a script, AFAIK. Don't think you can express that in Apache config terms.
    – Bittrance
    Mar 10, 2011 at 19:05

4 Answers 4

13

Create a new site file at /etc/apache2/sites-available/john.mysite.com

Set up the site something like this:

<VirtualHost *:80>
    ServerName john.mysite.com
    DocumentRoot /var/www/john
    <Directory /var/www/john/>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Enable the site with a2ensite john.mysite.com

Reload apache to enable the site: service apache2 restart

5
  • I tried this, - I replaced mysite.com with my actual site and I restarted apache differently: /etc/init.d/apache2 reload .. I didn't get any errors, but it doesn't seem to be working. Can I be missing something else? or does it take a while to work? Thanks.
    – JD Isaacks
    Mar 10, 2011 at 19:11
  • @John: As mentioned in one of the other answers, you'll need to set up DNS to point to the site as well. DNS will take some time, but the Apache part takes effect as soon as you restart the service. Mar 10, 2011 at 19:15
  • Thanks, I just set the DNS, so I will wait a while and see if it works. :)
    – JD Isaacks
    Mar 10, 2011 at 19:18
  • one last thing. http is pointing to /var/www/john but https is pointing to just /var/www any ideas? Thanks.
    – JD Isaacks
    Mar 10, 2011 at 19:44
  • 1
    @John You'll need to configure an SSL VirtualHost for the SSL listener, you can model it off of the default-ssl file under /etc/apache2/sites-available/. Keep in mind that the certificate will throw a warning if it doesn't match the domain name. Mar 10, 2011 at 19:53
3

Apart from configuring Apache, don't forget also the DNS part: you will need a record for john.mysite.com, pointing to the same IP address mysite.com points to (if using an A record), or to mysite.com itself (if using a CNAME record).

2
  • Thanks, I did what Shane Madden suggested, I also just added the subdomain in the DNS manager at vps.net to point to the same IP like you said. It still isn't working, would it take a while before it starts to work?
    – JD Isaacks
    Mar 10, 2011 at 19:17
  • 1
    Yes, public DNS always have some latency, when you make a modification it will need a while (usually 24-48 hours) to be acknowledged by the whole Internet. Meanwhile, if you want to test it, you can put an entry in the hosts file of any computer to map john.mysite.com to the server's IP address, and then test the URL in its browser.
    – Massimo
    Mar 10, 2011 at 19:35
3

These steps worked for me in Ubuntu 12.04 LTS x86_64:

You need to create a new site file at /etc/apache2/sites-available/john.mysite.com.conf

The extension .conf is very important, without it, the rest won't work!!

Then set up the site something like this:

<VirtualHost *:80>
    ServerName john.mysite.com
    DocumentRoot /var/www/john
    <Directory /var/www/john/>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Save the changes and enable the site with the command:

sudo a2ensite john.mysite.com

Reload apache to enable the site with:

sudo service apache2 reload

Restart apache service:

sudo service apache2 restart

After that, you need to set up DNS to point to the site as well (as a Subdomain), DNS will take some time but in my experience it only takes a few minutes (15).

Hope this helps!! :)

0

you need to create a vhost for your webserver (assuming apache) that has at least the following info

<VirtualHost *:80>
ServerName john.mysite.com
DocumentRoot /var/www/john
</VirtualHost>

this is not a definitive vhost file but is a starting point, it should go in the bottom of your httpd.conf file or wherever your system manages vhosts

You must log in to answer this question.

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