Chapter 6. Using the PHP scripting language


Hypertext Preprocessor (PHP) is a general-purpose scripting language mainly used for server-side scripting. Install PHP and run PHP code using a web server or the command-line interface.

6.1. Installing the PHP scripting language

In RHEL 9, PHP is available as the php RPM package (PHP 8.0), the php:8.1 module stream (PHP 8.1), or the php:8.2 module stream (PHP 8.2).

Procedure

  • To install PHP 8.0, enter:

    # dnf install php
  • To install the php:8.1 or php:8.2 module stream with the default profile, enter for example:

    # dnf module install php:8.1

The default common profile installs also the php-fpm package, and preconfigures PHP for use with the Apache HTTP Server or nginx.

  • To install a specific profile of the php:8.1 or php:8.2 module stream, use for example:

    # dnf module install php:8.1/profile
  • The available profiles for the php:8.1 and php:8.2 module streams are:

    • common - The default profile for server-side scripting using a web server. It includes the most widely used extensions.
    • minimal - This profile installs only the command line for scripting with PHP without using a web server.
    • devel - This profile includes packages from the common profile and additional packages for development purposes.
  • For example, to install PHP 8.1 for use without a web server, use:

    # dnf module install php:8.1/minimal

6.2.1. Using PHP with the Apache HTTP Server

In Red Hat Enterprise Linux 9, the Apache HTTP Server runs PHP by using FastCGI Process Manager (FPM) by default. Install and configure Apache with PHP.

Prerequisites

  • The PHP scripting language is installed on your system.

Procedure

  1. Install the httpd package:

    # dnf install httpd
  2. Start the Apache HTTP Server:

    # systemctl start httpd
    • Or, if the Apache HTTP Server is already running on your system, restart the httpd service after installing PHP:
# systemctl restart httpd
  1. Start the php-fpm service:

    # systemctl start php-fpm
  2. Optional: Enable both services to start at boot time:

    # systemctl enable php-fpm httpd
  3. To obtain information about your PHP settings, create the index.php file with the following content in the /var/www/html/ directory:

    # echo '<?php phpinfo(); ?>' > /var/www/html/index.php
  4. To run the index.php file, point the browser to:

    http://<hostname>/
  5. Optional: Adjust configuration if you have specific requirements:

    • /etc/httpd/conf/httpd.conf - generic httpd configuration
    • /etc/httpd/conf.d/php.conf - PHP-specific configuration for httpd
    • /usr/lib/systemd/system/httpd.service.d/php-fpm.conf - by default, the php-fpm service is started with httpd
    • /etc/php-fpm.conf - FPM main configuration
    • /etc/php-fpm.d/www.conf - default www pool configuration

Example 6.1. Running a "Hello, World!" PHP script using the Apache HTTP Server

  1. Create a hello directory for your project in the /var/www/html/ directory:

    # mkdir hello
  2. Create a hello.php file in the /var/www/html/hello/ directory with the following content:

    # <!DOCTYPE html>
    <html>
    <head>
    <title>Hello, World! Page</title>
    </head>
    <body>
    <?php
        echo 'Hello, World!';
    ?>
    </body>
    </html>
  3. Start the Apache HTTP Server:

    # systemctl start httpd
  4. To run the hello.php file, point the browser to:

    http://<hostname>/hello/hello.php

As a result, a web page with the “Hello, World!" text is displayed.

6.2.2. Using PHP with the nginx web server

You can run PHP code through the nginx web server. The steps below show how to install and configure nginx with PHP.

Prerequisites

  • The PHP scripting language is installed on your system.

Procedure

  1. Install the nginx package:

    # dnf install nginx
  2. Start the nginx server, or if it is already running, restart the nginx service after installing PHP:

    # systemctl start nginx
    # systemctl restart nginx
  3. Start the php-fpm service:

    # systemctl start php-fpm
  4. Optional: Enable both services to start at boot time:

    # systemctl enable php-fpm nginx
  5. To obtain information about your PHP settings, create the index.php file with the following content in the /usr/share/nginx/html/ directory:

    # echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.php
  6. To run the index.php file, point the browser to:

    http://<hostname>/
  7. Optional: Adjust configuration if you have specific requirements:

    • /etc/nginx/nginx.conf - nginx main configuration
    • /etc/nginx/conf.d/php-fpm.conf - FPM configuration for nginx
    • /etc/php-fpm.conf - FPM main configuration
    • /etc/php-fpm.d/www.conf - default www pool configuration
  8. Example — Running a "Hello, World!" PHP script: Create a hello directory for your project in the /usr/share/nginx/html/ directory:

    # mkdir hello
  9. Create a hello.php file in the /usr/share/nginx/html/hello/ directory with the following content:

    # <!DOCTYPE html>
    <html>
    <head>
    <title>Hello, World! Page</title>
    </head>
    <body>
    <?php
        echo 'Hello, World!';
    ?>
    </body>
    </html>
  10. Start the nginx server:

    # systemctl start nginx
  11. To run the hello.php file, point the browser to:

    http://<hostname>/hello/hello.php

    As a result, a web page with the “Hello, World!" text is displayed.

6.3. Running a PHP script using the command line

Run a PHP script from the command line to automate tasks or test code without using a web server.

Prerequisites

  • The PHP scripting language is installed on your system.

Procedure

  1. In a text editor, create a filename.php file

    Replace filename with the name of your file.

  2. Execute the created filename.php file from the command line:

    # php filename.php
  3. Example — Running a "Hello, World!" PHP script: Create a hello.php file with the following content using a text editor:

    <?php
        echo 'Hello, World!';
    ?>
  4. Execute the hello.php file from the command line. "Hello, World!" is printed:

    # php hello.php

6.4. Additional resources

  • httpd(8) - The manual page for the httpd service containing the complete list of its command-line options.
  • httpd.conf(5) - The manual page for httpd configuration, describing the structure and location of the httpd configuration files.
  • nginx(8) - The manual page for the nginx web server containing the complete list of its command-line options and list of signals.
  • php-fpm(8) - The manual page for PHP FPM describing the complete list of its command-line options and configuration files.
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top