Category Archives: Uncategorized

Step by Step procedure to install and Configure ANSIBLE on Ubuntu

Step by step procedure to install and configure ansible on ubuntu

Before installing ANSIBLE, let us understand,

What is ANSIBLE ?

1.Ansible Introduction :

Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero
downtime rolling updates.
Ansible’s goals are foremost those of simplicity and maximum ease of use. It also has a strong focus on security and reliability, featuring a minimum of moving parts, usage of OpenSSH for transport (with an accelerated socket mode and pull modes as alternatives),
and a language that is designed around auditability by humans – even those not familiar with the program.

While there are many popular configuration management systems available for Linux systems, such as Chef and Puppet, these are often more complex than many people want or need.Ansible is a great alternative to these options because it has a much smaller overhead to get started.

for more information visit, Ansible docs.
http://docs.ansible.com/

Assumptions
● You have a ubuntu 12.04 and you are logged-in as root.

2.How Does Ansible Work :
Ansible works by configuring client machines from an computer with Ansible components
installed and configured.
It communicates over normal SSH channels in order to retrieve information from remote
machines, issue commands, and copy files. Because of this, an Ansible system does not require any additional software to be installed on the client computers.
This is one way that Ansible simplifies the administration of servers.
Configuration files are mainly written in the YAML data  format due to its
expressive nature and its similarity to popular markup languages.Ansible can interact with clients through either command line tools or through its configuration scripts called Playbooks.

3. Install Ansible on an Ubuntu 12.04 :
To begin exploring Ansible , we need to install the Ansible package on at least one machine. I have used ubuntu 12.04

git clone git@github.com:ansible/ansible.git
cd ansible
source ./hacking/env-setup
sudo pip install paramiko PyYAML jinja2 --upgrade

We now have all of the software required to administer our servers through Ansible.
Set Up SSH Keys , Ansible primarily communicates with client computers through SSH.While it certainly has the ability to handle password-based SSH authentication, SSH keys help keep things simple.

4. Creating a SSH Pair :
@Server:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory ‘/home/a/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
Now use ssh to create a directory ~/.ssh as user b on Client. (The directory may already exist,
which is fine):
@Server:~> ssh root@123.123.123.123 mkdir -p .ssh
root@Client’s password:
Finally append a’s new public key to root@123.123.123.123:.ssh/authorized_keys and enter
client’s password one last time:
root@Server:~> cat .ssh/id_rsa.pub | ssh root@123.123.123.123 ‘cat >> .ssh/authorized_keys’
root@Client’s password:
From now on you can log into Client as root from A as a without password:
@Server:~> ssh root@123.123.123.123

5. Configuring Ansible Host:
Ansible keeps record of all of the servers that it know through a “hosts” file. We need to configure this file first before we can begin to communicate with our other computers.
Open the file and add,

vim /etc/ansible/hosts
[hosts]
devops ansible_ssh_host=123.123.123.123

Where hosts is list of nodes
devops = host name of one of the client.
123.123.123.123 is hosts ip.
To do this, we will create a directory in the Ansible configuration structure called group_vars.
Within this folder, we can create YAML-formatted files for each group we want to configure:

sudo mkdir /etc/ansible/group_vars
sudo nano /etc/ansible/group_vars/hosts

We can put our configuration in here. YAML files start with “—“, so make sure you don’t forget that part.

ansible_ssh_user: root
Save and close this file when you are finished.

6. Ansible test Examples:

Using Simple Ansible Commands
Now that we have our hosts set up and enough configuration details to allow us to
successfully connect to our hosts, we can try out our very first command.
Ping all of the servers you configured by typing:
root@server:/etc/ansible# ansible -m ping hosts
devops | success >> {
“changed”: false,
“ping”: “pong”
}
This is a basic test to make sure that Ansible has a connection to all of its hosts.

ansible -m shell -a ‘ifconfig’ devops

devops | success | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0c:29:a2:42:c4
inet addr:123.123.123.123 Bcast:10.27.111.255 Mask:255.255.255.0

ansible -m shell -a ‘free -m’ devops
devops | success | rc=0 >>
total used free shared buffers cached
Mem: 487 440 47 0 108 173
-/+ buffers/cache: 158 329
Swap: 507 66 441

