Introduction
PHP is one of the most popular programming languages for designing server-side web applications in the world today. It has extensive documentation, is highly stable, and is widely used in platforms such as WordPress and Wikipedia.
At the time of writing, PHP 8.4 has been released. However, because it is still relatively new, ecosystem support may be incomplete in some environments. For this reason, this guide focuses on PHP 8.3, which is currently considered stable and well-supported across most Linux distributions.
This guide is made for people who have no prior experience setting up a web server and for those who are just beginning to use a Linux operating system such as Ubuntu.
What You Will Learn
By the end of this guide, you will:
- Understand the difference between PHP-CLI, mod_php, and PHP-FPM
- Install PHP 8.3 with Apache on Ubuntu 22.04+
- Choose the appropriate PHP handler for your use case
- Verify and troubleshoot a working PHP installation
Prerequisites
For this guide to be most compatible with your setup, you should be using an Ubuntu operating system version 22.04 or newer, and it must be connected to the internet.
No prior server administration experience is assumed beyond your ability to read and follow instructions, navigate your system, and type commands into a terminal. You must also have sudo (administrator) privileges.
Description
It is important to note that PHP can be installed and used in different contexts depending on how it is executed.
PHP-CLI (Command Line Interface) runs in the terminal and is entirely text-based. Its primary use cases in production include running cron jobs, background workers, maintenance scripts, and other automation tasks.
Web-based PHP, on the other hand, runs through a web server such as Apache and allows interaction through a web browser with full graphical user interface (GUI) support.
Web-based PHP under Apache can be implemented in two major ways, each with its own strengths and trade-offs:
mod_php is generally easy to install and manage, but it is less efficient because PHP runs inside the Apache process itself. It is commonly used for local development, testing environments, or low-traffic production websites.
PHP-FPM (FastCGI Process Manager) is the industry standard for production environments. While it is slightly more complex to configure, it offers better performance, scalability, and process isolation. For publicly accessible or higher-traffic websites, PHP-FPM is typically the preferred option.
Warning
While it is perfectly fine—and often encouraged—to install both PHP-CLI and web-based PHP, it is not advisable to install both mod_php and PHP-FPM at the same time. These two Apache PHP handlers conflict with each other.
Choose one approach and stick to it. Advanced users may configure both manually, but this is not recommended for beginners.
Installation Steps
Begin by launching the Terminal application on Ubuntu. Once you have confirmed that your system is connected to the internet, you can proceed with the installation.
Option 1: mod_php Installation
Add the required repository, update your package list, and install Apache with PHP 8.3 using mod_php:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install apache2 php8.3 libapache2-mod-php8.3
Follow the prompts and confirm any installation requests as needed.
Option 2: PHP-FPM Installation
If you choose PHP-FPM, first add the repository and install Apache, PHP 8.3, and PHP-FPM:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install apache2 php8.3 php8.3-fpm
Next, enable the Apache modules required for PHP-FPM integration. These modules allow Apache to communicate with PHP-FPM over FastCGI:
sudo a2enmod proxy_fcgi setenvif
Enable the PHP-FPM configuration for Apache:
sudo a2enconf php8.3-fpm
To prevent conflicts, disable the standard PHP module used by mod_php:
sudo a2dismod php8.3
PHP-FPM works best with Apache’s event-based multiprocessing model. Disable the prefork MPM and enable the event MPM:
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
Finally, restart Apache to apply all changes:
sudo systemctl restart apache2
Troubleshoot Installation
In some cases, errors may arise during installation. While this section is not exhaustive, it covers some commonly encountered issues.
add-apt-repository: command not found
This typically occurs when the system is not a full Ubuntu installation (for example, certain minimal VPS images or containers).
Fix: A different setup guide may be required.Could not get lock /var/lib/dpkg/lock-frontend or resource temporarily unavailable
This usually means another package management process is running.
Fix: Wait several minutes for the process to complete. If it is clearly stuck, you may identify and terminate it carefully:
sudo fuser -v /var/lib/dpkg/lock-frontend
sudo kill -9 <PID>
sudo dpkg --configure -a
- Unmet dependencies This can result from previous incomplete installations. Fix: Repair dependencies with:
sudo apt --fix-broken install
- apache2.service failed or could not bind to address 0.0.0.0:80 Port 80 may already be in use by another web server such as Nginx. Fix: Stop the conflicting service and restart Apache:
sudo systemctl stop nginx
sudo systemctl start apache2
Installation Test
If the installation completes successfully, you can verify that PHP is working in both CLI and web server modes.
To test PHP-CLI, run:
php -v
If the PHP version is displayed, PHP-CLI is functioning correctly.
To test PHP through the web server, create the file /var/www/html/info.php with the following contents:
<?php
phpinfo();
Open http://localhost/info.php in your web browser. At the top of the page, you should see a large header displaying the PHP version currently in use (for example, PHP Version 8.3.x). Below this header is a series of tables showing detailed configuration information about your PHP installation.
At this point, your system should meet the following criteria:
- Apache is running and listening on port 80
- PHP 8.3 is available via the CLI
- Apache is correctly forwarding PHP requests
If either PHP mode does not work as expected after carefully following this guide, further troubleshooting may be required depending on your system environment.
Additional Configurations
While PHP may now be functional, additional extensions are often required for real-world applications.
Install commonly used extensions with:
sudo apt install php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-gd
Restart Apache to apply the changes:
sudo systemctl restart apache2
If you are using PHP-FPM, restart the FPM service as well:
sudo systemctl restart php8.3-fpm
After testing your setup, it is advised to remove the /var/www/html/info.php file you created earlier, as exposing phpinfo() publicly can leak sensitive configuration details.
Conclusion
At the end of this guide, your Ubuntu system should now be running PHP 8.3 integrated with Apache. You should be able to handle PHP server requests both locally and over an internet connection.
This guide demonstrates a basic setup and does not cover SSL, firewall rules, or advanced hardening required for internet-facing production servers.
Next Steps
After completing this setup, you may want to explore the following:
- Installing a database server such as MySQL or MariaDB
- Learning how to configure Apache Virtual Hosts
- Exploring PHP frameworks such as Laravel or Slim
- Hardening your server with basic security practices
I wish you success in all the web applications you create henceforth.
Top comments (0)