nginx php-fpm installation configuration

Nginx itself cannot handle PHP, it is just a web server. When a request is received, if it is a PHP request, it is sent to the PHP interpreter for processing, and the result is returned to the client.

Nginx generally sends the request to the fastcgi management process for processing, and the fascgi management process selects the cgi sub-process to process the result and returns the result to be processed by nginx

This article uses php-fpm as an example to introduce how to make nginx support PHP

1. Compile and install php-fpm

What is PHP-FPM

PHP-FPM is a PHP FastCGI manager for PHP only, available for download at http://php-fpm.org/download.

PHP-FPM is actually a patch to the PHP source code, designed to integrate FastCGI process management into PHP packages. It must be patched into your PHP source code before it can be used after compiling and installing PHP.

The new version of PHP has integrated php-fpm and is no longer a third-party package. It is recommended to use it . PHP-FPM provides a better PHP process management method, which can effectively control memory and processes, and can smoothly reload PHP configuration. It has more advantages than spawn-fcgi, so it is officially included by PHP. You can enable PHP-FPM with the --enable-fpm parameter in ./configure. Other parameters are configured with php. The meaning of the specific options can be found here .

Prepare
to execute under centos before installation

yum -y install gcc automake autoconf libtool make

yum -y install gcc gcc-c++ glibc

yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-devel

New version of php-fpm installation ( recommended installation method )

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir

make all install

The old version is manually patched and installed by php-fpm (the old version of the program is no longer available, let’s make a new version, here is a show)
wget http://cn2.php.net/get/php-5.2.17.tar.gz
wget http: //php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar zvxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14 .diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=/usr/local/php -with-config-file-path=/usr/local/php/ etc\
-with-mysql=/usr/local/mysql\
-with-mysqli=/usr/local/mysql/bin/mysql_config -with-openssl -enable-fpm -enable-mbstring\
-with-freetype-dir -with -jpeg-dir -with-png-dir -with-zlib-dir -with-libxml-dir=/usr -enable-xml\
-with-mhash -with-mcrypt -enable-pcntl -enable-sockets -with-bz2 -with-curl -with-curlwrappers\
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv -enable-bcmath\
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo\
-with-pdo-mysql
make all install

The above two methods can install php-fpm. After installation, the content is placed in the /usr/local/php directory

The above completes the installation of php-fpm.

The following is to set the php-fpm running user

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc / php - fpm . conf

修改
user = www-data
group = www-data

如果www-data用户不存在,那么先添加www-data用户
groupadd www-data
useradd -g www-data www-data

二、编译安装nginx

然后按照http://www.nginx.cn/install 安装nginx

三、修改nginx配置文件以支持php-fpm

nginx安装完成后,修改nginx配置文件为,nginx.conf

其中server段增加如下配置,注意标红内容配置,否则会出现No input file specified.错误

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

四、创建测试php文件

创建php文件

在/usr/local/nginx/html下创建index.php文件,输入如下内容

<?php
    echo phpinfo();?>

五、启动服务

启动php-fpm和nginx

/usr/local/php/sbin/php-fpm 
#手动打补丁的启动方式/usr/local/php/sbin/php-fpm start

sudo /usr/local/nginx/nginx

php-fpm关闭重启见文章结尾

六、浏览器访问

访问http://你的服务器ip/index.php,皆可以见到php信息了。

安装php-fpm时可能遇到的错误:

1. php configure时出错

configure: error: XML configuration could not be found

apt-get install libxml2 libxml2-dev  (ubuntu下)
yum -y install libxml2 libxml2-develcentos下)

2. Please reinstall the BZip2 distribution

wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz
tar -zxvf bzip2-1.0.5.tar.gz
cd bzip2-1.0.5
make
make install

3. php的配置文件中有一行--with-mysql=/usr
安装的时候提示:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.

这是由于安装mysql时没有安装mysql头文件,或者是路径指定不正确,php找不到mysql的头文件引起的错误提示。
解决方法。
(1.) 查看你的系统有没有安装mysql header
find / -name mysql.h
如果有。请指定--with-mysql=/跟你的正常路径。
如果没有。请看下一步。
(2.)redhat安装
rpm -ivh MySQL-devel-4.1.12-1.i386.rpm
(3.)ubuntu安装
apt-get install libmysqlclient15-dev
(4.)最后一步php的配置选项添加--with-mysql=/usr即可!

4.No input file specified.

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5. 如果php configure时缺库,可以先安装库(ubuntu下)

sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev

6. mcrypt.h not found. Please reinstall libmcrypt

apt-get install libmcrypt-dev

或者

cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install

7. php-fpm 5.4.7 如何关闭 重启?

php 5.4.7 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:

master进程可以理解以下信号

INT, TERM 立刻终止 QUIT 平滑终止 USR1 重新打开日志文件 USR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:

php-fpm 关闭:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重启:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

查看php-fpm进程数:

ps aux | grep -c php-fpm

8.命令行下执行php,提示找不到命令

-bash: /usr/bin/php: No such file or directory

vi /etc/profile

在文件底部增加一行配置
export PATH=/usr/local/php/bin:$PATH

保存退出

source /etc/profile

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326890157&siteId=291194637