Step 7: Install PHP

PHP

PHP allows you to run PHP scripts or apps on our VPS directly. Sometimes we will use Docker to host applications, but other times we might want to host it natively on the VPS. PHP is a popular web-scripting language because of its simplicity and extensive support. We can run a PHP app on our VPS easily. Other languages are complicated to set up and run due to version conflicts, such as Python and Ruby. We will use Docker to host these types of applications!

We will install PHP 7.4-FPM, which is the latest version, and a few commonly used modules. See more details from on how to install PHP 7.4 on Ubuntu 18.04. from Computing for Geeks.

Note

  • PHP 7.4 is for Apache

  • PHP 7.4-FPM is for Nginx

  1. First, let’s refresh our apt directory (unless you have done so recently) and then install PHP 7.4 for Nginx using the fpm package.

    sudo apt update
    sudo apt install -y php7.4-fpm php7.4-mysql
    
    • Note: You might get this error: E: Unable to locate package php7.4-fpm.

      • If so, you need to add the PHP repository before trying again.

    sudo add-apt-repository ppa:ondrej/php
    sudo apt install -y php7.4-fpm php7.4-mysql
    

    Output with missing repository

     1root@vps298933:~# sudo apt install -y php7.4-fpm php7.4-mysql
     2Reading package lists... Done
     3Building dependency tree
     4Reading state information... Done
     5E: Unable to locate package php7.4-fpm
     6E: Couldn't find any package by glob 'php7.4-fpm'
     7E: Couldn't find any package by regex 'php7.4-fpm'
     8E: Unable to locate package php7.4-mysql
     9E: Couldn't find any package by glob 'php7.4-mysql'
    10E: Couldn't find any package by regex 'php7.4-mysql'
    11
    12root@vps298933:~#
    13root@vps298933:~# sudo add-apt-repository ppa:ondrej/php
    14 Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.
    15
    16Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa
    17
    18You can get more information about the packages at https://deb.sury.org
    19
    20BUGS: This PPA now has a issue tracker:
    21https://deb.sury.org/#bug-reporting
    22
    23CAVEATS:
    241. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
    252. If you are using apache2, you are advised to add ppa:ondrej/apache2
    263. If you are using nginx, you are advise to add ppa:ondrej/nginx-mainline or ppa:ondrej/nginx
    27
    28PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/
    29
    30WARNING: add-apt-repository is broken with non-UTF-8 locales, see
    31https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:
    32
    33# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
    34 More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
    35Press [ENTER] to continue or Ctrl-c to cancel adding it.
    36
    37Hit:1 http://ppa.launchpad.net/ondrej/nginx/ubuntu bionic InRelease
    38Get:2 http://ppa.launchpad.net/ondrej/php/ubuntu bionic InRelease [20.8 kB]
    39Hit:3 http://security.ubuntu.com/ubuntu bionic-security InRelease
    40Hit:4 http://nova.clouds.archive.ubuntu.com/ubuntu bionic InRelease
    41Get:5 http://nova.clouds.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
    42Get:6 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 Packages [43.9 kB]
    43Get:7 http://nova.clouds.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
    44Get:8 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main Translation-en [21.8 kB]
    45Fetched 250 kB in 1s (432 kB/s)
    46Reading package lists... Done
    47root@vps298933:~#
    

    Output for a successful PHP install

     1root@vps298933:~# sudo apt install -y php7.4-fpm php7.4-mysql
     2Reading package lists... Done
     3Building dependency tree
     4Reading state information... Done
     5The following additional packages will be installed:
     6libpcre2-8-0 libsodium23 php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline psmisc tzdata
     7Suggested packages:
     8php-pear
     9The following NEW packages will be installed:
    10libpcre2-8-0 libsodium23 php-common php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-mysql php7.4-opcache php7.4-readline
    11psmisc tzdata
    120 upgraded, 12 newly installed, 0 to remove and 5 not upgraded.
    13Need to get 4712 kB of archives.
    14After this operation, 22.5 MB of additional disk space will be used.
    15Get:1 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 libpcre2-8-0 amd64 10.33-1+ubuntu18.04.1+deb.sury.org+1 [191 kB]
    16Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 tzdata all 2019c-0ubuntu0.18.04 [190 kB]
    17Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 psmisc amd64 23.1-1ubuntu0.1 [52.5 kB]
    18Get:4 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 libsodium23 amd64 1.0.18-1+ubuntu18.04.1+deb.sury.org+1 [150 kB]
    19Get:5 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php-common all 2:70+ubuntu18.04.1+deb.sury.org+6 [15.2 kB]
    20Get:6 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-common amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [976 kB]
    21Get:7 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-json amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [18.6 kB]
    22Get:8 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-opcache amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [195 kB]
    23Get:9 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-readline amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [12.2 kB]
    24Get:10 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-cli amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [1392 kB]
    25Get:11 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-fpm amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [1403 kB]
    26Get:12 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-mysql amd64 7.4.0-1+ubuntu18.04.1+deb.sury.org+1 [117 kB]
    27Fetched 4712 kB in 2s (2088 kB/s)
    28debconf: delaying package configuration, since apt-utils is not installed
    29Selecting previously unselected package tzdata.
    30(Reading database ... 11793 files and directories currently installed.)
    31. . .
    
  2. Verify the version of PHP to determine if it installed correctly. You should see a version similar to the output.

    php --version
    
    1root@vps298933:~# php --version
    2PHP 7.4.0 (cli) (built: Nov 28 2019 07:27:06) ( NTS )
    3Copyright (c) The PHP Group
    4Zend Engine v3.4.0, Copyright (c) Zend Technologies
    5    with Zend OPcache v7.4.0, Copyright (c), by Zend Technologies
    

PHP and Nginx

Now, we will create a phpinfo page to verify that Nginx and PHP work together. The first step is to tell Nginx to serve PHP pages. Next, is to test the configuration using a phpinfo file

  1. Edit the Nginx config file for the default site or directory using nano

    sudo nano /etc/nginx/sites-available/default
    
  2. Add index.php to the index file list on line 4.

  3. Uncomment the PHP lines so your file looks the same as the file below.

  4. change the PHP version in fastcgi_pass to php7.4-fpm.sock;

    /etc/nginx/sites-available/default
     1 #
     2 root /var/www/html;
     3
     4 index index.html index.htm index.nginx-debian.html index.php;
     5
     6 server_name _;
     7
     8 location / {
     9     # First attempt to serve request as file, then
    10     # as directory, then fall back to displaying a 404.
    11     try_files $uri $uri/ =404;
    12 }
    13
    14 # pass PHP scripts to FastCGI server
    15 #
    16 location ~ \.php$ {
    17     include snippets/fastcgi-php.conf;
    18
    19     # With php-fpm (or other unix sockets):
    20     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    21     # With php-cgi (or other tcp sockets):
    22     # fastcgi_pass 127.0.0.1:9000;
    23 }
    24
    25 # deny access to .htaccess files, if Apache's document root
    26 # concurs with nginx's one
    27 #
    28 location ~ /\.ht {
    29        deny all;
    30 }
    
  5. Verify that there are no configuration issues

    sudo nginx -t
    

    Attention

    Take note of the file name and line number of the error. It tells you exactly what and where the problem is.

    1root@vps298933:~# sudo nginx -t
    2nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/sites-enabled/default:62
    3nginx: configuration file /etc/nginx/nginx.conf test failed
    

    Success

    1root@vps298933:~# sudo nginx -t
    2nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    3nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  6. Fix, retest, then restart using systemctl

    sudo nginx -t
    sudo systemctl restart nginx
    

Create a phpinfo page

Now, we should test the configuration by creating a phpinfo page.

  1. Go to the site randomkeygen.com and grab a random alphanumeric password for the PHP info file. We don’t want to expose the system information.

  2. Next, we need to verify the location of our default web directory.

    • The default web dir for Nginx is /var/www/html/.

    • But, let’s verify!

  3. Edit the default config file to verify the dir of the root folder.

    • sudo nano /etc/nginx/sites-available/default

    • It should be: root /var/www/html;

    /etc/nginx/sites-available/default
    1 #
    2 root /var/www/html;
    3
    4 index index.html index.htm index.nginx-debian.html index.php;
    5
    6 server_name _;
    7
    8 location / {
    
  4. Use nano to create a new file. Substitute the random code to the command below.

    Command

    sudo nano /var/www/html/HSd3uR9yFZ.php
    
    HSd3uR9yFZ.php
    <?php phpinfo(); ?>
    
  5. Navigate to that page in the web browser using your IP address or domain name

    • http://example.com/HSd3uR9yFZ.php

  6. Verify that the page loads and that the PHP version is correct