Home >Backend Development >PHP Problem >How to install nginx php-fpm
nginx php-fpm installation method: first download the php source code package and upload it to the server; then release the installation package and configure parameters; then perform compilation; and finally modify php-fpm.conf.
The operating environment of this article: CentOS 7.2 system, PHP7.0.5 version, DELL G3 computer
Install the php environment under Linux and configure Nginx to support php -fpm module
The following takes CentOS 7.2 as an example to install the php operating environment. First open the php official website http://php.net/ and click Downloads in the navigation bar to enter the download page: http:// php.net/downloads.php
## Download the latest version of php 7.0.5 source code package here:
Download and upload to the server
Since PHP installation requires compilation, the server should ensure that gcc and g environments are installed
First release the installation package:
tar -xvzf php-7.0.5.tar.gz cd php-7.0.5
Next, configure the parameters. If there is no libxml2 and libxml2-devel before configuration, an error will be reported, so libxml2 should be updated and libxml2-devel should be installed. , use online installation:
yum -y install libxml2yum -y install libxml2-devel
. In addition, because different operating system environments have different completeness of system installation and development environment packages, it is recommended to do this when installing the operating system. If necessary, you can also execute all the commands at once to install the uninstalled components. If they are already installed, they may be upgraded. If the versions are exactly the same, no operation will be performed. In addition to the above two commands, the commands are summarized as follows:
yum -y install opensslyum -y install openssl-develyum -y install curlyum -y install curl-develyum -y install libjpegyum -y install libjpeg-develyum -y install libpngyum -y install libpng-develyum -y install freetypeyum -y install freetype-develyum -y install pcreyum -y install pcre-develyum -y install libxsltyum -y install libxslt-develyum -y install bzip2yum -y install bzip2-devel
The above packages are basically enough. If you find any problems, please add them. After the installation is complete, execute the configuration:
./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
In fact, there are more configuration items here than above. You can use the ./configure --help command to view all options. Note here that native support for --with-mysql no longer exists in php7, and the operations are all It becomes mysqli or pdo; the above options are completely sufficient in normal PHP development. If necessary later, you can choose to manually open the corresponding module
Then execute the compilation :
make
The compilation time may be a bit long. After the compilation is completed, perform the installation:
make install
above the default installation location of php It has been designated as /usr/local/php, then configure the corresponding file:
cp php.ini-development /usr/local/php/lib/php.inicp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.confcp sapi/fpm/php-fpm /usr/local/bin
Then set php.ini, use: vim /usr /local/php/lib/php.ini Open the php configuration file and find the cgi.fix_pathinfo configuration item. This item is commented out by default and has a value of 1. According to the official documentation, this is to prevent Nginx from making requests when the file does not exist. Sent to the back-end PHP-FPM module to avoid malicious script injection attacks, so this item should be uncommented and set to 0
After setting, save and exit
Another thing to note is that the location of the php.ini configuration file can be set in the pre-compile configuration parameters. The compilation parameters can be written as: --with-config-file-path=/usr/local/php In this case, PHP will go back to the specified directory to read the php.ini configuration file. If this parameter is not added, the default location will be the lib directory under the PHP installation directory. Specifically You can also view it in the phpinfo() output interface. If php.ini is placed in another location and PHP cannot read it, then all configuration modifications will not take effect. Please note this point
此时首先应该创建web用户:
groupadd www-data useradd -g www-data www-data
然后网上一些教程说让修改php-fpm.conf添加以上创建的用户和组,这时候使用 vim /usr/local/php/etc/php-fpm.conf 打开文件后找不到官方所提示的位置:
如果此时随便在一个位置添加,那么接下来启动php-fpm的时候,会报目录找不到的错误,所以不要在php-fpm.conf中添加用户和组,这个时候翻到php-fpm.conf最后一行会发现如下内容(如果编译时添加--prefix选项以下位置会自动补全,默认是下面是空的,要注意):
这里引入了php-fpm.d目录下所有的conf配置文件,但是NONE需要修改为我们的实际目录:/usr/local
默认情况下etc/php-fpm.d/下有一个名为www.conf.defalut的配置用户的文件,执行下面命令复制一个新文件并且打开:
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf vim /usr/local/php/etc/php-fpm.d/www.conf
默认user和group的设置为nobody,将其改为www-data
修改完成之后,保存并退出,然后执行以下命令启动php-fpm服务:
/usr/local/bin/php-fpm
启动完毕之后,php-fpm服务默认使用9000端口,使用 netstat -tln | grep 9000 可以查看端口使用情况:
9000端口正常使用,说明php-fpm服务启动成功
然后执行 vim /usr/local/nginx/nginx.conf 编辑nginx配置文件,具体路径根据实际的nginx.conf配置文件位置编辑,下面主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页:
然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:
这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root
修改完上面的,回到nginx.conf第一行,默认是#user nobody; 这里要去掉注释改为user www-data;或者user www-data www-data;表示nginx服务器的权限为www-data
修改完这些保存并退出,然后重启nginx:
/usr/local/nginx/nginx -s stop /usr/local/nginx/nginx
接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:
<?php phpinfo(); ?>
然后打开浏览器输入对应的地址进行访问,看到输出页面,说明nginx和php都配置成功了:
推荐学习:《PHP视频教程》
The above is the detailed content of How to install nginx php-fpm. For more information, please follow other related articles on the PHP Chinese website!