Install and Run Apache Web Server on Debian 12 with Easy Steps

Posted on

Install and Run Apache Web Server on Debian 12 with Easy Steps

Install and Run Apache Web Server on Debian 12 with Easy Steps

This tutorial aims to guide you through the process of installing and running the Install and Run Apache Web Server on Debian 12 Bookworm and creating an Apache Virtual Host file. Apache is a widely-used, open-source web server. Debian 12 comes with the latest Apache release, version 2.4.57. Follow the steps outlined below to Install and Run Apache Web Server on Debian 12 on your system.

Before you begin, ensure you have access to your server as a non-root user with sudo privileges and that you have set up a basic firewall. You can refer to the guide on Initial Server Setup with Debian 12 Bookworm for assistance with this.

Additionally, you’ll need a domain name pointed to your server’s IP address.

Now, let’s proceed with the steps to Install and Run Apache Web Server on Debian 12.

Step 1 – Install Apache From the Command Line on Debian 12

First, update your system’s package list:

sudo apt update

Next, install Apache using the following command:

sudo apt install apache2 -y

During installation, the Apache server should be activated. Verify its status with:

sudo systemctl status apache2

The output should resemble the following:

**Output**
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: **active** (**running**) since Sat 2023-06-17 04:56:20 EDT; 5s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1074 (apache2)
      Tasks: 55 (limit: 4653)
     Memory: 13.4M
        CPU: 84ms
     CGroup: /system.slice/apache2.service
...

Step 2 – Manage and Run Apache Web Server From Command Line

After installing Apache, it’s important to know some basic management commands.

To stop the web server:

sudo systemctl stop apache2

To start it again:

sudo systemctl start apache2

To restart the service:

sudo systemctl restart apache2

If you make configuration changes, reload the service:

sudo systemctl reload apache2

To disable the service:

sudo systemctl disable apache2

To re-enable the service to start on boot:

sudo systemctl enable apache2

Step 3 – Adjust UFW Firewall For Apache on Debian 12

Assuming you have enabled the UFW firewall, adjust the settings to allow external access to web ports. Allow Apache HTTP traffic:

sudo ufw allow 'WWW'

Reload the UFW firewall to apply the changes:

sudo ufw reload

Check the UFW status:

sudo ufw status

The output should show:

**Ouput**
Status: active

To                         Action      From
--                         ------      ----
WWW                        ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
WWW (v6)                   ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

Step 4 – Check Apache Web Server Status on Debian 12

Verify the Apache installation by accessing the default page.

Obtain your server’s IP address:

hostname -I

Alternatively, use the icanhazip tool:

curl -4 icanhazip.com

Enter your server’s IP address in a web browser:

http://your_server_ip

You should see the default Apache page:

[Image of default Apache page]

This confirms that Apache is working correctly.

Step 5 – Create a Virtual Host File in Apache Debian 12

Now, create your first virtual host file.

sudo mkdir /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com
sudo vi /var/www/example.com/index.html

Add the following content to the file:

<html>
<head>
<title>Welcome to orcacore.com!</title>
</head>
<body>
<h1>Success! The orcacore.com virtual host is working!</h1>
</body>
</html>

Save and close the file.

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

Add the following content to the file:

<VirtualHost *:80>
ServerAdmin admin@example.com
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>

Save and close the file.

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest

The output should be:

Syntax OK
sudo systemctl restart apache2

Test your Apache virtual host configuration:

http://example.com

You should see your Apache virtual host test page:

[Image of Apache virtual host test page]

For more information, visit Apache HTTP Server Documentation.

Conclusion

You have now learned to Install and Run Apache Web Server on Debian 12 Bookworm, manage the Apache service, adjust the UFW firewall, and create an Apache virtual host file.

Alternative Solutions for Serving Web Content on Debian 12

While Apache is a robust and popular choice, alternative solutions exist for serving web content on Debian 12. Here are two distinct approaches:

1. Using Nginx as a Reverse Proxy in front of Apache

This approach involves using Nginx, another popular web server, as a reverse proxy. Nginx excels at handling static content and managing concurrent connections efficiently. By placing Nginx in front of Apache, you can offload static content serving and improve overall performance, especially under heavy load. Apache can then focus on processing dynamic content.

Explanation:

Nginx sits between the client (web browser) and Apache. When a request comes in, Nginx determines if it can serve the content itself (e.g., images, CSS, JavaScript). If so, it does so directly, bypassing Apache. For dynamic content (e.g., PHP scripts), Nginx forwards the request to Apache. This architecture allows Nginx to handle a large volume of requests quickly, while Apache handles the more complex processing.

Code Example:

First, install Nginx:

sudo apt update
sudo apt install nginx -y

Configure Nginx to proxy requests to Apache. Create a new Nginx configuration file (e.g., /etc/nginx/sites-available/example.com):

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;  # Assuming Apache is running on port 8080
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Serve static files directly (optional)
    location ~* .(jpg|jpeg|png|gif|css|js|ico)$ {
        root /var/www/example.com; # Path to your static files
        expires 30d;
    }
}

Enable the new configuration and disable the default one:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Finally, restart Nginx:

sudo systemctl restart nginx

In this setup, you would also need to configure Apache to listen on a different port (e.g., 8080) and update your virtual host configuration accordingly. This ensures Nginx can properly proxy the requests.

2. Using a Lightweight Web Server like Lighttpd or Caddy

Another alternative is to use a lightweight web server such as Lighttpd or Caddy. These servers are designed to be resource-efficient and easy to configure. They are particularly suitable for serving static content or for smaller websites with moderate traffic.

Explanation:

Lighttpd and Caddy consume fewer system resources compared to Apache. Caddy, in particular, is known for its automatic HTTPS configuration and simplified configuration process. They are excellent choices when performance and ease of use are prioritized over the advanced features and flexibility of Apache.

Code Example (Using Caddy):

First, install Caddy. The specific steps may vary depending on your Debian 12 setup. Refer to the official Caddy documentation for the most up-to-date installation instructions: https://caddyserver.com/docs/install.

Once installed, create a Caddyfile in your site’s root directory (e.g., /var/www/example.com/Caddyfile):

example.com {
    root * /var/www/example.com
    file_server
}

This simple configuration tells Caddy to serve files from the specified root directory. Caddy automatically handles HTTPS using Let’s Encrypt by default.

To start Caddy, navigate to the directory containing the Caddyfile and run:

sudo caddy run

Caddy will automatically acquire a TLS certificate and start serving your website over HTTPS. The configuration is much simpler than the equivalent setup in Apache. This alternative web server is very lightweight compared to apache.