配置nginx 支持php
<p>一、确保php-fpm已经启动:</p><div class="cnblogs_code">
<pre>ps -A | grep php-fpm</pre>
</div>
<p>如果没有启动,则启动php-fpm:</p>
<div class="cnblogs_code">
<pre>/usr/local/sbin/php-fpm</pre>
</div>
<p>查看是否启动成功:</p>
<div class="cnblogs_code">
<pre>root@iZ25fm7iewtZ:/usr/local/etc# ps -ef | grep php-<span style="color: rgba(0, 0, 0, 1)">fpm
root </span><span style="color: rgba(128, 0, 128, 1)">3691</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">18</span>:<span style="color: rgba(128, 0, 128, 1)">49</span> ? <span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span> php-fpm: master process (/usr/local/etc/php-<span style="color: rgba(0, 0, 0, 1)">fpm.conf)
www</span>-data<span style="color: rgba(128, 0, 128, 1)">3692</span><span style="color: rgba(128, 0, 128, 1)">3691</span><span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">18</span>:<span style="color: rgba(128, 0, 128, 1)">49</span> ? <span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span> php-<span style="color: rgba(0, 0, 0, 1)">fpm: pool www
www</span>-data<span style="color: rgba(128, 0, 128, 1)">3693</span><span style="color: rgba(128, 0, 128, 1)">3691</span><span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">18</span>:<span style="color: rgba(128, 0, 128, 1)">49</span> ? <span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span> php-<span style="color: rgba(0, 0, 0, 1)">fpm: pool www
root </span><span style="color: rgba(128, 0, 128, 1)">4982</span> <span style="color: rgba(128, 0, 128, 1)">29553</span><span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">18</span>:<span style="color: rgba(128, 0, 128, 1)">59</span> pts/<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span>:<span style="color: rgba(128, 0, 128, 1)">00</span> grep --color=auto php-<span style="color: rgba(0, 0, 0, 1)">fpm
root@iZ25fm7iewtZ:</span>/usr/local/etc# netstat -tnl | grep <span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">
tcp </span><span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span> <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:* LISTEN</pre>
</div>
<p>修改nginx的配置文件,支持php文件的解析,找到location的添加位置,在后面添加下面这个location</p>
<div class="cnblogs_code">
<pre>location ~<span style="color: rgba(0, 0, 0, 1)"> \.php$ {
root </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">html;
fastcgi_pass </span><span style="color: rgba(128, 0, 128, 1)">127.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.1</span>:<span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">;
fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME</span>/usr/local/nginx/html/<span style="color: rgba(0, 0, 0, 1)">$fastcgi_script_name;
#以下方式也可以
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include fastcgi_params;
}</span></pre>
</div>
<p>重启nginx</p>
<div class="cnblogs_code">
<pre>service nginx restart</pre>
</div>
<p>进入web更目录,编辑index.php</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"><?php
echo "hello php !"</pre>
</div>
<p>浏览器中输入:localhost/index.php 即可</p>
</div>
<div id="MySignature" role="contentinfo">
博客里大都是转载的内容,其目的主要用户知识的组织和管理。<br><br>
来源:https://www.cnblogs.com/liyuanhong/p/12017232.html <br><div class="quote"><blockquote>楼主这个检查步骤很详细</blockquote></div><br>补充一下配置nginx的步骤:<br><br>二、修改nginx配置文件,比如在server块里添加:<br><br><div class="blockcode"><blockquote><br>location ~ \.php {<br> fastcgi_pass 127.0.0.1:9000;<br> fastcgi_index index.php;<br> fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;<br> include fastcgi_params;<br>}<br></blockquote></div><br><br><br>三、重启nginx使配置生效:<br><br><div class="blockcode"><blockquote>/usr/sbin/nginx -s reload</blockquote></div><br><br><br>四、最后在网站目录下放个探针测试一下:<br><br><div class="blockcode"><blockquote><br><?php phpinfo(); ?><br></blockquote></div><br><br><br>浏览器访问看看能不能正常解析。<br><br>小提示:如果遇到502错误,多数是php-fpm没启动或者端口不对;404的话检查下fastcgi_param路径配置。 楼主分享的步骤太实用啦!之前我搭环境的时候踩了好几个相关的坑,给大伙补充点注意事项,省得走弯路~
1. 留意php-fpm的监听形式
现在很多新版本的php-fpm默认用unix套接字监听而非9000端口,大家可以打开php-fpm.d目录下的www.conf查看listen配置项,如果是sock文件路径的话,nginx配置里的fastcgi_pass要对应填sock路径,别硬写127.0.0.1:9000,不然肯定会报502错误。
2. 运行用户权限要对齐
nginx的运行用户要和php-fpm池的运行用户保持一致,不然静态资源容易出403错误,php动态请求也可能有权限问题,碰到相关报错优先检查这里就行。
3. 改完配置先校验再生效
修改完nginx配置一定要先执行nginx -t
检查语法是否正确,确认没问题再用nginx -s reload
或者对应的systemctl命令重载配置,别直接重启服务,万一语法出错很容易导致线上业务中断。
另外用CentOS系列系统的小伙伴,如果所有配置都核对过还是连不上php-fpm,可以检查下selinux是不是没关闭,临时关闭执行setenforce 0
就可以,要永久关闭就得修改/etc/selinux/config文件哦~
祝大家都能一次配置成功哈哈😆
頁:
[1]