21.07.2025

Installing and Configuring PHP on Ubuntu

PHP (Hypertext Preprocessor) is a widely-used open-source programming language specifically designed for web development. Created by Rasmus Lerdorf in 1993, PHP originally stood for "Personal Home Page" but is now a recursive acronym for "PHP: Hypertext Preprocessor". PHP allows embedding server-side code into HTML, simplifying the creation of dynamic web pages that interact with users and databases. According to W3Techs data from July 2025, PHP is used on approximately 77% of all websites with a known server-side programming language. The current stable version is PHP 8.4.10, released on July 3, 2025.

PHP is popular due to its beginner-friendly simplicity and powerful capabilities for professional developers. It powers popular content management systems (CMS) like WordPress, Drupal, and Joomla, and is used on major platforms such as Wikipedia and Etsy.

Why Use PHP with a Web Server?

PHP plays a key role in web development thanks to the following capabilities:

These capabilities make PHP the foundation for dynamic web applications, providing the flexibility and functionality required for modern websites.

Prerequisites

Before installing PHP, ensure the following conditions are met:

It's also recommended to update the system before installation to avoid dependency issues.

Installing PHP on Ubuntu

PHP installation on Ubuntu is performed using the apt package manager. Follow these steps:

  1. Update package lists
    To ensure you're installing the latest software versions, update the package index:
    sudo apt update
  2. Install PHP and Apache module
    Install PHP and the integration module for Apache:
    sudo apt install php libapache2-mod-php

    This command will install the latest PHP version available in Ubuntu repositories (typically PHP 8.3 for Ubuntu 24.04) and the libapache2-mod-php module which enables Apache to process PHP scripts.

  3. Check installed PHP version
    After installation, verify the PHP version to confirm successful installation:
    php -v

    The output should display the version, for example:

    PHP 8.3.0 (cli) (built: Dec 10 2023 12:34:56) ( NTS )
  4. Install additional extensions
    For typical web development tasks, install the following extensions:
    sudo apt install php-mysql php-gd php-curl
    • php-mysql: Enables PHP to interact with MySQL databases, essential for applications like WordPress.
    • php-gd: Provides image processing functions, such as creating thumbnails or watermarks.
    • php-curl: Used for making HTTP requests, for example when integrating with APIs.

    If using a different PHP version, package names may include the version (e.g., php8.3-mysql). For standard installations, the specified package names will work as they match the default PHP version.

Configuring PHP

After installing PHP, configure its parameters to meet your application requirements. The main PHP configuration file for Apache is located at /etc/php/<version>/apache2/php.ini, where <version> is your installed PHP version (e.g., 8.3).

  1. Locate the configuration file
    For PHP 8.3, the file is at:
    /etc/php/8.3/apache2/php.ini

    To confirm the path, execute:

    php --ini

    This will show the location of php.ini for both Apache and CLI (command line).

  2. Open the file for editing
    Use a text editor like nano to edit the file:
    sudo nano /etc/php/8.3/apache2/php.ini
  3. Modify key parameters
    Find and modify the following parameters according to your needs:
    • memory_limit: Defines the maximum memory a PHP script can use. Default is often 128M. For more demanding applications, increase to:
      memory_limit = 256M
    • upload_max_filesize: Sets maximum upload file size. Default is 2M. For larger files, increase to:
      upload_max_filesize = 10M
    • post_max_size: Defines maximum POST data size. Must be greater than or equal to upload_max_filesize. For example:
      post_max_size = 12M
  4. Save changes and restart Apache
    After making changes, save the file (in nano press Ctrl+O, then Enter, and Ctrl+X to exit) and restart Apache to apply settings:
    sudo systemctl restart apache2

Integrating PHP with Apache

The libapache2-mod-php module automatically integrates PHP with Apache during installation. However, you can verify if the module is activated and enable it manually if needed.

  1. Check PHP module activation
    Execute:
    sudo a2enmod php8.3

    Replace 8.3 with your PHP version if different. If the module is already enabled, you'll see an activation message.

  2. Restart Apache
    Apply changes by restarting Apache:
    sudo systemctl restart apache2
  3. Check Apache status
    Verify Apache is functioning correctly:
    sudo systemctl status apache2

    The output should show active (running) status.

Testing PHP Installation

To verify PHP is properly installed and integrated with Apache, create a test page:

  1. Create info.php file
    Create a file in the web server directory /var/www/html/:
    echo "" | sudo tee /var/www/html/info.php

    The phpinfo() function outputs detailed PHP configuration information including version, modules, and settings.

  2. Check the page in browser
    Open a browser and navigate to http://localhost/info.php or http://<server_IP>/info.php. You should see a PHP information page showing version, installed modules, and configuration parameters.
  3. Security
    After testing, delete the info.php file as it may reveal sensitive server information:
    sudo rm /var/www/html/info.php

    If you wish to keep the file, restrict access by modifying permissions:

    sudo chmod 600 /var/www/html/info.php

Useful Configuration Tips

For optimizing PHP performance, configure additional parameters in php.ini:

Troubleshooting Common Issues

If PHP isn't functioning as expected, follow these steps:

  1. Check Apache status
    Verify Apache is running:
    sudo systemctl status apache2

    If the service isn't active, restart it:

    sudo systemctl restart apache2
  2. Review error logs
    PHP error logs are typically written to the file specified in php.ini (error_log parameter). Also check Apache logs:
    sudo cat /var/log/apache2/error.log
  3. Common issues
    • info.php page not displaying: Ensure the PHP module is activated (sudo a2enmod php8.3) and Apache is restarted.
    • Dependency errors: If installation failed, repair dependencies:
      sudo apt-get install -f
    • Permission issues: Verify files in /var/www/html/ are accessible to Apache:
      sudo chown -R www-data:www-data /var/www/html
      sudo chmod -R 755 /var/www/html

Installing and configuring PHP on Ubuntu is a straightforward process that prepares your server for dynamic web applications. By following these steps, you can install PHP, integrate it with Apache, configure key parameters, and test the setup. PHP remains an essential tool for web development thanks to its popularity, community support, and compatibility with popular CMS platforms like WordPress.

Table: Key php.ini Parameters

Parameter Description Recommended Value (Development) Recommended Value (Production)
memory_limit Maximum memory per script 256M 128M
upload_max_filesize Maximum upload file size 10M 2M
post_max_size Maximum POST data size 12M 8M
error_reporting Error reporting level E_ALL E_ALL & ~E_NOTICE
display_errors Display errors in browser On Off
date.timezone Timezone for date/time functions Europe/Moscow Europe/Moscow
max_execution_time Maximum script execution time (seconds) 60 30
session.gc_maxlifetime Session lifetime (seconds) 3600 1440