nginx运行php的终极方法,可避免许多不必要的坑

妙音 posted @ 2018年12月28日 00:56 in php with tags php nginx php-fpm , 3115 阅读
 

 说明

 
php有许多开源项目比较好像,在自己的机器上用docker搭建了很多。
 
每个docker都是跑了个apache,每个docker里面都有apache。
 
这样好像有点浪费资源,而平时都是用nginx,所以研究了用nginx运行php
 
中间竟然碰到许多坑,而回过头来看,其实都很简单。
 

nginx运行php的原理

 
nginx不能解析php,要和php-fpm配置使用。nginx负责将php的请求转给php-fpm解析处理,然后返回响应结果
 

nginx运行php会遇到哪些坑

 
错误的提示有很多,最基本有"File not found"、"403"、"404"、"nginx no input file specified"等等
 
总之能试出许多错误。**而归结到底,只有一个根本原因“权限”**
 
而最好的方法是,直接把nginx和php-fpm设置为root用户运行
 

测试环境

 
|对象|版本|
|--|--|
| 操作系统 | fedora27 |
| nginx    | 1.12.1   |
| php-fpm  | php7     |
 
 

配置方法

 
* 安装nginx
 
yum install nginx
 
* 安装php-fpm
 
yum install php-fpm
经过上面两步, php-fpm会自动将nginx配置好,只要将php放到/usr/share/nginx/html目录就可以运行。但是我不想放在这个目录。
 
 
* 设置php-fpm以root用户运行
 
1.打开文件 /usr/lib/systemd/system/php-fpm.service


2.在ExecStart 最后面加上参数-R标识可以用root身份运行(php-fpm默认不能用root运行)
(有可能改了php-fpm.server文件不能运行,请运行systemctl reload重新加载)


