Red Hat Training
A Red Hat training course is available for RHEL 8
Chapter 41. Using the PHP scripting language
Hypertext Preprocessor (PHP) is a general-purpose scripting language mainly used for server-side scripting, which enables you to run the PHP code using a web server.
In RHEL 8, the PHP scripting language is provided by the php
module, which is available in multiple streams (versions).
Depending on your use case, you can install a specific profile of the selected module stream:
-
common
- The default profile for server-side scripting using a web server. It includes several 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 thecommon
profile and additional packages for development purposes.
41.1. Installing the PHP scripting language
This section describes how to install a selected version of the php
module.
Procedure
To install a
php
module stream with the default profile, use:# yum module install php:stream
Replace stream with the version of PHP you wish to install.
For example, to install PHP 8.0:
# yum module install php:8.0
The default
common
profile installs also thephp-fpm
package, and preconfigures PHP for use with theApache HTTP Server
ornginx
.To install a specific profile of a
php
module stream, use:# yum module install php:stream/profile
Replace stream with the desired version and profile with the name of the profile you wish to install.
For example, to install PHP 8.0 for use without a web server:
# yum module install php:8.0/minimal
Additional resources
- If you want to upgrade from an earlier version of PHP available in RHEL 8, see Switching to a later stream.
- For more information on managing RHEL 8 modules and streams, see Installing, managing, and removing user-space components.
41.2. Using the PHP scripting language with a web server
41.2.1. Using PHP with the Apache HTTP Server
In Red Hat Enterprise Linux 8, 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 8.
This section describes how to run the PHP code using the FastCGI process server.
Prerequisites
The PHP scripting language is installed on your system.
Procedure
Install the
httpd
module:# yum module install httpd:2.4
Start the
Apache HTTP Server
:# systemctl start httpd
Or, if the
Apache HTTP Server
is already running on your system, restart thehttpd
service after installing PHP:# systemctl restart httpd
Start the
php-fpm
service:# systemctl start php-fpm
Optional: Enable both services to start at boot time:
# systemctl enable php-fpm httpd
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
To run the
index.php
file, point the browser to:http://<hostname>/
Optional: Adjust configuration if you have specific requirements:
-
/etc/httpd/conf/httpd.conf
- generichttpd
configuration -
/etc/httpd/conf.d/php.conf
- PHP-specific configuration forhttpd
-
/usr/lib/systemd/system/httpd.service.d/php-fpm.conf
- by default, thephp-fpm
service is started withhttpd
-
/etc/php-fpm.conf
- FPM main configuration -
/etc/php-fpm.d/www.conf
- defaultwww
pool configuration
-
Example 41.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:# mkdir hello
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>
Start the
Apache HTTP Server
:# systemctl start httpd
To run the
hello.php
file, point the browser to:http://<hostname>/hello/hello.php
As a result, a web page with the “Hello, World!” text is displayed.
Additional resources
41.2.2. Using PHP with the nginx web server
This section describes how to run PHP code through the nginx
web server.
Prerequisites
The PHP scripting language is installed on your system.
Procedure
Install an
nginx
module stream:# yum module install nginx:stream
Replace stream with the version of
nginx
you wish to install.For example, to install
nginx
version 1.18:# yum module install nginx:1.18
Start the
nginx
server:# systemctl start nginx
Or, if the
nginx
server is already running on your system, restart thenginx
service after installing PHP:# systemctl restart nginx
Start the
php-fpm
service:# systemctl start php-fpm
Optional: Enable both services to start at boot time:
# systemctl enable php-fpm nginx
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
To run the
index.php
file, point the browser to:http://<hostname>/
Optional: Adjust configuration if you have specific requirements:
-
/etc/nginx/nginx.conf
-nginx
main configuration -
/etc/nginx/conf.d/php-fpm.conf
- FPM configuration fornginx
-
/etc/php-fpm.conf
- FPM main configuration -
/etc/php-fpm.d/www.conf
- defaultwww
pool configuration
-
Example 41.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:# mkdir hello
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>
Start the
nginx
server:# systemctl start nginx
To run the
hello.php
file, point the browser to:http://<hostname>/hello/hello.php
As a result, a web page with the “Hello, World!” text is displayed.
Additional resources
41.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.
If you want to run php
scripts using only command-line, install the minimal
profile of a php
module stream.
See Installing the PHP scripting language.
Prerequisites
The PHP scripting language is installed on your system.
Procedure
In a text editor, create a
filename.php
fileReplace filename with the name of your file.
Execute the created
filename.php
file from the command line:# php filename.php
Example 41.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:<?php echo 'Hello, World!'; ?>
Execute the
hello.php
file from the command line:# php hello.php
As a result, “Hello, World!” is printed.
41.4. Additional resources
-
httpd(8)
— The manual page for thehttpd
service containing the complete list of its command-line options. -
httpd.conf(5)
— The manual page forhttpd
configuration, describing the structure and location of thehttpd
configuration files. -
nginx(8)
— The manual page for thenginx
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.