Lets install sendmail on client from Ansible server, to do that create playbook.yml file in
/etc/ansible 100 Jul 5 02:48 playbook.yml

root@server:/etc/ansible# cat playbook.yml

– hosts: devops
tasks:
– name: 1. install sendmail
apt: name=sendmail state=present

Now execute below command to install on remote server.
ansible-playbook –inventory-file=hosts playbook.yml –sudo –verbose
Below is the result,

ansible 1

Before executing above command, sendmail was not installed. ansible 2

Later after execution, sendmail installed on the remote host.

Advantages of Automation,

,Automation-Advantages

Hence we have Installed and Configured ANSIBLE, Enjoy !!!

Marketing Mix

Definition of Marketing Mix
According to Philip Kotler – Marketing Mix is the combination of four elements, called the 4P’s (product, Price, Promotion, and Place), that every company has the option of adding, subtracting, or modifying in order to create a desired marketing strategy”
According to Principles of Marketing, 14e, Kotler and Armstrong, 2012 – The Marketing Mix is the set of tactical marketing tools – Product, Price, Promotion, and Place – that the firm blends to produce the response it wants in the target market.”
marketingmixdiagram4c-vs-4p

Shell Script : Basic mathematics

#!/bin/bash
# SCRIPT: calc.sh
# PURPOSE: Addition, Subtraction, Division and Multiplication of
# two numbers.
#
#####################################################################
# Variable Declaration #
#####################################################################

clear #Clears Screen

Bold=”\033[1m” #Storing escape sequences in a variable.
Normal=”\033[0m”

echo “$Bold Basic mathematics using bash script $Normal\n”

items=”1. ADDITTION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT”

choice=

#####################################################################
# Functions Starts from here #
#####################################################################
exit_function()
{
clear
exit
}

#Function enter is used to go back to menu and clears screen

enter()
{
unset num1 num2
ans=
echo “”
echo -e “Do you want to continue(y/n):\c”
stty -icanon min 0 time 0
while [ -z “$ans” ]
do
read ans
done

#The while loop ensures that so long as at least one character is

if [ “$ans” = “y” -o “$ans” = “Y” ]
then
stty sane # Restoring terminal settings
clear
else
stty sane
exit_function
fi
}

#####################################################################
# CALC Script MENU #
#####################################################################

while true
do
echo “$Bold \tWelcome to Waseem’s Tutorial $Normal\n”
echo “\t$items \n”
echo -n “Enter your choice : ”
read choice

case $choice in
1) clear
echo “Enter two numbers for Addition : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
echo “$num1 + $num2 = `expr $num1 + $num2`”
;;
2) clear
echo “Enter two numbers for Subtraction : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
echo “$num1 – $num2 = $((num1-num2))”
enter ;;
3) clear
echo “Enter two numbers for Multiplication : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
echo “$num1 * $num2 = `echo “$num1*$num2″|bc`”
enter ;;
4) clear
echo “Enter two numbers for Division : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
let div=num1/num2
echo “$num1 / $num2 = $div”
enter ;;
5) exit_function ;;
*) echo “You entered wrong option, Please enter 1,2,3,4 or 5″
echo “Press enter to continue”
read
clear
esac

done

Output:-

Capture

Perform SCP Without Entering Password

In this article, I’ll explain how to perform scp without entering the password using the SSH Public Key authentication. There will be requirements like you would like to transfer files from one server to another with scripts and run it as cronjob. There are two levels of security in the SSH key based authentication. In order for you to login, you need both the private key and the passphrase. Even if one of them is compromised, attacker still cannot login to your account, as both of them are needed to login. This is far better than typical password based authentication, where if the password is compromised, attacker can gain access to the system.

There are two ways to perform ssh and scp without entering the password:

  1. No passphrase. While creating key pair, leave the passphrase empty. Use this option for the automated batch processing. for e.g. if you are running a cron job to copy files between machines this is suitable option.
  2. Use passphrase and SSH Agent. If you are using ssh and scp interactively from the command-line and you don’t want to use the password everytime you perform ssh or scp, I don’t recommend the previous option (no passphrase), as you’ve eliminated one level of security in the ssh key based authentication. Instead, use the passphrase while creating the key pair and use SSH Agent to perform ssh and scp without having to enter the password everytime as explained in the steps below.

