I was once a xampp users when I used to work on windows machine. Slowly shifted to linux machine with ubuntu. It’s quite overwhelming in the shift from windows to linux. Everything happens in just a click in windows but the fun part in linux is you get to play with everything going around.
Let’s learn to setup a webserver using apache and configure virtualhost on it.
(in ubuntu use)
sudo apt-get install apache2
(in centos use)
sudo yum install httpd
After successfully installing apache start the apache service. There are two ways you can start it.
sudo service apache2 start
I love these second one by getting into init.d when I am running the apache on docker container.
The location might be quite different based on the apache version you are using and the linux destro type.
Run to test:
when you first start your apache server it will show you kind of some warning message and the ip address. The IP address shown is the ip address where the apache will be running. Later we will see in details about the hosts names and all for now we will use that ip-address to see if the apache is running. On successful run the apache default page will shown which comes from this default location.
/var/www/html. Take it as the htdocs folder in xampp or www in wampp clear huh !
Some virtual host understanding
Up to now what we have accomplished is successfully run a apache web server. Also we served one website in /var/www/html file locations. What if we want to host or server other website too in that same web server. we are not going to create another webserver but we will utilize to run multiple websites in the same web server with different ip address or with different hostname but running in same ip address.
From windows users prospective who are familiar with xampp. It will be like adding multiple website inside the htdocs folder separated by folders that is what we do in xampp. But here we will use the concept of virtual hosting. It’s like creating a multiple webserver running a separate website. The reality is we are running a single apache webserver, clear huh !
Lets get into the setting parts of virtual host
The default page that got loaded when we run the default host was the default virtual host that gets run when no other virtual host matching the url are found.
- Name based virtual host:
The trick is we are running in the same ip address but the locations is different. ie
create a two directory in /var/www/html to serve as different separate website
Now we will setup the apache config file to serve both as a virtual host running in same ip address. that we will configure in /etc/hosts when working locally.
defining 2 virtual hosts in /etc/apache2/apache2.conf
This is how we define the virtual hosts,
setting the virtualhost name for us here we have vhost1.com and vhos2.com. When you try to browse it it wont open because these are the hostname we generated to use locally ,they are not globally available domain so to make it available within our system we add it to /etc/hosts. Add this line of code to the /etc/hosts
ip-address(of the localmachine) vhost1.com vhost2.com
Now when user tries to browse vhost1.com the virtual host setting will run the file in /var/www/html/vhost1 similarly for the vhost2.com.
These is the easy way to create a virtual hosting in apache.
2. IP based virtual host:
Another way to create a virtual host will be to give a separate ip address and a separate root document. Unlike what we did previously of assigning a different document root under same IP address.
This is how you can easily create as many virtual host as you want and run your application out there. These was just the basic understanding you would required to get started with apache and virtualhost setup. If there is anything you would like to know or I might be missing than drop me your message. Thanks