This is a sample tutorial about creating a virtual host in apache2, in this tutorial we will work on ubuntu 13.10, Lets start…
1- Make sure that you have LAMP (linux-apache2-mysql-php5) in your machine, if not please check this link .
2- Open your terminal and type
sudo su cd /etc/apache2/sites-available/
Here you can create your .conf file that will save your virtual host settings.
3- Create a .conf file with your virtual host name ( ex: test.conf )
this will create a test.conf file that will save your virtual host settings.
4- Inside your test.conf file type :
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName test.local DocumentRoot /var/www/test/index <Directory /var/www/test/index> DirectoryIndex index.php AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
- Line 1 of the file makes Apache to listen to all (*) IP addresses on port 80.
- Line 2 defines the web master’s E-mail address. If something bad happens to the site, Apache sends an alert E-mail to this address. You can enter your E-mail here.
- Line 3 defines host name that will be accessed throw the browser.
Line 4 defines the document root directory ( APP_DIR ). All files and directories under the document root will be accessible by web-users. You should set this to be the absolute path to skeleton application’s public directory. So, the directories and files inside public (like index.php , css , js , etc.) will be accessible, while directories and files above public directory (like config , module , etc.) wont’ be accessible by web users, which enhances the security of the web site.
Lines 5-10 define rules for the document root directory (APP_DIR). DirectoryIndex define the index file inside root directory. The AllowOverride All directive allows to define any rules in .htaccess files. The Order allow,deny and allow from all control a three-pass access control system, effectively allowing everyone to visit the site.
then save your settings as test.conf
5- Go to your hosts file to add host name to your apache
cd /etc nano hosts
then add ip address of your localhost and host name
then save your settings
6- Now we will save our new virtual host, type :
a2ensite test.local service apache2 reload
it will register the new virtual host then reload apache2 to save all settings.
7- We will create a sample project inside www to test our virtual host :
cd /var/www/ mkdir test cd test mkdir index cd index nano index.php
then add some lines to index.php
<h1>Hello, Iam inside My test project</h1> <p>File Directory: /var/www/test/index/index.php</p>
save your file then close.
8- Test time :), go to your browser then type test.local
This is a very sample way to create virtual host on your server, you can update your directories and host name as you want. If i forgot something or you have a problem please leave your comment.