Step:1. Verify that local-host and remote-host is running openSSH

[local-host]$ ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006

[remote-host]$ ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006

Step:2. Generate key-pair on the local-host using ssh-keygen

[local-host]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/waseem/.ssh/id_rsa):<Hit enter>
Enter passphrase (empty for no passphrase): <Enter your passphrase here>
Enter same passphrase again:<Enter your passphrase again>
Your identification has been saved in /home/waseem/.ssh/id_rsa.
Your public key has been saved in /home/waseem/.ssh/id_rsa.pub.
The key fingerprint is:

The public key and private key are typically stored in .ssh folder under your home directory. In this example, it is under /home/waseem/.sshd. You should not share the private key with anybody.

Step:3. Install public key on the remote-host.

Copy the content of the public key from the local-host and paste it to the /home/waseem/.ssh/authorized_keys on the remote-host. If the /home/waseem/.ssh/authorized_keys already has some other public key, you can append this to the end of it. If the .ssh directory under your home directory on remote-host doesn’t exist, please create it.
[remote-host]$ vi ~/.ssh/authorized_keys 

In simple words, copy the local-host:/home/waseem/.ssh/id_rsa.pub to remote-host:/home/jsmith/.ssh/authorized_keys

Step:4. Give appropriate permission to the .ssh directory on the remote-host.

[remote-host]$ chmod 755 ~/.ssh
[remote-host]$ chmod 644 ~/.ssh/authorized_keys

Step:5. Login from the local-host to remote-host using the SSH key

[local-host]$ <You are on local-host here>

[local-host]$ ssh -l waseem remote-host
Last login: Sat Jun 07 2008 23:03:04 -0700 [remote-host]$ <You are on remote-host here>

Step:6 Verify SCP working without password from local server

scp testscpwithoutpassword.csv  waseem@192.168.1.1:/home/waseem

Chapter 2 : Essential Linux Commands : Find

Find Command

find is frequently used command to find files in the UNIX filesystem
based on numerous conditions. Let us review some practice examples of find command.

Syntax: find [pathnames] [conditions]

#How to find files containing a specific word in its name?
The following command looks for all the files under /etc directory with mail in the filename.

# find /etc -name “*mail*”

How to find all the files greater than certain size?
The following command will list all the files in the system greater than
100MB.
# find / -type f -size +100M

How to find files that are not modified in the last x
number of days?
The following command will list all the files that were modified more
than 60 days ago under the current directory.
# find . -mtime +60

How to find files that are modified in the last x number of
days?
The following command will list all the files that were modified in the last
two days under the current directory.
# find . –mtime -2
How to delete all the archive files with extension *.tar.gz
and greater than 100MB?

# find / -type f -name *.tar.gz -size +100M -exec ls -l {} \;
# find / -type f -name *.tar.gz -size +100M -exec rm -f {} \;

How to archive all the files that are not modified in the
last x number of days?
The following command finds all the files not modified in the last 60
days under /home/jsmith directory and creates an archive files under
/tmp in the format of ddmmyyyy_archive.tar.
# find /home/jsmith -type f -mtime +60 | xargs tar -cvf
/tmp/`date ‘+%d%m%Y’_archive.tar`

Chapter 1 : Essential Linux Commands : Grep

Chapter 1 : Essential Linux Commands : Grep

Grep Command:

grep command is used to search files for a specific text. This is
incredibly powerful command with lots of options.
Syntax:

grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName

How can I find all lines matching a specific keyword on a
file?
In this example, grep looks for the text John inside /etc/passwd file and
displays all the matching lines.

# grep John /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash
Option -v, will display all the lines except the match. In the example
below, it displays all the records from /etc/password that doesn’t match
John.
Note: There are several lines in the /etc/password that doesn’t contain
the word John. Only the first line of the output is shown below.

# grep -v John /etc/passwd
jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash

How many lines matched the text pattern in a particular
file?
In the example below, it displays the total number of lines that contains
the text John in /etc/passwd file.

# grep -c John /etc/passwd
2
You can also get the total number of lines that did not match the specific
pattern by passing option -cv.

# grep -cv John /etc/passwd
39

How to search a text by ignoring the case?
Pass the option -i (ignore case), which will ignore the case while
searching.

