本文目录一览:

nginx和php的两种通信方式

Nginx与PHP的两种通信方式-unix socket和tcp socket

nginx配置php(nginx配置PHP)nginx配置php(nginx配置PHP)


nginx配置php(nginx配置PHP)


1、两者Nginx配置

unix socket

需要在nginx配置文件中填写php-fpm运行的pid文件地址。

location ~ .php$ {

    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;

    fastcgi_pass unix:/var/run/php5-fpm.sock;

    fastcgi_index index.php;

}

tcp socket

需要在nginx配置文件中填写php-fpm运行的ip地址和端口号。

location ~ .php$ {

    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;

    fastcgi_pass 127.0.0.1:9000;

    fastcgi_index index.php;

}

2、两者比较

从上面的可以看,unix socket减少了不必要的tcp开销,而tcp需要经过loopback,还要申请临时端口和tcp相关资源。但是,unix socket高并发时候不稳定,连接数爆发时,会产生大量的长时缓存,在没有面向连接协议的支撑下,大数据包可能会直接出错不返回异常。tcp这样的面向连接的协议,多少可以保证通信的正确性和完整性。

3、选择建议:如果是在同一台上运行的nginx和php-fpm,并发量不超过1000,选择unix socket,因为是本地,可以避免一些检查作(路由等),因此更快,更轻。 如果面临高并发业务,我会选择使用更可靠的tcp socket,以负载均衡、内核优化等运维手段维持效率。

如何正确配置 Nginx + PHP

1. php用php-fpm启动,然后nginx

location ~ .php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

这样就可以了

2.安装一个集成的软件phpstudy

云如何配置nginx支持php?

[root@redhat7 ~]# wget

[root@redhat7 ~]# tar xzvf php-7.1.2.tar.gz

[root@redhat7 ~]# cd php-7.1.2/

[root@redhat7 ~]# ./configure --prefix=/usr/local/php --enable-fpm

[root@redhat7 php-7.1.2]# makemake install

查看是否成功编译安装PHP

[root@redhat7 php-7.1.2]# php -v

PHP 7.1.2 (fpm-fcgi) (built: Apr 14 2017 20:21:53)

Copyright (c) 1997-2017 The PHP Group

Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

编译安装完成后PHP不具备配置文件php.ini,此时只需php.ini-production到 /usr/local/lib/php.ini即可,php.ini文件一般在/usr/local/lib/和/etc目录下

[root@localhost php-7.1.2]# cp php.ini-production /usr/local/lib/php.ini

[root@redhat7 php]# /usr/local/php/in/php-fpm

[14-Apr-2017 20:59:49] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)

[14-Apr-2017 20:59:49] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'

[14-Apr-2017 20:59:49] ERROR: FPM initialization failed

启动php-fpm发现缺乏配置文件/usr/local/php/etc/php-fpm.conf

此时只需php-fpm的配置文件在安装php时提供的配置文件的模版/usr/local/php/etc/php-fpm.conf.default到相应/usr/local/php/etc/php-fpm.conf即可

[root@redhat7 etc]# /usr/local/php/in/php-fpm

[14-Apr-2017 21:14:32] WARNING: Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/﹡.conf' from /usr/local/php/etc/php-fpm.conf at line 125.

[14-Apr-2017 21:14:32] ERROR: No pool defined. at least one pool section must be specified in config file

[14-Apr-2017 21:14:32] ERROR: failed to t process the configuration

[14-Apr-2017 21:14:32] ERROR: FPM initialization failed

[root@redhat7 etc]# cp php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@redhat7 etc]# cp /usr/local/php/etc/php-fpm.d/ /usr/local/php/etc/php-fpm.d/

[root@redhat7 etc]# /etc/init.d/php-fpm

[14-Apr-2017 21:23:02] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)

[14-Apr-2017 21:23:02] ERROR: FPM initialization failed

[root@redhat7 etc]# netstat -nldp|grep 9000

tcp 0 0 127.0.0.1:9000 0.0.0.0: LISTEN 3721/php-fpm: maste

