In today’s guide we will cover the installation of Nginx Web server with PHP-FPM on Alpine Linux server. Nginx is high performance, open source web server which can also be used as reverser proxy. When hosting a PHP application with Nginx web server, a FastCGI Process Manager is required for loading web pages. FPM is the recommended FastCGI handler to be used with Nginx.

Login to your Alpine Linux Server and make sure it is updated. If converting this steps to container image build process, the commands are to be added to your Dockerfile.

$ sudo apk update
v3.7.3-183-gcc9ad2b48d []
v20200626-2349-g06046a5a3e []
v20200626-2353-g8dcf96ab2b []
OK: 16383 distinct packages available

Also upgrade list of packages installed.

sudo apk upgrade

Once you’ve update application list index on Alpine Linux you can proceed to install and configure Nginx with PHP-FPM.

Install Nginx on Alpine Linux

Use the apk command line tool to install Nginx web server on Alpine Linux.

$ sudo apk add nginx
(1/3) Installing pcre (8.44-r0)
(2/3) Installing nginx (1.18.0-r3)
Executing nginx-1.18.0-r3.pre-install
(3/3) Installing nginx-openrc (1.18.0-r3)
Executing busybox-1.32.0-r1.trigger
2 errors; 114 MiB in 59 packages

Start nginx service after installation.

$  sudo rc-service nginx start
 * Caching service dependencies ...                                                                                                                         [ ok ]
 * /run/nginx: creating directory
 * /run/nginx: correcting owner
 * Starting nginx ...                                                                                                                                       [ ok ]

Confirm service status:

$ rc-service nginx status
 * status: started

Set nginx service to be started at system boot.

$ sudo rc-update add nginx default
 * service nginx added to runlevel default

Install curl:

$ sudo apk add curl
(1/5) Installing ca-certificates (20191127-r4)
(2/5) Installing brotli-libs (1.0.7-r5)
(3/5) Installing nghttp2-libs (1.41.0-r0)
(4/5) Installing libcurl (7.71.1-r0)
(5/5) Installing curl (7.71.1-r0)
Executing busybox-1.32.0-r1.trigger
Executing ca-certificates-20191127-r4.trigger
2 errors; 117 MiB in 64 packages

Use curl command to confirm the web server is working.

$ curl -ik localhost
HTTP/1.1 404 Not Found
Server: nginx
Date: Thu, 13 Aug 2020 19:24:32 GMT
Content-Type: text/html
Content-Length: 146
Connection: keep-alive

Install PHP-FPM on Alpine Linux

Next instal php packages and include php-fpm extension on Alpine Linux.

sudo apk --update add \
        php7 \
        php7-bcmath \
        php7-ctype \
        php7-curl \
        php7-fpm \
        php7-gd \
        php7-iconv \
        php7-intl \
        php7-json \
        php7-mbstring \
        php7-mcrypt \
        php7-mysqlnd \
        php7-opcache \
        php7-openssl \
        php7-pdo \
        php7-pdo_mysql \
        php7-pdo_pgsql \
        php7-pdo_sqlite \
        php7-phar \
        php7-posix \
        php7-session \
        php7-soap \
        php7-xml \

Start and enable service.

$ sudo rc-service php-fpm7 start
 * Caching service dependencies ...                                                                                                                         [ ok ]
 * Checking /etc/php7/php-fpm.conf ...
 * /run/php-fpm7: creating directory
 * Starting PHP FastCGI Process Manager ...                                                                                                                 [ ok ]

PHP and FPM configuration files are available in the following directory.

$ ls /etc/php7/
conf.d        php-fpm.conf  php-fpm.d     php.ini

You can install vim and use it to modify configuration files.

sudo apk add vim

You can now upload PHP web application data and configure Nginx to serve your web content.

Learning materials:

More on Alpine Linux.

Install Telnet on Alpine Linux

Install MariaDB on Alpine Linux


Please enter your comment!
Please enter your name here