# grep -i john /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash
How do I search all subdirectories for a text matching a
specific pattern?
Use option -r (recursive) for this purpose. In the example below, it will
search for the text “John” by ignoring the case inside all the
subdirectories under /home/users.
This will display the output in the format of “filename: line that matching
the pattern”. You can also pass the option -l, which will display only the
name of the file that matches the pattern.

grep “kernel: *.” *
messages.4:Jul 12 17:01:02 cloneme kernel: ACPI: PCI
interrupt for device 0000:00:11.0 disabled
messages.4:Oct 28 06:29:49 cloneme kernel: ACPI: PM-Timer
IO Port: 0x1008
messages.4:Oct 28 06:31:06 btovm871 kernel: sda: sda1
sda2 sda3
messages.4:Oct 28 06:31:06 btovm871 kernel: sd 0:0:0:0:
Attached scsi disk sda
.
.
In the above example it matches for kernel and colon symbol followed
by any number of spaces/no space and “.” matches any single
character.

# grep -ri john /home/users
/home/users/subdir1/letter.txt:John, Thanks for your
contribution.
/home/users/name_list.txt:John Smith
/home/users/name_list.txt:John Doe

# grep -ril john /root

/home/users/subdir1/letter.txt
/home/users/name_list.txt

Regular Expression in Grep
Regular expressions are used to search and manipulate the text, based
on the patterns. Most of the Linux commands and programming
languages use regular expression.
This hack explains how to use most frequently used reg-ex operators in
Grep command.
Beginning of line ( ^ )
In grep command, caret Symbol ^ matches the expression at the start of
a line. In the following example, it displays all the line which starts with
the Nov 10. i.e All the messages logged on November 10.
$ grep “^Nov 10″ messages.1
Nov 10 01:12:55 gs123 ntpd[2241]: time reset +0.177479 s
Nov 10 01:17:17 gs123 ntpd[2241]: synchronized to
LOCAL(0), stratum 10
Nov 10 01:18:49 gs123 ntpd[2241]: synchronized to
15.1.13.13, stratum 3
Nov 10 13:21:26 gs123 ntpd[2241]: time reset +0.146664 s

Nov 10 13:25:46 gs123 ntpd[2241]: synchronized to
LOCAL(0), stratum 10
Nov 10 13:26:27 gs123 ntpd[2241]: synchronized to
15.1.13.13, stratum 3
The ^ matches the expression in the beginning of a line, only if it is the
first character in a regular expression. ^N matches line beginning with
N.
End of the line ( $)
Character $ matches the expression at the end of a line. The following
command will help you to get all the lines which ends with the word
“terminating”.
$ grep “terminating.$” messages
Jul 12 17:01:09 cloneme kernel: Kernel log daemon
terminating.
Oct 28 06:29:54 cloneme kernel: Kernel log daemon
terminating.
From the above output you can come to know when all the kernel log
has got terminated. Just like ^ matches the beginning of the line only if
it is the first character, $ matches the end of the line only if it is the last
character in a regular expression.
Count of empty lines ( ^$ )
Using ^ and $ character you can find out the empty lines available in a
file. “^$” specifies empty line.
$ grep -c “^$” messages anaconda.log
messages:0
anaconda.log:3
The above commands displays the count of the empty lines available in
the messages and anaconda.log files.

Single Character (.)
The special meta-character “.” (dot) matches any character except the
end of the line character. Let us take the input file which has the content
as follows.
$ cat input
1. first line
2. hi hello
3. hi zello how are you
4. cello
5. aello
6. eello
7. last line
Now let us search for a word which has any single character followed by
ello. i.e hello, cello etc.,
$ grep “.ello” input
2. hi hello
3. hi zello how are you
4. cello
5. aello
6. eello
In case if you want to search for a word which has only 4 character you
can give grep -w “….” where single dot represents any single character.
Zero or more occurrence (*)
The special character “*” matches zero or more occurrence of the
previous character. For example, the pattern ’1*’ matches zero or more
’1′.
The following example searches for a pattern “kernel: *” i.e kernel: and
zero or more occurrence of space character.

