Skip to main content

How To Configure Apache Virtual Hosts on CentOS 7

Introduction

Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server.

Requirements

  • A desktop machine running Linux
  • A static IP address.

Set Up

sudo yum update
sudo yum install httpd (for Centos )
sudo apt-get install httpd (for Ubuntu )
start Httpd service
/etc/init.d/httpd start ( Centos )
/etc/init.d/apache2 start  ( Ubuntu )

Create a New Directory

First,make a directory structure where we will keep the new website’s information.
mkdir -p /var/www/html/www.sam.com

Grant Permissions

chown -R apache:apache /var/www/html/www.sam.com\
chmod -R 755 /var/www/html

Create the Page

We need to create a new file called index.html within our configurations directory.
Vim /var/www/html/www.sam.com/index.html
Add the following content.
<html>
<head>
<title>www.sam.com</title>
</head>
<body>
<h1>The sam.com virtual host is working!</h1>
</body>
</html>
Save and close the file when you are finished.

Turn on Virtual Hosts

The next step is to enter into the apache configuration file itself.
sudo vi /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 80
<VirtualHost *:80>
ServerName www.sam.com
ServerAlias sam.com
DocumentRoot /var/www/html/www.sam.com
ErrorLog /var/www/html/www.sam.com/error.log
CustomLog /var/www/html/www.sam.com/access.log combined
</VirtualHost>
Save & Exit
When you are finished, You can check the syntax of files with the following command:
sudo apachectl configtest
After the syntax check is done, restart Apache to make these changes take effect.
sudo systemctl restart httpd

Setting Up the Local Hosts

Now, you need to create DNS record for www.vhost1.com to test the virtual hosts. One easy way is to add a “domain name to IP mapping” entry to the /etc/hosts file.
Add the following lines, using the static IP address of your server:
127.0.0.1  www.sam.com
Save and exit the file.
Now on the desktop Linux computer, open your web browser and go to the URLs
http://www.sam.com

Comments

Popular posts from this blog

As a symfony developer what you should take care when symfony standard edition is legacy:

Now  symfony standard edition is legacy . So no need to worry much just go threw article and understand whats new in symfony 4 if standard edition is legacy now. This is the main difference in Symfony 4 from symfony 2 and symfony 3 . Older symfony versions provide by default doctrine, twig, form, validation, profiler, annotations and many other components. But some time user do not want to use it. Suppose what if user do not want to doctrine or twig? Still he has doctrine and twig in his dependency. That makes symfony heavy framework. When starting a new project developers do not like to assemble things manually. So symfony 2 and 3 provides standard edition for developers to start a big project. But was hard to scale down project and decrease dependency if user do not require some packages. Symfony 2 and 3  are big success and in years developers are now aware with composer and dependency injection. So adding/removing a new package is not a big deal. So symfony chose...

Learn Angular 4 from scratch

Angular 4 is the latest version of AngularJs released in March 2017. The basic steps to accomplish our first Angular 4 hello world example. Here, we’ll simply create a component that passes the title from component to the view. 1. Install Angular CLI The Angular CLI is a command line interface that helps to scaffold and build angular apps in Node.js style. Also, it set up the angular 4 environments and will be helpful for us to work with angular apps like creating, running, etc. Now, install Angular-CLI. <code>npm install -g angular-cli</code> Once installation is done, just make sure that it’s installed by checking it’s version. <code>ng -v</code> 2. Install an Angular 4 Hello World App Here, we will create an app named hello-world ng new hello-world Now, go to the directory of a newly created project. cd hello-world Before running your app, let’s dive a bit deeper in coding behind the picture. 2.1 Fi...

3 Node.js Alternatives To WordPress

3 Node.js Alternatives To WordPress As compare to WordPress there is alternative option like Node.js. There are many various latest challengers that have large communities, support themes, plugins and are easy to install on your own server. Here are 3 you might want to checkout. KeystoneJS KeystoneJS is a content management system and framework to build server applications that interact with a database. It is based on the Express framework for Node.js and uses MongoDB for data storage. It represents a CMS alternative for web developers who want to build a data-driven website but don’t want to get into the PHP platform or large systems like WordPress. EnduroJS Enduro.js offers a fast and easy way to build your project, beautiful and simple admin UI and integrated tools to build everything on the server. It works without a database, instead relying on a system of flat files that can be opened directly in a text editor. It is easy to install and can be deployed via Git. ...