Easy Guide To Install PHP 7.3 on Ubuntu 20.04

Posted on

Easy Guide To Install PHP 7.3 on Ubuntu 20.04

Easy Guide To Install PHP 7.3 on Ubuntu 20.04

In this article on the Orcacore website, we want to teach you How To Install PHP 7.3 on Ubuntu 20.04. PHP is an open-source server-side scripting language that many devs use for web development.

It is also a general-purpose language that you can use to make lots of projects, including Graphical User Interfaces (GUIs). The abbreviation PHP initially stood for Personal Homepage. But now it is a recursive acronym for Hypertext Preprocessor.

PHP is mostly used for making web servers. It runs on the browser and is also capable of running on the command line. So, if you don’t feel like showing your code output in the browser, you can show it in the terminal.

Before you start to set up PHP 7.3 on Ubuntu 20.04, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 20.04.

Now follow the steps below to Install PHP 7.3 on Ubuntu 20.04.

Set up PHP 7.3 on Ubuntu 20.04

Here you will install PHP 7.3 from the PPA repository. Follow the steps below:

Add PPA Ondrej Repository

First, add the PPA repo on Ubuntu 20.04 with the following command:

sudo add-apt-repository ppa:ondrej/php

Then, update your local package index with the command below:

sudo apt-get update

Now you can use the following command to Install PHP 7.3 on Ubuntu 20.04:

sudo apt-get install -y php7.3

When you are done, check your PHP 7.3 installation by checking its version:

php -v

In your output you will see:

Output
PHP 7.3.33-1+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Nov 19 2021 06:25:05) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.33-1+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

The location of the PHP installed on your Ubuntu 20.04 is /etc/php/7.3.

php.ini Configuration Files

And there are two ini configuration files for PHP. One for the CLI (Command-line interface) and another for the Web (Apache2).

/etc/php/7.3/apache2/php.ini
/etc/php/7.3/cli/php.ini

The ini modules files for PHP on Ubuntu 20.04 are located in the:

/etc/php/7.3/mods-available/pods.ini
/etc/php/7.3/mods-available/json.ini
...

Also, you can list available modules with PHP with the command below:

php -m

In your output you will see:

Output
[PHP Modules]
calendar
Core
ctype
date
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
openssl
pcntl
pcre
PDO
Phar
posix
readline
Reflection
session
shmop
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
Zend OPcache
zlib
[Zend Modules]
Zend OPcache

The executable PHP binaries on Ubuntu 20.04 are located in the:

/usr/bin/php
/usr/bin/php7.3

Conclusion

At this point, you have learned to Install PHP 7.3 on Ubuntu 20.04 by using the PPA repository. Just remember PHP 7.3 is an old version of PHP and it is recommended to use the latest versions.

Hope you enjoy it. You may also like these articles:

How To Install PHP 7.4 on Ubuntu 22.04

How To Install PHP 7.4 on Ubuntu 20.04

Alternative Methods for Installing PHP 7.3 on Ubuntu 20.04

While the PPA method outlined above is a straightforward way to install PHP 7.3 on Ubuntu 20.04, it’s beneficial to explore alternative approaches. This provides flexibility and a deeper understanding of the system. Note that using older PHP versions like 7.3 is generally not recommended for production environments due to security vulnerabilities and lack of support. However, for specific testing or legacy application requirements, these methods can be useful.

Here are two alternative ways to achieve the same goal, How To Install PHP 7.3 on Ubuntu 20.04:

1. Using Docker

Docker offers a containerized environment, allowing you to run PHP 7.3 without directly installing it on your host system. This is particularly useful for isolating dependencies and avoiding conflicts with other PHP versions or system configurations.

Explanation:

