For running any PHP application on the localhost you require localhost. The localhost is called a local server that is created in your system to run an application. We all use Apache or Nginx server to run a PHP script. But, if you are moving to any framework like Laravel. Then you can run the application on localhost using the command line. But, sometimes if you want to register your local site for any third-party API service like Google reCAPTCHA. Then that time it won’t accept the localhost domain. If you don’t want to deploy your application on the webserver then how you will fulfill this requirement for a domain or host. Why don’t you create a virtual host? Yes, it is absolutely right, you can create a virtual host with any domain name in the local system. Today, in this post, I will be helping you for creating a virtual host in apache server.
After creating the virtual host in apache server, you will be able to run the application of PHP and it’s framework. So, let’s continue it.
- 1 Prerequisites
- 2 Create Virtual Host in Ubuntu
- 2.1 Step 1: Open Terminal and Navigate to Apache Directory
- 2.2 Step 2: Navigate to sites-available directory
- 2.3 Step 3: Copy default conf file with new name for Virtual host
- 2.4 Step 4: Edit the new conf file
- 2.5 Step 5: Enable Virtual Domain conf file
- 2.6 Step 6: Add the Virtual Domain in Apache Host File
- 3 Conclusion
Before moving to this post, please make sure you are ready with the below requirements.
- Ubuntu 20.04.2 LTS (As an Operating System)
- An Ubuntu user with Sudo Privileges
- A terminal and command-line access
- Apache Server or LAMP setup
So, let’s follow the steps for creating the virtual host in Ubuntu.
Create Virtual Host in Ubuntu
In the very first step, we have to navigate to the apache server folder. Hence, just open the terminal and navigate to the apache2 folder. Because in LAMP stack the apache2 package is available for the apache server.
$ cd /etc/apache2
Inside the apache2 directory there is a folder named sites-available. So, firstly list the available directories and files to check all.
/etc/apache2$ ls -a
Here, you can see the available directories and files are listed from the apache2 folder.
In this step, we have to enter into the sites-available directory. So, just change the directory using the cd command.
/etc/apache2$ cd sites-available
After changing the current directory to sites-available, you can list out the files.
Here, you will see the 000-default.conf file. This is basically a default configuration file of apache index page. for creating the virtual host
Step 3: Copy default conf file with new name for Virtual host
In this step, we will copy the 000-default.conf file to our expected domain name. Here, I want to have a virtual host with the domain name www.mylaravelapp.com. Hence, I copied the default conf file with the expected-domain-name.conf.
$sudo cp 000-default.conf mylaravelapp.com.conf
Here, I copied the default conf file to mylaravelapp.com.conf.
Once, the file is copied, you will be able to see the copied file in the directory.
Step 4: Edit the new conf file
In this step, we will edit the created virtual host file. So, just open it in vi editor or default editor in Ubuntu.
sudo gedit mylaravelapp.com.conf
Once the file is opened, it will look like this. Here, you will see some lines are commented.
Here, we have to edit few lines.
- Uncomment the ServerName (line number 9) and put the virtual domain name. The domain name must be same as we created the .conf file.
- In the DocumentRoot (line number 12), you have to specify the project folder for which you are creating this virtual host.
ServerName www.mylaravelapp.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html/laravel/blog
So, in the above lines, you can see, I have added the ServerName, and DocumentRoot. For the DocumentRoot, I have specified the path of one of my Laravel application.
Here, you can see, after updating the file it will look like this.
After that just save the file and close it. After closing the file, continue to the terminal and enable this conf file.
Step 5: Enable Virtual Domain conf file
To enable the updated configuration file, you will have to run the below command.
$sudo a2ensite mylaravelapp.com.conf
The above command will ask to reload the apache server.
Hence, after reloading the apache server, just proceed to the last step.
Step 6: Add the Virtual Domain in Apache Host File
Last, but not least, you have to add the created virtual domain in the apache host file. So, you have to navigate to /etc folder.
$cd /etc then /etc$ ls -l
After navigating to the directory, just list out the files and folders.
Here, you will find the host file as showing below.
You have to edit this hosts file to add the virtual domain here.
sudo gedit hosts
After opening the hosts file in editor, you have to register your virtual domain here.
Here, I have added the above IP address with the virtual domain. You can see the default IP address of localhost. So, we won’t change anything else. Just add your virtual host below the default host.
That’s it for the virtual host. Now, you can access your virtual domain in the browser. Just open it, and see the result.
Here, I am running the Laravel app using this virtual domain. You can see the application is running perfectly on the virtual host.
The virtual host concept is really awesome when you want to run your application in the local environment. By following the above steps, you will be able to create and use the virtual host in Ubuntu. You can create more than one host in the Apache server. Just need to specify the project folder path that you want to run inside it. I hope this post will resolve your issue and you will be able to run the application on a local and virtual server.