[root@redhat7 php-7.1.2]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@redhat7 php-7.1.2]# chmod a+x /etc/init.d/php-fpm

[root@redhat7 php-7.1.2]# ll /etc/init.d/php-fpm

-rwxr-xr-x 1 root root 2401 4月 14 21:26 /etc/init.d/php-fpm

[root@redhat7 php-7.1.2]# /etc/init.d/php-fpm start

Starting php-fpm [14-Apr-2017 21:28:09] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)

[14-Apr-2017 21:28:09] ERROR: FPM initialization failed

failed

[root@redhat7 php-7.1.2]# netstat -nldp |grep 9000

tcp 0 0 127.0.0.1:9000 0.0.0.0: LISTEN 3721/php-fpm: maste

[root@redhat7 php-7.1.2]# kill 3721

[root@redhat7 php-7.1.2]# netstat -nldp |grep 9000

[root@redhat7 php-7.1.2]# /etc/init.d/php-fpm start

Starting php-fpm done

[root@redhat7 php-7.1.2]# serv php-fpm status

php-fpm (pid 3927) is running...

[root@redhat7 php-7.1.2]# chkconfig --add php-fpm

[root@redhat7 php-7.1.2]# chkconfig php-fpm --ll 345 on

配置nginx支持PHP

修改nginx的配置文件,支持php文件的解析,找到location的添加位置,在后面添加下面这个location

