How to install Apache web server on Ubuntu

The content of webpages on the internet is mainly served up by a web server. The most widely used web server is the Apache (HTTP) which is an open source and therefore a free web server that was sponsored, maintained and developed by the Apache Software Foundation. For this reason, Apache is available on most Linux distributions, Ubuntu, RHEL and Fedora (although it is packaged in different ways) . Apache web server, which powers over 50% of websites worldwide, has many powerful features built into it, like reliable media support and dynamically loadable modules. As it is well known for its flexibility and performance, It has been integrated with other popular software.

It is quite easy in general to configure a webserver to run in Ubuntu. Apache however, can be configured in multiple ways in order to serve the requests of many domains. It can also encrypt communications (via HTTPS) and secure websites using various types of authentication.

In this guide, we will try to explain how to install and configure an Apache web server on Ubuntu.

Read: How to uninstall apache2 on Ubuntu 20.04

1 – Apache installation

Since Apache is available in the default software repositories of Ubuntu, you can install it like any other package using package management tools.

First you need to update the local package repository of Ubuntu by executing the following command in the terminal:

sudo apt update

Now issue the command below in order to install Apache web server. Enter y/Y when prompted :

sudo apt install apache2

Linux apache install | install apache Ubuntu

Read: How to change Apache Document root directory on Ubuntu 18.04

Once the installation is complete, you can check the version of the installed apache2 web server by running :

apache2 version

You may be interested to read : Network configuration in Ubuntu

2 – Configuration of the firewall

In order to enable access to Apache, we first need to display the application profiles in order to configure the firewall. Run the command below :

sudo ufw app list

You can see in the output above, three Apache profiles: Apache, Apache Full and Apache Secure. They all provide different security levels.

Port 80 will be opened for traffic if we allow Apache on UFW. This is achieved by enabling the most restrictive profile.The following command will configure UFW in order to allow Apache :

sudo ufw allow ‘Apache’

ubuntu apache config

In order to see that Apache has been enabled on the firewall, you can run the command:

sudo ufw status

You may want to read : How to set up a firewall on Ubuntu 18.04 ?

2 – Configuration of the Apache Web server

First we would need to make sure that the apache web server is up and running before configuring its settings. This is achievable via the command :

sudo systemctl status apache2

You should be able to see that the status is active (running) as shown above.

Read: How to Install Redis Server on Ubuntu 22.04

In order to confirm that your Apache web server is running properly, access the default Apache landing page via your IP address:

Now type in the command below in order to know your IP address :

3 Setting up virtual hosts

It is possible to host more than one website on a single machine using Apache virtual hosts.The virtual hosts allow you to specify the site document root ( or where the website files will be stored and served in response to various requests) and for each site, you will have the possibility to enable a security policy for each domain along with SSL certificates,.

The document root can be created in any location of your choice. In this article we will use the directory structure below:

For each domain we want to host on the server, we will create a different folder within the /var/www directory .

Read: Linux directories explained

In order to store the website files, we will create a directory called public_html under the /var/www folder.

Issue the command below in order to create the directory mydomain.com ( the -p argument is used to create parent directories) :

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

As shown above in the snapshots, the commands were executed with the sudo user and the new files and directories created, are owned by the root user. We can change the ownership of the root directory in order to avoid any permission issues. This can be achieved via the command :

sudo chown -R $USER:$USER /var/www/mydomain.com/public_html

To make sure the permissions are correct, type in :

sudo chown -R 755 /var/www/mydomain.com

Now create an index.html file inside the root directory so that we can try to access it afterwards and check if Apache is indeed running our domain name .

Issue the nano command or with your favorite editor, proceed as follows:

nano /var/www/mydomain.com/public_html/index.html

Then add the following HTML code lines:

<html>
<head>
<title> test mydomain.com </title>
</head>
<body>
<h1> access performed successfully </h1>
</body>
</html>

Once you are done, save the file and exit (in nano you should use Ctrl+X and then enter Y before hitting Enter ).

Read: How to install and uninstall applications on Ubuntu 

Apache stores the configuration files of its virtual hosts in the directory /etc/apache2/sites-available. Although Apache has a default configuration file (000-default.conf) and in order for the Apache web server to respond to different domain requests, we have to create a new virtual host configuration file.

As before, use nano or open your preferred editor and create the following Virtual Host configuration file at : /etc/apache2/sites-available/mydomain.com.conf

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

Then enter the following lines in the file :

<VirtualHost *:80>

ServerAdmin admin@mydomain.com

ServerName mydomain.com

ServerAlias www.mydomain.com

DocumentRoot /var/www/mydomain.com/html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save and close when you are finished. Here is a simple explanation of some of the fields above :

  • ServerName: This should be your domain name which should match for this virtual host configuration.
  • ServerAlias: You can insert here other domains which should correspond to this virtual host as well, like for instance the www.mydomain.com.
  • DocumentRoot: Apache will serve the domain files from the directory that you specify in this field.
  • ErrorLog, CustomLog: Refers to the location of the log files.

For the virtual host configuration file name, it would be best to use your own domain name (best practice). You can however name it as you wish.

We now need to establish a symbolic link between the virtual host file and the directory /sites-enabled, which is read by apache2 when it starts up.

In order to enable the virtual host, the easiest way would be to use the a2ensite tool as follows :

sudo a2ensite mydomain.com.conf

Now disable the default site configuration file 000-default.conf:

sudo a2dissite 000-default.conf

Once done, you can test the configuration you created for any syntax errors using the command below:

sudo apachectl configtest

Read: How to install MySQL on Ubuntu 18.04

As you can see, we are getting an error which is rather common in the Ubuntu 18.04 version. In order to resolve it, please proceed as follows:

Enter the following command in order to resolve the above-mentioned error:

echo “ServerName mydomain.com | sudo tee /etc/apache2/conf-available/servername.conf

And then issue the command :

sudo a2enconf servername

You can then test whether the syntax is ok by running the command again:

Finally you need to restart Apache server in order to implement your changes. Issue the command below :

sudo systemctl restart apache2

linux apache restart

Your domain name is now served by Apache. You can also check that your index.html (the file we created above) is being accessed by calling your IP Address in the browser :

4 Some Apache server management commands

In order for you to manage some Apache server operations, we provide some basic commands that you can run in your Terminal:

sudo systemctl start apache2

This will start the Apache server.

sudo systemctl stop apache2

This command will stop the Apache server.

sudo systemctl restart apache2

This will stop and start the Apache server again.

sudo systemctl reload apache2

This will apply the configuration changes without restarting the service.

sudo systemctl enable apache2

This will start Apache at boot up.

sudo systemctl disable apache2

This will disable starting Apache every time you boot your system.

Read: How to use scp command in Linux to transfer files securely using ssh

Conclusion

You have learned the steps to install and configure the Apache web server on your Ubuntu system. You are now able to create an apache virtual host configuration on a single Ubuntu server. You can repeat the steps above in order to create additional virtual hosts for multiple domains .


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

Leave a Reply