Chapter 6. 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. 6.1. Installing the PHP scripting language In RHEL 9, PHP is available in the following versions and formats: PHP 8.0 as the php RPM package PHP 8.1 as the php:8.1 module stream PHP 8.2 as the php:8.2 module stream Procedure Depending on your scenario, complete one of the following steps: To install PHP 8.0, enter: To install the php:8.1 or php:8.2 module stream with the default profile, enter for example: 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: Available profiles are as follows: 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 interface 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: Additional resources Managing software with the DNF tool 6.2. Using the PHP scripting language with a web server 6.2.1. Using PHP with the Apache HTTP Server In Red Hat Enterprise Linux 9, the Apache HTTP Server enables 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 9. You can run the PHP code using the FastCGI process server. Prerequisites The PHP scripting language is installed on your system. Procedure Install the httpd package: Start the Apache HTTP Server : Or, if the Apache HTTP Server is already running on your system, restart the httpd service after installing PHP: Start the php-fpm service: Optional: Enable both services to start at boot time: To obtain information about your PHP settings, create the index.php file with the following content in the /var/www/html/ directory: To run the index.php file, point the browser to: 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 Create a hello directory for your project in the /var/www/html/ directory: Create a hello.php file in the /var/www/html/hello/ directory with the following content: Start the Apache HTTP Server : To run the hello.php file, point the browser to: As a result, a web page with the "Hello, World!" text is displayed. Additional resources Setting up the Apache HTTP web server 6.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 Install the nginx package: Start the nginx server: Or, if the nginx server is already running on your system, restart the nginx service after installing PHP: Start the php-fpm service: Optional: Enable both services to start at boot time: To obtain information about your PHP settings, create the index.php file with the following content in the /usr/share/nginx/html/ directory: To run the index.php file, point the browser to: 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 6.2. Running a "Hello, World!" PHP script using the nginx server Create a hello directory for your project in the /usr/share/nginx/html/ directory: Create a hello.php file in the /usr/share/nginx/html/hello/ directory with the following content: Start the nginx server: To run the hello.php file, point the browser to: As a result, a web page with the "Hello, World!" text is displayed. Additional resources Setting up and configuring NGINX 6.3. Running a PHP script using the command-line interface A PHP script is usually run using a web server, but also can be run using the command-line interface. Prerequisites The PHP scripting language is installed on your system. Procedure In a text editor, create a filename .php file Replace filename with the name of your file. Execute the created filename .php file from the command line: Example 6.3. Running a "Hello, World!" PHP script using the command-line interface Create a hello.php file with the following content using a text editor: Execute the hello.php file from the command line: As a result, "Hello, World!" is printed. 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.

Commands:
dnf install php
dnf module install php:8.1
dnf module install php:8.1/ profile
dnf module install php:8.1/minimal
dnf install httpd
systemctl start httpd
systemctl restart httpd
systemctl start php-fpm
systemctl enable php-fpm httpd
echo '<?php phpinfo(); ?>' > /var/www/html/index.php
http://<hostname>/
mkdir hello
<!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>
systemctl start httpd
http://<hostname>/hello/hello.php
dnf install nginx
systemctl start nginx
systemctl restart nginx
systemctl start php-fpm
systemctl enable php-fpm nginx
echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.php
http://<hostname>/
mkdir hello
<!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>
systemctl start nginx
http://<hostname>/hello/hello.php
php filename .php
<?php echo 'Hello, World!'; ?>
php hello.php