Este conteúdo não está disponível no idioma selecionado.

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 Toggle word wrap

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

  • Configure the Apache HTTP Server and php-fpm to run PHP scripts.

    1. Install the httpd package:

      # dnf install httpd
      Copy to Clipboard Toggle word wrap
    2. Start the Apache HTTP Server:

      # systemctl start httpd
      Copy to Clipboard Toggle word wrap

      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 Toggle word wrap
    3. Start the php-fpm service:

      # systemctl start php-fpm
      Copy to Clipboard Toggle word wrap
    4. Optional: Enable both services to start at boot time:

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

      http://<hostname>/
      Copy to Clipboard Toggle word wrap
    7. 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
  • Run the "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 Toggle word wrap
    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 Toggle word wrap
    3. Start the Apache HTTP Server:

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

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

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

      See httpd(8), httpd.conf(5), and php-fpm(8) man pages for more information.

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

  • Configure the nginx web server and php-fpm to run PHP scripts.

    1. Install the nginx package:

      # dnf install nginx
      Copy to Clipboard Toggle word wrap
    2. Start the nginx server:

      # systemctl start nginx
      Copy to Clipboard Toggle word wrap

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

      # systemctl restart nginx
      Copy to Clipboard Toggle word wrap
    3. Start the php-fpm service:

      # systemctl start php-fpm
      Copy to Clipboard Toggle word wrap
    4. Optional: Enable both services to start at boot time:

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

      http://<hostname>/
      Copy to Clipboard Toggle word wrap
    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
  • Run the "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 Toggle word wrap
    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 Toggle word wrap
    3. Start the nginx server:

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

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

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

      See nginx(8) and php-fpm(8) man pages for more information.

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. Open a text editor and create a PHP file named hello.php with the following content:

    <?php
        echo 'Hello, World!';
    ?>
    Copy to Clipboard Toggle word wrap
  2. Open a terminal and navigate to the directory containing hello.php.
  3. Run the PHP script from the command line:

    $ php hello.php
    Hello, World!
    Copy to Clipboard Toggle word wrap

    See php(1) man pages for more information.

Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat