Back

How to Install and Configure an Apache Web Server on Ubuntu

Introduction

Apache is one of the most popular web servers in the world, known for its flexibility and robustness. In this guide, you will learn how to install and configure Apache on an Ubuntu server, allowing you to host websites and applications.

Prerequisites

  • An updated Ubuntu server.
  • Access with a user who has sudo privileges.
Step 1: Update the System

Before starting, it’s good practice to update the system’s package index:

sudo apt update
sudo apt upgrade -y
Step 2: Install Apache

Install Apache using the apt package manager:

sudo apt install apache2 -y
Step 3: Verify Apache Installation

To ensure Apache is installed correctly, check the status of the service:

sudo systemctl status apache2

You should see an output indicating that Apache is active and running.

Step 4: Configure the Firewall

Ensure that the firewall allows HTTP and HTTPS traffic:

sudo ufw allow 'Apache Full'
Step 5: Test Apache

Open a web browser and navigate to your server’s public IP address (e.g., http://your_server_ip). You should see Apache’s default page.

Step 6: Configure Virtual Hosts (Optional)

To host multiple websites on the same server, you can configure virtual hosts.

  • Create a directory for your site:
sudo mkdir -p /var/www/your_domain.com/html
sudo chown -R $USER:$USER /var/www/your_domain.com/html
  • Create a configuration file for the virtual host:
sudo nano /etc/apache2/sites-available/your_domain.com.conf
  • Add the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain.com
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /var/www/your_domain.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • Enable the new virtual host:
sudo a2ensite your_domain.com.conf
sudo systemctl reload apache2

Conclusion

You have successfully installed and configured Apache on Ubuntu. You are now ready to host your websites.

Nexto
Nexto