How to Install Apache Web Server on Debian 12 (Complete Guide)
Hello geekandnix fellow, in this tutorial, I will show you how to install the Apache web server on Debian 12 Bookworkm.
By completing this tutorial, you will have the basic knowledge of Apache configuration files and directories, how to create an Apache virtual host, how to manage Apache modules and services, and the last step-by-step to debug the Apache web server.
Let’s get things done!
Prerequisites
Before proceeding, here are the requirements:
- A Debian 12 server initialized with the non-root user as administrator privileges and installed UFW (Uncomplicated Firewall). Use this excellent guide for initial server setup with Debian 12.
- A domain name – public or local domain for Apache virtual host.
Installing Apache on Debian
Apache is a web server that powers 30.2% of websites on the internet. On Linux, Apache is available by default on most distribution repositories, including Debian. In this case, you’ll install Apache 2.4 using APT via the Debian repository.
To install the Apache web server on Debian, follow these steps:
1. First, run the apt
command below to update your Debian package index and get the latest package information.
sudo apt update
Get:1 http://security.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Hit:2 http://deb.debian.org/debian bookworm InRelease
Get:3 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:4 http://security.debian.org/debian-security bookworm-security/main Sources [84.3 kB]
Fetched 188 kB in 6s (33.7 kB/s)
Reading package lists... Done
2. Now, run the command below to install Apache to your Debian server. Enter y
to proceed with the installation.
sudo apt install apache2
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0
0 upgraded, 9 newly installed, 0 to remove and 34 not upgraded.
Need to get 2,279 kB of archives.
After this operation, 8,220 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian bookworm/main amd64 libapr1 amd64 1.7.2-3 [102 kB]
Get:2 http://deb.debian.org/debian bookworm/main amd64 libaprutil1 amd64 1.6.3-1 [87.8 kB]
3. After you’ve installed Apache, let’s take a look at the Apache configuration files and directories that the administrator must know.
Main Apache configurations:
/etc/apache2/
: the default configuration directory for Apache web server./etc/apache/apache.conf
: The main config file for Apache./etc/apache/ports.conf
: The config file for managing Apache ports. By default, Apache uses standard HTTP port 80 and HTTPS port 443.
Global configurations for Apache web server:
/etc/apache2/conf-availabe
: The directory where Apache global configuration is stored./etc/apache2/conf-enabled
: The directory where Apache global configuration is enabled.
Apache virtual host configuration directory:
/etc/apache2/sites-available
: This is where you store Apache virtual host files./etc/apache2/sites-enabled
: In this directory, you will find enabled Apache virtual hosts.
Apache modules configuration directory:
/etc/apache2/mods-available/
: The directory where Apache modules are stored./etc/apache2/mods-enabled/
: The directory where enabled Apache modules are stored.
The default DocumentRoot and log directory for Apache:
/var/www/html
: This is the default DocumentRoot directory of Apache, where theindex.html
is stored./var/log/apache2
: If you’re usingrsyslog
, then your Apache log files will be stored in this directory, both access and error logs.
4. Lastly, check the following programs for managing the Apache web server:
apache2
service: the default Apache service name that you can manage via thesystemctl
utility.apachectl
: The command line interface for managing the Apache web server, including start, stop, restart, verify Apache syntax, and listing virtual hosts.a2enmod
anda2dismod
: To enable Apache module, run thea2enmod
command. When you need to disable the Apache module, execute thea2dismod
command.a2ensite
anda2disite
: When you want to enable Apache virtual host, run thea2ensite
command. If you want to disable the virtual host, then use thea2ensite
command.a2enconf
anda2disconf
: To enable Apache global configuration within the/etc/apache2/conf-available/
directory, run the commanda2enconf
. If you need to disable the configuration, then run thea2disconf
command.
Allowing traffic to Apache via UFW
With the Apache installed, you must ensure that access to the Apache web server (HTTP and HTTPS) is permitted by the server. You’ll open both HTTP and HTTPS protocols on Debian via UFW (Uncomplicated Firewall) and allow traffic to Apache.
To open HTTP and HTTPS ports via UFW (uncomplicated Firewall), execute the following actions:
1. Run the command below to check the UFW application profile WWW Full
.
sudo ufw app info 'WWW Full'
As you can see below, the WWW Full
profile will open ports 80 and 443 for the Apache web server.
Profile: WWW Full
Title: Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)
Ports:
80,443/tcp
2. Run the ufw allow
command below to enable the WWW Full
profile. If successful, you will see an output Rule added
.
sudo ufw allow 'WWW Full'
Rule added
Rule added (v6)
3. Now, run the command below to check the UFW status.
sudo ufw status
As pointed out below, traffic to the WWW Full
profile is allowed.
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
WWW Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
WWW Full (v6) ALLOW Anywhere (v6)
4. Lastly, open your preferred web browser and visit http://SERVER-IP/. If your Apache and UFW configuration succeeds, you will see the Apache default index.html
page.
How to create Apache virtual host on Debian
Now that traffic to Apache is permitted, let’s move on to create a virtual host on Apache.
The term virtual host is a process of running multiple websites such as domain1.com and domain2.com on a single machine. The virtual host allows you to host multiple websites with an Apache web server on a single Debian server.
Before creating a virtual host, prepare the following:
- A domain name, whether public or local.
- DocumentRoot where your web application is stored.
- Optional: Path of access and error logs for easier debugging when errors occur.
In this demo, you’ll create a new Apache virtual host with the following:
Domain Name | DocumentRoot | Log Path |
---|---|---|
mysite.com | /var/www/mysite | /var/log/apache2/mysite-access.log /var/log/apache2/mysite-error.log |
To create an Apache virtual host, complete the following steps:
1. First, run the command below to create a new DocumentRoot directory /var/www/mysite
and default index.html
page.
mkdir -p /var/www/mysite
echo "<h1>Welcome to mysite.com - Geekandnix</h1>" > /var/www/mysite/index.html
2. Run the chmod
command below to change the ownership of the /var/www/mysite
directory to user www-data
. Applying this, you will allow the Apache web server to read and write files within the /var/www/mysite
directory.
sudo chown -R www-data:www-data /var/www/mysite
3. Now, run the vim
editor to create a new virtual host configuration /etc/apache2/sites-available/mysite.conf
.
sudo nano /etc/apache2/sites-available/mysite.conf
Insert the configuration below and make sure to change the DocumentRoot, ServerName, and path log files with your information.
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin [email protected]
DocumentRoot /var/www/mysite
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
</VirtualHost>
When done, save and exit the file.
4. After you’ve created a virtual host file, run the a2ensite
command below to activate the virtual host mysite.conf
. This will create a symlink of the file mysite.conf
to the /etc/apache2/sites-enabled/
directory.
sudo a2ensite mysite.conf
Enabling site mysite.
To activate the new configuration, you need to run:
systemctl reload apache2
5. Next, run the following apachectl
command to verify your Apache syntax and list virtual hosts.
sudo apachectl configtest
sudo apachectl -S
If you get the output Syntax OK, it means that you have proper Apache syntax. When the virtual host enabled, you should see the file mysite.conf
are listed in the VirtualHost configuration section.
Syntax OK
...
VirtualHost configuration:
*:80 is a NameVirtualHost
default server 127.0.2.1 (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost 127.0.2.1 (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost mysite.com (/etc/apache2/sites-enabled/mysite.conf:1)
ServerRoot: "/etc/apache2"
6. Now, run the systemctl
command below to restart Apache and implement your changes.
sudo systemctl restart apache2
7. Lastly, back to your web browser and visit your domain name such as http://mysite.com/. If your virtual host configuration succeeds, you should see the index.html
page that you have created.
How to Use Apache modules on the Debian system?
Now that you have created an Apache virtual host, learn how to enable and disable Apache modules via the command line.
Apache web server is famous for its flexibility and modularity. Apache modules allow administrators to extend the functionality of the Apache web server, you can easily turn on or turn off the Apache module via the command line.
Below are some of the most used Apache modules:
- mod_rewrite
- mod_deflate
- mod_cache
- mod_ssl
- mod_proxy
- mod_security
To enable and use Apache modules, follow the steps below:
1. To see the list of available modules in Apache, check the /etc/apache2/mods-available
directory with the command below.
ls -ah /etc/apache2/mods-available
. authz_host.load dialup.load ldap.conf proxy_express.load session_dbd.load
.. authz_owner.load dir.conf ldap.load proxy_fcgi.load session.load
access_compat.load authz_user.load dir.load log_debug.load proxy_fdpass.load setenvif.conf
actions.conf autoindex.conf dump_io.load log_forensic.load proxy_ftp.conf setenvif.load
actions.load autoindex.load echo.load lua.load proxy_ftp.load slotmem_plain.load
alias.conf brotli.load env.load macro.load proxy_hcheck.load slotmem_shm.load
alias.load buffer.load expires.load md.load proxy_html.conf socache_dbm.load
allowmethods.load cache_disk.conf ext_filter.load mime.conf proxy_html.load socache_memcache.load
--SNIP--
2. Now, run the a2enmod
command below to enable Apache modules. In this example, you will enable the rewrite
module for Apache.
a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
3. If you want to disable a specific Apache module, run the a2dismod
command below.
a2dismod modulename
4. After you have enabled or disabled the module, run the command below to restart the apache2
service.
sudo systemctl restart apache2
5. Lastly, run the apachectl
command below to check the enabled Apache modules.
sudo apachectl -M
You can see the Apache rewrite
module is enabled.
Loaded Modules:
...
rewrite_module (shared)
setenvif_module (shared)
status_module (shared)
Managing Apache service on Debian
So far, you have created an Apache virtual host and learned how to manage Apache modules. Following this, you’ll be managing Apache service via the systemctl
command.
Learn how to manage Apache service with the following:
1. To start the Apache web server, run the following systemctl
command.
sudo systemctl start apache2
2. Once Apache starts, verify it using the command below. If Apache running, you should get the output active (running)
.
sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running) since ; 31s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 5726 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 5732 (apache2)
Tasks: 55 (limit: 2307)
Memory: 14.8M
CPU: 173ms
CGroup: /system.slice/apache2.service
├─5732 /usr/sbin/apache2 -k start
├─5733 /usr/sbin/apache2 -k start
└─5734 /usr/sbin/apache2 -k start
3. Additionally, run the ss
command below to check the Apache port. If your Apache web server is running, you should see that 80 or 443 are used by the apache2
program.
ss -tulpn | grep apache
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("apache2",pid=5734,fd=3),("apache2",pid=5733,fd=3),("apache2",pid=5732,fd=3))
4. When you need to stop the apache2
service, run the command below.
sudo systemctl stop apache2
5. Lastly, run the following command to restart the apache2
service when needed. Especially after making changes to Apache configuration or enabling Apache modules.
sudo systemctl restart apache2
How to debug Apache when an error occurs?
When configuring Apache, sometimes errors will occur. As an administrator, you must know the basics of how to debug the Apache web server, which will make the debugging process faster.
To debug the Apache web server, follow the sequences below:
1. First, run the command below to check the command below to ensure you have the correct Apache syntax. This will be the first thing to do when debugging Apache, and make sure you have the output Syntax is OK
.
sudo apachectl configtest
2. Run the command below to verify the apache2
service status. Make sure that the Apache web server is running.
sudo systemctl status apache2
3. Optionally, you can also run the ss
command below to check the Apache port. If the Apache web server running, you should get ports 80 and 443 are used by the Apache program.
ss -tulpn | grep apache
4. Now, visit your domain name via a web browser or check it via the curl
command below. If you get the HTTP status code 4xx or 5xx, that means that you have an error in your Apache web server.
curl -I http://mysite.com/
HTTP/1.1 200 OK
Date: Fri, 15 Mar 2024 20:17:18 CET
Server: Apache/2.4.57 (Debian)
Last-Modified: Fri, 15 Mar 2024 20:11:58 CET
ETag: "2c-613b89ebdeeee"
Accept-Ranges: bytes
Content-Length: 44
Content-Type: text/html
5. Next, check the log files (access and error logs) of your virtual host with the cat
or tail
command below.
# show all logs
cat /var/log/apache2/mysite-error.log
# show last log
tail -f /var/log/apache2/mysite-error.log
6. Additionally, you can also use the journalctl
command below to get logs of the apache2
service.
# same cat command
journalctl -u apache2
# same as tail command
journalctl -t -u apache2
Conclusion
Well done! You have installed the Apache web server, created a virtual host, and managed Apache modules and services on the Debian 12 server. Furthermore, you also have learned step-by-step how to debug the Apache web server when errors/problems occur.
With that knowledge in your arsenal, you; ‘re ready to deploy applications with Apache. From here, you can also follow this path to deploy your application:
- How to install PHP on Debian 12.
- How to install and use MariaDB Server on Debian 12.
- How to install and use MySQL 8.0 on Debian 12.
If you are interested in an alternative to Apache, take a look at How to install the Nginx web server on Debian 12.