大学网 > php中文网 > 运维nginx怎么启动php正文

nginx怎么启动php

中国大学网 2024-10-17
如何在 Nginx 中启动 php?在 nginx 中启动 php 的步骤:1. 安装 php;2. 安装 php-fpm;3. 配置 nginx 虚拟主机以使用 php-fpm。

nginx怎么启动php

如何启动 nginx 中的 PHP

简要回答:

在 nginx 中启动 PHP 的步骤:

  1. 安装 PHP
  2. 安装 PHP-FPM(FastCGI 进程管理器)
  3. 配置 nginx 虚拟主机以使用 PHP-FPM

详细说明:

立即学习“PHP免费学习笔记(深入)”;

1. 安装 PHP:

使用以下命令安装 PHP:

sudo apt-get update
sudo apt-get install php7.4-fpm

2. 安装 PHP-FPM:

使用以下命令安装 PHP-FPM:

sudo apt-get install php7.4-fpm

3. 配置 nginx 虚拟主机:

打开虚拟主机配置文件(例如,/etc/nginx/sites-available/example.com),并添加以下配置:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

其中:

  • location ~ \.php$:匹配所有以 .php 结尾的文件
  • try_files $uri =404;:如果文件不存在,则返回 404 错误
  • fastcgi_pass unix:/run/php/php7.4-fpm.sock;:将请求转发到 PHP-FPM 套接字
  • fastcgi_index index.php;:指定默认索引文件
  • include fastcgi_params;:包含 FastCGI 参数

保存并退出配置文件。

重启 nginx:

sudo systemctl restart nginx

PHP 现在应该在 nginx 中启动并运行。

以上就是nginx怎么启动php的详细内容,更多请关注中国大学网其它相关文章!