Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 6. Using the PHP scripting language
Hypertext Preprocessor (PHP) is a general-purpose scripting language mainly used for server-side scripting. Install PHP and run PHP code using a web server or the command-line interface.
6.1. Installing the PHP scripting language Copier lienLien copié sur presse-papiers!
In RHEL 9, PHP is available as the php RPM package (PHP 8.0), the php:8.1 module stream (PHP 8.1), or the php:8.2 module stream (PHP 8.2).
Procedure
To install PHP 8.0, enter:
# dnf install phpTo install the
php:8.1orphp:8.2module stream with the default profile, enter for example:# dnf module install php:8.1
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.1orphp:8.2module stream, use for example:# dnf module install php:8.1/profileThe available profiles for the
php:8.1andphp:8.2module streams are:-
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 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:
# dnf module install php:8.1/minimal
6.2. Using the PHP scripting language with a web server Copier lienLien copié sur presse-papiers!
6.2.1. Using PHP with the Apache HTTP Server Copier lienLien copié sur presse-papiers!
In Red Hat Enterprise Linux 9, the Apache HTTP Server runs PHP by using FastCGI Process Manager (FPM) by default. Install and configure Apache with PHP.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Install the
httpdpackage:# dnf install httpdStart the
Apache HTTP Server:# systemctl start httpd-
Or, if the
Apache HTTP Serveris already running on your system, restart thehttpdservice after installing PHP:
-
Or, if the
# systemctl restart httpd
Start 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
-
Example 6.1. Running a "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.php
As a result, a web page with the “Hello, World!" text is displayed.
6.2.2. Using PHP with the nginx web server Copier lienLien copié sur presse-papiers!
You can run PHP code through the nginx web server. The steps below show how to install and configure nginx with PHP.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
Install the
nginxpackage:# dnf install nginxStart the
nginxserver, or if it is already running, restart thenginxservice after installing PHP:# systemctl start nginx# 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
-
Example — Running a "Hello, World!" PHP script: 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.
6.3. Running a PHP script using the command line Copier lienLien copié sur presse-papiers!
Run a PHP script from the command line to automate tasks or test code without using a web server.
Prerequisites
- The PHP scripting language is installed on your system.
Procedure
In a text editor, create a
filename.phpfileReplace filename with the name of your file.
Execute the created
filename.phpfile from the command line:# php filename.phpExample — Running a "Hello, World!" PHP script: Create a
hello.phpfile with the following content using a text editor:<?php echo 'Hello, World!'; ?>Execute the
hello.phpfile from the command line. "Hello, World!" is printed:# php hello.php
6.4. Additional resources Copier lienLien copié sur presse-papiers!
-
httpd(8)- The manual page for thehttpdservice containing the complete list of its command-line options. -
httpd.conf(5)- The manual page forhttpdconfiguration, describing the structure and location of thehttpdconfiguration files. -
nginx(8)- The manual page for thenginxweb 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.