How to Install Apache Web Server on Alpine Linux

Apache is a free and open-source web server developed and maintained by the Apache Foundation, which is satisfactorily fast and easy to install and configure.

It provides lots of modules for customizing your site to your preference. For example, you can enable SSL for your website, configure virtual hosts, and so on.

In this guide, we will demonstrate how to install and configure the Apache webserver on Alpine Linux.

Install Apache Webserver in Alpine Linux

To get started, log into your Alpine Linux instance and update the package index using the following apk command.

# apk update

Once the repositories have been refreshed, install the Apache webserver as shown.

# apk add apache2
Install Apache in Alpine Linux
Install Apache in Alpine Linux

Next, start the Apache web server, run the command:

# rc-service apache2 start
OR
# service apache2 start

To confirm or verify that Apache is running, execute the command.

# service apache2 status
Check Apache in Alpine Linux
Check Apache in Alpine Linux

Alternatively, you can browse the IP address of your Alpine Linux on a web browser and this is what you will get.

http://Server-IP-Address
Check Apache Page in Alpine Linux
Check Apache Page in Alpine Linux

This is a sure confirmation that the Apache webserver was installed.

Hosting a Website with Apache on Alpine Linux

In Alpine Linux, the Document root is located in the /var/www/localhost/htdocs path. This is where the default index.html file is found whose content we have just seen on the browser.

To host your own site, rename or delete the default index.html file and place or create your own.

Here is a sample of our custom file.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Test Site</title>
  </head>
  <body>
    <h1>Success! The Test Site is working!</h1>
  </body>
</html>

Save the file and reload Apache.

# service apache2 restart

Head over to your browser and reload the page. Your site will be displayed as shown.

Check Website Page
Check Website Page

Install PHP on Alpine Linux

If you are intending to host PHP sites, then you need to install PHP for Apache, run the command:

# apk add php$phpverx-apache2
Install PHP in Alpine Linux
Install PHP in Alpine Linux

To confirm that PHP is installed, create an info.php file in the Document root.

$ sudo nano /var/www/localhost/htdocs/info.php

Paste the following lines of code.

<?php
phpinfo():
?>

Save the file and exit.

Then browse the following URL to access the PHP page.

http://Server-IP-Address/info.php

You should get this page, a clear indication that PHP has successfully been installed on Alpine Linux.

Check PHP in Alpine Linux
Check PHP in Alpine Linux

And it’s a wrap! In this guide, we have demonstrated how you can install the Apache webserver on Alpine Linux.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.