Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install Apache and Set Up a Virtual Host on Ubuntu

This guide shows you how to install the Apache web server and host a website with a virtual host. It needs root access, so it is for VPS and dedicated server users.

VPS and Cloud Servers2 min read13 steps1 screenshots

What are Apache and a virtual host?

Apache is a program that shows websites to visitors. When someone types your address, Apache sends back the page.

A virtual host is a settings file. It lets one server host many websites. Each website gets its own folder and its own settings.

Steps

  1. Log in to your server with SSH. SSH lets you type commands on a remote computer.
  2. Update the package list.
    apt update
  3. Install Apache.
    apt install -y apache2
  4. Check that Apache is running.
    systemctl status apache2
    You should see the word "active".
    apache 2 default page
  5. Make a folder for your site. Replace example.com with your own domain name.
    mkdir -p /var/www/example.com/public_html
  6. Create a test page.
    echo "<h1>It works!</h1>" > /var/www/example.com/public_html/index.html
    This writes a small web page into the new folder.
  7. Create the virtual host file.
    nano /etc/apache2/sites-available/example.com.conf
  8. Paste this text into the file. Then save and close.
    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
        CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    </VirtualHost>
    The DocumentRoot line tells Apache where your site files live.
  9. Turn the site on.
    a2ensite example.com.conf
  10. Optional: turn off the default welcome site.
    a2dissite 000-default.conf
  11. Test your settings for mistakes.
    apache2ctl configtest
    You want to see "Syntax OK".
  12. Reload Apache so it reads the new file.
    systemctl reload apache2
  13. Open your domain in a browser. You should see "It works!".
Tip: Your domain must point to the server's IP address through DNS. DNS is like a phone book that turns a website name into a number. If the page does not load, check your DNS records first. If a firewall is on, allow port 80.

Quick recap

  • Install Apache with apt install apache2.
  • Make a folder for each website.
  • Write one virtual host file per site in /etc/apache2/sites-available/.
  • Turn it on with a2ensite, test with apache2ctl configtest, then reload.