LewlSauce
LewlSauce

Reputation: 5872

Apache 2 subdomain?

I've been looking for articles pertaining to how to do this, but I'm finding any detailed and straight forward instructions. I do know there's a lot of information pertaining to this, but maybe I'm just not looking for the right things.

In the control panel for my domain name, I added blog.domain.com to go to my webserver's IP address. However, within Apache's configuration, I'd like to be able to direct blog.domain.com to a certain folder.

What file do I need to modify and what do I need to add to it?

Upvotes: 1

Views: 279

Answers (1)

Giacomo1968
Giacomo1968

Reputation: 26066

You are looking for NameVirtualHost. I use it all the time & it works great!

It’s not clear what OS you are using, but in general you need to first activate NameVirtualHost for the port you want. I will assume you will use port 80, so find this line in your Apache config & set as so:

NameVirtualHost *:80

Make sure your Apache config is set to list to port 80. Which should be the case, but adding here for reference:

Listen 80

Then for your subdomain, here is where the magic happens. Again, I am just doing the basics so adjust to whatever your server settings are:

<VirtualHost *:80>
  ServerName blog.domain.com
  ServerAlias blog.domain.com

  DocumentRoot /var/www/blog.domain.com

</VirtualHost>

The key is the ServerName and the wildcard on VirtualHost. That basically states, “Okay, we are using NameVirtualHost on port 80, this config is for the server name blog.domain.com so I will pay attention to all options in this config & apply them only to blog.domain.com. And the DocumentRoot should be what I indicate in this config.”

EDIT: Adding additional advice based on the original posters comment below.

First, do not edit /etc/apache2/sites-available/default in any way. Instead create a new config file just for your new subdomain. This makes it easier to manage. I will assume you need to run sudo and edit with nano for my examples:

sudo nano /etc/apache2/sites-available/blog.domain.com.conf

And add the VirtualHost stuff I have above to that new blog.domain.com.conf file. Of course make sure your VirtualHost directives match what you want; mine is only a bare example.

Now if that’s done, you need to create a symbolic link from sites-available to sites-enabled like so:

sudo ln -s /etc/apache2/sites-available/blog.domain.com.conf /etc/apache2/sites-enabled/blog.domain.com.conf

Okay, that’s all done? Since it seems like you have a similar Apache2 config layout like the Ubuntu 12.04 servers I have worked on, go in this file to see if NameVirtualHost is set:

sudo nano /etc/apache2/ports.conf

You should see two lines like so:

NameVirtualHost *:80
Listen 80

Okay, all set? Now, restart Apache & you should be set!

If you want to test, create a test file in your document root for the subdomain that has this line in it; I am assuming you can use PHP:

<?php
echo $_SERVER['SERVER_NAME'];
?>

If all works, it should echo back the subdomain of the host that directory is setup for: blog.domain.com

Upvotes: 5

Related Questions