location ~ .php$ {

root /usr/share/nginx/html; #指定php的根目录

fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

Thinkphp5项目在nginx部署

1,切换到nginx的配置目录,找到nginx.conf文件

    cd   /usr/local/nginx/conf

    vim  nginx.conf

2,如果是单项目部署的话,只需要在nginx.conf文件里面加上以下

server{

        listen 80;

        # 域名,本地测试可以使用127.0.0.1或localhost

        server_name ;

        # php项目根目录

        root /home/data-www/blog;

        location /{

                # 定义首页索引文件的名称

                index index.php index.html index.htm;

                # 影藏入口文件

                if (-f $request_filename/index.html){

                            rewrite (.) $1/index.html break;

                }

                if (-f $request_filename/index.php){

                            rewrite (.) $1/index.php;

                }

                if (!-f $request_filename){

                            rewrite (.) /index.php;

                }

                try_files $uri $uri/ /index.php?$query_string;

        }

        # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.

        # Fastcgi和程序(PHP)沟通的协议

        .location ~ ..php${

                # 设置端口

                fastcgi_pass 127.0.0.1:9000;

                # 设置nginx的默认首页文件

                fastcgi_index index.php;

                # 设置脚本文件请求的路径

                fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;

                # 引入fastcgi的配置文件

                include fastcgi_params;

                fastcgi_split_path_ ^(.+?.php)(/.)$;

                set $path_ $fastcgi_path_;

                fastcgi_param PATH_INFO $path_;

                try_files $fastcgi_script_name =404;

        }

}

3,如果多项目部署,就需要配置vhost

步:编辑nginx.conf文件,在加上     include    vhost/.conf;

第二步:进入vhost文件夹,创建    域名.conf    文件,如创建一个:quanma.meyat.conf

第三步:编辑quanma.meyat.conf文件,内容如下:

        server

        {

                listen 80;

                server_name quanma.meyat;

                index index.html index.htm index.php default.html default.htm default.php;

                root /data/wwwroot/default/quanma/public/;

                #error_page 404 /404.html;

                location / {

                        index index.html index.php;

                        if (-f $request_filename/index.html){

                                rewrite (.) $1/index.html break;

                        }

                        if (-f $request_filename/index.php){

                                rewrite (.) $1/index.php;

                        }

                        if (!-f $request_filename){

                                rewrite (.) /index.php;

                        }

                        try_files $uri $uri/ /index.php?$query_string;

                }

                location ~ [^/].php(/|$)

                {

                        # comment try_files $uri =404; to enable path

                        #try_files $uri =404;

                        fastcgi_pass 127.0.0.1:9000;

                        fastcgi_index index.php;

                        include fastcgi_params;

                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                        fastcgi_split_path_ ^(.+?.php)(/.)$;

                        set $path_ $fastcgi_path_;

                        fastcgi_param PATH_INFO $path_;

                        try_files $fastcgi_script_name =404;

                        #include fastcgi.conf;

                        #include path.conf;

            }

            location ~ ..(gif|jpg|jpeg|png|bmp|swf)$

            {

                    expires 30d;

            }

            location ~ ..(js|css)?$

            {

                    expires 12h;

            }

            # Disallow access to .ht, .svn, .bzr, .git, .hg, .cvs directories

            location ~ /.(ht|svn|bzr|git|hg|cvs) {

                    deny all;

            }

            #access_log /date/nginx/bmp.conf/access.log main;

}

如何正确配置 Nginx 和 PHP

直接贴上代码逐行进行讲解,此处贴出一个能正常启动php脚本的最简nginx vhost配置:

[plain] view plain copy

server {

listen 8011;

server_name test;

location ~ .php?.$ {

root /share/test;

fastcgi_pass 127.0.0.1:9000;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

1、个大括号 server{ }:不必多说,代表一个的server,

2、listen 8011:代表该server8011端口

3、location ~ .php?.${

}:代表一个能匹配对应uri的location,用于匹配一类uri,并对所匹配的uri请求做自定义的逻辑、配置。这里的location,匹配了所有带.php的uri请求,例如:

4、root /share/test:请求资源根目录,告诉匹配到该location下的uri到/share/teset文件夹下去寻找同名资源。

5、fastcgi_pass 127.0.0.1:9000:这行开始是本文的重点:这行代码的意思是,将进入到该location内的uri请求看做是cgi程序,并将请求发送到9000端口,交由php-fpm处理。

6、fastcgi_param SCRIPT_FILENAME

$document_root$fastcgi_script_name;

:这行配置意思是:动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。由于我的nginx中只有fastcgi_params文件,没有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具体值,就必须要动态的添加这行配置。

7、include fastcgi_params; 引入fastcgi配置文件

以上就是最简洁版的nginx启动php脚本的最简配置,当重启nginx之后,在/share/test目录下创建一个xx.php文件,输入?php

echo "hello world"; ?保存,然后在浏览器中访问localhost:8011/xx.php

就可以在网页上显示hello world了。

mac配置php-fpm,nginx运行多版本php

1、brew 安装 php5.6 php5.7 nginx

2、配置php-conf

3、配置虚拟主机

安装好brew

用brew 命令安装,如果速度太慢或访问不了,自行goole brew 换源

brew search php  查看可用的php版本

brew install php@5.6  安装php5.6

brew install php@7.1 安装php5.6

brew install nginx       安装nginx

1、修改php5.6 php-fpm的端口为9056

    cd usr/local/etc/php/5.6 # 到php5.6的目录下

    vi    php-fpm.conf # 修改文件

        listen = 127.0.0.1:9056  # 修改此端口

        daemonize = yes # 修改为允许后台启动php-fpm

2、修改php5.6 php-fpm的端口为9070

    cd /usr/local/etc/php/7.1/php-fpm.d # 到php7.1的目录下

    vi # 修改端口

        listen = 127.0.0.1:9056  # 修改此端口    

    vi    php-fpm.conf  # 修改文件

         daemonize = yes # 修改为允许后台启动php-fpm

3、启动php-fpm

    cd /usr/local/in # 到此目录,建立两个软链接指向不同版本的php

切换到root用户

./php-fpm56 

./php-fpm71

启动后可看到php-fpm的进程,则成功

ps-ef | grep php-fpm

cd /usr/local/etc/nginx/  # 到nginx的目录下

默认的配置文件到server下(此目录用来存虚拟主机文件)

这里我在server创建了这两个

vi local.php56.conf # 修改本地域名和nginx到php-fpm端口,按照这种方法修改另一个

nginx # 启动nginx

nginx -s reload # 修改配置文件,重新加载nginx

vi /etc /hosts  # 修改host 加上映射关系

cd  /usr /local/var/www # 在此目录下建立一个index.php

echo "?php php();" index.php

在浏览器访问可看到