Installing PHP

Install php8.1 fpm

First install the php8.1 fpm


$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.1-fpm

# if you are installing laravel, then install these following too:

$ sudo apt install openssl php-common php-curl php-json php-mbstring php-mysql php-xml php-zip php-gd

# if you will use mongodb as driver later on (vide `composer require jenssegers/mongodb`) then install the mongodb drivers prior to that

$ sudo apt install php-dev php-pear
$ sudo pecl install mongodb

# After installing the above, you can install jenssegers/mongodb via composer

Testing the installation

You can test the installation via the following:


$ systemctl status php8.1-fpm

Now it is time to use php8.0-fpm to serve the php pages.

Nginx config file

Check out the nginx config in order to know how to setup nginx as webserver for php


Edit the following in nginx (/etc/nginx/sites-enabled/default):

```nginx
server {

    server_name example.xopun.com;
    listen 80;
    root /var/www/example.xopun.com;

    index index.php;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }


    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

Installing composer for PHP

To install the composer for php, you'll need to install the dependencies first.

Installing the dependency

$ sudo apt update
$ sudo apt install php-cli unzip

Download and install the composer

$ cd ~
$ curl -sS https://getcomposer.org/installer -o composer-setup.php

Using curl, fetch the latest signature and store it in a variable.

$ HASH=`curl -sS https://composer.github.io/installer.sig`

Following script should be exeuted to check if the script is safe to run.

$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } 
else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You should see the following:

Installer verified

The following command will download and install Composer as a system-wide command named composer, under /usr/local/bin:

$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You should get the following output:

All settings correct for using Composer Downloading...

Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer

To test your installation, run the composer:

$ composer

You should get the following:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.5 2020-04-10 11:44:22

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
...

Troubleshooting while installing PHP

if you have more than one php in the same machine, you can switch to different by the following:

$ sudo update-alternatives --config php

This will bring up screen like below:

There are 4 choices (can be more/less for you depending upon your installation) for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.2   72        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode
  3            /usr/bin/php7.1   71        manual mode
  4            /usr/bin/php7.2   72        manual mode
Press <enter> to keep the current choice[*], or type selection number:
Last Updated: