HomeApacheHow to setup Apache to host a website in Linux

How to setup Apache to host a website in Linux

Apache web server is one of the most widely used web servers in the world. One of its powerful features is the ability to host multiple websites on a single server, each with its own domain name. This is achieved using Virtual Hosts. When a requested is received by the Apache server on a virtual host system, it reads the hostname from the client’s HTTP header and utilizes it to route traffic to the correct domain.

Prerequisites

Before we begin, ensure you have the following:

  • An Linux VPS with Apache installed. (Here, Ubuntu)
  • Root or sudo access to your server.
  • Domain names pointing to your server’s IP address. (Here we use a test domain domain1.com)

Learn how to host a Website in Apache Virtual Host

  1. Confirm Apache is installed
    systemctl status apache2

    2. Create a directory structure.

    Create a directory structure for your websites. In this example, we’ll set up directories for domain1.com.

    mkdir -p /var/www/domain1.com/public_html

    3. Create a sample index page for domain1.com

    vi /var/www/domain1.com/public_html

    Add the following content to the file;


    4. Create Virtual Configuration

    Create a new virtual host configuration file for domain1.com:

    vi /etc/apache2/sites-available/domain1.com.conf

    Add the following content to the file;

    5. Enable the Virtual Host

    Enable the new virtual host with the following command:

    a2ensite domain1.com.conf

    6. Test the Configuration

    Check the configuration for syntax errors:

    apache2ctl configtest

    If the output is Syntax OK as below, proceed to the next step.

    7. Restart Apache

    Restart Apache to apply the changes:

    systemctl restart apache2

    8. Test Your Virtual Host

    Open a web browser and navigate to http://domain1.com. You should see the sample index page you created earlier. Thus you can see your website if you have correctly pointed your Domain name to your server’s IP address.

    Conclusion

    You have successfully set up Apache Virtual Hosts on your Ubuntu server. This allows you to host multiple websites on a single server, each with its own domain name. Repeat these steps to add more virtual hosts as needed.

    Scroll to Top