Easy Way To Install PHP 7.3 on Ubuntu 22.04
In this tutorial from Orcacore, we will explore the Easy Way To Install PHP 7.3 on Ubuntu 22.04. Installing PHP 7.3 on Ubuntu 22.04 is a crucial step for developers and system administrators tasked with maintaining or running PHP applications that specifically require this older version. While Ubuntu 22.04 comes equipped with more recent PHP versions as its defaults, it remains entirely feasible to add and manage PHP 7.3 to ensure compatibility with legacy systems and software.
PHP 7.3 brought a host of improvements and new features to the table. These included new syntax features designed to streamline the development process, the ability to throw exceptions during JSON parsing or encoding, enhanced support for same-site cookies, improved logging capabilities in the FastCGI Process Manager (php-fpm), expanded LDAP controls, support for case-mapping and case-folding in mbstring functions, password hashing improvements, and an updated PCRE library.
Currently, the latest iteration of PHP is PHP 8.4. To learn more about its newest features, you can read our guide on What’s New in PHP 8.4.
Before proceeding, ensure you are logged into your Ubuntu 22.04 server as a non-root user with sudo privileges. If you need assistance setting this up, refer to our guide on Initial Server Setup with Ubuntu 22.04.
1. Add PPA Ondrej Repository on Ubuntu 22.04
We will be installing PHP 7.3 from a PPA repository. PPA, short for Personal Package Archive, enables developers and Linux users to create and manage their own repositories for distributing software. PPAs offer a convenient way to access newer software versions or software not included in the official Ubuntu repositories.
First, add the PPA repository to your Ubuntu 22.04 system by running the following command:
sudo add-apt-repository ppa:ondrej/php
Next, update your local package index to reflect the newly added repository. This ensures your system is aware of the available packages within the PPA:
sudo apt-get update
2. Install PHP 7.3 on Ubuntu 22.04
Now that the PPA repository is added and your package index is updated, you can install PHP 7.3 using the following command:
sudo apt-get install -y php7.3
Ubuntu PHP 7.3 | Verify Installation
After the installation process is complete, it’s essential to verify that PHP 7.3 has been installed correctly. You can do this by checking the PHP version:
php -v
The output should resemble the following, confirming the successful installation of PHP 7.3:
PHP 7.3.33-1+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Jan 20 2024 10:23:45) ( 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+ubuntu22.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
As you can see, you have successfully installed PHP 7.3. Now, let’s determine the location of the PHP.ini configuration files on Ubuntu 22.04.
php.ini Configuration Files on Ubuntu 22.04
The main directory for PHP 7.3 configuration files on Ubuntu 22.04 is /etc/php/7.3
. Within this directory, you’ll find separate php.ini
files for the command-line interface (CLI) and the web server (Apache2).
# /etc/php/7.3/apache2/php.ini
# /etc/php/7.3/cli/php.ini
The module-specific configuration files (e.g., for extensions like json
, pdo
, etc.) are located in the /etc/php/7.3/mods-available/
directory.
# /etc/php/7.3/mods-available/pods.ini
# /etc/php/7.3/mods-available/json.ini
...
To list all available PHP modules, you can use the following command:
php -m
The output will display a list of enabled modules, similar to the example below:
[PHP Modules]
Core
ctype
date
dom
fileinfo
filter
hash
iconv
json
libxml
openssl
pcre
PDO
...
Note: The executable PHP binaries on Ubuntu 22.04 are located in the following directories:
/usr/bin/php
/usr/bin/php7.3
For more comprehensive information about PHP, refer to the official PHP Documentation.
Conclusion
By following these steps, you can easily install PHP 7.3 on Ubuntu 22.04, ensuring the compatibility and stability of your PHP applications. The PPA repository simplifies the installation process, allowing you to manage different PHP versions on your server. Although the current stable version of PHP is PHP 8.3, you can easily upgrade by following this link to get the latest version of PHP on Ubuntu 22.04. It is generally recommended to stay updated with the latest versions for security and performance benefits. The easy way to install PHP 7.3 on Ubuntu 22.04 enables you to support older PHP versions.
We hope you found this guide helpful. You might also find these articles useful:
- How To Install PHP 7.4 on Ubuntu 22.04
- How To Install PHP 7.4 on AlmaLinux 8
- Install PHP 7.4 on Debian 11
FAQs
<div id="rank-math-faq">
<div id="faq-question-1727079523967">
<h3>What is PHP 7.3, and why install it on Ubuntu 22.04?</h3>
<p>PHP 7.3 is a widely used scripting language version that offers compatibility for older applications. Installing it on Ubuntu 22.04 ensures that applications dependent on PHP 7.3 continue to function properly.</p>
</div>
<div id="faq-question-1727079550675">
<h3>How do I add the PHP 7.3 repository on Ubuntu 22.04?</h3>
<p>You can add the repository using the command: <code>sudo add-apt-repository ppa:ondrej/php</code>.</p>
</div>
<div id="faq-question-1727079566659">
<h3>How can I check if PHP 7.3 is installed successfully?</h3>
<p>Run <code>php -v</code> to verify the installed PHP version.</p>
</div>
</div>
Alternative Ways to Install PHP 7.3 on Ubuntu 22.04
While the PPA method is generally the simplest, here are two alternative approaches for installing PHP 7.3 on Ubuntu 22.04. These methods may be useful in specific scenarios, such as when you need more control over the installation process or when PPAs are not preferred.
1. Using Docker
Docker provides a containerization platform, allowing you to run applications in isolated environments. This is an excellent option for managing different PHP versions without conflicting with the system’s default PHP installation.
Explanation:
Docker allows you to create a container with PHP 7.3 pre-installed. You can then run your application within this container, isolating it from the host system’s PHP environment. This approach offers excellent portability and reproducibility.
Steps:
-
Install Docker: If you don’t have Docker installed, follow the official Docker documentation for Ubuntu: https://docs.docker.com/engine/install/ubuntu/
-
Create a Dockerfile: Create a file named
Dockerfile
in your project directory with the following content:
FROM php:7.3-apache
# Install any necessary extensions
RUN apt-get update && apt-get install -y
libpng-dev
libjpeg62-turbo-dev
libfreetype6-dev
zip
unzip
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
&& docker-php-ext-install -j$(nproc) gd mysqli pdo pdo_mysql zip
# Set working directory
WORKDIR /var/www/html
# Copy your application files
COPY . .
# Expose port 80
EXPOSE 80
- Build the Docker Image: In the same directory as your
Dockerfile
, run the following command to build the Docker image:
docker build -t my-php73-app .
- Run the Docker Container: After the image is built, run the container with the following command:
docker run -d -p 8080:80 my-php73-app
This will start the container in detached mode (-d) and map port 8080 on your host machine to port 80 inside the container. You can then access your application by navigating to http://localhost:8080
in your web browser.
2. Compiling from Source
Compiling PHP from source offers maximum control over the installation process. This method is more complex but allows you to customize the installation and choose specific configurations.
Explanation:
This method involves downloading the PHP 7.3 source code, configuring the build options, and compiling the code into executable binaries. This provides the greatest flexibility but requires a deeper understanding of the build process.
Steps:
-
Download PHP 7.3 Source Code: Download the source code from the official PHP website: https://www.php.net/downloads.php. Choose a stable 7.3 version.
-
Install Build Dependencies: Before compiling, you need to install the necessary build dependencies:
sudo apt-get update
sudo apt-get install -y build-essential libxml2-dev libjpeg62-turbo-dev libpng-dev libfreetype6-dev libbz2-dev libcurl4-openssl-dev libxslt1-dev libonig-dev
- Extract the Source Code: Extract the downloaded source code archive:
tar -xvf php-7.3.x.tar.gz (replace php-7.3.x with the actual file name)
cd php-7.3.x
- Configure the Build: Configure the build process using the
./configure
script. You can specify various options to enable or disable features. For example:
./configure --with-apxs2=/usr/bin/apxs2 --with-mysqli --with-pdo-mysql --with-openssl --with-curl --with-gd --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib --with-freetype-dir=/usr/include/freetype2
Note: The --with-apxs2
option is used if you want to use PHP 7.3 with Apache. Adjust the paths according to your system configuration.
- Compile and Install: Compile the source code using the
make
command:
make
After compiling, install the binaries using the make install
command with sudo:
sudo make install
- Configure PHP: Copy the default
php.ini
file:
sudo cp php.ini-production /usr/local/etc/php/php.ini
- Configure Apache (if applicable): If you’re using Apache, you’ll need to enable the PHP module. Edit your Apache configuration file (usually
/etc/apache2/apache2.conf
or/etc/httpd/conf/httpd.conf
) and add the following line:
LoadModule php7_module /usr/lib/apache2/modules/libphp7.3.so
(The exact path to the module may vary depending on your system.)
Restart Apache:
sudo systemctl restart apache2
After completing these steps, PHP 7.3 should be installed and configured on your Ubuntu 22.04 system. You can verify the installation by creating a phpinfo.php
file and accessing it through your web browser.
These alternative methods provide more flexibility in install PHP 7.3 on Ubuntu 22.04 but require more technical expertise than the PPA method. Choose the method that best suits your needs and technical capabilities. This is another easy way to install PHP 7.3 on Ubuntu 22.04.