Artigos‎ > ‎Compartilhamento de Arquivos‎ > ‎Apache‎ > ‎

Ubuntu Server and Apache2 Virtual Hosts

Link: http://blog.chadwollenberg.com/2008/09/30/ubuntu-server-and-apache2-virtual-hosts/

 

Every now and then, I have a need for virtual hosting. I’ve gotten a bit soft, because Drupal does it automatically for you, by simply putting your website directive right in the /path/to/web/drupal/sites/www.example.com/

But alas, you can’t use Drupal for everything. Its been at least a year since I’ve messed with Virtual Hosts in Apache2, so I had to rehash a few things in my mind. Of course, in Apache2, all virtual hosts are handled in /etc/apache2/sites-available/ (keep in mind I’m on ubuntu, if you’re on another distro, your path will varry). When you want to create a new virtual host, you simply create a file within this directory with your virtualhost directives, then do the command

a2ensite nameofnewhost

What this does, is ln -s over to /etc/apache2/sites-enabled/ and you can check this by navigating there and seeing the linked directories. Conversly, to dissable a site, you would do

a2dissite nameofnewhost

I believe these commands are specific to debian, so if you’re using a different distro, you should just ln -s them.

My goal was to have the main site of my web project a drupal installation for knowledgebase documentation, audio and video uploads, and then have a subdomain called blogs where I am going to install Wordpress MU so that I can have many blogs working off this blogs.mydomain.org/yournamehere subdomain. I’m doing some additional configurations of subdomains, but for the purposes of this article, lets just say we want to set up www.mydomain.org and blogs.mydomain.org.

So what do I do, well instead of going on my instincts and just copying the default configuration, and enabling the blogs subdomain, I read up on the subject, and somwhere along the way, I ended up taking out NameVirtualHost * at the top of my virtualhost files. Anyway, after much configuration troubleshooting, I did two things that made a difference. In my /etc/hosts file, I did go ahead and hardcode the domain names to localhost, and to the static IP of the server so that it looked something like this

127.0.0.1       localhost www.mydomain.org mydomain.org blogs.mydomain.org
10.1.10.6       www.mydomain.org mydomain.org blogs.mydomain.org

The second thing I did was I set up two files in the /etc/apache2/sites-available/ directory called main and blogs with the following configurations, main:

NameVirtualHost *

ServerName www.mydomain.org
ServerAlias mydomain.org
ServerAdmin youremail@mydomain.org

DirectoryIndex index.php index.html
DocumentRoot /var/www/main

Options Indexes FollowSymLinks MultiViews
# pcw AllowOverride None
AllowOverride All
Order allow,deny
allow from all
# This directive allows us to have apache2’s default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/

ErrorLog /var/log/apache2/main/error.log

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

CustomLog /var/log/apache2/main/access.log combined
ServerSignature On

 

It should be noted that I did create a directory in /var/log/apache2 for the error logs of main. This can be done by simply doing mkdir /var/log/apache2/main

Now I enable this site by doing a2ensite main

Keep in mind that I have already dissabled the default configuration by doing a2dissite default. You may need to do this on your server first. Then I created my subdomain blogs file in /etc/apache2/sites-available/blogs with the following:

ServerName blogs.mydomain.org
ServerAdmin youremail@mydomain.org

DirectoryIndex index.html index.php
DocumentRoot /var/www/blogs

Options Indexes FollowSymLinks MultiViews
# pcw AllowOverride None
AllowOverride All
Order allow,deny
allow from all
# This directive allows us to have apache2’s default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/

ErrorLog /var/log/apache2/blogs/error.log

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

CustomLog /var/log/apache2/blogs/access.log combined
ServerSignature On

 

Again, I now enable this site by doing

a2ensite blogs

and away we go. Now I have to restart the web server to pick up the changes.

/etc/init.d/apache2 restart

It should be noted, that you only need to enter the NameVirtualHosts * directive in one of your sites files for it to pick it up for all sites. However, if you were to disable that host, you would need to specify the directive elsewhere.

To make sure I was not getting the same output for both sites, I put an index.html file in both root directories (/var/www/main and /var/www/blogs) with a notice “This is site 1″ and “this is site 2″. This is important, because sometimes if configurations go wrong with virtualhosts, it uses one root directory for both sites. This is a quick and easy way to make sure this is not happening.

I hope this helps anyone that is having issues with Virtual Hosts in Apache2. It is slightly confusing, but once you get the hang of it, you can see why its so easy to set up new virtual directories for web sites. If you have any questions, feel free to post here, or in the Linuxbasement.com forums, or email me at chad@linuxbasement.com

 

Comments