配置服务器 —— 配置Nginx

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LBinin/article/details/70185640

配置php-fpm

进入php的 sbin 目录下启动 php-fpm

[root@iZ2844brz0xZ ~]# cd /usr/local/php7/sbin/
[root@iZ2844brz0xZ sbin]# ./php-fpm 

(若提示没有权限,在命令前加上 sudo 即可。)

发现提示下方信息:

[15-Apr-2017 17:06:21] ERROR: failed to open configuration file '/usr/local/php7/etc/php-fpm.conf': No such file or directory (2)
[15-Apr-2017 17:06:21] ERROR: failed to load configuration file '/usr/local/php7/etc/php-fpm.conf'
[15-Apr-2017 17:06:21] ERROR: FPM initialization failed

告诉我们在 /usr/local/php7/etc/ 目录下没有找到 php-fpm.conf 配置文件。

那我们进入对应文件夹查看文件

[root@iZ2844brz0xZ sbin]# cd /usr/local/php7/etc/
[root@iZ2844brz0xZ etc]# ll

显示

total 16
-rw-r--r-- 1 root root 1239 Apr 15 07:33 pear.conf
-rw-r--r-- 1 root root 4468 Apr 15 07:33 php-fpm.conf.default
drwxr-xr-x 2 root root 4096 Apr 15 07:33 php-fpm.d

发现 php-fpm.conf 多了一个 default 结尾,我们只需要把文件重新命名就好了

[root@iZ2844brz0xZ etc]# mv php-fpm.conf.default php-fpm.conf --利用mv移动命令更改名字

重新进入php的 sbin 目录下启动 php-fpm

[root@iZ2844brz0xZ etc]# cd /usr/local/php7/sbin/
[root@iZ2844brz0xZ sbin]# ./php-fpm

这时候提示:

[15-Apr-2017 17:08:23] WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 125.
[15-Apr-2017 17:08:23] ERROR: No pool defined. at least one pool section must be specified in config file
[15-Apr-2017 17:08:23] ERROR: failed to post process the configuration
[15-Apr-2017 17:08:23] ERROR: FPM initialization failed

告诉我们在 /usr/local/php7/etc/php-fpm.d/ 目录下没有找到 .conf 文件,问题出在125行,我们用vim编辑文件

[root@iZ2844brz0xZ sbin]# vim /usr/local/php7/etc/php-fpm.conf

进入后输入命令 :125 跳转到125行

include=/usr/local/php7/etc/php-fpm.d/*.conf

给我们的信息是要找 php-fpm.d 目录下的以 .conf 结尾的文件,输入:q! 退出,进入对应文件夹,查看文件

[root@iZ2844brz0xZ sbin]# cd /usr/local/php7/etc/php-fpm.d/
[root@iZ2844brz0xZ php-fpm.d]# ll

发现以下文件

total 20
-rw-r--r-- 1 root root 18521 Apr 15 07:33 www.conf.default

重命名

[root@iZ2844brz0xZ php-fpm.d]# mv www.conf.default www.conf

回到 sbin 目录下重新启动 php-fpm

[root@iZ2844brz0xZ php-fpm.d]# cd /usr/local/php7/sbin/
[root@iZ2844brz0xZ sbin]# ./php-fpm 

若没有提示信息表明php-fpm启动成功啦~

用以下命令查看fpm进程

[root@iZ2844brz0xZ ~]# ps aux | grep php-fpm

配置Nginx

进入Nginx目录,编辑 nginx.conf 配置文件

[root@iZ2844brz0xZ nginx]# cd /usr/local/nginx/
[root@iZ2844brz0xZ nginx]# cd conf/
[root@iZ2844brz0xZ conf]# vim nginx.conf

看到 server 模块下有:

location / {
            root   html;
            index  index.html index.htm;
        }

这段表示所有的文件都会进入html这个目录下,我们现在编写与php相关的语句

在下面我们可以找到配置文件已经帮我们写好了我们只需要把前面的注释符号 # 去掉就好了

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

输入:wq 保存退出

进入nginx的html 目录,新建一个文件测试一下

[root@iZ2844brz0xZ conf]# cd ..
[root@iZ2844brz0xZ nginx]# cd html/
[root@iZ2844brz0xZ html]# vim test.php

输入

<?php
    phpinfo();

忘了说,修改完配置文件记得要重新启动nginx哦

[root@iZ2844brz0xZ ~]# cd /usr/local/nginx/sbin/
[root@iZ2844brz0xZ sbin]# ./nginx -s reload

打开网址,运行成功~


附:

Nginx和php-fpm的运行原理 作者:zilu (写的很棒~)

猜你喜欢

转载自blog.csdn.net/LBinin/article/details/70185640