Install Apache 2.4 Web Server on Ubuntu 24.04 LTS Server

I needed a development Web Server on my in-house network for website development. This Web Server will not be visible to the outside Internet, only to computers logged on through the local network. These are the steps I used to install and configure the Apache 2.4 Web Server on a in-house Ubuntu 24.04 LTS server.

Before installing any new applications, confirm that you system is up to date. Open your terminal window by pressing the Ctl, Alt and ‘T’ keys simultaneously. Enter the following command on the terminal.

Install mySQL and PHP before continuing with the Apache2 Web Server installation.

To install the Apache2 Web Server, enter the following command.

Start the service and confirm the installation.

The status command will display the following results, indicating the the Apache2 service is running.

Another way to test that the Apache2 Web Server is active is by viewing the default Apache2 web page. Open a browser on the Ubuntu Server, where Apache2 is installed, and type http://localhost/ in the browser’s address line. This page will display on the browser showing that Apache2 is working correctly.

If you have an active firewall on Ubuntu Server you will need to open the necessary ports for communication. Apache, by default, listens on port 80 (HTTP) and port 443 (HTTPS). The command below will open these ports on the Ubuntu Server. If you have configured Apache2 to use ports other than 80 and 443, you will need to adjust this command to open the correct ports.

$ sudo ufw allow 'Apache Full'

You can confirm the change with this commend.

$ sudo ufw status

The output will show that HTTP and HTTPS traffic is allowed.

Status: active

To                         Action      From
--                         ------      ----
Apache Full                ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
Apache Full (v6)           Allow       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

Now create a directory for the development website files and folders. By default Apache2’s web root is located in /var/www/html. But a new website directory can be located anywhere. For this development website I created it at /var/www/ and called it example.com using the following command.

$ sudo mkdir /var/www/example.com

Then below the example.com directory I create a /public_html/ directory. This is where all the website files and folders will be created.

$ sudo mkdir /var/www/example.com/public_html

Set ownership of the directory to your Ubuntu username, to avoid permission issues, when creating files.

$ sudo chown -R $USER:$USER /var/www/example.com/public_html
$ sudo chmod -R 755 /var/www/example.com/public_html

To allow the Apache2 Web Server to serve this web app, using a domain name of your choice, you will add it to a virtual host file in the /etc/apache2/sites-available/ directory. There is already a virtual host file, named 000-default.conf, located in this folder. This is the virtual host file that Apache2 is using to server the default web page. You can use the 000-default.conf file for your new web app. But it is best to create an new file, with a name that has a meaning for you, for the new virtual host. In my case I decided on using the file name vhosts.conf. Change directories to the /etc/apache2/sites-available/ directory. Using the VI text editor, I created a vhosts.conf file.

$ cd /etc/apache2/sites-available
$ sudo vi vhosts.conf

Within this file I entered and saved the following block.

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the vhosts.conf file with the a2ensite tool.

$ sudo a2ensite vhosts.conf

But before Apache2 will serve this new web app, you will need to disable the 000-default.conf file. Do this with the a2dissite tool.

$ sudo a2dissite 000-default.conf

Then, test for configuration errors in the virtual host file.

$ sudo apache2ctl configtest

The following output will indicate no errors in the virtual host file.

Output
Syntax OK

Restart Apache2 to apply the changes.

$ sudo systemctl restart apache2

Create a simple index.html page for the example.com web app. Using a text editor open a file in the /var/www/example.com/public_html directory

$ sudo vi /var/www/example.com/public_html/index.html

Add the following html code to the index.html file and save the file.

<!DOCTYPE html>
<html>
<head>
   <title>Test example.com Web Page</title>
</head>
<body>
   <h1>Welcome the the EXAMPLE.COM Web Page!</h1>
</body>
</html>

Modifying the DNS Server is the preferred way to resolve domain names. But for small development environments, the Hosts files on the Ubuntu Server, and each computer needing to access the domain, can be modified. To configure the Ubuntu Server’s Hosts file, open the Hosts file with a text editor – such a notepad – enter the URL you will use for the web site on the same line as the localhost, each seperated by a space.

The Ubuntu Server’s Hosts file will look like this.

127.0.0.1          localhost example.com www.example.com
127.0.1.1          ubuntu-server-name

To configure the individual’s computer Hosts file, open its Hosts file and, on a new line, enter the Ubuntu Server’s IP Address, followed by the URL you will use for the web site. Below is an example of what the Hosts file may like with the website URL added.

127.0.0.1          localhost
127.0.1.1          ubuntu-server-name 
192.168.1.2        www.example.com example.com

Apache2 should now be serving the example.com domain name. Test it by opening a browser and entering the URL http://www.example.com in the address line.