The two most common tools for this are the Apache and nginx servers.
Notes:
- You'll need to edit a few system configuration files. If you're uncomfortable with
vim
, replace vim
with nano
, or gedit
in the following commands. For example, sudo vim
will become sudo -H gedit
or sudo nano
.
- Once you're done setting it up, have a look at How to avoid using sudo when working in /var/www?
- A more detailed guide is available from the Ubuntu LTS Server Guide.
Apache
First, install Apache:
sudo apt-get install apache2
The Apache configuration files are located in /etc/apache2
. You'll typically be interested in:
/etc/apache2/sites-available
- contains the Virtual Host definitions. Definitions are enabled and disabled using the a2ensite
and a2dissite
commands. The enabled site definitions are linked to /etc/apache2/sites-enabled
.
/etc/apache2/conf-available
- contains custom configuration files. They are enabled and disabled using the a2enconf
and a2disconf
commands. The enabled site configuration files are linked to /etc/apache2/conf-enabled
.
/var/www/html
- the default directory that Apache serves.
For most instructions, I'll assume we are in /etc/apache2
.
VirtualHost setup
Let us create a new site. There's a default configuration available in sites-enabled/default.conf
. We will make a copy of this, and work on it:
sudo cp sites-available/000-default.conf sites-available/my-name.conf
sudo vim sites-available/my-name.conf
Change the ServerName
, so that it uses, for example, myname.com
:
ServerName myname.com
Change the DocumentRoot
, so that it uses, for example, /var/www/my-name
:
DocumentRoot /var/www/my-name
Save the file, and enable it:
sudo a2ensite my-site
Now, we need to set up the directory for the site:
sudo mkdir /var/www/my-name
We'll set permissions for convenience:
sudo chown $USER:www-data /var/www/my-name
sudo chmod g+s /var/www/my-name
Add a few HTML files here.
Since the virtual host is to run locally, we need to map myname.com
to a local address. To do this, we need to edit /etc/hosts
:
sudo vim /etc/hosts
Add a line like so:
127.0.0.2 myname.com myname
Save, and then restart Apache:
sudo service apache2 restart
Now, you can browse to http://myname.com
or http://myname
, and the contents of /var/www/my-name
will be displayed.