Home  >  Article  >  Backend Development  >  How to compile and install php7 from source code

How to compile and install php7 from source code

coldplay.xixi
coldplay.xixiforward
2020-06-23 18:00:205017browse

How to compile and install php7 from source code

## Download the latest version of php 7.0.5 source code package:

 

Download and upload to the server

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

Recommended tutorial: "

PHP Video Tutorial"

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 libxml2
yum -y install libxml2-devel

Supplementary, because Different operating system environments have different completeness of system installation and development environment packages. Therefore, it is recommended to make necessary selections when installing the operating system. You can also execute all the commands in a unified manner to install the uninstalled components. If they are already installed, It may be upgraded. If the version is completely consistent, no operation will be performed. In addition to the above two commands, the commands are summarized as follows:

How to compile and install php7 from source code

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

How to compile and install php7 from source code

The above packages are basically enough. If you find any problems, 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 the above. You can use the ./configure --help command. View all options. Note here that native support for --with-mysql no longer exists in php7, and operations have become mysqli or pdo. The above options are completely sufficient for normal PHP development. If necessary, you can choose Manually open the corresponding module

Then execute the compilation:

make

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

make install
The default installation location of php is already above Specify it as /usr/local/php, then 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
ln -s /usr/local/php/sbin/php-fpm /usr/local/bin

Then set php.ini, use: vim /usr/local/php/lib/php.ini to open the php configuration file and find 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 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

 

# Save and exit after setting

Another place to note is the location of the php.ini configuration file. Set in the configuration parameters before compilation. 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 you do not add this The default location of the parameters is 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 cannot be read by PHP, then all configuration modifications will not take effect. This should be noted

At this time, you should first create a web user:

groupadd www-data
useradd -g www-data www-data

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 official location cannot be found:

 

If you add it in a random location at this time, then the next step When starting php-fpm, a directory not found error will be reported, 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 added during compilation The --prefix option will automatically complete the following positions. The default is empty below, please note):

 

All confs in the php-fpm.d directory are introduced here. Configuration file, but NONE needs to be modified to our actual directory: /usr/local

  

  默认情况下etc/php-fpm.d/下有一个名为www.conf.defalut的配置用户的文件,执行下面命令复制一个新文件并且打开:

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

  默认user和group的设置为nobody,将其改为www-data

  

  修改完成之后,保存并退出,然后执行以下命令启动php-fpm服务:

/usr/local/bin/php-fpm

  启动完毕之后,php-fpm服务默认使用9000端口,使用 netstat -ntlp | 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();
?>

The above is the detailed content of How to compile and install php7 from source code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete