Ubuntu 16.04 LTS – How To Install Apache and Set Up Virtual HOST

Apache is the most popular web server on the internet. It is used to serve more than half of all active websites. Although there are many viable web servers that will serve your content, it is helpful to understand how Apache works because of its ubiquity. In this article, we will examine some general configuration files and options that can be controlled within them. I’ll be logged in as root.


Setup apache2 and enable virtual host.


Step 1 – Update repositories.

root@mail:/# apt-get update
root@mail:/# apt-get upgrade

Step 2 – Install apache2

root@mail:/# apt-get install apache2

Step 3 – Your site directory is located in /var/www/html/. If you want you can change it. Now if you write the ip address on your server in browser you will see this:

Configuration of Virtual HOSTS

Step 4 – First – I will create folder where I will put site content. I will give full permission on these folders.

root@mail:/# mkdir -p /var/www/html/mydomain.com
root@mail:/# mkdir -p /var/www/html/myseconddomain.com
root@mail:/# chmod 777 -R /var/www/html/

Step 5 – Second – I will create ‘conf’ file for each sites(domains). Execute the following commands for creation of virtual hosts. You just have to change mydomain.com and myseconddomain.com with your own.

root@mail:/# cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mydomain.com.conf
root@mail:/# cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myseconddomain.com.conf

Step 6 – Тhird – I will configure mydomain.com. You have to change in config file the route for your site. Open config file with this command nano /etc/apache2/sites-available/mydomain.com.conf and make changes. Your configuration have to look like this:

<VirtualHost *:80>
#       ServerAdmin webmaster@localhost
        ServerName mydomain.com
        ServerAlias www.mydomain.com
        DocumentRoot /var/www/html/mydomain.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 6 – Fourth – I will configure myseconddomain.com. You have to change in config file the route for your site. Open config file with this command nano /etc/apache2/sites-available/myseconddomain.com.conf and make changes. For example I will also change port 80 to port 8888. If you want to open this domain you have to write in your browser www.myseconddomain.com:8888. Your configuration have to look like this:

Listen 8888
<VirtualHost *:8888>
#       ServerAdmin webmaster@localhost
        ServerName myseconddomain.com
        ServerAlias www.mysecondDomain.com
        DocumentRoot /var/www/html/myseconddomain.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 7 – Fifth – enable each of these sites.

root@mail:/# a2ensite mydomain.com.conf
root@mail:/# a2ensite myseconddomain.com.conf

Step 8 – Sixth – For the changes to take effect you have to restart apache2.

root@mail:/# service apache2 reload

Step 9 – Seventh – Copy index.html file and make changes for the testing. For the second domain myecondomain.com open index.html file and write “My second domain”.

root@mail:/# cp /var/www/html/index.html /var/www/html/myseconddomain.com/index.htnl
root@mail:/# echo “My second domain.” > /var/www/html/myseconddomain.com/index.html

Step 10 – Eighth – Reload apache2 again

root@mail:/# service apache2 reload

Step 11 – Nineth – Write in browser the ip address of your server and you will see index.html from here /var/www/html/index.html.

Step 12 – Tenth – Write in browser the ip address of your server with port 8888 and you will see index.html from here /var/www/html/myseconddomain/index.html. For exapmple 192.168.0.222:8888.

DNS Configuration

This is very important for your Domain.

1. You must have DNS record for each of your sites in your domain provider.

2. If your hosting is in your home you have to open and redirect the ports which you will use. But if you don’t use router before your server and your server has real ip address you don’t have to do it. You just protect your server from attacks with the right configuration of firewall.

3. If your hosting server is in internal network and you want to open your site from internal network you must have configured static dns in your router. You also can make static dns record in your server.

I’ll show you how to make this in the following themes.

11 ThemeHow to install php,phpmyadmin and mysql on Ubuntu (Server) 16.04 LTS Step-by-Step

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website