Serving Information Simply

Time to Read

6 minutes

How To Configure the Apache Web Server on an Ubuntu

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

    cd /var/www
  

create a new folder with your website name or as per your convenience.

    Mkdir your_domainname
  

Go into same folder and create index.html page (homepage).

    cd your_domainname
  

    nano index.html
  

Write a simple html code

click on ctrl+x to exit and press y to save it.

To see the file

    cat index.html
  

 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:

    cd /etc/apache2/sites-available/
  

Create new conf file.

    nano your_domain.conf
  

Add below line with your own site name and save it.

To verify the configuration.

    ls -l
  

    cat your_domain.conf
  

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:

    a2ensite your_domain
  

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:

    a2dissite 000-default.conf
  

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:

    a2enmod info
  

Likewise, you can disable a module using the a2dismod command:

    a2dismod info
  

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.

    apache2ctl configtest
  

Restart the Apache:

To validate the new website and virtual host working or not you can two methods:

  1. 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
  



Leave a Reply


Sanchit Agrawal

Blog Author

Discover more from Sanchit Gurukul

Subscribe now to keep reading and get access to the full archive.

Continue Reading

%d bloggers like this: