Detailed introduction to installing the php environment under Linux and configuring Nginx to support the php-fpm module (picture and text)

黄舟
Release: 2023-03-06 21:36:01
Original
2252 people have browsed it

This article mainly introduces the detailed explanationLinuxInstallation PHP environment and configure Nginx to support the php-fpm module, which has certain reference value. Interested friends can refer to it.

The following takes CentOS 7.2 as an example to install the PHP operating environment. First open the PHP official website. Click Downloads in the navigation bar to enter the download page. Download the latest version of php 7.0.5 source code package here:

 

After downloadingupload to the server

Because PHP installation requires compilation, the server should ensure the installation of gcc and g++ environments

First release the installation package:

tar -xvzf php-7.0.5.tar.gz
cd php-7.0.5
Copy after login

Next, configure the parameters. If there is no libxml2 and libxml2-devel will report an error, so you should update libxml2 and install libxml2-devel. Use online installation:

yum -y install libxml2
yum -y install libxml2-devel
Copy after login

Additionally, because of different operating system environments, the completeness of the system installation development environment package is also different, so it is recommended Make the necessary selections when installing the operating system. 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 operations will be performed. The commands are except for the above two. In addition, the summary is as follows:

yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel
Copy after login

The above packages are basically enough. If you find any problems, please add them. After the installation is completed, execute the configuration:

The code is as follows:

./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-
mysql
i --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-m
hash
 --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mb
string
 --enable-
opcache
 --enable-pcntl --enable-shmop --enable-
soap
 --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
Copy after login

Actually, there are more configuration items here than above. You can use the ./configure --help command to view all options. Note here that in php7, --with-mysql native support no longer exists, and the operations have become mysqli or pdo. ;The above options are completely sufficient for normal PHP development. If needed later, you can choose to manually open the corresponding module

and then perform compilation:

make
Copy after login

The compilation time may be a bit long, After the compilation is completed, perform the installation:

make install
Copy after login

The default installation location of php has been specified as /usr/local/php. Next, configure the corresponding file:

cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin
Copy after login

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 by default and has a value of 1, according to According to the official document, this is to prevent Nginx from sending requests to the back-end PHP-FPM module when the file does not exist, thereby avoiding 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, and the compilation parameters can 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. , the details can also be viewed in the phpinfo() output interface. If php.ini is placed in other locations and PHP cannot read it, then all configuration modifications will not take effect. Please pay attention to this.

You should first create a web user:

groupadd www-data
useradd -g www-data www-data
Copy after login

Then some online tutorials say to modify php-fpm.conf to add the users and groups created above. At this time, use vim /usr/local/etc/php-fpm.conf After opening the file, the officially prompted location cannot be found:

 

If you add it in a random location at this time, then when you start php-fpm, the directory will be reported. error, so do not add users and groups in php-fpm.conf. At this time, go to the last line of php-fpm.conf and you will find the following content (if you add the --prefix option when compiling, the following The position will be automatically completed. The default is empty below, please note):

 

All conf configuration files in the php-fpm.d directory are introduced here, but NONE Need to be modified to our actual directory: /usr/local

 

By default, there is a configuration named www.conf.defalut under etc/php-fpm.d/ For the user's file, execute the following command to copy a new file and open it:

cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
vim /usr/local/etc/php-fpm.d/www.conf
Copy after login

The default user and group settings are nobody, change them to www-data

 

After the modification is completed, save and exit, then execute the following command to start the php-fpm service:

/usr/local/bin/php-fpm
Copy after login

After starting, the php-fpm service uses port 9000 by default. Use netstat -tln | grep 9000 to view the port. Usage:

  

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
Copy after login

接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:

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

然后打开浏览器输入对应的地址进行访问,看到输出页面,说明nginx和php都配置成功了:

相关文章:

Mac默认使用自己安装的php

centos6.7安装php7的详细介绍

详细介绍CentOS7编译安装PHP7的示例代码(图)

The above is the detailed content of Detailed introduction to installing the php environment under Linux and configuring Nginx to support the php-fpm module (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template