Home Backend Development PHP Tutorial Compile and install PHP on Linux

Compile and install PHP on Linux

Jun 02, 2018 am 09:34 AM
linux php compile

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

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

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的支持)
Copy after login

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

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

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

最后,简单说下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
Copy after login

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

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

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

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

好了,关于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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

See all articles