<-- Previous Contents Page

Creating a Web server

This section shows how to set up a web server on your droplet. It doesn't cover actually writing web pages. That's a very big subject and there is already plenty of material about it on the web. There are also many good books on the subject including David Karlins' Building Websites for Dummies published by John Wiley. This section explains how to configure the web server that's already installed on your droplet and where to put the pages once you've created them.

If you specified Debian when you created your droplet, the Apache web server will already be installed. You need to configure it.

From your local machine, log into your server as root:

    ssh root@example.com
or on Windows:
        ssh -i ~/.ssh/id_rsa_root root@example.com
    

In the directory /etc/apache2/sites-available there's a file 000-default.conf. You need to edit it. Uncomment the ServerName line and set it to your domain name. Add a serverAlias line for your www subdomain. Change the ServerAdmin line to contain your email address.

Initially the file starts something 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
        # 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 www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
    
Change it to something 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
        # 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
        ServerAlias www.example.com
        ServerAdmin youremailaddress
        DocumentRoot /var/www/html
To write out the changes, see the line at the bottom of the command window - hold down the ctrl key and type 'o', hit the enter key to confirm the destination file, then ctrl and 'x' to quit the editor.

Now you can start the web server:

    systemctl start apache2

When Apache is installed, a standard home page is set up. You should be able to see it from your web browser by navigating to your web site - my case to http://example.com/

Having said that, the Chrome web browser no longer likes http pages. It will try to use https://example.com/ instead, which doesn't work yet.

Setting up HTML pages

The control file that you edited earlier 000-default.conf says that the document root is the directory /var/www/html. This is where your HTML pages live. It contains HTML files. and subdirectories containing HTML files. Each subdirectory of that should contain a file called index.html. For example, /var/www/html/index.html is your home page. Its URL is http://example.com/ or http://www.example.com/

Within the document root directory the subdirectory names determine the URL of each page. If you create a directory /var/www/html/about and a file /var/www/html/about/index.html, the URL for that page is http://example.com/about/.

If you create a file /var/www/html/about/us.html, its URL is http://example.com/about/us.html

The home page determines which pages can be found. If the home page contains a link to http://example.com/about/ then somebody visiting the site can find that page. If that page contains a link to http://example.com/about/us.html, then a visitor can find that, and so on.

One way to create web pages is to do it on your local computer using your chosen web page writing software and then copy the pages across to the web server using the scp command in a command window. For example, if your command window is in the about directory on your local machine and that contains the files index.html and us.html, and you've already created a directory /var/www/html/about on the web server, you can copy the files over like so:

    scp index.html us.html root@example.com:/var/www/html/about/