Table of Contents
1. 安装nginx并启动
2. 安装php及相关
3. FastCGI
4. PHP-FPM
5. 运行php站点
Home Backend Development PHP Tutorial Ubuntu上配置nginx+php+fastcgi的流程

Ubuntu上配置nginx+php+fastcgi的流程

Jun 23, 2016 pm 01:43 PM

这两天需要在Ubuntu14.04上搭建nginx和php的环境,配置的过程中在fastcgi的问题上花了很多时间,网上的资料大多年久失修,于是决定将整个过程记录下来,分享给大家。


准备:

apt-get update
Copy after login


1. 安装nginx并启动

apt-get install nginxsudo /etc/init.d/nginx start
Copy after login

2. 安装php及相关

sudo apt-get install php5-cli php5-cgi php5-fpm php5-mcrypt php5-mysql
Copy after login

3. FastCGI

在运行php站点之前需要先了解下FastCGI,FastCGI是一个可伸缩地、高速地在HTTP server和动态脚本语言间通信的接口,它采用C/S结构,可以将HTTP服务器和脚本解析服务器分开,当HTTP服务器遇到动态请求时,会将请求转发给FastCGI进程,FastCGI进程执行动态脚本后再将结果返回给HTTP服务器,HTTP服务器最后将结果返回给浏览器,这在很大程度上提高了请求的响应速度。


实现FastCGI有几种方式,PHP-CGI、PHP-FPM、Spawn-FCGI

PHP-CGI是PHP自带的FastCGI管理器,启动PHP-CGI可以使用如下命令:

php-cgi -b 127.0.0.1:9000

PHP-CGI有两个问题,一是变更php.ini后需要重启php-cgi才能生效,二是php-cgi进程崩溃或者被杀死后php就不能运行了。


PHP-FPM是从PHP 5.3.3之后新加入的CGI管理器,在更改PHP配置之后不需要重启,且由于加入了守护进程,所以即使被杀死之后也能快速重启。


Spawn-FCGI是一个通用的FastCGI管理器,而不仅仅只针对PHP一种脚本语言。但它在效率、CPU占用等方面都不如PHP-FPM。


4. PHP-FPM

既然PHP-FPM的方式最好,那么我们现在就来配置一下。由于PHP-FPM是一个独立的进程,所以需要与Nginx进行通信。目前有两种通信方式,tcp和socket。两种方式的区别和配置方式可以参见这两篇文章:

nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置

Php-fpm TcpSocket vs UnixSocket


在这里简要说一下配置方式,两种方式都主要修改nginx的配置文件(/etc/nginx/sites-available/default)和fpm的配置文件(/etc/php5/fpm/pool.d/www.conf)。


TCP配置方式需要将nginx配置文件中相应的反向代理的fastcgi_pass参数改为127.0.0.1:9000,将fpm配置文件中的listen参数修改为127.0.0.1:9000。


Socket配置方式需要将nginx配置文件中相应的反向代理的fastcgi_pass参数改为unix:/dev/shm/fpm-cgi.sock,将fpm配置文件中的listen参数修改为/dev/shm/fpm-cgi.sock,如果考虑到高并发可以将listen.backlog参数改为-1,内存积压无限大。另外,还可以提高内核级别的并发连接数,修改/etc/sysctl.conf:

sudo echo 'net.core.somaxconn = 2048' >> /etc/sysctl.confsudo sysctl -p
Copy after login

两种配置方式修改完之后都需要重启php-fpm和nginx。

sudo service php5-fpm restartsudo /etc/init.d/nginx reload
Copy after login


5. 运行php站点

以上配置完成之后,我们就可以写一个简单的php页面来测试一下。

创建一个网站目录:

sudo mkdir /var/www/
Copy after login

在nginx配置文件中修改root目录为 root /var/www;

添加一个测试页面:

sudo vi /var/www/test.php
Copy after login

加入以下代码:

<?php phpinfo();?>
Copy after login

此时在浏览器输入地址http://localhost/test.php,就可以看到php的信息了。


常用命令:

1. 检查nginx启动出错

sudo nginx -c /etc/nginx/nginx.conf -t
Copy after login


如果大家觉得对自己有帮助的话,还希望能帮顶一下,谢谢:)
个人博客:http://blog.csdn.net/zhaoxy2850
本文地址:
转载请注明出处,谢谢!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles