Este conteúdo não está disponível no idioma selecionado.
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 Copiar o linkLink copiado para a área de transferência!
In RHEL 10, PHP is available in PHP 8.3 as the php RPM package.
Procedure
To install PHP 8.3, enter:
dnf install php
# dnf install phpCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
4.2. Using the PHP scripting language with a web server Copiar o linkLink copiado para a área de transferência!
4.2.1. Using PHP with the Apache HTTP Server Copiar o linkLink copiado para a área de transferência!
In Red Hat Enterprise Linux 10, the Apache HTTP Server allows 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 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 httpd
# dnf install httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
Apache HTTP Server:systemctl start httpd
# systemctl start httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Or, if the
Apache HTTP Serveris already running on your system, restart thehttpdservice after installing PHP:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
php-fpmservice:systemctl start php-fpm
# systemctl start php-fpmCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Enable both services to start at boot time:
systemctl enable php-fpm httpd
# systemctl enable php-fpm httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow To 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.php
# echo '<?php phpinfo(); ?>' > /var/www/html/index.phpCopy to Clipboard Copied! Toggle word wrap Toggle overflow To run the
index.phpfile, point the browser to:http://<hostname>/
http://<hostname>/Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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 hello
# mkdir helloCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
hello.phpfile in the/var/www/html/hello/directory with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
Apache HTTP Server:systemctl start httpd
# systemctl start httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow To run the
hello.phpfile, point the browser to:http://<hostname>/hello/hello.php
http://<hostname>/hello/hello.phpCopy to Clipboard Copied! Toggle word wrap Toggle overflow As 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 Copiar o linkLink copiado para a área de transferência!
You can run PHP code through the nginx web server.
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 nginx
# dnf install nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
nginxserver:systemctl start nginx
# systemctl start nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow Or, if the
nginxserver is already running on your system, restart thenginxservice after installing PHP:systemctl restart nginx
# systemctl restart nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
php-fpmservice:systemctl start php-fpm
# systemctl start php-fpmCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Enable both services to start at boot time:
systemctl enable php-fpm nginx
# systemctl enable php-fpm nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow To 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.php
# echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.phpCopy to Clipboard Copied! Toggle word wrap Toggle overflow To run the
index.phpfile, point the browser to:http://<hostname>/
http://<hostname>/Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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 hello
# mkdir helloCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
hello.phpfile in the/usr/share/nginx/html/hello/directory with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
nginxserver:systemctl start nginx
# systemctl start nginxCopy to Clipboard Copied! Toggle word wrap Toggle overflow To run the
hello.phpfile, point the browser to:http://<hostname>/hello/hello.php
http://<hostname>/hello/hello.phpCopy to Clipboard Copied! Toggle word wrap Toggle overflow As 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 Copiar o linkLink copiado para a área de transferência!
A PHP script is usually run using a web server, but also can be run 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!'; ?><?php echo 'Hello, World!'; ?>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Open a terminal and navigate to the directory containing
hello.php. Run the PHP script from the command line:
php hello.php
$ php hello.php Hello, World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow See
php(1)man pages for more information.