How to Install Apache Web Server on Ubuntu/Debian Print

  • 0

Introduction

Apache is a widely used open-source web server that supports various modules to extend functionality, such as handling dynamic content (PHP, Python), security, and caching. Apache is flexible and customizable, supporting virtual hosts to serve multiple sites simultaneously.

In this guide, we will install Apache on Ubuntu (20.04 or newer) and Debian (11 or newer). For editing configuration files, we will use vi, but you may prefer nano for easier pasting and editing.

 

Installation Guide

 

1. Update the System

apt update && apt upgrade -y

 

2. Install Apache

apt install apache2 -y

 

3. Start Apache

systemctl start apache2
systemctl enable apache2
systemctl status apache2

 

4. Open Ports 80 and 443

Open ports in your firewall for HTTP and HTTPS:

IPtables:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
apt install iptables-persistent
iptables-save | tee /etc/iptables/rules.v4

UFW:

ufw allow 'Apache Full'
ufw reload

 

5. Create a New Virtual Host

Create a configuration file for your domain:

vi /etc/apache2/sites-available/your_domain.ltd.conf

Paste your virtual host configuration inside the file (replace your_domain.ltd and admin@your_domain.ltd with your actual domain and email).

 

<VirtualHost *:80>
ServerAdmin admin@your_domain.ltd
ServerName your_domain.ltd

DocumentRoot /var/www/html/your_domain.ltd
DirectoryIndex index.html index.php

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

 

Disable the default virtual host:

a2dissite 000-default

Enable your new virtual host:

a2ensite your_domain.ltd
systemctl reload apache2

 

6. Create a Test Page

mkdir -p /var/www/html/your_domain.ltd
vi /var/www/html/your_domain.ltd/index.html

Paste your HTML content, then save and exit (Esc → :wq → Enter).

chown -R www-data:www-data /var/www/html/your_domain.ltd
systemctl restart apache2

 

7. Set Up the SSL Certificate

apt install certbot python3-certbot-apache

Enable SSL for your domain (replace your_domain.ltd and admin@your_domain.ltd accordingly):

certbot --apache --agree-tos --redirect -d your_domain.ltd -m admin@your_domain.ltd
systemctl restart apache2

Verify SSL in your browser by visiting your test page via HTTPS. Click the padlock icon to view certificate details.

 

8. Manual and Automatic SSL Renewal

Certificates are valid for 90 days. To manually renew:

certbot renew

Certbot usually sets up a cron job in /etc/cron.d/ to automatically renew certificates:

cat /etc/cron.d/certbot

 

You're all set! Certbot will handle automatic renewals and reload Apache as needed.


Was this answer helpful?

« Back

Powered by WHMCompleteSolution