Nov 18, 2019 9:30 AM Sep 12, 2020 3:15 AM

Installing Apache on Debian/Ubuntu

In this article I will explain how to install an Apache server on a Debian/Ubuntu operating system.

This instruction was executed on operating systems: Debian 10, Debian 9, Ubuntu 20.10, Ubuntu 20.04, Ubuntu 19.10.

Installing Apache server

Before any software installation, it is recommended to update the list of repository packages by running the command in the terminal:

sudo apt-get update

Install the Apache server by running the command in the terminal:

sudo apt-get install apache2

Apache installation command

Checking Apache status

Let's check if Apache is enabled to start automatically when the system is booted by running the command in the terminal (expected result "enabled"):

sudo systemctl is-enabled apache2

Let's check if Apache is active at the moment by running the command in the terminal (expected result "active"):

sudo systemctl is-active apache2

We can also check the Apache status with additional information about the service by running the command in the terminal:

sudo systemctl status apache2

–°hecking Apache status

If the Apache server is not installed, an error will be displayed when checking the status:

Error checking Apache status

Testing Apache access

Let's make sure that the Apache server is working by entering the IP address of your server in the browser:

http://192.168.0.101

or for localhost

http://localhost

If all is well, you will see the Apache welcome page:

Apache welcome page

Testing PHP on Apache

Install PHP by running the command in the terminal:

sudo apt-get install php7.4

You can learn more about how to install PHP 7.x on Debian and how to install PHP 7.x on Ubuntu.

Create a test file that will return information about our server by running the command in the terminal:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Let's make sure that the Apache server correctly displays the content generated by the PHP script by opening this page in the browser:

http://192.168.0.101/info.php

or for localhost

http://localhost/info.php

A similar page will be displayed:

Testing PHP on Apache

After checking for security reasons, you need to delete this file by running the command in the terminal:

sudo rm /var/www/html/info.php

Apache process management

To manage the server, you may need the following commands.

Start Apache

Start the Apache server by running the command in the terminal:

sudo service apache2 start

Stop Apache

Stop the Apache server by running the command in the terminal:

sudo service apache2 stop

Restart Apache

Restart the Apache server by running the command in the terminal:

sudo service apache2 restart

Verifying Apache configuration

Before restarting the Apache server, you need to check the configuration for validity by running the command in the terminal:

sudo apache2 -t

Reloading Apache configuration

We can apply configuration changes without losing connections by running the command in the terminal:

sudo service apache2 reload

Other articles