PHP

Unfortunately, PHP and FPM under Ubuntu use the version of PHP installed to name the UNIX socket that NGINX will use to communicate with PHP so depending on the version of Ubuntu installed, the name will be different:

# Ubuntu 16.04 LTS
VERSION=7.0
 
# Ubuntu 18.04 LTS
VERSION=7.2

Install PHP and configure initial settings:

sudo apt-get -y install php$VERSION-common php$VERSION-cli php$VERSION-fpm php-apcu
 
cat > /tmp/myphpconfig.ini << EOF
cgi.fix_pathinfo=0
date.timezone = "$(cat /etc/timezone)"
expose_php = Off
default_charset = "UTF-8"
EOF
 
sudo mv /tmp/myphpconfig.ini /etc/php/$VERSION/fpm/conf.d/
 
cat > /tmp/www_local.conf << EOF
[www]
;Switch from the dynamic process manager to the ondemand manager
    pm=ondemand
 
;Bump up PHP's max_children to support more concurrent connections
    pm.max_children=8
EOF
 
sudo mv /tmp/www_local.conf /etc/php/$VERSION/fpm/pool.d

Once PHP is installed, you can test it to make sure it is installed correctly:

cat > /tmp/php-test << EOF
server {
  listen 8080;

  root /usr/share/nginx/html;

  location / { 
    index info.php;
  }

  location ~ \.php\$ {
    include fastcgi_params;
    fastcgi_pass unix:/run/php/php$VERSION-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
    fastcgi_param PATH_INFO \$uri;  # necessary for URL rewrite
    fastcgi_index info.php;
  }
}
EOF

sudo mv /tmp/php-test /etc/nginx/sites-available/
sudo ln -s /etc/nginx/sites-available/php-test /etc/nginx/sites-enabled/php-test

cat > /tmp/info.php << EOF
<?php phpinfo(); ?>
EOF

sudo mv /tmp/info.php  /usr/share/nginx/html/info.php

Then restart the server for the changes to take effect.

sudo service php$VERSION-fpm restart
sudo service nginx restart

Test the PHP installation opening the info.php which should display the PHP information page. After testing, delete the file to prevent “exposing” too much information to the public about your installation.

sudo rm -f /usr/share/nginx/html/info.php
sudo /bin/rm /etc/nginx/sites-enabled/php-test
sudo /bin/rm /etc/nginx/sites-available/php-test
sudo service nginx stop


Last Updated: February 17, 2019