How to add multiple web sites your Linux/Debian/Ubuntu Apache web server

Settuping up Apache to host a site on a single domain name is pretty straight-forward. But setting up multiple web sites with their own domain names is much more complicated.

Prerequisites

  • Make sure you have already installed Apache.
  • Make sure that the relevant domain (or sub-domain) is pointing to your server.

1. Create New Directory

You need to create a directory that will be used to store web files for your new web site.

It can be anywhere on your drive, but really you should create a folder inside your "/var/www/" because that's what it is there for.

Syntax

mkdir [directory]
  • mkdir -- A program ("Make Directory") used to create file directories.
  • [directory] -- The directory filepath where your new web site will be stored.

Example

mkdir /var/www/example

2. Change Directory

Apache domain/site configurations files are stored in "/etc/apache2/sites-available/", so let's go there.

cd /etc/apache2/sites-available/
  • cd -- (Change Directory) A program for changing the current/working directory.

3. Copy Default Configuration File

You need to create a settings file for the domain name that will be handled by Apache.

The default configuration file is called "000-default.conf", so let's use that file as the basis of the new site you will be adding.

If you'd rather use an FTP client to duplicate the file, you can go ahead and do that.

Syntax

cp 000-default.conf [site].conf
  • cp -- A program that copies files and folders.
  • [site] -- The name/filename that will identify your need site. Usually people use domain name (e.g. "example.conf" or "example.com.conf"). It doesn't really matter what you call it, but it should be a name that is meaningful to you so you can easily identify some time in the future, especially if you plan to add many sites.

Example

cp 000-default.conf example.conf

4. Edit New Configuration File

Now you need to edit the file we just created. So let's use a program called "nano" to do that.

If you'd rather use some other CLI or GUI text editor, there's no need to use nano.

Syntax

nano [name].conf
  • nano -- A program used to create and edit text files.
  • [name] -- This is the configuration file we created in the previous step.

Example

nano example.conf

5. You should see a new screen that displays text that looks like this...

# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to

...followed by a lot more text.

When a hash sign (#) appears at the beginning of a line, everything follows it (on that line) will be disregarded by any software that tried to parse this file.

Lines that begin with a hash sign are called "comment(s)"; they provide notes and instructions for you (the system administrator) along with some sample settings you may want to use/enable later.

In the first paragraph of comments, there may or may not be a line that looks like "ServerName domain.com".

A line that begins with "ServerName" (followed by a domain name) tells Apache the domain name that this file is meant for.

If there is already an commented line with ServerName, add a new line without a hash sign. Conversely, if there is an uncommented line that begin with "ServerName"--meaning, a line that does not begin with a hash symbol--erase the domain name that's already there and add the domain name you want to use.

If you are using "nano", use your arrow keys to position your cursor as needed.

# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName example.com

6. Set web directory

Use the directory you created in step 1.

ServerAdmin webmaster@localhost
DocumentRoot /var/www/example

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.

7. Save the file.

Press control key on your keyboard and then x key.

You will asked:

"Save modified buffer?"

Type "y" and then press enter.

Conversely, you could type "n" to exit the program without saving changes.

8. Activate Site

Syntax

a2ensite [name]
  • a2ensite -- A progam that activates host configuration files for Apache.
  • [name] -- Use the filename for the file created in thesites-available folder (step 2). You can take out the ".conf" part of the file name or include it--there's no difference for this step.

Example

a2ensite example

9. Reload Apache

service apache2 reload
  • service -- A program that manages background programs. (ie. programs that running even after you close/exit Bash or the terminal.)

  • apache2 -- The web server--an aplication that handles requests from web browsers.

10. Done

Now you can visit your site using the domain name provided.

You should see a page that looks like...

Index of /
Name    Last modified   Size    Description

Parent Directory        -    

Apache/2.4.41 (Ubuntu) Server at example.com Port 443

This the kind of page Apache returns when a user tries to view a directory without an HTML (or PHP) index file (e.g. index.html).