Red Hat Training

A Red Hat training course is available for RHEL 8

16.2. Uso del lenguaje de programación PHP con un servidor web

16.2.1. Uso de PHP con el servidor HTTP Apache

En RHEL 8, la página Apache HTTP Server permite ejecutar PHP como un servidor de procesos FastCGI. El Gestor de Procesos FastCGI (FPM) es un demonio PHP FastCGI alternativo que permite a un sitio web gestionar altas cargas. PHP utiliza FastCGI Process Manager por defecto en RHEL 8.

Esta sección describe cómo ejecutar el código PHP utilizando el servidor de procesos FastCGI.

Requisitos previos

Procedimiento

  1. Instale el módulo httpd:

    # yum module install httpd:2.4
  2. Inicie el Apache HTTP Server:

    # systemctl start httpd

    O, si el Apache HTTP Server ya está funcionando en su sistema, reinicie el servicio httpd después de instalar PHP:

    # systemctl restart httpd
  3. Inicie el servicio php-fpm:

    # systemctl start php-fpm
  4. Opcional: Habilite ambos servicios para que se inicien en el momento del arranque:

    # systemctl enable php-fpm httpd
  5. Para obtener información sobre su configuración de PHP, cree el archivo index.php con el siguiente contenido en el directorio /var/www/html/:

    echo '<?php phpinfo(); ?>' > /var/www/html/index.php
  6. Para ejecutar el archivo index.php, dirija el navegador a:

    http://<hostname>/
  7. Opcional: Ajuste la configuración si tiene requisitos específicos:

    • /etc/httpd/conf/httpd.conf - configuración genérica de httpd
    • /etc/httpd/conf.d/php.conf - Configuración específica de PHP para httpd
    • /usr/lib/systemd/system/httpd.service.d/php-fpm.conf - por defecto, el servicio php-fpm se inicia con httpd
    • /etc/php-fpm.conf - Configuración principal del FPM
    • /etc/php-fpm.d/www.conf - configuración por defecto de la piscina www

Ejemplo 16.1. Ejecutar un script PHP "¡Hola, mundo! PHP utilizando el servidor HTTP Apache

  1. Cree un directorio hello para su proyecto en el directorio /var/www/html/:

    # mkdir hello
  2. Cree un archivo hello.php en el directorio /var/www/html/hello/ con el siguiente contenido:

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

    # systemctl start httpd
  4. Para ejecutar el archivo hello.php, dirija el navegador a:

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

    Como resultado, se muestra una página web con el texto "Hello, World!".