Red Hat Training
A Red Hat training course is available for RHEL 8
42.2.2. 使用带有 nginx web 服务器的 PHP
本节论述了如何通过 nginx
web 服务器运行 PHP 代码。
先决条件
在您的系统上安装 PHP 脚本语言。
请参阅安装 PHP 脚本语言。
流程
安装
nginx
模块流:# yum module install nginx:stream
使用要安装的
nginx
版本替换 stream。例如,要安装
nginx
版本 1.18::# yum module install nginx:1.18
启动
nginx
服务器:# systemctl start nginx
或者,如果
nginx
服务器已在您的系统中运行,请在安装 PHP 后重启nginx
服务:# systemctl restart nginx
启动
php-fpm
服务:# systemctl start php-fpm
可选: 在引导时启用这两个服务:
# systemctl enable php-fpm nginx
要获取 PHP 设置的信息,请在
/usr/share/nginx/html/
目录中使用以下内容创建index.php
文件:echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.php
要运行
index.php
文件,请将浏览器指向:http://<hostname>/
可选:如果您有具体要求,请调整配置:
-
/etc/nginx/nginx.conf
-nginx
主配置 -
/etc/nginx/conf.d/php-fpm.conf
- FPM 配置nginx
-
/etc/php-fpm.conf
- FPM 主配置 -
/etc/php-fpm.d/www.conf
- 默认www
池配置
-
例 42.2. 运行"Hello, World!" 使用 nginx 服务器的 PHP 脚本
在
/usr/share/nginx/html/
目录中为您的项目创建一个hello
目录:# mkdir hello
在
/usr/share/nginx/html/hello/
目录中创建一个包含以下内容的hello.php
文件:# <!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>
启动
nginx
服务器:# systemctl start nginx
要运行
hello.php
文件,请将浏览器指向:http://<hostname>/hello/hello.php
因此,会显示带有 "Hello, World!" 文本的网页。
其他资源