Unix 系统下的 Nginx 1.4.x

本文档包括使用 PHP-FPM 为 Nginx 1.4.x HTTP 服务器安装和配置 PHP 的说明和提示。

本指南假定您已经从源代码成功构建 Nginx,并且其二进制文件和配置文件都位于 /usr/local/nginx。 如果您使用其他方式获取的 Nginx,请参考 » Nginx Wiki 并对照本文档完成安装。

本文档仅包含 Nginx 服务器的基本配置,它将通过 80 端口提供 PHP 应用的处理能力。 如果您需要超出本文档范围的安装配置指导,建议您查阅 Nginx 和 PHP-FPM 的文档。

需要注意的是,本文档一律使用 'x' 来表示版本号,请根据实际情况将 'x' 替换为对应的版本号。

  1. 建议您访问 Nginx Wiki » 安装 页面以获取并在您的系统上安装 Nginx。

  2. 获取并解压 PHP 源代码:

    tar zxf php-x.x.x
  3. 配置并构建 PHP。在此步骤您可以使用很多选项自定义 PHP,例如启用某些扩展等。 运行 ./configure --help 命令来获得完整的可用选项清单。 在本示例中,我们仅进行包含 PHP-FPM 和 MySQL 支持的简单配置。

    cd ../php-x.x.x
    ./configure --enable-fpm --with-mysql
    sudo make install
  4. 创建配置文件,并将其复制到正确的位置。

    cp php.ini-development /usr/local/php/php.ini
    cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
    cp sapi/fpm/php-fpm /usr/local/bin
  5. 需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。

    将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0

    打开 php.ini:

    vim /usr/local/php/php.ini

    定位到 cgi.fix_pathinfo= 并将其修改为如下所示:

  6. 在启动服务之前,需要修改 php-fpm.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行。

    vim /usr/local/etc/php-fpm.d/www.conf


    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = www-data
    group = www-data

    然后启动 php-fpm 服务:


    本文档未涵盖对 php-fpm 进行进一步配置的信息,如果您需要更多信息,请查阅相关文档。

  7. 配置 Nginx 使其支持 PHP 应用:

    vim /usr/local/nginx/conf/nginx.conf

    修改默认的 location 块,使其支持 .php 文件:

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

    下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

    location ~* \.php$ {
        fastcgi_index   index.php;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

    重启 Nginx。

    sudo /usr/local/nginx/sbin/nginx -s stop
    sudo /usr/local/nginx/sbin/nginx
  8. 创建测试文件。

    rm /usr/local/nginx/html/index.html
    echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

    打开浏览器,访问 http://localhost,将会显示 phpinfo() 。

通过以上步骤的配置,Nginx 服务器现在可以以 SAPI SAPI 模块的方式支持 PHP 应用了。 当然,对于 Nginx 和 PHP 的配置,还有很多可用的选项, 请在对应的源代码目录执行 ./configure --help 来查阅更多配置选项。

add a note

User Contributed Notes 10 notes

8 years ago
Building from source is not easy if something is a bit different, and I had a hard time with some directory and configuration options. I was floundering around the web until I found this site that translated from Chinese. No one else had the solution. I couldn't get php fpm to start until I changed the directory (Item 2.ERROR: Unable to globalize). I had other issues listed but I was able to solve them. Please don't delete this, it is very useful info.

The original site (it is in Chinese, not my site, but I want to give credit):

(there is some more there, you can goto the site)


Installation problems:

1. configure: error:. Xml2-config not found Please check your libxml2 installation.


apt-get install libxml2-dev

2.Warning: Declaration of PEAR_Installer :: download () should be compatible with & PEAR_Downloader :: download ($ params) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib.phar/PEAR /Installer.php on line 43

Warning: Declaration of PEAR_PackageFile_Parser_v2 :: parse () should be compatible with PEAR_XMLParser :: parse ($ data) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib.phar/PEAR/PackageFile/ Parser / v2.php on line 113
[PEAR] Archive_Tar - already installed: 1.3.13
[PEAR] Console_Getopt - already installed: 1.3.1
[PEAR] Structures_Graph- already installed: 1.0.4

Warning: Declaration of PEAR_Task_Replace :: init () should be compatible with PEAR_Task_Common :: init ($ xml, $ fileAttributes, $ lastVersion) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib. phar / PEAR / Task / Replace.php on line 31
[PEAR] XML_Util - already installed: 1.2.3

Warning: Declaration of PEAR_Task_Windowseol :: init () should be compatible with PEAR_Task_Common :: init ($ xml, $ fileAttributes, $ lastVersion) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib. phar / PEAR / Task / Windowseol.php on line 76

Warning: Declaration of PEAR_Task_Unixeol :: init () should be compatible with PEAR_Task_Common :: init ($ xml, $ fileAttributes, $ lastVersion) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib. phar / PEAR / Task / Unixeol.php on line 76
[PEAR] PEAR - already installed: 1.9.5


Workaround not found (http://pear.php.net/bugs/bug.php?id=20554)

3. Start php-fpm

1.ERROR: failed to open configuration file '/usr/local/etc/php-fpm.conf': No such file or directory (2)
ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
ERROR: FPM initialization failed


Php-fpm.conf copy files from the source file to that location.

cp /root/php-7.0.0alpha1/sapi/fpm/php-fpm.conf /usr/local/etc/php-fpm.conf

2.ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret = 2) from /usr/local/etc/php-fpm.conf at line 125.
ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
ERROR: FPM initialization failed


Edit /usr/local/etc/php-fpm.conf document introduced * .conf part, change to the correct path include = / usr / local / etc / php-fpm.d / *. Conf

If there is no /usr/local/etc/php-fpm.d directory, create the directory.

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


cp www.conf.default www.conf

4.ERROR: [pool www] can not get gid for group 'nobody'
ERROR: FPM initialization failed


Www.conf open files, user and group users into nginx default settings, usually the default is www-data.
uanaoeng at outlook dot com
5 years ago
When I run the following command to start the php-fpm:

I got the following error information:
Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret=2) from /usr/local/etc/php-fpm.conf at line 143.

