Setting up websites under Apache on Ubuntu Server

I was asked today by a colleague how I set up websites on a server without a GUI. So, I thought I would do a quick run through of the commands I use to set up a new website using Ubuntu 10.04. The following should work just fine on all subsequent versions of Ubuntu though.

This tutorial assumes that you’re comfortable logging into your server via SSH. It also assumes that you are logging in as someone other than the root user (which you should be if you’re not) and using the sudo command to run further commands with root privileges.

First of all make sure that your hostname is set:

The first command will show you the short version of the hostname. The second will show the fully qualified domain name for your system. If there is a problem with either, you must set this properly before continuing.

Next, make sure that you have apache2 installed:

Also, because we are going to be configuring name-based virtual hosts for our websites, we should disable the default virtual host:

Ok, now we’re ready to start serving websites. To create your first site their are several steps we will go over. First of all, lets create a user:

Replace example with the name you want to use for the website you’re setting up. Follow the instructions for setting the password etc. This will create a home directory for the user in /home/example

Now switch to the the user you just created:

Switch the user’s directory and create a web-facing folder:

Create a file so that we can test that apache is serving our content. I use the nano editor but feel free to use any editor you want.

Just add something like ‘Apache is serving my content…Woohoo’ and save. Switch back from the user you created:

Now we need to set up the virtual host. Create a file with the same name as the domain name:

and add the following content:

Again, replace with your own domain name and change the SeverAdmin email to your own email. Notice that the DocumentRoot points to the web-facing directory we setup earlier. Now we need to enable the virtual host we just created and restart the server:

Now, assuming that you having pointed your domain at the server’s IP, you should now be able to access your website via a browser.

One final step to go over is to enable FTP access so that you can upload data to your website. I’m going to show you how to use the more secure SFTP protocol that almost all FTP software supports. First of all, we need to allow SFTP connections. Make sure that the following line exists in /etc/ssh/sshd_config:

If not, add it in, then at the end of the file add:

Save the file and restart SSH:

Add the group sftp like so:

Now all we need to do is add users that we want to give SFTP access to, to this group. To make sure they don’t go wandering outside their home directory, we’ll also set permissions to stop them:

Now you should be able to SFTP into the server using the username and password you setup. The user will only be able to see their home directory and nothing else.

Hopefully this will help you to set up sites quickly in the future without having to install cpanel, plesk or the like.

Good luck!

Leave a Reply