Chapter 4. Using the PHP scripting language


Hypertext Preprocessor (PHP) is a general-purpose scripting language mainly used for server-side scripting. You can use PHP to run the PHP code by using a web server.

4.1. Installing the PHP scripting language

In RHEL 10, PHP is available in PHP 8.3 as the php RPM package.

Procedure

  • To install PHP 8.3, enter:

    # dnf install php
    Copy to Clipboard

4.2. Using the PHP scripting language with a web server

4.2.1. Using PHP with the Apache HTTP Server

In Red Hat Enterprise Linux 10, the Apache HTTP Server allows you to run PHP as a FastCGI process server. FastCGI Process Manager (FPM) is an alternative PHP FastCGI daemon that allows a website to manage high loads. PHP uses FastCGI Process Manager by default in RHEL 10.

Prerequisites

  • The PHP scripting language is installed on your system.

Procedure

  1. Start the Apache HTTP Server:

    # systemctl start httpd
    Copy to Clipboard

    Or, if the Apache HTTP Server is already running on your system, restart the httpd service after installing PHP:

    # systemctl restart httpd
    Copy to Clipboard
  2. Start the php-fpm service:

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

    # systemctl enable php-fpm httpd
    Copy to Clipboard
  4. 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
    Copy to Clipboard
  5. To run the index.php file, point the browser to:

    http://<hostname>/
    Copy to Clipboard
  6. 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 4.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
    Copy to Clipboard
  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>
    Copy to Clipboard
  3. Start the Apache HTTP Server:

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

    http://<hostname>/hello/hello.php
    Copy to Clipboard

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

4.2.2. Using PHP with the nginx web server

You can run PHP code through the nginx web server.

Prerequisites

  • The PHP scripting language is installed on your system.

Procedure

  1. Start the nginx server:

    # systemctl start nginx
    Copy to Clipboard

    Or, if the nginx server is already running on your system, restart the nginx service after installing PHP:

    # systemctl restart nginx
    Copy to Clipboard
  2. Start the php-fpm service:

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

    # systemctl enable php-fpm nginx
    Copy to Clipboard
  4. 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
    Copy to Clipboard
  5. To run the index.php file, point the browser to:

    http://<hostname>/
    Copy to Clipboard
  6. 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

Example 4.2. Running a "Hello, World!" PHP script using the nginx server

  1. Create a hello directory for your project in the /usr/share/nginx/html/ directory:

    # mkdir hello
    Copy to Clipboard
  2. 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>
    Copy to Clipboard
  3. Start the nginx server:

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

    http://<hostname>/hello/hello.php
    Copy to Clipboard

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

4.3. Running a PHP script using the command line

A PHP script is usually run using a web server, but also can be run using the command line.

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
    Copy to Clipboard

Example 4.3. Running a "Hello, World!" PHP script using the command line

  1. Create a hello.php file with the following content using a text editor:

    <?php
        echo 'Hello, World!';
    ?>
    Copy to Clipboard
  2. Execute the hello.php file from the command line:

    # php hello.php
    Copy to Clipboard

    As a result, "Hello, World!” is printed.

4.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.
Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

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

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

Theme

© 2025 Red Hat