How to host server on local PC(ubuntu) and set up virtual hosts

5,852

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.

Share:
5,852

Related videos on Youtube

Tasdik Rahman
Author by

Tasdik Rahman

Updated on September 18, 2022

Comments

  • Tasdik Rahman
    Tasdik Rahman over 1 year

    I am new to Ubuntu and I wanted to host a server on it.

    I googled a bit and read something about Apache and IIS and I couldn't get much out of it. Right now I am confused about how to proceed.

    What I want to do is

    • Host server on local PC and set up virtual hosts on same PC

    • in virtual host, have “myname.com” opened in browser

    Any relevant links which could serve as a tutorial?

    Sorry if it has been already asked, it would be kind of you if you could redirect to that thread.

    • muru
      muru over 9 years
    • Tasdik Rahman
      Tasdik Rahman over 9 years
      So I have to use apache to do the things if I am not wrong?
    • muru
      muru over 9 years
      Depends on what you intend to do. For many things Apache is more commonly used and so you'll find more documentation. nginx is also an option. IIS is Windows-only, no help for that here.
    • Tasdik Rahman
      Tasdik Rahman over 9 years
      Sorry to confuse you. What I meant was, will I be able to complete the stated tasks with apache? Sorry for my ignorance, but I am a complete newbie
    • muru
      muru over 9 years
      Yes. Both Apache and nginx can do that.
    • Tasdik Rahman
      Tasdik Rahman over 9 years
      Can you point me to some good introductory tutorials on apache. I googled it but I am not sure on which one to go with?
    • muru
      muru over 9 years
      I'll write on here, but it will take a while.
  • Tasdik Rahman
    Tasdik Rahman over 9 years
    Hey @muru. I think I did something wrong, I have asked another question here. Can you help me link