To fix this error, do:
change the string "include=NONE/etc/php-fpm.d/*.conf" to "include=etc/php-fpm.d/*.conf" in the file "/usr/local/etc/php-fpm.conf" with text editor.

After that, I try to start the php-fpm again and got error information again:
Nothing matches the include pattern '/usr/local/etc/php-fpm.d/*.conf' from /usr/local/etc/php-fpm.conf at line 143.

To fix this error, do:
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
Leon Nguyen vnlab
2 years ago
To maximize the server performance with highest speed, with fewest resources, i have decided to compile PHP 8 manually from minimal Linux/Unix-based OS (my Linux distro is rpm-based packages)

My system has successfully powered up by PHP 8.0.9 - with Opcache/JIT enabled & other several PHP extension module. Here is several experience that may save you several hours of research.

1. Before step (3) - Configure and build PHP - to ensure that no errors occurs, you can refer to my prerequisites library.

sudo dnf install \
gcc gcc-c++ make cmake autoconfig \
zlib zlib-devel pcre pcre-devel \
libxml2-devel libxslt-devel \
bzip2-devel curl-devel libzip-devel\
sqlite-devel \
systemd-devel \
openssl-devel \
libffi-devel \
libpng libpng-devel libwebp libwebp-devel libjpeg libjpeg-devel libXpm libXpm-devel \
freetype-devel \
gmp-devel \
libldb-devel \
libc-client libc-client-devel \
openldap openldap-devel \
oniguruma oniguruma-devel \
net-snmp-devel readline-devel unixODBC-devel \
uw-imap uw-imap-devel uw-imap-static uw-imap-utils \
libicu-devel \
enchant2 enchant2-devel \
gd gd-devel \
libsodium libsodium-devel \
libtidy libtidy-devel