grep “kernel: *.” *
messages.4:Jul 12 17:01:02 cloneme kernel: ACPI: PCI
interrupt for device 0000:00:11.0 disabled
messages.4:Oct 28 06:29:49 cloneme kernel: ACPI: PM-Timer
IO Port: 0x1008
messages.4:Oct 28 06:31:06 btovm871 kernel: sda: sda1
sda2 sda3
messages.4:Oct 28 06:31:06 btovm871 kernel: sd 0:0:0:0:
Attached scsi disk sda
.
.
In the above example it matches for kernel and colon symbol followed
by any number of spaces/no space and “.” matches any single
character.

Installing Apache and PHP from source

I’ve just been through the process of installing the latest versions (as of this writing) of Apache 2.4.2 and PHP 5.4.5 from source on a new Debian 6 virtual machine for the first time.

I had some small issues along the way and thought I’d do a short post on the process I went through to get it working and resolve various errors which may help someone out. First we will install Apache, followed by PHP.

Prerequisites

Before starting you will need make, gcc and g++ installed.

apt-get install make gcc g++
I also had the latest version of MySQL installed (5.5.25a), as I wanted to use this later on. The latest version was downloaded from the Dotdeb repository

Installing Apache 2.4.2 from source

Apache was then downloaded from http://httpd.apache.org/download using wget on one of the mirrors in my country and then extracted.

wget http://apache.mirror.uber.com.au//httpd/httpd-2.4.2.tar.gz
tar xf httpd-2.4.2.tar.gz
cd httpd-2.4.2/
./configure –enable-so

Configuring Apache Portable Runtime library …
checking for APR… no
configure: error: APR not found. Please read the documentation.
–enable-so is needed later on for PHP.

At this point I downloaded APR 1.4.6 from apr.apache.org/download using wget and then extracted it.

cd ..
wget http://apache.mirror.uber.com.au//apr/apr-1.4.6.tar.gz
tar xf apr-1.4.6.tar.gz
cd apr-1.4.6/
./configure
make
make install
Apache will also need APR-util installed, which can be downloaded from the apr.apache.org/download page too. When you run configure you will have to specify where the apr config is, you should see this location in the output from make install after finishing apr above.

cd ..
wget http://apache.mirror.uber.com.au//apr/apr-util-1.4.1.tar.gz
tar xf apr-util-1.4.1.tar.gz
cd apr-util-1.4.1/
./configure –with-apr=/usr/local/apr/bin/apr-1-config
make
make install
Now that’s complete we can try Apache again.

cd ../httpd-2.4.2/
./configure –enable-so

checking for pcre-config… false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
pcre-8.30.tar.gz was then downloaded from http://pcre.org and for something different, I uploaded it to the server with SFTP to the root directory.

cd ..
tar xf pcre-8.30.tar.gz
cd pcre-8.30/
./configure
make
make install
Now to try Apache again…

cd ../httpd-2.4.2/
./configure –enable-so
make
make install
That’s it, Apache should now be ready to go, let’s move into the default installation and start it up.

cd /usr/local/apache2/bin/
./apachectl start
/usr/local/apache2/bin/httpd: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
ldconfig
./apachectl start
./apachectl -v
Server version: Apache/2.4.2 (Unix)
Server built: Jul 26 2012 20:51:23
That error relating to the shared libraries was resolved by running ldconfig, see the man page on that for more information.

Browsing to the IP address in my web browser now brings up the Apache default page. As ServerSignature is enabled by default, using Firebug for FireFox I can see “Apache/2.4.2 (Unix)” as the Server header in the response headers, confirming the new install of Apache has served my page.

Installing PHP 5.4.5 from source

PHP was downloaded from http://au.php.net/get/php-5.4.5.tar.gz/from/a/mirror and then uploaded to the root directory on the server using SFTP.

I’m configuring PHP with apxs, this wasn’t working for me unless Apache was configured with –enable-so. I was after MySQL support, feel free to leave –with-mysql off.

cd /root
tar xf php-5.4.5.tar.gz
cd php-5.4.5/
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=mysqlnd

configure: error: xml2-config not found. Please check your libxml2 installation.
apt-get install libxml2-dev
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=mysqlnd
make
make install
php -v
PHP 5.4.5 (cli) (built: Jul 26 2012 21:18:58)
Configuration

Once you have both Apache and PHP installed and running there is a bit of configuration to do so that Apache knows how to use PHP. I created a phpinfo file here /usr/local/apache2/htdocs/phpinfo.php and it will not display in a browser prior to configuration.

