How to install PHP 7 fpm on Alpine Linux

Posted on in Categories , , , , last updated August 2, 2017

I am a new user of Alpine Linux. How do I install PHP 7.x along with Nginx web server on Alpine Linux?

Nginx is a free and open source web server. You need nginx to display static or dynamic web pages. Nginx can also act as a reverse proxy and load balancer. PHP is a free and open source server side programming language. This tutorial shows how to install PHP 7.x along with nginx on Alpine Linux.

Step 1: Install the Nginx web server

Type the following apk command to install nginx server on an Alpine Linux:
# apk update && apk upgrade
# apk add nginx

Step 2: Install the PHP 7.x

Type the following apk command to install the PHP 7.x language runtime engine:
# apk add php7 php7-fpm php7-opcache
Sample outputs:

Fig.01: Installing PHP 7
Fig.01: Installing PHP 7

Step 3: Install the PHP 7.x modules

Add popular PHP modules such as MySQL, GD and others:
# apk add php7-gd php7-mysqli php7-zlib php7-curl
Sample outputs:

Fig.02: Installing PHP 7 modules
Fig.02: Installing PHP 7 modules

To search other modules run the following apk command. Run:
# apk search php7
# apk search php7 | more

Sample outputs:

php7-intl-7.1.5-r0
php7-openssl-7.1.5-r0
php7-dba-7.1.5-r0
php7-sqlite3-7.1.5-r0
php7-pear-7.1.5-r0
php7-tokenizer-7.1.5-r0
php7-phpdbg-7.1.5-r0
cacti-php7-1.0.0-r1
xapian-bindings-php7-1.4.3-r2
php7-litespeed-7.1.5-r0
php7-gmp-7.1.5-r0
php7-pdo_mysql-7.1.5-r0
php7-pcntl-7.1.5-r0
php7-common-7.1.5-r0
php7-xsl-7.1.5-r0
php7-fpm-7.1.5-r0
php7-imagick-3.4.3-r2
php7-mysqlnd-7.1.5-r0
php7-enchant-7.1.5-r0
php7-pspell-7.1.5-r0
php7-redis-3.1.2-r1
php7-snmp-7.1.5-r0
php7-doc-7.1.5-r0
php7-fileinfo-7.1.5-r0
php7-mbstring-7.1.5-r0
php7-dev-7.1.5-r0
php7-pear-mail_mime-1.10.0-r0
php7-xmlrpc-7.1.5-r0
php7-embed-7.1.5-r0
php7-xmlreader-7.1.5-r0
php7-pear-mdb2_driver_mysql-1.5.0b4-r0
php7-pdo_sqlite-7.1.5-r0
php7-pear-auth_sasl2-0.2.0-r0
php7-exif-7.1.5-r0
php7-recode-7.1.5-r0
php7-opcache-7.1.5-r0
php7-ldap-7.1.5-r0
php7-posix-7.1.5-r0
php7-pear-net_socket-1.1.0-r0
php7-session-7.1.5-r0
php7-gd-7.1.5-r0
php7-gettext-7.1.5-r0
php7-mailparse-3.0.2-r1
php7-json-7.1.5-r0
php7-xml-7.1.5-r0
php7-7.1.5-r0
php7-iconv-7.1.5-r0
php7-sysvshm-7.1.5-r0
php7-curl-7.1.5-r0
php7-shmop-7.1.5-r0
php7-odbc-7.1.5-r0
php7-phar-7.1.5-r0
php7-pdo_pgsql-7.1.5-r0
php7-imap-7.1.5-r0
php7-pear-mdb2_driver_pgsql-1.5.0b4-r0
php7-pdo_dblib-7.1.5-r0
php7-pgsql-7.1.5-r0
php7-pdo_odbc-7.1.5-r0
php7-xdebug-2.5.3-r1
php7-zip-7.1.5-r0
php7-apache2-7.1.5-r0
php7-cgi-7.1.5-r0
php7-ctype-7.1.5-r0
php7-amqp-1.9.0-r0
php7-mcrypt-7.1.5-r0
php7-wddx-7.1.5-r0
php7-pear-net_smtp-1.8.0-r1
php7-bcmath-7.1.5-r0
php7-calendar-7.1.5-r0
php7-tidy-7.1.5-r0
php7-dom-7.1.5-r0
php7-sockets-7.1.5-r0
php7-zmq-1.1.3-r1
php7-memcached-3.0.3-r1
php7-soap-7.1.5-r0
php7-apcu-5.1.8-r1
php7-sysvmsg-7.1.5-r0
php7-zlib-7.1.5-r0
php7-imagick-dev-3.4.3-r2
php7-ftp-7.1.5-r0
php7-sysvsem-7.1.5-r0
php7-pear-net_idna2-0.2.0-r1
php7-pdo-7.1.5-r0
php7-pear-auth_sasl-1.1.0-r0
php7-bz2-7.1.5-r0
php7-mysqli-7.1.5-r0
php7-pear-net_smtp-doc-1.8.0-r1
php7-simplexml-7.1.5-r0
php7-xmlwriter-7.1.5-r0

