Install PHP from prebuilt packages on Alpine Linux with Lighttpd and MongoDB

Install PHP on Alpine Linux

First, let's install PHP. Alpine Linux official repositories contain slightly outdated PHP versions. You can install PHP on Alpine Linux using apk tool:

apk add --no-cache php7

3rd party repository Alpine repository provides the latest PHP versions.

Adding repository to your Alpine:

apk add --no-cache wget ca-certificates \
&& wget -O /etc/apk/keys/ \
&& echo "" >> /etc/apk/repositories PHP packages are prefixed with php7.2, php7.1 and php7.0. Installing latest PHP is as simple as:

apk add --no-cache php7.2

Install Lighttpd

apk add --no-cache lighttpd

Configure Lighttpd

After successful installation, create a new site specific configuration file. For example, /etc/lighttpd/localhost.conf. This file can reside in any location, but we'll use this /etc/lighttpd for this example.

# /etc/lighttpd/localhost.conf

# Site public root folder
server.document-root = "/var/www/app/public"

# Server port
server.port = 80

# Assigning file mimetypes
mimetype.assign = (
  ".html" => "text/html",
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png"

# Index file
index-file.names = ( "index.html" )

Test your configuration file:

lighttpd -t -f /etc/lighttpd/localhost.conf

And start webserver:

lighttpd -D -f /etc/lighttpd/localhost.conf

Install MongoDb

apk add --no-cache mongodb

Development on localhost using built-in web server

If you're developing PHP applications on your local workstation, PHP also has a useful built-in web server for localhost development. It is not intended to be used for production environments.

Inside your project folder you can run it from the command line using PHP CLI SAPI:

php -S localhost:8000

Visit http://localhost:8000/ in your browser to access it.