November 22, 2016

Setup Apache Virtual Host on Ubuntu

Setup Apache Virtual Host on Ubuntu


https://www.ubuntu.com/The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible.

Apache breaks its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host.





Prerequisites

Before you begin this tutorial, you should create a non-root user as described in steps 1-4 here.

You will also need to have Apache installed in order to work through these steps. If you haven't already done so, you can get Apache installed on your server through apt-get:
sudo apt-get update
sudo apt-get install apache2

1. Create Virtual Directory Name

Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory. We will create a directory here for both of the virtual hosts we plan on making.
mkdir -p /var/www/sean.local/public_html
mkdir -p /vaw/www/michael.local/public_html

2. Create file index.html demo

We're just going for a demonstration, so our pages will be very simple. We're just going to make an index.html page for each site.

- site sean.local

nano /var/www/sean.local/public_html/index.html
In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:
<html>
  <head>
    <title>Welcome to sean.local!</title>
  </head>
  <body>
    <h1>Success!  The sean.local virtual host is working!</h1>
  </body>
</html>
Save and close the file when you are finished.
- site michael.local
nano /var/www/michael.local/public_html/index.html
<html>
  <head>
    <title>Welcome to michael.local!</title>
  </head>
  <body>
    <h1>Success!  The michael.local virtual host is working!</h1>
  </body>
</html>

Save and close the file when you are finished.

3. Create Virtual Host Config

- site sean.local

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sean.local.conf
<VirtualHost *:80>
    ServerName sean.local
    ServerAlias www.sean.local
    ServerAdmin sean@sean.local
    DocumentRoot /var/www/sean.local/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

- site michael.local
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/michael.local.conf
<VirtualHost *:80>
    ServerName michael.local
    ServerAlias www.michael.local
    ServerAdmin micael@michael.local
    DocumentRoot /var/www/michael.local/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

As you can see, there's not much here. We will customize the items here for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

4. Enable New Virtual Host

Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.
We can use the a2ensite tool to enable each of our sites like this:
sudo a2ensite sean.local.conf
sudo a2ensite michael.local.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart

Conclusion

If you followed along, you should now have a single server handling two separate domain names. You can expand this process by following the steps we outlined above to make additional virtual hosts.
There is no software limit on the number of domain names Apache can handle, so feel free to make as many as your server is capable of handling.

source
Previous Post
Next Post

post written by:

0 comments: