About Apache

Apache is the first HTTP server software to serve 100 million websites. It currently serves up to 39% of web sites on the internet. It has a modular structure for its functionality, with each part that describes an individual site called a virtual host. Virtual hosts makes it possible for one apache server to host several domains or pages.

The server works by serving files from a directory configured for each domain name. Such that site visitors have the contents of this directory presented to them, through a parallel process that serves the same for every domain on the server. This can be done for as many domains as needed, depending on the server's resource limits and computing power.

Here we will go through, how to set up Apache virtual hosts for CentOS 7.

'

Setting up Apache

To get up and running, we will need the following prerequisites :

» A linux server plan having CentOS 7

» A non-root user having sudo privileges to gain admin rights.

To install apache, use yum with the following commands.

 sudo yum -y install httpd 

Then set up apache to work as a service in CentOS and work by it self after system reboot.

 sudo systemctl enable httpd.service 

Step 1: Setting up the directory

The document root for all the web files in each virtual host is set up at:

 /var/www 

Within the document root we create sub folders each representing a domain and then a public_html directory within each domain directory, that will hold the application files.

We can create this nested directories with the command mkdir -p

 
sudo mkdir -p /var/www/site.com/public_html
sudo mkdir -p /var/www/site2.com/public_html
 

Step 2: Giving them permissions

Now we change the file permissions for the directories, so they are owned by the regular user and not root. This is done with the chown command, enabling the user to change files in his own web directories.

 
sudo chown -R $USER:$USER /var/www/site.com/public_html
sudo chown -R $USER:$USER /var/www/site2.com/public_html
 

The $USER being the user name with which you are currently logged in. This makes the regular user now own the public_html directories where its web content would be stored.

The document root's permissions are also amended to have the appropriate read, write and excute permissions.

 
sudo chmod -R 755 /var/www

Step 3:Setting up sample pages for each Virtual Host

Web contents are served from the public_html folder, in each domain's directory. The default file served, being the index.html file, when a visitor browses the domain. Such that when a visitor opens site.com in browser, the index.html file in its public_html directory is served.

We can start by working on this file using a Vim editor, with the following command :

 
vi /var/www/site.com/public_html/index.html

We will edit the HTML content of the file, to show that our Apache setup is working. Its content should look like this :

 

< html >
  < head >
    < title >Welcome to Site.com!< title >
  
  < body >
    < h1 >Great stuff! The site.com virtual host is working!< h1 >
  < body >
< html >


Save the file and quit when finnished, by pressing the escape key followed by :wq and the enter key.

For this demo, the same index.html file, can be copied into the second virtual host's public_html directory, as a template for its default page.

 
cp /var/www/site.com/public_html/index.html /var/www/site2.com/public_html/index.html

To edit the second webpage, we fire up the VI editor the second time.

 
vi /var/www/site2.com/public_html/index.html

 

< html >
  < head >
    < title >Welcome to Site2.com!< title >
  
  < body >
    < h1 >Great stuff! The site2.com virtual host is working!< h1 >
  < body >
< html >


Save the file and quit when finnished, by pressing the escape key followed by :wq and the enter key.

With these two files, we can test the virtual host configuration once finnished.

Step 4: Setting up the Virtual Hosts

The virtual host is where the site's configurations are set up, such that Apache would handle requests to each domain, based on these configurations.

The directory named sites-available will be used for all the Vhost files and the one named sites-enabled symbolic links to each Vhost we create. These two folders can be created with the commands :

 
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled

We then configure Apache to query the sites-enabled directory to get the virtual hosts. This is done through its httpd.conf file configuration. This is done with the following command :

 
sudo vi /etc/httpd/conf/httpd.conf

Append the following code to the end of the httpd.conf file.

 
IncludeOptional sites-enabled/*.conf

With that we can create the Vhost for site.com

Step 5: The first Virtual Host

We start by creating a Vhost configuration file within the sites-availble directory for each domain

 
sudo nano /etc/httpd/sites-available/site.com.conf

The configuration is opened with a pair of content tags named VirtualHost with a designated port for listening to incomming requests.

 

< VirtualHost *:80 >

< VirtualHost >


Following we will insert the main server name as the domain name , www.site.com. Then an alias, for the server name that also resolves to site.com. Such that requests to www.site.com and those to site.com serve the same content.

 

< VirtualHost *:80 >
    ServerName www.site.com
    ServerAlias site.com
< VirtualHost >


Also, note that an A record or CNAME which resolves www requests to the server's IP, is necessary for the www alias of the damin to resolve well. A wild card (*) can also be used for this purpose, making requests such as http://site.com, https://site.com and http://www.site.com to resolve in the right way.

 
< VirtualHost *:80 >
 ServerName www.site.com
    ServerAlias site.com
    DocumentRoot /var/www/site.com/public_html
    ErrorLog /var/www/site.com/error.log
    CustomLog /var/www/site.com/requests.log combined
< VirtualHost >

With this we can save, close the file and quit, when finnished, by pressing the escape key followed by :wq and the enter key.

Then copy the configurations for site.com to site2.com

 
sudo cp /etc/httpd/sites-available/site.com.conf /etc/httpd/sites-available/site2.com.conf

Then customise site2.com.conf to present the second domain name.

 
sudo vi /etc/httpd/sites-available/site2.com.conf

The customised file should look like this :

 

< VirtualHost *:80 >
 ServerName www.site2.com
    ServerAlias site2.com
    DocumentRoot /var/www/site2.com/public_html
    ErrorLog /var/www/site2.com/error.log
    CustomLog /var/www/site2.com/requests.log combined
< VirtualHost >


Step 6: Activate the Virtual Hosts

To do this we create a symbolic link for each virtual host in the sites-enabled folder. To help Apache to be aware that they need to be served to visitors.

 
sudo ln -s /etc/httpd/sites-available/site.com.conf /etc/httpd/sites-enabled/site.com.conf
sudo ln -s /etc/httpd/sites-available/site2.com.conf /etc/httpd/sites-enabled/site2.com.conf

After this restart Apache so the changes you have made can work.

 
sudo apachectl restart

You can also make changes to your host file by typing:
 
sudo vi /etc/hosts

Then you include your server IP addresses next to each domain name


127.0.0.1   localhost
127.0.1.1   guest-desktop
server_ip_address example.com
server_ip_address example2.com

Step 7: Test is out!

To test your new configurations, simply visit your new domain using a browser.

 
http://site.com

You should see a page that looks like this:

Great stuff! The site.com virtual host is working!

The same should be the case for site2.com, if all the configutration was well done.

Conclusion

With this you can have your CentOS 7 server handling different virtual hosts, with each one serving multiple pages. The Apache server has no limits to the number of domains it can serve through its virtual host. As long as your machine has enough CPU and RAM for the job.

Phanerus Banner