How to Install Apache on Ubuntu 22.04 LTS

Apache, also known as Apache HTTP server, has been one of the most widely used web server applications globally for the past few decades. It is a free and open-source web application software maintained by the Apache Software Foundation. Apache provides some powerful features with dynamically loadable modules, easy integration with other software, and handling of static files, among other popular features.

In the following tutorial, you will learn how to install Apache Web Server on Ubuntu 22.04 LTS Jammy Jellyfish using the command line terminal for desktop or server and basic configuration and creating a TLS/SSL certificate with Let’s Encrypt.

Updating System

Before proceeding, please update your system to ensure all existing packages are up to date to avoid conflicts during the tutorial and good practice.

sudo apt update && sudo apt upgrade

Install Apache

Apache is featured in Ubuntu 22.04’s default repository and easiest for most users. However, in time Ubuntu LTS release versions can fall behind; if you would like a newer version, please check out my tutorial How to Upgrade Apache on Ubuntu 22.04 LTS.

In your command terminal, execute the following command to install Apache.

sudo apt install apache2 -y

Check to see if Apache2 is running correctly using the following systemctl command.

systemctl status apache2

Example output if everything is ok:

How to Install Apache on Ubuntu 22.04 LTS

Configure UFW Firewall

Debian users who have installed UFW will need to adjust the UFW rules to allow outside access to the default web ports. Luckily, during the installation, Apache registers itself with UFW to provide a few profiles that can be used to enable or disable access, making it easy and quick to configure.

By default, UFW should be installed. But if it was removed and needs to be re-installed, use the following command.

sudo apt install ufw -y

Next, enable UFW. When you enable the firewall, it will deny all incoming and allow all outgoing by default.

sudo ufw enable

First, list the application profiles to see the Apache profiles available by the following command.

sudo ufw app list

Example output:

How to Install Apache on Ubuntu 22.04 LTS

From the output above, you have three profile options to choose from. To break it down, Apache runs on port 80 (HTTP), Apache Secure runs on port 443 (HTTPS), and Apache Full is a combination of allowing both. The most common is either Apache Full or Apache Secure.

The Apache Full profile will be enabled for the tutorial, which can be done with the following command.

sudo ufw allow 'Apache Full'

Example output:

How to Install Apache on Ubuntu 22.04 LTS

As above, the rules have been added for both IPV4 and IPV6.

Confirm the firewall rules are active with the following command.

sudo ufw status

Example output:

How to Install Apache on Ubuntu 22.04 LTS

As above, the tutorial allowed the Apache Full UFW rule option.

After UFW is configured, check to make sure you can see the Apache landing page in your Internet Browser.

http://your_server_ip

Alternative, try the localhost.

http://localhost

If all is working well, you should land on the following page:

How to Install Apache on Ubuntu 22.04 LTS

Create Virtual Host

Using the Apache web server, you can create virtual hosts to manage configurations for more than one domain running on a single server. If you have used Nginx before, it is the equivalent of server blocks. In the example below, the tutorial will create a domain example-domain.com which you will replace with your domain name.

By default, Apache on Ubuntu 22.04 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. If you are operating one website, you can modify this server block to suit your own. However, if you are hosting multiple websites, you need to create a new directory structure for your various domains.

Create and or Configure Directories

Firstly, leave /var/www/html the directory intact as the default directory.

cd /var/www/

Create a new directory, /example.com/, and replace the example with your domain name with the following command.

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

The next step is to assign ownership of the directory with the www-data environment variable.

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

Now create a sample page index.html using your favorite text editor. The tutorial will use nano as below.

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

In the file, copy and paste the following code:

<html>
 <head>
  <title>Welcome to Linuxcapable.com</title>
 </head>
 <body>
   <h1>Success!  The tutorial server block is working! Thanks Linuxcapable.com :D</h1>
 </body>
</html>

Save the file (CTRL+O), then exit (CTRL+X).

Create Virtual Host

Now that you have created a landing page and set the correct ownership and permissions. You can now proceed to create a virtual host file. By default, all virtual host files will need to be located at /etc/apache2/sites-available/ directory.

First, use your favorite text editor to create a configuration file located at /etc/apache2/sites-available/example.com.conf as below.

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

Now, copy and paste the following into the configuration block file. Note to replace your ServerName, ServerAlias, and Document root with your own:

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

Do not forget to change the required Server directives to your own.

Save the configuration file using (CTRL+O) and exit with (CTRL+X).

Enable Virtual Host

Now that your virtual host configuration file is ready, it is time to enable it. With Apache, unlike Nginx, where you would create a symlink using the ln -s command, Apache uses its tools, as the tutorial will show below:

First, disable the existing default installed server block file 000-default.conf with the a2dissite command:

sudo a2dissite 000-default.conf

Next, run a dry run to see if any errors are present before enabling example.com.conf.

sudo apache2ctl configtest

Now enable your virtual host file with the a2ensite command.

sudo a2ensite example_domain.conf

Example output:

How to Install Apache on Ubuntu 22.04 LTS

Now restart the Apache webserver to make your new virtual host live with the following command.

sudo systemctl restart apache2

