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 Copy linkLink copied to clipboard!
To develop and deploy server-side web applications, install PHP on your system. In RHEL 10, the PHP scripting language is available in PHP 8.3 as the php RPM package.
Procedure
Install PHP 8.3:
# dnf install php
Additional resources
4.2. Using the PHP scripting language with a web server Copy linkLink copied to clipboard!
4.2.1. Using PHP with the Apache HTTP Server Copy linkLink copied to clipboard!
In Red Hat Enterprise Linux 10, you can use the Apache HTTP Server to run PHP as a FastCGI process server. As an alternative PHP FastCGI daemon, you can use FastCGI Process Manager (FPM) to manage high website 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 Serverandphp-fpmto run PHP scripts.Install the
httpdpackage:# dnf install httpdStart the
Apache HTTP Server:# systemctl start httpdOr, if the
Apache HTTP Serveris already running on your system, restart thehttpdservice after installing PHP:# systemctl restart httpdStart the
php-fpmservice:# systemctl start php-fpmOptional: Enable both services to start at boot time:
# systemctl enable php-fpm httpdTo obtain information about your PHP settings, create the
index.phpfile with the following content in the/var/www/html/directory:# echo '<?php phpinfo(); ?>' > /var/www/html/index.phpTo run the
index.phpfile, point the browser to:http://<hostname>/Optional: Adjust configuration if you have specific requirements:
-
/etc/httpd/conf/httpd.conf- generichttpdconfiguration -
/etc/httpd/conf.d/php.conf- PHP-specific configuration forhttpd -
/usr/lib/systemd/system/httpd.service.d/php-fpm.conf- by default, thephp-fpmservice is started withhttpd -
/etc/php-fpm.conf- FPM main configuration -
/etc/php-fpm.d/www.conf- defaultwwwpool configuration
-
Run the "Hello, World!" PHP script using the Apache HTTP Server.
Create a
hellodirectory for your project in the/var/www/html/directory:# mkdir helloCreate a
hello.phpfile 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>Start the
Apache HTTP Server:# systemctl start httpdTo run the
hello.phpfile, point the browser to:http://<hostname>/hello/hello.phpAs a result, a web page with the "Hello, World!" text is displayed.
See
httpd(8),httpd.conf(5), andphp-fpm(8)man pages for more information.
4.2.2. Using PHP with the nginx web server Copy linkLink copied to clipboard!
Configure the nginx web server to process PHP scripts on your RHEL 10 system.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Configure the
nginxweb server andphp-fpmto run PHP scripts.Install the
nginxpackage:# dnf install nginxStart the
nginxserver:# systemctl start nginxOr, if the
nginxserver is already running on your system, restart thenginxservice after installing PHP:# systemctl restart nginxStart the
php-fpmservice:# systemctl start php-fpmOptional: Enable both services to start at boot time:
# systemctl enable php-fpm nginxTo obtain information about your PHP settings, create the
index.phpfile with the following content in the/usr/share/nginx/html/directory:# echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.phpTo run the
index.phpfile, point the browser to:http://<hostname>/Optional: Adjust configuration if you have specific requirements:
-
/etc/nginx/nginx.conf-nginxmain configuration -
/etc/nginx/conf.d/php-fpm.conf- FPM configuration fornginx -
/etc/php-fpm.conf- FPM main configuration -
/etc/php-fpm.d/www.conf- defaultwwwpool configuration
-
Run the "Hello, World!" PHP script using the nginx server.
Create a
hellodirectory for your project in the/usr/share/nginx/html/directory:# mkdir helloCreate a
hello.phpfile 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>Start the
nginxserver:# systemctl start nginxTo run the
hello.phpfile, point the browser to:http://<hostname>/hello/hello.phpAs a result, a web page with the "Hello, World!" text is displayed.
See
nginx(8)andphp-fpm(8)man pages for more information.
4.3. Running a PHP script using the command line Copy linkLink copied to clipboard!
A PHP script is usually run by using a web server. However, you can also run the script by using the command line.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Open a text editor and create a PHP file named
hello.phpwith the following content:<?php echo 'Hello, World!'; ?>-
Open a terminal and navigate to the directory containing
hello.php. Run the PHP script from the command line:
$ php hello.php Hello, World!See
php(1)man pages for more information.