Nginx的发音为“Engine-X”,这是Web服务器和反向代理服务器。 Nginx以其速度和以最佳资源使用方式同时处理大量请求的能力而闻名。
PHP-FPM代表“PHP-FastCGI流程管理器”。 CGI是指通用网关接口,该脚本被编写为Web服务器与动态内容服务程序之间的接口。它像Web服务器本身一样侦听端口,并在PHP和Web服务器之间传递请求。
本教程提供有关如何使用PHP-FPM安装和配置Nginx的说明,这将帮助您在Nginx中执行PHP程序。
正如我们之前在 阿帕奇vs Nginx,与Nginx相比,Apache在处理重负载和处理大量请求时相对较慢。
1.安装Nginx
你可以 从源代码安装Nginx,或使用发行版随附的软件包管理工具进行安装。
例如,在Ubuntu上,您可以使用apt-get安装nginx,如下所示。
$ sudo apt-get install nginx
启动nginx服务器,如下所示:
$ sudo service nginx start
转到http:// {your-ip-address}并确保您看到了Nginx’s welcome page.
2.安装PHP5-FPM
接下来,使用发行版随附的软件包管理工具安装php5-fpm。
例如,在Ubuntu上,您可以使用apt-get安装php5-fpm,如下所示。
$ sudo apt-get install php5-fpm
3.将PHP配置添加到Nginx
Next, modify the /etc/nginx/网站可用/默认 file, and add the following lines.
$ sudo vi /etc/nginx/网站可用/默认 server { listen 80; root /usr/share/nginx/www; index index.php index.html index.htm; server_name example.com; location / { try_files $uri $uri/ /index.html; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/www; } # pass the PHP scripts to FastCGI server listening 上 127.0.0.1:9000 location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
4.在php5-fpm www.conf中设置监听参数
接下来,我们需要在php-frpm配置中进行以下更改。
默认情况下,您’将在www.conf文件中看到以下侦听条目
$ sudo vi /etc/php5/fpm/pool.d/www.conf listen = 127.0.0.1:9000
在www.conf文件中,将所有内容保持不变,并将上面的侦听行替换为以下所示的行。
$ sudo vi /etc/php5/fpm/pool.d/www.conf listen = /var/run/php5-fpm.sock
5.重新启动Nginx和PHP5-FPM并进行测试
重新启动php5-fpm和nginx,如下所示
$ sudo service nginx restart $ sudo service php5-fpm restart
最后,在nginx文档根目录中创建以下index.php文件,并对其进行测试。
$ sudo vi /usr/share/nginx/www <?php phpinfo( ); ?>
最后,打开浏览器并转到http://localhost/index.php(或使用您的IP地址),这将执行index.php文件并显示php信息。
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
方便的教程,谢谢,