The Apache HTTP Server, commonly called Apache, is a free and open-source web server software. It played a key role in the early growth of the World Wide Web and remains one of the most popular web servers in the world. It is known for its flexibility, power, and broad compatibility.
Prerequisites
- A Linux distribution (this guide focuses on Ubuntu/Debian).
- Root access or
sudoprivileges.
Step 1: Installing Apache
Apache is available in Ubuntu's default software repositories, which makes installation very simple.
# Update the package list
sudo apt update
# Install Apache2
sudo apt install -y apache2
Step 2: Configuring the firewall
If a firewall is active on your server (such as UFW), you need to allow web traffic.
# List the application profiles for UFW
sudo ufw app list
# Allow traffic on ports 80 (HTTP) and 443 (HTTPS)
sudo ufw allow 'Apache Full'
# Check the status
sudo ufw status
Step 3: Verify that the web server is working
Once the installation is complete, the Apache service should start automatically. You can verify it.
sudo systemctl status apache2
To confirm that it is accessible from the outside, open a web browser and enter your server's IP address. You should see the default Apache2 welcome page for Ubuntu.
If you see the default page, your Apache web server is installed and operational.
Step 4: Set up a site with a Virtual Host
"Virtual Hosts" allow you to host multiple websites on a single server. It is good practice to create a virtual host for each site, even if you only have one.
1. Create the site directory
We will create a directory for our `example.com` site in `/var/www`.
sudo mkdir -p /var/www/example.com/html
2. Set the correct permissions
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
3. Create a sample page
nano /var/www/example.com/html/index.html
Add this simple HTML content:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
4. Create the Virtual Host file
Apache stores site configurations in `/etc/apache2/sites-available`. Let's create one for our site.
sudo nano /etc/apache2/sites-available/example.com.conf
Paste this basic configuration:
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
5. Enable the new site
Apache uses symbolic links to enable sites. The `a2ensite` tool makes this easy.
# Enable the new site
sudo a2ensite example.com.conf
# Disable the default site
sudo a2dissite 000-default.conf
# Check the configuration for syntax errors
sudo apache2ctl configtest
# Reload Apache so the changes take effect
sudo systemctl reload apache2
If you have configured your DNS locally (via the `hosts` file) or publicly so that `example.com` points to your server's IP, you should now see your new welcome page when accessing it.
Conclusion
You now have a working Apache web server, capable of hosting one or more sites. This is the foundation of many web applications and websites. The next steps usually involve securing your site with HTTPS (using Let's Encrypt and Certbot), installing a programming language such as PHP, or setting up a database such as MySQL.
Comments