Host multiple websites from Ubuntu using Apache

If you have a dedicated Ubuntu web server, and want to host multiple websites from it, the easiest way to do it is using Apache Name-Based Hosting. This method of web hosting relies on the client ( people browsing the Internet ) typing the domain name of the website they want to visit. using this method, people cannot get to your website using an IP address, they must type the domain name on their browser to get to your website. to complete this guide, we need to have a valid domain name that we want to redirect to our web server running apache.

Note: In this guide I will use forevergeeks.com  as example with the external IP address 45.79.134.144 pointing to my server. please change the information to match yours as we go along.

DNS Setup

The first thing you need to do is to change your DNS record from your DNS provider to point to your server IP address. for example, this is how the DNS record for forevergeeeks.com are setup:

DNS zone

The DNS record change takes awhile to propagate some times, so you might not see the change happening right away.Once you are done setting up the IP address for your domain, let’s work on the server.

Installing Apache

I’ll assume that you haven’t installed apache on your server yet, and to make things easier, I will install MySQL and PHP along with Apache as well. most websites are built on WordPress, Drupal, and other content management engines that utilize the whole LAMP stack.

In Ubuntu, the easiest way to install the whole LAMP stack is using “tasksel”.  from your server terminal type “sudo apt-get install tasksel and then type sudo apt-get update && sudo tasksel install lamp-server

enter a MySQL password when prompted:

image

After the installation is done. type the IP address of your server in the browser, and you should see that apache was successfully installed:

image

Setting up the VirtualHosts

The VirtualHosts files in Apache are saved on this path /etc/apache2/site-available/ you can either use FTP to upload the virtualhost file to this location, or you can create the file through the terminal. to create it on terminal use touch. for example sudo touch forevergeeks.com.conf and then sudo nano forevergeeks.com.conf to enter your virtualhost information. my forevergeeks.com.conf information looks like this:

<VirtualHost *:80>
        ServerAdmin youremailaddress.com
        ServerName forevergeeks.com
        ServerAlias www.forevergeeks.com

        DocumentRoot /var/www/forevergeeks
        <Directory />
                Options -Indexes
                AllowOverride All
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory “/usr/lib/cgi-bin”>
                AllowOverride All
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ “/usr/share/doc/”
    <Directory “/usr/share/doc/”>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

change the virtualhost information to reflect yours. also remember to change or create the directory for the “DocumentRoot” where the website files will be hosted.

To enable the VirtualHost type this:

sudo a2ensite nameofyourvirtualhost for example, to enable my virtualhost I’ll type this sudo a2ensite forevergeeks.com.conf and then reload apache sudo service apache2 reload

type the URL of your domain in the browser and you should get something similar to this now:

image

that means your virtualhost is now working. upload your web files to the “DocumentRoot” folder you created above to make your website go live!

Disable directory listing

to prevent directory listing on your Virtualhost, type this this on terminal sudo a2dismod autoindex and then reload apache to take effect.

Enable Mod_Rewrite

If your website was built with WordPress or any other content management software that allows you to setup custom permalinks, then you most likely will need to enable Mod Rewrite on the server. to do that type this on terminal sudo a2enmod rewrite and reload apache.

Repeat the same process for all the websites you want to host on this server. there is no limit on the amount of websites you can host on a single server, as long as you have the resources, RAM, CPU, Storage, bandwidth, etc, to do it.