How to Configure a Rackspace Cloud Server to Use Virtual Hosts (Ubuntu 10.04LTS)

We're not going to talk about how to install the LAMP stack; that's beyond the scope of this post, but here is a great tutorial. Rackspace is also now providing the option to create a Cloud Server with LAMP already installed and that's what we do. Either way, get things running to the point where you're able to execute a PHP script.

Step #1: Secure the Server with a Firewall

Review the services you want to make publicly accessible; for us it's usually FTP, SSH, HTTP, and HTTPS.

First, allow currently established session to continue to function (e.g, your current SSH session):

sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Allow FTP:

sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT

sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT

Allow SSH:

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Allow HTTP:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Allow HTTPS:

sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Drop everything else:

sudo iptables -A INPUT -j DROP

Save the roles to the iptables.rules file:

sudo iptables-save > /etc/iptables.rules

You may need to switch to the root user as sudo might still get denied (the command for this is 'sudo su -'). Make sure you exit root once you get done.

Now you need to apply these rules to the network card that is configured to work with your public IP address. This can be done by opening the /etc/network/interfaces file.

sudo vim /etc/network/interfaces

Right below the line that says "iface eth0 inet static" insert the following:

pre-up iptables-restore < /etc/iptables.rules

Now reboot to make sure everything loads properly.

Step #2: Set the Virtual Hosts Permissions

If you've not done so, add the main user to the Apache user group (www-data):

sudo usermod -a -G www-data username

Log out and back in again to enable the group change, then verify it:

username@www:~$ groups

username sudo www-data

Let's make sure that the web directory is owned by the main user and is part of the Apache group:

sudo chown -R username /var/www

sudo chgrp -R www-data /var/www

We also want any new files created to be part of the www-data group, so let's set the sticky bit:

sudo chmod -R 2750 /var/www

Now, you will at some point probably need to allow write access to Apache. For example, Drupal needs to be able to write to a site's 'files' directory. The line below will accommodate this need:

sudo chmod -R 2770 /var/www/drupal6/sites/default/files

Step #3: Create the directory for the new site

Create the directory where our site is going to live:

mkidr /var/www/drupal6

Let's create an temporary index file so that we have something to test once we get the Apache Virtual Hosts properly configured:

echo "<?php echo date(DATE_RFC822); ?>" > /var/www/drupal6/index.php

Step #2: Set up Apache Virtual Hosts

Ubuntu's configuration is atypical from what I was used to for a long time; there is a layer of abstraction on top of things that can take a bit of getting used to if you're coming from another flavor of Linux.

Under /etc/apache there are several directories; the one's we're going to focus on are "sites-available" and "sites-enabled."

Execute a directory listing of the "sites-available" directory to see the following:

default

default-ssl

default.template


cat the default and default-ssl files; you'll see that they are service as 'catch all' configuration files for the server as there is no defined ServerName value.

The default.template file, though is the one we want to focus on as it contains the code necessary to set up a new site, both for HTTP and HTTPS.

Create a copy of that file for your use:

sudo cp /etc/apache2/sites-available/default.template /etc/apache2/sites-available/drupal6

Now open the file for editing:

sudo vim /etc/apache2/sites-available/drupal6

You'll want to replace all of the instances of "/var/www/example.com" with "/var/www/drupal6". In vim it's done like this:

:%s/\/var\/www\/example\.com/\/var\/www\/drupal6/g

Do the same for the log files:

:%s/\/var\/log\/apache2\/example\.com/\/var\/log\/apache2\/drupal6/g

And finally, for the domain names:

:%s/example\.com/drupal6site\.com/g

When you get done, your file will look similar to this one.

Now enable the site (this is the layer of abstraction I was talking about):

sudo a2ensite drupal6

Your output should look like this:

Enabling site drupal6.

Run '/etc/init.d/apache2 reload' to activate new configuration!

Let's do as it says:

sudo /etc/init.d/apache2 reload

Now, navigate to your site's URL and you should see the date output in UTC format:

Wed, 27 Jul 11 14:19:45 -0500

You should be good to go!

Credits

http://www.rackspace.com/knowledge_center/index.php/Virtual_Hosts_Permis...

http://www.rackspace.com/knowledge_center/index.php/Ubuntu_-_Apache_Virt...