Source code compilation and installation of PHP7

Common steps for installing Linux software source code

  1. wget download compressed package
  2. Unzip and enter the source directory
  3. ./configure run configuration to add configuration constants (./configure --help)
  4. make compile
  5. make install install

Source code compilation and installation of PHP

Download the installation package

wget http://am1.php.net/get/php-7.2.4.tar.gz

wget [parameter] [URL]
wget command introduction

Unzip the installation package and enter the source directory

tar -zxvf php-7.2.4.tar.gz cd php-7.2.4 

Introduction to the tar command

configure configure

  • View configure configuration parameters
    ./configure --help
  • Specify the installation directory
    --prefix=/usr/bin/php
  • Specify the storage directory of php.ini.
    --with-config-file-path=/usr/bin/php/etc
    If not set, the default is lib, and the configuration parameter specifies the ini of cli. The ini of fpm should be set in fpm.conf

The common error in this step is the lack of installation dependent software such as gcc, autoconfig, libxml2, etc., you can install the corresponding dependent software according to the promptssudo apt install xxx

compile

make

The common error in this step is that the memory is not enough (virtual memory exhausted: Cannot allocate memory), you can try to reset the configure parameter, do not install the fileinfo extension, or set the swap partition

Install

make install

Test if the installation is successful

/usr/bin/php/bin/php -m

Add PHP to environment variables

sudo ~/.bashrc
alias php7=/usr/bin/php
source ~/.bashrc

This method only adds php to the environment variable for the current user, not for all users. If you want to add environment variables for all users, you need to modify /etc/profile.

Add php.ini file

cp php.ini-development /usr/bin/php/etc
mv /usr/bin/php/etc/php.ini-development /usr/bin/php/etc/php.ini 

Of course, the above directory where php.ini is stored is not necessarily correct. The directory where php.ini is stored depends on the parameters configured during configure. Therefore, it is reasonable to execute
php -i | grep php.inito get the directory that should be stored.

Source code compilation and installation of the pit encountered by php

Missing dependent software

  • no gcc
    sudo apt install gcc
  • autoconfig
    sudo apt install autoconfig
  • no libxml2
    sudo apt install libxml2-dev

In fact, if the software is missing libxml2, it apt install libxml2may not be installed, because in your apt source, the package of libxml2 may be called libxml2-dev, so you can search for what is related to this package according to the error prompt.

It does not take effect after configuring php.ini

This situation is usually caused by the incorrect location of php.ini. If it is not set during configure --with-config-file-path=Path, the default is in the lib directory of the installation directory, and if it is set, it is in the corresponding directory.

You can php -i | grep php.iniobtain the directory in which php.ini should be stored, and the corresponding introduction of php.ini will take effect.

Not enough memory during compilation

Commonly used in servers with less than 2G memory ( virtual memory exhausted: Cannot allocate memory),
there are two solutions:

  • Do not install the fileinfo extension
    Because the fileinfo extension will be installed by default after 5.3, if your server memory is too small, an error of insufficient memory will be reported during the compilation process. It can be solved by resetting the parameters of ./configure --disable-fileinfoand recompiling. Of course, this approach does not cure the symptoms. It is recommended to solve by setting virtual memory

  • Configure virtual memory (swap partition)
    Use free to view memory information free
    Use to sudo swapon -sview swap partition information
    Steps to set virtual memory:

创 建 swap 文 件
cd /var
sudo dd if=/dev/zero of=swapfile bs=1024 count=2048000 sudo mkswap swapfile 激 活 sudo chmod 600 swapfile sudo swapon swapfile 开 机 自动 创建 sudo vim /etc/fstab /swapfile none swap sw 0 0 

Compile and install PHP extension from source code

Download the installation package
wget
Enter the source code directory
cd
Execute phpize (in the bin directory under the PHP directory) to generate the configure file
/usr/bin/php/bin/phpize
Execute configure to set the parameters

./configure  --with-php-config=/usr/bin/php/bin/php-config 

Compile and install
make && make install

Other one-click installation methods:

  • apt install
  • pecl install (/usr/bin/php/bin/pecl)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324906229&siteId=291194637