keskiviikko 25. syyskuuta 2013

How To Set Up Apache Virtual Hosts

About virtual hosts
Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name.

For example you have one computer which runs apache. Then you set three websites there: test1.com test2.com test3.com and all of the pages can be accessed from Iternet even though the computer has one IP address. 

Set Up
First install apache2


sudo apt-get install apache2

Create new folder where you want to host your pages for example (where -P creates automatically all the parent folders):

sudo mkdir -p /var/www/sivusto1/public_html

(This step is not mandatory if you for example run pages from user home directory.)
Grant user ownership to the folder

sudo chown -R $USER:$USER /var/www/sivusto1/public_html 


Make sure everyone can read your new files

sudo chmod -R 755 /var/www
  
(Continue from here) 

Create new index.html under public_html

sudoedit /var/www/sivusto1/public_html/index.html

Add some test html to the index file

<html>
  <head>
    <title>www.sivusto1.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host one!</h1>
  </body>
</html>
 
Save and exit (CTRL + O and CTRL + X)

Here I have created two virtual host pages.



Create new virtual host file

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/sivusto1.com


Turn on virtual hosts

sudoedit /etc/apache2/sites-available/sivusto1.com

Edit the file like this. ServerAdmin for example your name or email, ServerName is the address of your page and ServerAlias is of course alias address, you can add multiple aliases.

<VirtualHost *:80>
ServerAdmin webmaster@sivusto1.com
ServerName sivusto1.com
ServerAlias www.sivusto1.com

From below set the document root:

DocumentRoot /var/www/sivusto1/public_html

Then save and exit nano.


Activate your new host

sudo a2ensite sivusto1.com

to disable host: sudo a2dissite sivusto1.com 

Restart your server


sudo service apache2 restart

You may see and error log during restarting, but it's nothing to worry about


Set up your local hosts (This step is for testing purposes only. It mimics the DNS search and should not be applied in production server)

Open your hosts file with:

sudoedit /etc/hosts

And create lines:

#Virtual Hosts 
computeriphere sivusto1.com
computeriphere www.sivusto1.com



Save and exit nano. Now it's time to try your site. Open browser and type either sivusto1.com or www.sivusto1.com and your page will load!



Original instructions: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-12-04-lts

Questions: Can I insert all the pages to the default file and not duplicate it every time ? Answer: probably yes, but it is easier to maintain available sites with different files.

My personal virtualhost setting with Varnish:

<VirtualHost *:8080>
        ServerAdmin niki.ahlskog@gmail.com
        ServerName nikiahlskog.com
        ServerAlias www.nikiahlskog.com
        DocumentRoot /home/niki/public_html/

        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /home/niki/public_html/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

</VirtualHost>

Other example:

<VirtualHost *:8080>
          ServerAdmin niki.ahlskog@gmail.com
        ServerName nikiahlskog.com
        ServerAlias www.nikiahlskog.com
        DocumentRoot /home/niki/public_html/

        <Directory /home/niki/public_html/folder/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

1 kommentti:

  1. Nice Post!!! Web hosting is, at its most basic, simply the storage of files for one or more Internet websites as well as the service of maintaining those files, serving the needs of the people who own the website being hosted and maintaining the immense computer servers that store the billions of bits of data that every website creates. If any person is interest to have Cheapest Web Hosting he/ she must find out the trusted online shop.

    VastaaPoista