Or use the grep command along with the apk command:
# apk search php7 | grep -i gd

Step 4: Start the php-fpm7 server

Make sure the nginx and php-fpm7 starts when system reboots:
# rc-update add nginx default
* service nginx added to runlevel default
# rc-update add php-fpm7 default
* service php-fpm7 added to runlevel default

Command to restart nginx and PHP7-fpm server

# rc-service nginx restart
# rc-service php-fpm7 restart

Sample outputs:

Fig.03: Starting/Stopping PHP and Nginx services
Fig.03: Starting/Stopping PHP and Nginx services

Command to stop nginx and PHP7-fpm server

# rc-service nginx stop
# rc-service php-fpm7 stop

Command to start nginx and PHP7-fpm server

# rc-service nginx start
# rc-service php-fpm7 start

Step 5: Configure PHP 7

Update you virtual host config file as follows
# vi /etc/nginx/conf.d/ssl.newsletter.cyberciti.biz.conf
Append/edit in server context:

   location ~ \.php$ {
              fastcgi_pass      127.0.0.1:9000;
              fastcgi_index     index.php;
              include           fastcgi.conf;
    }

Save and close the file. Here is a full config file:

## START: SSL/HTTPS newsletter.cyberciti.biz ###
server {                                        
    listen 443 http2;
    server_name newsletter.cyberciti.biz; 
    ssl on;
    ssl_certificate /etc/nginx/ssl/letsencrypt/newsletter.cyberciti.biz/newsletter.cyberciti.biz.cer;
    ssl_certificate_key /etc/nginx/ssl/letsencrypt/newsletter.cyberciti.biz/newsletter.cyberciti.biz.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    ssl_dhparam /etc/nginx/ssl/letsencrypt/newsletter.cyberciti.biz/dhparams.pem;
    ssl_prefer_server_ciphers on;
 
    ## Improves TTFB by using a smaller SSL buffer than the nginx default
    ssl_buffer_size 8k;
 
    ## Enables OCSP stapling
    ssl_stapling on;
    resolver 8.8.8.8;
    ssl_stapling_verify on;
 
    ## Send header to tell the browser to prefer https to http traffic
    #add_header Strict-Transport-Security max-age=31536000;
 
    ## SSL logs ##
    access_log /var/log/nginx/newsletter.cyberciti.biz_ssl_access.log;
    error_log /var/log/nginx/newsletter.cyberciti.biz_ssl_error.log;
    #-------- END SSL config -------##
 
   root /var/www/localhost/htdocs;
   index         index.html index.htm index.php;
   server_name   newsletter.cyberciti.biz;
   # configure php
   location ~ \.php$ {
              fastcgi_pass      127.0.0.1:9000;
              fastcgi_index     index.php;
              include           fastcgi.conf;
    }
    # rest of your config ##
}                                               
## END SSL newsletter.cyberciti.biz ######

Make sure you restart the nginx and php7:
# rc-service nginx restart
# rc-service php-fpm7 restart

Step 6: Test PHP 7

Create a php script file in your root directory such as /var/www/localhost/htdocs
# vi test.php
Add the following:

<?php
  phpinfo();
?>

Save and close the file. Test it:
http://your-domain/test.php
OR
https://your-domain/test.php

This entry is 2 of 4 in the Installing Linux, Nginx, MySQL/MariaDB, PHP (LEMP stack) in Alpine Linux series. Keep reading the rest of the series:
  1. Install Nginx On Alpine Linux
  2. Install PHP7-fpm On Alpine Linux
  3. How to install and configure logrotate
  4. How to install Letsencrypt free SSL/TLS for Nginx certificate on Alpine Linux

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.