Heim  >  Artikel  >  Backend-Entwicklung  >  Mac os X下使用Nginx跟PHP的连接问题

Mac os X下使用Nginx跟PHP的连接问题

WBOY
WBOYOriginal
2016-07-29 09:03:29791Durchsuche

安装Nginx

使用brew包管理工具来安装Nginx

http://brew.sh/

官网上一目了然,使用非常简单。

安装成功后,直接在terminal里执行brew install nginx即可

安装完,在/usr/local/Cellar/下可以看到安装的nginx包,Cellar目录是专门用来放brew安装的包的,所有的相关配置文件在/usr/local/etc/nginx/下。

配置Nginx

初始的Nginx listen的是8080端口,依照惯例改成80端口即可,注意把自带的Apache给关了。

这时候执行nginx命令,浏览器里localhost就可以看到welcome了,但是还不能执行PHP文件,因为压根就没配置跟PHP的连接,这里就要使用php-fpm了,先要在nginx的配置文件里添加fastcgi的配置:

location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/Cellar/nginx/1.8.0/html$fastcgi_script_name;
            include        fastcgi_params;
        }
可以参照上面的配置进行对应的修改。

配置php-fpm

然后在php-fpm的配置文件里修改error_log以及pid的配置:

进入配置文件,sudo vim /private/etc/php-fpm.conf

error_log = /usr/local/var/log/php-fpm.log
pid = /usr/local/var/run/php-fpm.pid
参照进行对应的修改。

打开php-fpm:

sudo /usr/sbin/php-fpm 

重启nginx

nginx -s reload

在服务器根目录下测试PHP文件。


Nginx跟Apache最直观的区别,就是Apache可以将PHP作为一个子模块,从而直接解析PHP,而Nginx做不到,只能通过fastcgi模式跟PHP进行连接,当然Apache也可以用fastcgi模式。而php-fpm(进程管理器)就是管理fastcgi的工具,在PHP5.3版本之后,PHP都会自带的。

fastcgi以及php-fpm相关知识参考:

php中fastcgi和php-fpm是什么东西

以上就介绍了Mac os X下使用Nginx跟PHP的连接问题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn