How To Create Virtual Host on Ubuntu OS (LAMP)
## Note:: Assuming you have installed LAMP ##
Case: We want to create a virtual host name as "test.localhost"
Step 1 — Create the Directory Structure:
- Create a folder name as "test.localhost" in /var/www directory.
sudo mkdir -p /var/www/test.localhost
Step 2 — Grant Permissions:
sudo chown -R $USER:$USER /var/www/test.localhost
sudo chmod -R 755 /var/www
Step 3 — Create Demo Page for Virtual Host
- Create index.php file inside the folder /var/www/test.localhost
and write below sample text in index.php file and save.
<?php
echo 'Virtual host testing succesfullly done...';
?>
Step 4 — Create New Virtual Host Files
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.localhost.conf
Now Open the new file as follow
sudo gedit /etc/apache2/sites-available/test.localhost.conf
Copy the code below and replace with all old text inside this file
<VirtualHost *:80>
ServerAdmin admin@test.localhost
ServerName test.localhost
ServerAlias www.test.localhost
DocumentRoot /var/www/test.localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file.
Step 5 — Link our virtual host with localhost in /etc/hosts file:
Open file hosts as follow
sudo gedit /etc/hosts
Add the below line in host file
127.0.1.1 test.localhost
Save and close the file.
Step 6 — Enable the New Virtual Host Files
We can use the a2ensite tool to enable each of our sites like this:
sudo a2ensite test.localhost.conf
When you are finished, you need to restart Apache to make these changes take affect:
sudo service apache2 restart
Step 7 — Test your site
http://test.localhost
You should see a page that looks like this:
Virtual host testing succesfullly done...
Finished!!!!!!!
Case: We want to create a virtual host name as "test.localhost"
Step 1 — Create the Directory Structure:
- Create a folder name as "test.localhost" in /var/www directory.
sudo mkdir -p /var/www/test.localhost
Step 2 — Grant Permissions:
sudo chown -R $USER:$USER /var/www/test.localhost
sudo chmod -R 755 /var/www
Step 3 — Create Demo Page for Virtual Host
- Create index.php file inside the folder /var/www/test.localhost
and write below sample text in index.php file and save.
<?php
echo 'Virtual host testing succesfullly done...';
?>
Step 4 — Create New Virtual Host Files
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.localhost.conf
Now Open the new file as follow
sudo gedit /etc/apache2/sites-available/test.localhost.conf
Copy the code below and replace with all old text inside this file
<VirtualHost *:80>
ServerAdmin admin@test.localhost
ServerName test.localhost
ServerAlias www.test.localhost
DocumentRoot /var/www/test.localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file.
Step 5 — Link our virtual host with localhost in /etc/hosts file:
Open file hosts as follow
sudo gedit /etc/hosts
Add the below line in host file
127.0.1.1 test.localhost
Save and close the file.
Step 6 — Enable the New Virtual Host Files
We can use the a2ensite tool to enable each of our sites like this:
sudo a2ensite test.localhost.conf
When you are finished, you need to restart Apache to make these changes take affect:
sudo service apache2 restart
Step 7 — Test your site
http://test.localhost
You should see a page that looks like this:
Virtual host testing succesfullly done...
Finished!!!!!!!
How To Create Virtual Host on Ubuntu OS (LAMP)
Reviewed by Web Technology Funda
on
6:08:00 AM
Rating:
No comments