How to install/upgrade Apache web server on Ubuntu

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Apache HTTP server is one of the most widely used web servers on the internet. Known for its robustness, it is the web server of choice for many beginners and professionals alike. This tutorial will guide you through the installation and upgrade process of Apache web server on an Ubuntu Linux system. We will start with the basics and gradually move on to more advanced configurations.

Prerequisites

  • A machine running Ubuntu Linux
  • Access to a user account with sudo privileges
  • An active internet connection

Installing Apache

To install Apache on your Ubuntu system, follow these steps:

  1. Open a terminal window.
  2. Update the package list with the following command:
    sudo apt update
  3. Install Apache by running:
    sudo apt install apache2

Once the installation is complete, you can verify that Apache is running by typing the following command:

sudo systemctl status apache2

If Apache is running, you will see an active status in the output.

Configuring the Firewall

It’s essential to allow traffic to your Apache server through the firewall. Use the following commands:

sudo ufw allow 'Apache Full'

Check the ufw status to ensure the rules are applied:

sudo ufw status

Upgrading Apache

If you already have Apache installed and want to upgrade to the latest version, the process is similarly straightforward. Run the following commands:

  1. Update the package list:
    sudo apt update
  2. Upgrade the installed packages, including Apache, with the command:
    sudo apt upgrade

If there are any configuration file changes, you may be prompted during the upgrade on how to proceed. Select the appropriate option as per your requirements.

Advanced Apache Configurations

For advanced users, there are a multitude of configuration options available within Apache. Some of these include setting up virtual hosts, secure connections using SSL/TLS, and optimizing performance.

Creating Virtual Hosts

Create and edit a new virtual host file using your preferred text editor, such as nano:

sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following content, substituting your_domain with your actual domain:

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

To enable the new virtual host file, run:

sudo a2ensite your_domain.conf

Remember to disable the default site if not needed:

sudo a2dissite 000-default.conf

After making changes, always restart Apache to apply them:

sudo systemctl restart apache2

Enabling SSL/TLS

To secure your Apache server with SSL/TLS, first install the certbot software and its Apache plugin with the command:

sudo apt install certbot python3-certbot-apache

Next, run certbot to obtain a free Let’s Encrypt SSL certificate:

sudo certbot --apache

The certbot tool will guide you through the process of obtaining and installing the SSL certificate. After completion, verify the HTTPS version of your site is accessible.

Monitoring and Troubleshooting

For monitoring the health and performance of your Apache server, consider checking server status:

sudo apt install apache2-utils

Then enable the mod_status module and configure it to your needs to see server metrics in real-time.

Additional troubleshooting may involve examining log files located in /var/log/apache2/.

Conclusion

By following the steps in this tutorial, you now have a working Apache server on your Ubuntu system. Whether you’re setting up a local environment for development or deploying to a production server, Apache remains a versatile and reliable choice.