5.2. Utilisation du langage de script PHP avec un serveur web
5.2.1. Utilisation de PHP avec le serveur HTTP Apache
Dans Red Hat Enterprise Linux 9, le site Apache HTTP Server
vous permet d'exécuter PHP en tant que serveur de processus FastCGI. FastCGI Process Manager (FPM) est un démon PHP FastCGI alternatif qui permet à un site web de gérer des charges élevées. PHP utilise FastCGI Process Manager par défaut dans RHEL 9.
Cette section décrit comment exécuter le code PHP en utilisant le serveur de processus FastCGI.
Conditions préalables
- Le langage de script PHP est installé sur votre système.
Procédure
Installez le paquetage
httpd
:# dnf install httpd
Démarrer le site
Apache HTTP Server
:# systemctl start httpd
Ou, si le service
Apache HTTP Server
fonctionne déjà sur votre système, redémarrez le servicehttpd
après avoir installé PHP :# systemctl restart httpd
Démarrez le service
php-fpm
:# systemctl start php-fpm
Facultatif : Activez les deux services pour qu'ils démarrent au moment du démarrage :
# systemctl enable php-fpm httpd
Pour obtenir des informations sur vos paramètres PHP, créez le fichier
index.php
avec le contenu suivant dans le répertoire/var/www/html/
:echo '<?php phpinfo() ; ?>' > /var/www/html/index.php
Pour exécuter le fichier
index.php
, pointez le navigateur sur :http://<hostname>/
En option : Ajustez la configuration si vous avez des besoins spécifiques :
-
/etc/httpd/conf/httpd.conf
- configuration génériquehttpd
-
/etc/httpd/conf.d/php.conf
- Configuration spécifique à PHP pourhttpd
-
/usr/lib/systemd/system/httpd.service.d/php-fpm.conf
- par défaut, le servicephp-fpm
est démarré avechttpd
-
/etc/php-fpm.conf
- Configuration principale du FPM -
/etc/php-fpm.d/www.conf
- configuration par défaut du poolwww
-
Exemple 5.1. Exécution d'un script PHP "Hello, World!" En utilisant le serveur HTTP Apache
Créez un répertoire
hello
pour votre projet dans le répertoire/var/www/html/
:# mkdir hello
Créez un fichier
hello.php
dans le répertoire/var/www/html/hello/
avec le contenu suivant :# <!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>
Démarrer le site
Apache HTTP Server
:# systemctl start httpd
Pour exécuter le fichier
hello.php
, pointez le navigateur sur :http://<hostname>/hello/hello.php
Le résultat est l'affichage d'une page web avec le texte "Hello, World !
Ressources supplémentaires
5.2.2. Utiliser PHP avec le serveur web nginx
Cette section décrit comment exécuter du code PHP via le serveur web nginx
.
Conditions préalables
- Le langage de script PHP est installé sur votre système.
Procédure
Installez le paquetage
nginx
:# dnf install nginx
Démarrez le serveur
nginx
:# systemctl start nginx
Ou, si le serveur
nginx
fonctionne déjà sur votre système, redémarrez le servicenginx
après avoir installé PHP :# systemctl restart nginx
Démarrez le service
php-fpm
:# systemctl start php-fpm
Facultatif : Activez les deux services pour qu'ils démarrent au moment du démarrage :
# systemctl enable php-fpm nginx
Pour obtenir des informations sur vos paramètres PHP, créez le fichier
index.php
avec le contenu suivant dans le répertoire/usr/share/nginx/html/
:echo '<?php phpinfo() ; ?>' > /usr/share/nginx/html/index.php
Pour exécuter le fichier
index.php
, pointez le navigateur sur :http://<hostname>/
En option : Ajustez la configuration si vous avez des besoins spécifiques :
-
/etc/nginx/nginx.conf
-nginx
configuration principale -
/etc/nginx/conf.d/php-fpm.conf
- Configuration FPM pournginx
-
/etc/php-fpm.conf
- Configuration principale du FPM -
/etc/php-fpm.d/www.conf
- configuration par défaut du poolwww
-
Exemple 5.2. Exécution d'un script PHP de type "Hello, World!" En utilisant le serveur nginx
Créez un répertoire
hello
pour votre projet dans le répertoire/usr/share/nginx/html/
:# mkdir hello
Créez un fichier
hello.php
dans le répertoire/usr/share/nginx/html/hello/
avec le contenu suivant :# <!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>
Démarrez le serveur
nginx
:# systemctl start nginx
Pour exécuter le fichier
hello.php
, pointez le navigateur sur :http://<hostname>/hello/hello.php
Le résultat est l'affichage d'une page web avec le texte "Hello, World !
Ressources supplémentaires