Install PHP from prebuilt packages on Alpine Linux with Apache HTTP server and PostgreSQL

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

PHP.earth Alpine repository provides the latest PHP versions.

Adding PHP.earth repository to your Alpine:

apk add --no-cache wget ca-certificates \
&& wget -O /etc/apk/keys/phpearth.rsa.pub https://repos.php.earth/alpine/phpearth.rsa.pub \
&& echo "https://repos.php.earth/alpine/v3.7" >> /etc/apk/repositories

PHP.earth 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 Apache HTTP server

apk add --no-cache apache2

Install PostgreSQL

apk add --no-cache postgresql

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.