Apache should currently serve the landing page you created for your new domain. To test this, open your Internet Browser and type in your domain name HTTP://example_domain where you should get the following landing page you created in the index.html file.

How to Install Apache on Ubuntu 22.04 LTS

Congratulations, you have successfully created your virtual host and made it work successfully on your domain.

Secure Webserver Files

One of the most common mistakes many users make is not fixing the permissions of files/folders. Many users even give complete read/write and execute access to the public.

Use the following command to search for all folders and files and set the most commonplace secure permissions. Make sure to change permissions on any files/directories afterward. For example, phpBB requires some folders to be 777.

sudo find /var/www/example.com/ -type d -exec chmod 755 "{}" \;
sudo find /var/www/example.com/ -type f -exec chmod 644 "{}" \;

Make sure to change /var/www/example.com/ to your root directory location.

Note that this does not make your Apache server secure; it eliminates a prevalent risk out of many.

Secure Apache with Let’s Encrypt SSL Free Certificate

Ideally, you would want to run your Apache on HTTPS using an SSL certificate. The best way to do this is to use Let’s Encrypt, a free, automated, and open certificate authority run by the nonprofit Internet Security Research Group (ISRG).

First, install the certbot package with the following command.

sudo apt install python3-certbot-apache -y

Once installed, run the following command to start the creation of your certificate.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email you@example.com -d www.example.com

This ideal setup includes force HTTPS 301 redirects, a Strict-Transport-Security header, and OCSP Stapling. Just make sure to adjust the e-mail and domain name to your requirements.

Now your URL will be HTTPS://www.example.com instead of HTTP://www.example.com.

Note if you use the old HTTP URL, it will automatically redirect to HTTPS.

Optionally, you can set a cron job to renew the certificates automatically. Certbot offers a script that does this automatically, and you can first test to make sure everything is working by performing a dry run.

sudo certbot renew --dry-run

If everything is working, open your crontab window using the following terminal command.

sudo crontab -e

Next, please specify the time when it should auto-renew. This should be checked daily at a minimum, and if the certificate needs to be renewed, the script will not update the certificate. If you need help finding a good time to set, use the crontab.guru free tool.

00 00 */1 * * /usr/sbin/certbot-auto renew

How to Load Apache Module

For users who would like to load an apache module and learn how to disable it, the first task is to print what is already loaded on your apache server using the following command.

apachectl -M

Example output:

How to Install Apache on Ubuntu 22.04 LTS

Next, let us enable the SSL module.

sudo a2enmod ssl

Example output:

How to Install Apache on Ubuntu 22.04 LTS

The module is enabled successfully; now, reload or restart the service.

sudo systemctl restart apache2

Next, check to see if the module is loaded after the reboot.

apachectl -M

Example output:

How to Install Apache on Ubuntu 22.04 LTS

As above, the module is now present.

Next, to disable a module, use the following command.

sudo a2dismod ssl

Do not forget to reload or restart the Apache service.

sudo systemctl restart apache

The modules directory available can be found at /etc/apache2/mods-available/ and for modules enabled /etc/apache2/mods-enabled/.

ManageApache Service

Now that Apache is running on your server successfully, some management keynotes are as follows.

Apache Server Logs

Apache server logs can be found in the directory /var/log/apache2/ with access.log and error.log respectfully being the default access and error names given. This can be changed to other names in your virtual host configuration file in the future.

Apache Commands

The following commands that you will undoubtedly use in your day-to-day management when working with Apache. Some of the most common are:

Stop Apache webserver:

sudo systemctl stop apache2

Start Apache webserver:

sudo systemctl start apache2

Restart Apache webserver:

sudo systemctl restart apache2

Reload Apache webserver:

sudo systemctl reload apache2

Disable Apache on server boot:

sudo systemctl disable apache2

Enable Apache on server boot:

sudo systemctl enable apache2

How to Update Apache

To update Apache in the future, this is done with the command you used to check if your system is up to date. Note, always create backups or images if you have an Apache service running critical services. Usually, it’s pretty safe to upgrade, but sometimes bugs can occur like any software upgrade.

To update Apache, use the following command:

sudo apt update

If an Apache upgrade is available, use the command:

sudo apt upgrade

How to Remove (Uninstall) Apache

Removing Apache if you no longer use it can be done using the following command.

sudo systemctl disable apache2 --now

Now using the following command, remove Apache and all traces of data for complete removal.

sudo apt autoremove apache2 --purge -y

Leftover files may still exist in the /etc/apache2/ main folder, so let us remove this directory.

sudo rm -R /etc/apache2/

Note this will clear your custom configuration files. Make sure to back up if you may use it again on Github or a similar type of service.

Comments and Conclusion

In the tutorial, you have learned how to install Apache 2 on Ubuntu 22.04 LTS Jammy Jellyfish and create a virtual host file to get your server or website online.

In conclusion, Apache has been the most popular web application server for a very long time. However, Nginx has slowly caught up and overtaken the lead. This could be due to the increasing popularity of microservices and Nginx’s ability to handle larger loads more efficiently. Nevertheless, Apache remains one of the most deployed servers in use today. If you are starting with hosting your web applications, we recommend trying Apache first, as it is still one of the most accessible options to get started with.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!