Docker uses images as blueprints for creating containers. We can pull a pre-built PHP 7.3 image from Docker Hub or create our own custom image. The container will then run PHP 7.3 in an isolated environment, accessible through port mapping.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the official Docker installation guide for Ubuntu.

    sudo apt update
    sudo apt install docker.io
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull a PHP 7.3 Image: A suitable base image is often php:7.3-apache or php:7.3-fpm. The "apache" variant includes Apache web server, while the "fpm" variant is suitable for use with Nginx.

    docker pull php:7.3-apache
  3. Create a Dockerfile (Optional): If you need specific extensions or configurations, create a Dockerfile in your project directory.

    FROM php:7.3-apache
    
    # Install extensions
    RUN docker-php-ext-install mysqli pdo pdo_mysql
    
    # Copy your PHP files
    COPY . /var/www/html/
    
    # Expose port 80
    EXPOSE 80
  4. Build the Image (If using Dockerfile):

    docker build -t my-php73-app .
  5. Run the Container:

    docker run -d -p 8080:80 my-php73-app

    This command runs the container in detached mode (-d) and maps port 8080 on your host machine to port 80 inside the container (-p 8080:80). You can now access your PHP application by navigating to http://localhost:8080 in your web browser.

  6. Verify Installation: To verify PHP 7.3 is running correctly, create a phpinfo.php file in your project directory with the following content:

    <?php
    phpinfo();
    ?>

Access http://localhost:8080/phpinfo.php in your browser to see the PHP configuration.

Benefits:

  • Isolation: Prevents conflicts with other PHP versions or system dependencies.
  • Reproducibility: Ensures consistent environments across different machines.
  • Portability: Easily deploy your application to different environments.

2. Compiling from Source (Advanced)

Another approach, though more complex, is to compile PHP 7.3 directly from its source code. This method offers the highest degree of customization but requires more technical expertise. This approach to How To Install PHP 7.3 on Ubuntu 20.04 is not recommended for beginners.

Explanation:

Compiling from source involves downloading the PHP 7.3 source code, configuring the build process with specific options, and then compiling and installing the binaries.

Steps:

  1. Download the PHP 7.3 Source Code: Visit php.net or a similar archive to download the PHP 7.3 source code (e.g., php-7.3.33.tar.gz).

  2. Install Build Dependencies: You’ll need several development tools and libraries to compile PHP.

    sudo apt update
    sudo apt install build-essential libxml2-dev libssl-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libgmp-dev
  3. Extract the Source Code:

    tar -xvf php-7.3.33.tar.gz
    cd php-7.3.33
  4. Configure the Build: The ./configure script allows you to specify various options, such as the installation directory, enabled extensions, and web server integration.

    ./configure --prefix=/usr/local/php73 --with-apxs2=/usr/bin/apxs2 --with-mysqli --with-pdo-mysql --with-openssl --with-curl --with-jpeg --with-png --with-freetype --with-bz2 --with-gmp
    • --prefix: Specifies the installation directory.
    • --with-apxs2: Integrates with Apache (if you’re using Apache).
    • Other --with-* options enable specific extensions. Adjust these options based on your needs.
  5. Compile and Install:

    make
    sudo make install
  6. Configure Apache (If applicable): If you used the --with-apxs2 option, the make install command should have created a PHP module for Apache. Enable the module and restart Apache:

    sudo a2enmod php7.3
    sudo systemctl restart apache2
  7. Configure the PHP CLI: Create a symbolic link to make the php command point to your newly installed PHP 7.3 binary:

    sudo ln -s /usr/local/php73/bin/php /usr/local/bin/php73

    Then, add /usr/local/bin to your PATH environment variable.

  8. Verify Installation:

    php73 -v

    This should display the PHP 7.3 version information.

Benefits:

  • Full Control: Complete control over the build process and enabled extensions.
  • Customization: Tailor the PHP installation to your specific requirements.

Drawbacks:

  • Complexity: Requires more technical expertise and manual configuration.
  • Maintenance: You are responsible for managing updates and security patches.

Remember to choose the method that best suits your needs and technical expertise. The PPA method is the simplest, while Docker offers isolation and portability. Compiling from source provides the most control but is the most complex. While this article focuses on How To Install PHP 7.3 on Ubuntu 20.04, always consider the security implications of using older PHP versions and prioritize using supported versions whenever possible.