配置 Nginx 运行 PHP 的步骤包括:安装 PHP、安装 Nginx、编辑 Nginx 主配置文件添加 PHP 配置、创建 PHP 文件、重启 Nginx、测试 PHP,成功后会显示 "PHP is working!" 消息。

如何配置 Nginx 以运行 PHP
步骤 1:安装 PHP
在你的服务器上安装 PHP,命令如下:
<code>sudo apt-get install php7.4-fpm</code>
步骤 2:安装 Nginx
立即学习“PHP免费学习笔记(深入)”;
如果尚未安装 Nginx,请使用以下命令:
<code>sudo apt-get install nginx</code>
步骤 3:配置 Nginx
编辑 Nginx 的主配置文件 /etc/nginx/nginx.conf,添加以下配置块:
<code>server {
listen 80;
server_name example.com;
root /var/www/html;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}</code>步骤 4:创建 PHP 文件
在 Nginx 的根目录 (/var/www/html) 中,创建一个 PHP 文件,例如 index.php:
<code class="php"><?php echo "PHP is working!"; ?></code>
步骤 5:重启 Nginx
重启 Nginx 以应用更改:
<code>sudo systemctl restart nginx</code>
步骤 6:测试 PHP
访问你的网站(http://example.com),你应该看到 "PHP is working!" 消息,表明 PHP 已成功与 Nginx 集成。
以上就是nginx如何配置php的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号