Apache is one of the most popular web servers on the internet. In This tutorial we are going to learn how to configure new virtual server and activate it.
Before you begin exploring your Apache configurations, you should have Apache installed on your server. You can learn how by following another article.
How to check if Apache2 is already install:
ls -f /etc/apache2
Virtual Host File
The default virtual host declaration can be found in a file called 000-default.conf within the sites-available/ directory. You can learn about the general format of a virtual host file by examining this file.
sudo cat /etc/apache2/sites-available/000-default.conf
The default virtual host is configured to handle any request on port 80, the standard HTTP port. This is defined in the declaration header where it says *:80, meaning port 80 on any interface.
However, this does not mean that it will necessarily handle each request to the server on this port. Apache uses the most specific virtual host definition that matches the request. If there was a more specific definition, it could supersede this definition.
In First step we need to create own site page to link with virtual host. To create a new website page, we need to go
create a new folder with your website name or as per your convenience.
Go into same folder and create index.html page (homepage).
Write a simple html code
click on ctrl+x to exit and press y to save it.
To see the file
Now, we are going to create new virtual host for our website.
Run the following command to open your virtual host file, making sure to replace the your_domain variable with your actual domain name:
Create new conf file.
Add below line with your own site name and save it.
To verify the configuration.
Enabling Sites and Modules
Once you have a virtual host file that meets your requirements, you can use the tools included with Apache to transition it into live websites. To create a symbolic link in the sites-enabled directory to an existing file in the sites-available directory, issue the following command. Make sure to replace your_domain with the name of your own virtual host site configuration file:
After enabling a site, issue the following command to tell Apache to reload its configuration files, allowing the change to propagate:
systemctl reload apache2
systemctl status apache2
There is also a companion command for disabling a virtual host. It operates by removing the symbolic link from the sites-enabled directory. For example, with your virtual host site enabled, you can disable the default 000-default site:
Modules can be enabled or disabled by using the a2enmod and a2dismod commands respectively. They work in the same way as the a2ensite and a2dissite versions of these commands. For example, to enable the info module, you can use the following command:
Likewise, you can disable a module using the a2dismod command:
Remember to restart Apache after modifying configuration files and enabling or disabling modules.
To validate the Apache configuration is correct and there is no error you can use below command.
Restart the Apache:
To validate the new website and virtual host working or not you can two methods:
- Go to web browser and enter the server ip and see if page is coming or not:
- Use curl command to see if you are getting correct site
curl -v http://your_server_ip