How to Install Apache on Ubuntu: A Step-by-Step Guide

This article guides installing Apache, an open-source and reliable web server, on Ubuntu Linux using the package manager. It covers initial installation steps, opening HTTP and HTTPS ports, and setting up a virtual host for hosting multiple websites. Detailed commands for installation, testing, and configuration ensure a successful Apache setup on Ubuntu.

This article explains how to install Apache on the Ubuntu Linux system.

Apache is a popular open-source web server used to serve web pages online. It is widely used because of its flexibility, security, and reliability. On Ubuntu Linux, Apache can be easily installed using the package manager.

The server provides powerful features that can be extended through modules, making it a versatile choice for many web applications.

Apache is a popular choice for web server software on Ubuntu Linux systems because it is open-source, flexible, secure, and reliable. It has been around for a long time and is well-supported by a large community of developers and users.

Additionally, Apache can be easily installed on Ubuntu Linux using the package manager, making it a convenient web hosting option.

Install Apache

On Ubuntu and Debian systems, the Apache package and the service are called apache2.

It is included in the default Ubuntu repositories, making the installation straightforward.

To install Apache, run the command below.

sudo apt update
sudo apt install apache2

Once Apache is installed, it will start up automatically.

To verify that the service is running, run the command below.

sudo systemctl status apache2

The command should output lines similar to the one below.

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Wed 2024-01-17 17:25:14 CST; 1min 22s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3668 (apache2)
      Tasks: 55 (limit: 2261)
     Memory: 4.9M
        CPU: 51ms
     CGroup: /system.slice/apache2.service
             ├─3668 /usr/sbin/apache2 -k start
             ├─3670 /usr/sbin/apache2 -k start
             └─3671 /usr/sbin/apache2 -k start
...

Allow traffic on HTTP and HTTPS ports

Apache, by default, listens on port 80 (HTTP) and 443 (HTTPS). If you need to connect to the server, you may have to open the necessary ports for communication.

Here’s how to do that on Ubuntu Linux. Run the command below to open the firewall, assuming UFW is being used.

sudo ufw allow 'Apache Full'

Setup Virtual Host

A virtual host is an Apache configuration directive that allows you to run multiple websites on a single server. Typically, a virtual host describes one website. This means multiple domain names can be hosted on a single server, with separate content for each domain.

Apache ships with one virtual host enabled by default. All domains that point to the server IP address will match the default virtual host. If you are hosting a single website, you can upload its content in the default virtual host location and edit the host configuration in the Apache configuration file.

If you are hosting a single website, you can upload its content in /var/www/html and edit the virtual host configuration found in the /etc/apache2/sites-enabled/000-default.conf file.

To test a virtual host, run the command below to create a website folder called example.com in the /var/www/ directory.

sudo mkdir -p /var/www/example.com/public_html

Then, create a basic HTML index file in the directory.

sudo nano /var/www/example.com/public_html/index.html

Copy and paste the content below into the file.

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Save the file and exit.

Next, run the command below to change the ownership of the web content to Apache user (www-data).

sudo chown -R www-data: /var/www/example.com

Next, create a virtual host file for the website. Apache vhosts files are stored in /etc/apache2/sites-available directory.

sudo nano /etc/apache2/sites-available/example.com.conf

A very basic vhost file should resemble the one below.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Save the file.

Next, run the command below to enable the configuration.

sudo a2ensite example.com

Test the config and ensure nothing is wrong.

sudo apachectl configtest
sudo systemctl restart apache2

When all is configured correctly, you should see a test page when browsing the website.

That should do it!

Conclusion:

  • Apache is a versatile and widely used open-source web server providing powerful features for web applications.
  • Installing Apache on Ubuntu Linux is straightforward using the package manager, making it a convenient web hosting option.
  • Configuring virtual hosts allows running multiple websites on a single server, each with separate content and domain names.

Comments

53 responses to “How to Install Apache on Ubuntu: A Step-by-Step Guide”

  1. […] management system (CMS), can be installed on a web server to create and manage dynamic websites. Apache, a widely trusted web server software, and Ubuntu, a widespread and reliable Linux distribution, […]

Leave a Reply

Your email address will not be published. Required fields are marked *