First off, let’s copy the library file over to Apache.

cp /root/php-5.4.5/libs/libphp5.so /usr/local/apache2/modules/
Next you will want to edit the Apache configuration file in /usr/local/apache2/conf/httpd.conf and add the following lines into it.

AddType text/html .php
AddHandler php5-script .php
This line should already be in there, referring to the .so file that was previously copied.

LoadModule php5_module modules/libphp5.so
You probably want to edit DirectoryIndex index.html and include index.php after this.

Once complete, restart Apache.

cd /usr/local/apache2/bin/
./apachectl restart

Configure apache to use virtual hosts on ubuntu server

This tutorial assumes you have Apache setup correctly with a default config.
In this tutorial we will be using just one domain name (domain.com) replace this with your domain anytime you see it. at the end will be instruction on how to add your second, third, fourth, etc…

Making A Uniform Directory Structure

First we need to create our directory structure, you can do this however you like but I recommend following some sort of naming standard to avoid confusion.
cd /var/www
mkdir -p vhosts/domain.com/htdocs (this is where you will put your html.)
mkdir -p vhosts/domain.com/log (this will be for your log files)
chmod -R 755 vhosts/domain.com
[edit]Making A Default Page

The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.
So for the domain create an index.html file:
cd vhosts/domain.com/htdocs
nano index.html
add the following into the file:



domain.com


OK. Now we have the basic structure needed for our domains we can look at defining one virtual host.
[edit]Configuring NameVirtualHost

With virtual hosts, one thing that often catches people out is the NameVirtualHost setting.
For each port that Apache listens to, we need to define a NameVirtualHost. The issue that can catch people lies in the fact that you can only define it once per port.
In itself, that’s no problem but the default vhost has already defined a generic NameVirtualHost – adding another one will cause warnings and errors.
The easiest thing to do is to remove the generic NameVirtualHost from the default vhost file and then add some specific ones to the main apache config file.
That way, if we ever delete or change the default vhost we don’t have to worry about where the NameVirtualHost setting is defined and so on.
OK. Let’s move into the apache directory and then open the default vhost:
cd /etc/apache2/

sudo nano sites-available/default
Delete the ‘NameVirtualHost *’ line and change the next ‘VirtualHost’ line so the file begins like this:

ServerAdmin webmaster@localhost

DocumentRoot /var/www/

Once that is done, we can open the main apache2.conf:
sudo nano apache2.conf
Make sure the following line is in the file and not commented out:
Include /etc/apache2/sites-enabled/
Also make sure the following is in the file:
NameVirtualHost *:80


NameVirtualHost *:443

If the above code is not in apache2.conf, I found that it would cause errors when the second vhost was enabled.
Let’s reload Apache now:
sudo /etc/init.d/apache2 reload
[edit]Making A Custom Virtual Host

We’ve set up the basics and now we’re ready to add our own virtual hosts so we can start to serve our domains.
Let’s go ahead and create the vhost file for domain.com:
nano /etc/apache2/sites-available/domain.com
The contents need to look like this:
# Place any notes or comments you have here
# It will make any customisation easier to understand in the weeks to come

# domain: domain1.com
# public: /var/www/vhosts/domain.com/domain.com/

# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster@domain.com
ServerName domain.com
ServerAlias www.domain.com

# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /var/www/vhosts/domain.com/htdocs

# Custom log file locations
LogLevel warn
ErrorLog /var/www/vhosts/domain.com/log/error.log
CustomLog /var/www/vhosts/domain.com/log/access.log combined


[edit]Using a2ensite

Now we have the site available, we need to enable it:
sudo a2ensite domain.com
The result should be:
Site domain.com installed; run /etc/init.d/apache2 reload to enable.
This does seem like good advice so:
/etc/init.d/apache2 reload
[edit]Test And Navigate

Now navigate to your site:
http://domain.com
[edit]Rinse and Repeat

To create and enable domain2.com simply go through the process again:
nano /etc/apache2/sites-available/domain2.com

Enter the details for domain2.com as per the example shown above
Then enable the site and restart Apache:
sudo a2ensite domain2.com

sudo /etc/init.d/apache2 reload
Finally navigate to your second domain!