Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Apache Setup in Debian/Ubuntu

Tip

This works for Raspberry Pi as well, if you are running Raspberry Pi OS (a Debian derivative)

Setup

Install Apache, with PHP support:

sudo apt update

sudo apt install apache2 php libapache2-mod-php php-cli

Optionally, add support for some commonly-used PHP modules:

sudo apt install php-mysql php-curl php-gd php-mbstring php-xml

The PHP module will be enabled automatically.

Restart Apache:

sudo systemctl enable --now apache2

sudo systemctl restart apache2

Verify that both services are available and working:

systemctl status apache2

php -v

Test

Create this test page as /var/www/html/info.php:

<!DOCTYPE html>
<html>

<head>
    <title>PHP Test Site</title>
</head>

<body>
    <?php
    phpinfo();
    ?>
</body>

</html>

Then open it in a web browser to test.

Locally: http://localhost/info.php

or

From another computer: http://SERVER_IP/info.php

If it displays the PHP information page, Apache is processing PHP correctly.

Important

Remove the test file afterward because it exposes configuration details.

Serve Additional Sites

This will use “site1”, on port 8080, as an example.

Create directory with index file

Create local directory:

sudo mkdir -p /var/www/site1

Add a simple index file:

echo "<html><body><p>Site 1</p></body></html>" | sudo tee /var/www/site1/index.html

Add ports to Apache

Edit the ports configuration:

sudo vi /etc/apache2/ports.conf

Add:

Listen 8080

Create virtual-host configuration

Create /etc/apache2/sites-available/site1.conf and add:

<VirtualHost *:8080>
    ServerName site1.local

    DocumentRoot /var/www/site1

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

    ErrorLog ${APACHE_LOG_DIR}/site1-error.log
    CustomLog ${APACHE_LOG_DIR}/site1-access.log combined
</VirtualHost>

Note

The ServerName value is only a label. Since the site uses a different port, you don’t need a subdomain or local DNS.

Enable the site

sudo a2ensite site1.conf

Test the configuration:

sudo apachectl configtest

Restart Apache:

sudo systemctl reload apache2

Test locally and over the network

On the Apache server:

curl http://localhost:8080

From another device on the LAN, open: http://SERVER_IP:8080