[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
ExecStart=/usr/sbin/php-fpm --nodaemonize -R
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target


3.打开文件/etc/php-fpm.d/www.conf

4.将修改user和group为root
user=root
group=root
 
* 设置nginx以root身份运行
 
1.打开/etc/nginx/nginx.conf

2.将user改为root
 
* 配置自己的php项目
 
将下面内容放到/etc/nginx/conf.d/目录下
server {
        listen 8080;
        server_name  localhost;

        root  /home/xxxxx;      #改成自己的地址
        index  /_h5ai/public/index.php;   #改成自己的php首页
        include /etc/nginx/default.d/*.conf;    #使用已经配置好的location

        location / {
        }
}
 

排错方法

 
如果在配置过程中,遇到了错误。用什么方法调试呢?
 
* nginx调试方法
 
nginx的任务是把php的路径调对。可以先把php-fpm注释掉,专门调nginx,当能从浏览器下载php文件,说明配置正确。
 
* php-fpm调试方法
 
用命令运行php-fpm会有清晰的输出
 
/usr/sbin/php-fpm --nodaemonize -R
 

总结

 
nginx运行php总的说来很简单,由于权限问题,会导致遇到许多的坑。
 
实际很多情况下,都是给内部用,直接用root就可以。当然这样方法在正式环境不安全。
 
来源
 

 

Avatar_small
sagame66 说:
2020年11月21日 19:33

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. sagame66

Avatar_small
rappers weed 说:
2020年11月21日 21:25

Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC. rappers weed

Avatar_small
best legal steroids 说:
2020年11月23日 16:16

You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. best legal steroids

Avatar_small
Arabic majlis floor 说:
2020年11月24日 20:18

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information. Arabic majlis floor sofa

Avatar_small
buy soma 500 说:
2020年11月25日 18:33

This is my first time i visit here. I found such a substantial number of interesting stuff in your blog especially its examination. Really its inconceivable article. Keep it up. perro

Avatar_small
SaaS Security 说:
2020年12月14日 15:03

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!!

Avatar_small
pretty gaming 说:
2020年12月15日 14:45

Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work. pretty gaming

Avatar_small
Portugal property 说:
2020年12月15日 20:04

I think this is definitely an amazing project here. So much good will be coming from this project. The ideas and the work behind this will pay off so much. Portugal property

Avatar_small
luxury homes carmel 说:
2020年12月17日 12:11

I was impressed with the site that you created, so memotipasi many people to be more advanced, there also kunjugi me, as a comparison amber park review

Avatar_small
먹튀검증사이트 说:
2020年12月18日 05:25

You guardians do an astounding web diary, and have some unfathomable substance. Continue doing extraordinary. 먹튀검증사이트

Avatar_small
CarShield Protection 说:
2020年12月18日 14:45

This was a shocking post. It has some look at here fundamental data on this subject. CarShield Protection Plans

Avatar_small
BUY LSD ONLINE 说:
2020年12月18日 19:08

pleasant post, stay aware of this fascinating work. It truly regards realize that this subject is being secured likewise on this site so cheers for setting aside time to talk about this! MAGIC MUSHROOMS PSILOCYBIN

Avatar_small
Yacht security 说:
2020年12月21日 00:49

definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog! Yacht security

Avatar_small
no excuses runner 说:
2020年12月21日 21:55

Access to UFABET Mobile, online football betting website Provide complete services through UFABET entrance, apply today, receive a maximum bonus of 5000 baht for UFA888 members ยูฟ่าเบท

Avatar_small
hyll on Holland show 说:
2020年12月22日 15:44

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. hyll on Holland showflat

Avatar_small
sofa bed melbourne 说:
2020年12月23日 14:39

This blog was extremely helpful. I really appreciate your kindness in sharing this with me and everyone else! sofa bed melbourne

Avatar_small
ryse residences cond 说:
2020年12月25日 15:22

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. one north eden showflat

Avatar_small
buy lsd online 说:
2020年12月28日 02:40

 If someone week i really ashen-haired not actually pretty, whether you will lite grope a present, thought to follow us to displays bursting with ends of the earth considerably? Inside the impeccant previous, sea ever have dried-up, my hubby and i only may very well be with all of you connected thousands of samsara. buy lsd online

Avatar_small
Situs Judi Online Pk 说:
2021年1月03日 23:33

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. sky hdri

Avatar_small
Betfair vedonlyöntib 说:
2021年1月04日 15:16

Extremely pleasant article, I appreciated perusing your post, exceptionally decent share, I need to twit this to my adherents. Much appreciated!. Betfair vedonlyöntibonus

Avatar_small
Resistance bands 说:
2021年1月05日 22:42

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. Resistance bands

Avatar_small
buy orginal ps14 说:
2021年1月06日 15:17

For a long time me & my friend were searching for informative blogs, but now I am on the right place guys, you have made a room in my heart! buy orginal ps14

Avatar_small
Lsd 说:
2021年1月06日 21:25

Interesting post. I Have Been wondering about this issue. so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks Lsd

Avatar_small
Nevada 说:
2021年1月08日 16:49

I have added and shared your site to my social media accounts to send people back to your site because I am sure they will find it extremely helpful too. Nevada

Avatar_small
f1 2021 说:
2021年1月09日 02:32

Extremely pleasant article, I appreciated perusing your post, exceptionally decent share, I need to twit this to my adherents. Much appreciated!.  f1 2021

Avatar_small
furnace replacement 说:
2021年1月09日 20:16

I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading. I had to go show it to my friend and he ejoyed it as well! furnace replacement

Avatar_small
wedding dresses west 说:
2021年1月11日 17:02

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. wedding dresses west yorkshire

Avatar_small
Assam 1st Class Que 说:
2023年7月11日 00:32

Assam 1st Class Question Paper 2024 will help a lot to the Students to Estimate their writing speed in the Examination 2024, After Completing the Preparation of entire Syllabus, this Assam Sample Paper 2024 will also help to revise the Exam Patter, Students can easily score high marks as they are perfect in every corner of the subject.These Assam 1st Question Paper 2024 are Prepared as Assam 1st Class Question Paper 2024 per the updated Board of Education in the Assam State Syllabus to help Students in their exam Preparations, which Aims to build both the Practical knowledge as well as analytical Skills for Assam of Every Student.

Avatar_small
meidir 说:
2023年10月06日 18:35

The the next occasion I read a blog, Hopefully it doesnt disappoint me around this blog. What i’m saying is, I know it was my substitute for read, but I really thought youd have something interesting to express. All I hear is often a number of whining about something you could fix in the event you werent too busy in search of attention. 花藝學校

Avatar_small
meidir 说:
2023年10月13日 11:41

Spot lets start on this write-up, I truly believe this excellent website needs considerably more consideration. I’ll oftimes be once more to read a lot more, thank you for that info. 才藝教學

 

 

===================

 

 

Thank you for taking the time to publish such a detailed and informative write-up. 興趣班

 

====================

 

An intriguing discussion may be valued at comment. There’s no doubt that that you should write much more about this topic, it will not become a taboo subject but normally consumers are too few to talk on such topics. To a higher. Cheers 手機回收

 

 

=====================

 

 

I am frequently to blogging and i also truly appreciate your posts. Your content has really peaks my interest. I am about to bookmark your web site and keep checking for brand spanking new details. 電腦回收

Avatar_small
chromeheartshoodies. 说:
2023年10月13日 13:58

chromeheartshoodies hoodies are available at just official store to purchase the high quality products at cheap price.

Avatar_small
travis scott 说:
2023年10月13日 14:42

This is a very nice blog and learned more knowledge to read this post thanks for sharing this informative post.

Avatar_small
hellfire club 说:
2023年10月13日 14:43

I like it very much . It is too much informative for me . I have learnt alot of things from there .

Avatar_small
BAD BUNNY 说:
2023年10月13日 14:43

Its an interesting and useful information. We all get highly motivation from this .https://badbunnymerch.shop/

Avatar_small
STUSST 说:
2023年10月13日 14:44

I like it very much . It is too much informative for me . I have learnt alot of things from there .

Avatar_small
NIRVANA 说:
2023年10月13日 14:45

I like your blog because it is so informative. I learned a lot from your post thank you.

Avatar_small
meidir 说:
2023年10月14日 01:38

I am not real superb with English but I find this very leisurely to understand . heart

Avatar_small
Top SEO 说:
2024年3月05日 18:48

As a busy professional, finding time to prioritize my health was always a challenge. However, persistent neck and shoulder pain forced me to seek help from the best chiropractor in Woodland Hills, Dr. Shane Young, DC. His clinic in Chatsworth was conveniently located, making it easy for me to schedule appointments around my hectic schedule. From the moment I walked into his clinic, I was impressed by the warm and welcoming atmosphere. Dr. Young's expertise and attention to detail were evident from our first consultation. Through a combination of chiropractic adjustments and ergonomic advice, my neck and shoulder pain have significantly improved. I can't thank Dr. Young enough for helping me live pain-free and thrive in my professional life.

Avatar_small
Top SEO 说:
2024年3月08日 12:44

very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post! gocengqq

Avatar_small
Top SEO 说:
2024年3月09日 14:53 Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. here
Avatar_small
Top SEO 说:
2024年3月09日 15:00

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. aslidomino

Avatar_small
Top SEO 说:
2024年3月10日 14:22

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. bonusqq

Avatar_small
Top SEO 说:
2024年3月10日 14:30

Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. king gacor

Avatar_small
Top SEO 说:
2024年3月10日 20:13 Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. Betensured
Avatar_small
Top SEO 说:
2024年3月13日 07:04 I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. ptliga
Avatar_small
Top SEO 说:
2024年3月13日 13:38

Our company is the best answer for all household appliance repairs. We are locally owned and operated company that has been serving many areas of Vancouver: Coquitlam, Port Coquitlam, Pitt Meadows, Maple Ridge, Port Moody. We offer quality service and/or repair for all major home appliances: refrigerators, washers and dryers, dishwashers, garburators, stoves, ovens, hood fans, microwaves etc. Technicians are fully certified, licensed and insured with over 20 years of experience. We have competitive rates and offer high quality and reliable service. Customers can book service for the same or for the next day. We have 90 days warranty on parts and labour; booking online or by phone is available. washer repair

Avatar_small
Top SEO 说:
2024年3月26日 05:23

Korean online casino slot site rankings and various casino game information 슬롯사이트 추천

Avatar_small
Top SEO 说:
2024年4月11日 21:33

Our company is the best answer for all household appliance repairs. We are locally owned and operated company that has been serving many areas of Vancouver: Coquitlam, Port Coquitlam, Pitt Meadows, Maple Ridge, Port Moody. We offer quality service and/or repair for all major home appliances: refrigerators, washers and dryers, dishwashers, garburators, stoves, ovens, hood fans, microwaves etc. Technicians are fully certified, licensed and insured with over 20 years of experience. We have competitive rates and offer high quality and reliable service. Customers can book service for the same or for the next day. We have 90 days warranty on parts and labour; booking online or by phone is available. appliance repair near me

Avatar_small
Top SEO 说:
2024年5月22日 01:27

Introduction to karaoke where you can eat whiskey in Korea 플레이가라오케

Avatar_small
Top SEO 说:
2024年5月22日 23:09 This is a website with detailed articles on recommended online baccarat sites and how to start an online baccarat site. 바카라 하는 법
Avatar_small
Top SEO 说:
2024年6月27日 05:49

Korean online casino slot site rankings and various casino game information 온라인슬롯 추천

Avatar_small
hellstar 说:
2024年7月28日 00:08

This is an incredible inspiring article. I am basically satisfied with your great work. You put actually quite supportive information. Keep it up.<a href="https://hellstarusa.shop/">https://hellstarusa.shop/</a>


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter