News
Happy System Administrator Appreciation Day - to the true heroes of IT!
Serverspace Black Friday
BK
July 27 2025
Updated July 21 2025

Installing and Configuring PHP on Ubuntu

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:

  • Dynamic content generation: PHP enables creating web pages whose content changes based on user input, time, database data, or other factors. For example, PHP can generate personalized pages like user profiles or search results.
  • Database integration: PHP supports working with various databases including MySQL, PostgreSQL, SQLite and others. This makes it ideal for applications requiring data storage and processing, such as e-commerce stores or blogs.

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:

  • Installed Apache web server: PHP will integrate with Apache as described in the "Web Server Installation" article. To verify Apache installation, execute:
    apache2 -v

    If Apache is not installed, follow the instructions from the mentioned article.

  • Ubuntu operating system: This article assumes Ubuntu 24.04 (LTS) is used. Instructions can be adapted for other Ubuntu versions or Linux distributions.
  • Administrator privileges: Root or sudo access is required for installing packages and modifying configuration files.
  • Terminal access: Ensure you have terminal access locally or via SSH for remote servers.

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:

  • Error reporting settings
    For development, enable full error display to simplify debugging. Locate in php.ini:
    error_reporting = E_ALL
    display_errors = On

    In production environments, disable error display for security:

    display_errors = Off

    Errors will instead be logged to a file (e.g., /var/log/php_errors.log if configured).

  • Setting timezone
    Configure the date.timezone parameter for correct date/time function operation:
    date.timezone = Europe/Moscow

    Supported timezones are listed in PHP documentation.

  • Additional parameters
    Consider configuring these parameters:
    • max_execution_time: Maximum script execution time in seconds. Default is 30 seconds. For long operations, increase to:
      max_execution_time = 60
    • session.gc_maxlifetime: Session lifetime in seconds. Default is 1440 (24 minutes). For longer sessions:
      session.gc_maxlifetime = 3600

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
Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.