Setting Up Apache Virtual Hosts on Ubuntu

It is very easy just read out this article.
I have explained in very simple lines.
Follow each steps.

You need to have Apache installed in order to work through these steps. If you haven’t already done so, you can get Apache installed on your server through apt-get:

Open terminal and type this to install Apache
sudo apt-get update
sudo apt-get install apache2

After this change directory to sites-available
just type the following command

Step 1

mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:~$ cd /etc/apache2/sites-available/
mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:/etc/apache2/sites-available$ ll

cd /etc/apache2/sites-available

if u want to see how may virtual hosts are there just type ll
ll means list all the files in pwd. It will list like this

-rw-r–r– 1 root root 1332 Jan 7 2014 000-default.conf
-rw-r–r– 1 root root 6437 Jan 7 2014 default-ssl.conf

Step 2

now use cp command to get copy

mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:/etc/apache2/sites-available$ sudo cp 000-default.conf mahi.mahtab.com.conf
[sudo] password for mahtab:

After this it will ask for your system password. Just enter the password and click enter.
Here we have use sudo. It is because the file permission to cp(copy-paste) is given to root. so after adding sudo it will work .

Step 3

Now use a nano command to open and edit few things

mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:/etc/apache2/sites-available$ sudo nano mahi.mahtab.com.conf
# The ServerName directive sets the request scheme, hostname and port t$
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request’s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName mahi.mahtab.com //Just change here.Remove # and write your own port

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/mytest //And also here choose the folder in which your project is

# Available loglevels: trace8, …, trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

Now just click ctrl+x and press Y and press enter.it will save

Step 4

Enable the new virtual host file

mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:/etc/apache2/sites-available$ sudo a2ensite mahi.mahtab.com.conf

Now restart the apache2

for this type sudo service apache2 reload

mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:/etc/apache2/sites-available$ sudo service apache2 reload

Step 5

Set up local host files

mahtab@mahtab-HP-Pavilion-TS-Sleekbook-15:/etc/apache2/sites-available$ sudo nano /etc/hosts

it will open like this

127.0.0.1 localhost
127.0.1.1 mahtab-HP-Pavilion-TS-Sleekbook-15
127.0.0.1 mahi.mahtab.com       // Just add this line

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Just press ctrl+x and press y and enter.
now go to your browser it will work fine

Thanks
subscribe to get new posts daily

Advertisements