Laravel Live Japan

Nginx 1.4.x (Unix システム用)

このドキュメントは、Nginx 1.4.x HTTPサーバー 向けに PHP を PHP-FPM とともにインストールし、設定する方法を示します。

このガイドは、あなたが Nginx をソースコードからビルドしたことを想定しています。 よって、すべてのバイナリと設定は /usr/local/nginx にあると想定しています。 仮にこれに当てはまらず、Nginx を他の手段で取得した場合は、 » Nginx Wiki を参照し、 このマニュアルをあなたのセットアップに読み替えてください。

このガイドは、Nginx サーバーが PHP アプリケーションを実行できるように設定し、 それを port 80 で公開するための基本的な設定を示します。 あなたのセットアップを最適化したい場合、Nginx と PHP-FPM のドキュメント を調べることを推奨します。セットアップの最適化は、このガイドの範囲外です。

このドキュメント全体を通して、バージョン番号は ドキュメント全体が将来でも正しく保たれるようにするため、 'x' で置き換えられている点に注意してください。 'x' については、必要に応じて対応するバージョン番号に置き換えるようにしてください。

  1. あなたのシステム向けに Nginx を取得し、インストールするために、 Nginx Wiki の » install を参照することを推奨します。

  2. PHP のソースコードを取得し、展開します。

    tar zxf php-x.x.x
    
  3. PHP を configure し、ビルドします。 ここで PHP を様々なオプションを使ってカスタマイズできます。 たとえば、どの拡張モジュールを有効にするか、のようなものです。 利用できるオプションの一覧については、 ./configure --help を 参照ください。 私達の例では、PHP-FPM を MySQLi のサポートを有効にして シンプルに configure することにします。

    cd ../php-x.x.x
    ./configure --enable-fpm --with-mysqli
    make
    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= の行を探し、以下のように修正します:

    cgi.fix_pathinfo=0
    
  6. サービスを開始する前に、php-fpm が www-data ユーザーと www-data グループで 必ず実行されるように、php-fpm.conf も修正しなければいけません:

    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 サービスを開始できます:

    /usr/local/bin/php-fpm
    

    このガイドでは php-fpm をこれ以上設定しませんが、 php-fpm のさらなる設定に興味がある場合、ドキュメントを参照ください。

  7. Nginx が PHP アプリケーションの実行をサポートするように設定しなければなりません。

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

    Nginx が、.php ファイルをサービスすることを認識させるため、 デフォルトの location ブロックを修正します:

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

    次のステップは、 .php ファイルへのリクエストが PHP-FPM バックエンドに渡されるようにすることです。 コメントアウトされているデフォルトの PHP location ブロックを 以下のように修正します:

    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        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() が表示されるはずです。

上のステップに従うことで、 FPM SAPI モジュールとして PHP をサポートした Nginx Web サーバーを実行できるでしょう。 もちろん、さらに沢山の設定オプションが Nginx と PHP では利用可能です。 さらに多くの情報を得るには、対応するソースツリーで、 ./configure --help とタイプしてみてください。

add a note

User Contributed Notes 8 notes

up
45
Lenny
9 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)

blog.dream1987.top/?paged=2

Installation problems:

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

solution:

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

solution:

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

solution:

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

solution:

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

solution:

cp www.conf.default www.conf

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

solution:

Www.conf open files, user and group users into nginx default settings, usually the default is www-data.
up
9
Leon Nguyen vnlab
4 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
up
7
uanaoeng at outlook dot com
6 years ago
When I run the following command to start the php-fpm:
/usr/local/bin/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
up
4
1097625354 at qq dot com
7 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
up
5
Akash Kumar Sharma
9 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" .
up
2
nguyentienlong88 at gmail dot com
10 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
up
1
timy.shark
3 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;
    fastcgi_pass    127.0.0.1:9000;
    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>
up
1
610010559 at qq dot com
6 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 .
To Top