Changes

Configuring an Ubuntu 11.04 Based Web Server

7,516 bytes added, 19:03, 20 June 2011
New page: It is surprising the number of people who now host their own web sites. Some reasons for this are that doing so is now relatively easy and many people now have high speed internet connecti...
It is surprising the number of people who now host their own web sites. Some reasons for this are that doing so is now relatively easy and many people now have high speed internet connections. In addition, Linux provides a free, enterprise level operating system with everything needed to host a web server for free (with the exception of the hardware of course).

In this chapter we will explain how to configure an Ubuntu system to act as a web server.

== Requirements for Configuring a Web Server ==

To set up your own web site you need a computer, an operating system, a web server, a domain name, a name server and an IP address.

The computer can be any system capable of running Linux. In terms of an operating system, we will assume you are using Ubuntu 11.04. Ubuntu supports the Apache web server which can easily be installed once Ubuntu is up and running. A domain name can be registered with any domain name registration service.

If your ISP provides static IP addresses then you will need to associate your domain with your static IP address. This is achieved using a name server. Some domain registration services will provide this service for you. If yours does not, you can create an account at [http://www.zoneedit.com http://www.zoneedit.com] and use their name servers to point your domain name at your static IP address.

If you do not have a static IP address (i.e. your ISP provides you with a dynamic address which changes frequently) then you can use one of a number of free services that map your dynamic IP address to your domain name. One such service is provided by [http://www.dnsExit.com http://www.dnsExit.com].
Once you have your domain name and your name server configured the next step is to install and configure your web server.

== Installing the Apache Web Server on Ubuntu 11.04 ==

The standard web server on Linux is Apache. The web server is the technology that receives requests from web browsers and serves up the requested web pages to those browsers.

The desktop version of Ubuntu does not install the Apache web server by default. The first step in setting up a web server, therefore, is to install Apache. This can be performed either from the command-line or using the Ubuntu Software Center. To use the software center, click on the Ubuntu Software Center item in the Unity desktop launcher. Use the Search box to search for apache2. After the search completes, the Apache HTTP Server metapackage should be listed in the results panel. Select the item and click on the corresponding Install button to begin the installation.

To install Apache from the command-line start a terminal window (Ctrl-Alt-T) and run the following command at the command prompt:

<pre>
sudo apt-get install apache2
</pre>

The installation process will not only install, but also start up the web server.

== Testing the Web Server ==

Once the installation is complete the next step is to verify the web server is up and running. To do this fire up the web browser by clicking on the Firefox item in the Launcher and enter 127.0.0.1 in the address bar (127.0.0.1 is the loop-back network address which tells the system to connect to the local machine). The browser should load a page that reads It works!.

== Configuring the Apache Web Server for Your Domain ==

The next step in setting up your web server is to configure it for your domain name. This is performed in the ''/etc/apache2'' directory. To configure the web server open a terminal window and change directory to ''/etc/apache2''. In this directory you will find two sub-directories, sites-available and sites-enabled. Change directory into ''sites-available''. In this directory you will find a default file which may be used as a template for your own site.

Copy the default file to a new file with a name which matches your domain name. For example:

<pre>
sudo cp default myexample.com
</pre>

Edit your myexample.com file using your favorite editor using the sudo command to ensure you have write permission to the file. The top of the file will appear as follows:

<pre>
<VirtualHost *>
ServerAdmin webmaster@localhost

DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</pre>

If you are using an older version of Ubuntu, the file on your system may have a NameVirtualHost line at the top of the file. If so, comment it out by prefixing it with a '#' character:

<pre>
#NameVirtualHost *
</pre>

The reason for this is that this directive is already specified in the sites-enabled/000-default file.

<pre>
NameVirtualHost 68.206.209.234
</pre>

If the above line is not commented out the following warning will appear when the Apache server is restarted:

<pre>
[warn] NameVirtualHost *:0 has no VirtualHosts
</pre>

The ServerAdmin directive defines an administrative email address for people wishing to contact the web master for your site. Change this to an appropriate email address where you can be contacted:

<pre>
ServerAdmin [email protected]
</pre>

Next the ServerName and ServerAlias directives need to be defined so that the web server knows which virtual host this configuration file refers to:

<pre>
ServerName myexample.com
ServerAlias www.myexample.com
</pre>

In the next stage we need to define where the web site files are going to be located using the DocumentRoot directive. The tradition is to use ''/var/www/domain-name'':

<pre>
DocumentRoot /var/www/myexample.com
</pre>

Having completed the changes we now need a copy of the ''/etc/apache2/sites-available/myexample.com'' configuration file in ''/etc/apache2/sites-enabled''. We do this by creating a symbolic link from the file in ''sites-available'':
cd /etc/apache2/sites-enabled

<pre>
ln -s /etc/apache2/sites-available/myexample.com .
</pre>

Next, create the /var/www/myexample.com directory and place an index.html file in it. For example:

<pre>
<html>
<title>Sample Web Page</title>
<body>
Welcome to MyExample.com
</body>
</html>
</pre>

The last step is to restart the Apache web server to make sure it picks up our new settings:

<pre>
sudo /etc/init.d/apache2 restart
</pre>

== Web Server and Firewall Issues ==

If your Ubuntu system is configured to use a firewall, you will need to ensure that HTTP traffic on port 80 is permitted in order for external system to be able to access your web server. By default traffic on port 80 will be allowed as part of the Apache installation process. Refer to the [[Ubuntu 11.04 Firewall Basics]] and [[Using gufw and ufw to Configure an Ubuntu 11.04 Firewall]] chapters of this book for details on configuring Ubuntu Firewalls.

If the Ubuntu system running your web server sits on a network protected by a firewall (either another computer running a firewall, or a router or wireless base station containing built in firewall protection) you will need to configure the firewall to forward port 80 to your web server system. The mechanism for performing this differs between firewalls and devices so check your documentation to find out how to configure port forwarding.

Once everything is configured it should be possible to enter the domain name of your web site into a browser anywhere in the world and access your web server.