Once in awhile, we need to create locally domain alias for our projects in XAMPP. In this article, we will see how to set up a Virtual host locally with XAMPP/LAMPP in Ubuntu.
Let’s consider a scenario you have project myproject in your htdocs folder and you want to access this project as myproject.com on your local server/machine. If your goal is the same then tag along.
Step 1 – Create your host/ local domain name.
Open your terminal anywhere and type the following command.
sudo gedit /etc/hosts
This will open hosts.txt file. Next, you will add your host address with domain name alias. In the file there will be some content already do not mess with that, just add your one-line like below.
127.0.0.1 localhost 127.0.0.2 www.myproject.com # 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
On the second line I have added my custom hosts that will run on 127.0.0.2 , you are free to use any IP address.
Step 2 – Create a virtual host and assign our directory path
Now on your terminal type the following command.
sudo gedit /opt/lampp/etc/extra/httpd-vhosts.conf
This will open up httpd-vhosts.conf file. What you have to do just go end of the file and add the following lines.
<VirtualHost 127.0.0.2:80> DocumentRoot "/opt/lampp/htdocs/myproject" DirectoryIndex index.php <Directory "/opt/lampp/htdocs/myproject"> Options All AllowOverride All Require all granted </Directory> </VirtualHost>
Now that our domain is pointed to the corresponding directory.
Step 3 – Enable custom Virtual hosts configuration
On your terminal type the following command to enable the use of custom virtual hosts.
sudo gedit /opt/lampp/etc/httpd.conf
Now in httpd.conf go to the line number 488 and you will see
#Include etc/extra/httpd-vhosts.conf just remove the hash(#) from the line.
So that’s it. Now start your LAMPP server and test with http://myproject.com on any browser. It will surely work.
That was all about how to set up a Virtual host locally with XAMPP/LAMPP in Ubuntu. This way you can create unlimited custom domain for each projects of htdocs.
I hope this article will help you. In case of any issues and query , leave your comment in below comments box section.
- Simple CRUD application in core PHP
- How to create REST API in PHP
- Simple CRUD application in laravel
- Create PDF using mPDF in core PHP
- send email using mail function in PHP
- Upload image using AJAX in PHP
- Implement social login with google in PHP
- Useful PHP Array methods you should know
- Everything you should know about sessions in PHP
- What are Cookies in PHP and how to use it