Home  >  Article  >  Backend Development  >  Compile and install PHP on Linux

Compile and install PHP on Linux

不言
不言Original
2018-06-02 09:34:036961browse

This article mainly introduces the compilation and installation of PHP on Linux. It has certain reference value. Now I share it with everyone. Friends in need can refer to

which was compiled and installed on the server before. PHP running environment, but after a while after the installation, I almost forgot about it. I just remembered some simple steps here and there. Now I have installed it again on the new server and recorded the installation process for easy review later. It also provides As a reference for newbies.

1. First, you need to download the PHP source code installation package. You can download it from the PHP official website http://php.net/. After entering the official website, click the "Downloads" option in the navigation bar, that is, It can be included in the download page. At this time, you can choose the download server according to the region. Here I choose cn2.php.net to download

2. After the download is completed, use the commonly used ftp tool to transfer the source code Upload the package to the server where PHP is to be installed. Here we temporarily place it in the /tmp directory

(Note: You can also directly use the Linux file download tool wget to download under Linux, in the form For wget -O php-5.6.15.tar.gz http://cn2.php.net/get/php-5.6.15.tar.gz/from/this/mirror, -O should be used here to download Name the file, otherwise the name of the downloaded file will be "mirror". In addition, you can also use Sohu's mirror address to download http://mirrors.sohu.com/php/***)

3. Unzip the compressed file

# tar -zxf php-5.6.15.tar.gz

4. Previous stepGenerate the php-5.6.15 directory, enter the directory, Execute configure in the directory Executable files (where \ represents newline in Linux commands), MySQL and some other extension libraries must be installed before installation. Specifically, you can supplement the uninstalled extensions according to the error message during the installation process

# ./configure \
--prefix=/usr/local/php-5.6.15 \
--with-config-file-path=/usr/local/php-5.6.15/etc \
--with-mysqli \
--with-xmlrpc \
--with-openssl \
--with-zlib \
--with-freetype-dir \
--with-png-dir \
--with-jpeg-dir \
--with-gd \
--with-iconv=/usr/local/libiconv \
--enable-sockets \
--enable-zend-multibyte \
--enable-soap \
--enable-mbstring \
--enable-static \
--enable-gd-native-ttf \
--with-mcrypt \
--with-mhash \
--with-curl \
--with-xsl \
--enable-ftp \
--with-libxml-dir \
--enable-pcntl \
--enable-bcmath \
--enable-fpm

The corresponding explanation is as follows:

--prefix=/usr/local/php-5.6.15 \ (指定PHP的安装目录)
--with-apxs2=/usr/sbin/apxs \ (使PHP支持Apache服务器,使用yum安装Apache时,apxs默认在usr/sbin/目录下)
--with-config-file-path=/usr/local/php-5.6.12/etc \ (指定PHP的配置文件php.ini目录)
--with-mysqli[=DIR] \ (指定mysqli的安装路径,使PHP支持mysqli,PHP最新版本已经不再支持mysql扩展了)
--with-xmlrpc \ (使PHP支持XML-RPC,即XML远程方法调用)
--with-openssl \ (使PHP编译安装openssl模块,加密传输https时用到的)
--with-zlib[=DIR] \ (配置gd库的支持包zlib,zlib是提供数据压缩用的函式库)
--with-freetype-dir[=DIR] \ (配置gd库的支持包freetype,FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎)
--with-png-dir[=DIR] \ (配置gd库的支持包libpng)
--with-jpeg-dir[=DIR] \ (配置gd库的支持包libjpeg)
--with-gd \ (静态编译gd库)
--with-iconv=/usr/local/libiconv \ (iconv命令可以将一种已知的字符集文件转换成另一种已知的字符集文件)
--enable-sockets \ (打开对socket的支持)
--enable-zend-multibyte \ (支持zend的多字节)
--enable-soap \ (打开对soap的支持,简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML的协议,它被设计成在WEB上交换结构化的和固化的信息)
--enable-mbstring \ (打开对多字节多字符串的支持)
--enable-static \ (生成静态链接库,在编译过程中,就将静态库中的代码载入程序,由此生成出的可执行程序在运行中不再需要静态库,但因为库中的程序代码被复制进目标程序中,因此生成的程序体积会比较大)
--enable-gd-native-ttf \  (支持TrueType字符串函数库)
--with-mcrypt \ (mcrypt算法扩展)
--with-mhash \  (mhash算法扩展)
--with-curl \ (打开curl浏览工具的支持)
--with-xsl \ (打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件)
--enable-ftp \ (打开对ftp的支持)
--with-libxml-dir \ (打开libxml2库的支持)
--enable-pcntl \ (扩展可以支持php的多线程操作)
--enable-bcmath \ (打开图片大小调整,用到zabbix监控的时候用到了这个模块)
--enable-fpm (打开对php-fpm的支持)

(Note: The above configuration options for compilation and installation are relatively complete and can be configured selectively, but there are also Some configuration options are not given. In addition, Since PHP has integrated the GD library, the premise is that the support package should be installed in advance, such as zlib, png, jpeg, freetype etc. Extension, use yum or download the source code package to compile and install manually. After installation, recompile and install PHP

6. PHP installation is completed, configure and enter /usr In /local/php-5.6.15/etc, it is found that there is no php.ini file. At this time, the php.ini-production under the installation package directory /tmp/php-5.6.15 should be copied to

/usr/local /php-5.6.15/etc Next

# cp /tmp/php-5.6.15/php.ini-production php.ini

(Note: php.ini-development is suitable for development programs, that is, for testing, php.ini-production has higher security settings, it is suitable for online use as a product. Generally, php.ini-production is changed to php.ini to ensure that the test environment (local) is consistent with the official environment (online). The relevant configuration of PHP will not be explained here.)

7. Add /usr/local/php-5.6.15/bin

to the system environment variables for convenience Use commands such as php and phpize

# vi /etc/profile

and add export PATH="$PATH:/usr/local/php-5.6.15/bin" at the end of the opened file. After saving the file, restart the system

8. Run php. The operation of PHP is equivalent to running php-fpm

# /usr/local/php-5.6.15/bin/php-fpm -c /usr/local/php-5.6.15/etc/php.ini -y /usr/local/php-5.6.15/etc/php-fpm.conf

最后,简单说下PHP的开机启动设置,因为一般的开机启动软件都应该被作为一项系统服务保存在 /etc/rc.d/init.d 中,所以php-fpm也应该作为一个可运行文件存在于该目录下(与MySQL的mysqld以及Apache的httpd相似),然而进入该目录下并未发现php-fpm,没关系,那我们就自己写一个脚本吧。具体脚本如下,可以直接在 /etc/rc.d/init.d 下新建一个php-fpm文件,复制下面代码,修改响应配置与自己所安装的PHP环境相同即可。

#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php-5.6.15/etc/php.ini
 
# Source function library.
. /etc/rc.d/init.d/functions
 
PHP_PATH=/usr/local/php-5.6.15
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH/etc/php-fpm.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
 
rh_start() {
  $DAEMON -y $CONFIGFILE || echo -n " already running"
}
 
rh_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
 
rh_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
 
case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        rh_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        rh_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        rh_stop
        sleep 1
        rh_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
         exit 3
        ;;
esac
exit 0

之后可以通过使用service 命令进行操作

# service php-fpm start 启动php-fpm,
# service php-fpm stop 关闭php-fpm,
# service php-fpm restart重启php-fpm

使用chkconfig 命令添加、删除和查看系统开机自启动服务

# chkconfig --list 显示开机可以自动启动的服务 
# chkconfig --add php-fpm 添加开机自动启动php-fpm服务 
# chkconfig --del php-fpm 删除开机自动启动php-fpm服务

好了,关于PHP的安装基本上就完成啦~

The above is the detailed content of Compile and install PHP on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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