You may not receive error when issuing ./configure, make && make install. Even when you manually build your PHP extension package later.

2. At step 5 - before modifying php.ini - you may double-check whether the current PHP is reading which php.ini file by this command

php -ini | grep php.ini
# My result. It is different from tutorial.
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini
610010559 at qq dot com
4 years ago
in linux system, if you want to add php-fpm service to system service; it is easy to do that, because php offer the relate shell script:

after make and make install;enter the source code package sapi/fpm/init.d.php-fpm ; type the code in the linux system.
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

then you can use "php-fpm {start|stop|force-quit|restart|reload|status|configtest} " command to control your php-fpm program more convenient .
Akash Kumar Sharma
8 years ago
If you get "File not found" error then add "root ROOT_DIR_LOCATION" directive to PHP location block i.e. "location ~* \.php$ { }" , where ROOT_DIR_LOCATION is root directory like "/usr/share/nginx/html" .
2 years ago
Great effort for this tutorial, thanks a ton, here are my notes if may help anyone, considering default install on WSL2 container
Note: composer added for Laravel framework.
These are NOT to be copy and pasted as it is, need to be selective as there are annotations beside some commands,
Also I've commented # some of the defaults written by the original editor due to mismatch to my current environment.

sha256sum php-x.x.x.tar.gz => e847745fd66fc8c57fac993a609fefcded93fddccd225f0620a26bb5ae5753c3
tar zxf php-x.x.x
sudo apt install build-essential
sudo apt install -y composer nginx pkgconf libxml2-dev libsqlite3-dev zlib1g-dev
cd php-x.x.x
./configure --enable-fpm --with-mysqli <==Thank you for installing PHP.
make <== takes a while
sudo make test <== optional as recommended
sudo make install <= /usr/local/bin /usr/local/php
sudo cp php.ini-development /usr/local/lib/php.ini
sudo cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
sudo cp sapi/fpm/php-fpm /usr/local/bin
sudo vim /usr/local/php/php.ini <= cgi.fix_pathinfo=0
sudo vim /usr/local/etc/php-fpm.d/www.conf <= user=www-data group=www-data
sudo vim sapi/fpm/php-fpm.conf <= edit include=etc/php-fpm.d/*.conf <- relative path
sudo cp sapi/fpm/php-fpm.conf /usr/local/etc
/usr/local/bin/php -v <= $PATH$ uses /usr/local/bin at first /usr/local/lib/php.ini
sudo /usr/local/bin/php-fpm <= start the service
sudo vim /etc/nginx/sites-available/default <= edit the following under server {}
location / {
#root html;
#index index.php index.html index.htm;
location ~* \.php$ {
#fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;

*Restart nginx (root /var/www/html) if root path doesn't exist, default is: /usr/share/nginx/html
sudo nginx -t <= Test Nginx conf files
sudo service nginx reload
sudo service nginx stop
sudo service nginx start <= [OK]

php-fpm needs to be in a service, or kill process manually ps -aux | grep php-fpm ; kill <pid>
nguyentienlong88 at gmail dot com
8 years ago
at step 3, after command "sudo make install"
if there is problem with pear.php.net (https). Need to change this line (from https to http) in Makefile
PEAR_INSTALLER_URL = http://pear.php.net/install-pear-nozlib.phar
1097625354 at qq dot com
5 years ago
cp php/php.ini-development php/lib/php.ini
cp php/etc/php-fpm.conf.default php/etc/php-fpm.conf
cp php/etc/php-fpm.d/www.conf.default php/etc/php-fpm.d/www.conf
nguyentienlong88 at gmail dot com
8 years ago
at step 3, if there is no configure script yet, you need to rebuiding configure script using this command:

./buildconf --force
sp at rysmax dot com
6 years ago
Translate errors:

Теперь перейдите в http://localhsot
Теперь перейдите в http://localhost

В этом разделе описивыется
В этом разделе описывается

запрещаем Nginx от отправлять
запрещаем Nginx отправлять
To Top