Step 7: Install 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.


  • 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'
    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 ( for Supported Ubuntu Releases ( are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.
    16Debian oldstable and stable packages are provided as well:
    18You can get more information about the packages at
    20BUGS: This PPA now has a issue tracker:
    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
    28PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly:
    30WARNING: add-apt-repository is broken with non-UTF-8 locales, see
    31 for workaround:
    33# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
    34 More info:
    35Press [ENTER] to continue or Ctrl-c to cancel adding it.
    37Hit:1 bionic InRelease
    38Get:2 bionic InRelease [20.8 kB]
    39Hit:3 bionic-security InRelease
    40Hit:4 bionic InRelease
    41Get:5 bionic-updates InRelease [88.7 kB]
    42Get:6 bionic/main amd64 Packages [43.9 kB]
    43Get:7 bionic-backports InRelease [74.6 kB]
    44Get:8 bionic/main Translation-en [21.8 kB]
    45Fetched 250 kB in 1s (432 kB/s)
    46Reading package lists... Done

    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:
     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 bionic/main amd64 libpcre2-8-0 amd64 [191 kB]
    16Get:2 bionic-updates/main amd64 tzdata all 2019c-0ubuntu0.18.04 [190 kB]
    17Get:3 bionic-updates/main amd64 psmisc amd64 23.1-1ubuntu0.1 [52.5 kB]
    18Get:4 bionic/main amd64 libsodium23 amd64 [150 kB]
    19Get:5 bionic/main amd64 php-common all [15.2 kB]
    20Get:6 bionic/main amd64 php7.4-common amd64 [976 kB]
    21Get:7 bionic/main amd64 php7.4-json amd64 [18.6 kB]
    22Get:8 bionic/main amd64 php7.4-opcache amd64 [195 kB]
    23Get:9 bionic/main amd64 php7.4-readline amd64 [12.2 kB]
    24Get:10 bionic/main amd64 php7.4-cli amd64 [1392 kB]
    25Get:11 bionic/main amd64 php7.4-fpm amd64 [1403 kB]
    26Get:12 bionic/main amd64 php7.4-mysql amd64 [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;

     1 #
     2 root /var/www/html;
     4 index index.html index.htm index.nginx-debian.html index.php;
     6 server_name _;
     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 }
    14 # pass PHP scripts to FastCGI server
    15 #
    16 location ~ \.php$ {
    17     include snippets/fastcgi-php.conf;
    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;
    23 }
    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


    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


    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 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;

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


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


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