# 下載php wget https://www.php.net/distributions/php-7.2.16.tar.gz # 解壓 tar -zxvf php-7.2.16.tar.gz
[root@cloudhost ~]# ll 總用量 19232 drwxrwxr-x 14 root root 4096 3月 5 19:05 php-7.2.16 -rw-r--r-- 1 root root 19686462 4月 12 15:50 php-7.2.16.tar.gz
[root@cloudhost ~]# groupadd www-data [root@cloudhost ~]# useradd -g www-data www-data
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --enable-mbstring --with-curl=/usr/local/curl --with-gd --with-zlib --with-bz2 --enable-sockets --enable-sysvsem --enable-sysvshm -enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli --with-jpeg-dir=/usr --with-png-dir=/usr --with-openssl --with-libdir=/lib/x86_64-linux-gnu/ --enable-ftp --with-gettext --with-xmlrpc --enable-opcache --with-iconv --enable-mysqlnd --with-mysqli=mysqlnd --with-iconv-dir --with-kerberos --with-pdo-sqlite --with-pear --enable-libxml --enable-shmop --enable-xml --enable-opcache
[root@cloudhost php-7.2.16]# make && make install
安裝成功後,顯示以下信息php
Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/ Installing PHP CLI binary: /usr/local/php/bin/ Installing PHP CLI man page: /usr/local/php/php/man/man1/ Installing PHP FPM binary: /usr/local/php/sbin/ Installing PHP FPM defconfig: /usr/local/php/etc/ Installing PHP FPM man page: /usr/local/php/php/man/man8/ Installing PHP FPM status page: /usr/local/php/php/php/fpm/ Installing phpdbg binary: /usr/local/php/bin/ Installing phpdbg man page: /usr/local/php/php/man/man1/ Installing PHP CGI binary: /usr/local/php/bin/ Installing PHP CGI man page: /usr/local/php/php/man/man1/ Installing build environment: /usr/local/php/lib/php/build/ Installing header files: /usr/local/php/include/php/ Installing helper programs: /usr/local/php/bin/ program: phpize program: php-config Installing man pages: /usr/local/php/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/php/lib/php/ [PEAR] Archive_Tar - installed: 1.4.4 [PEAR] Console_Getopt - installed: 1.4.1 [PEAR] Structures_Graph- installed: 1.1.1 [PEAR] XML_Util - installed: 1.4.3 [PEAR] PEAR - installed: 1.10.7 Wrote PEAR system config file at: /usr/local/php/etc/pear.conf You may want to add: /usr/local/php/lib/php to your php.ini include_path /root/php-7.2.15/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f phar.phar /usr/local/php/bin/phar Installing PDO headers: /usr/local/php/include/php/ext/pdo/
從上一步的安裝選項看出,咱們將配置文件設置在了 /usr/local/php/etc
目錄下,須要將配置文件拷貝到該目錄。mysql
[root@cloudhost php-7.2.16]# cp php.ini-development /usr/local/php/etc/php.ini [root@cloudhost php-7.2.16]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@cloudhost php-7.2.16]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
當PHP編譯安裝完成後,php-fpm
還不是系統服務。爲了方便啓動、中止、重啓php-fpm
,能夠將其註冊爲系統服務。linux
init.d.php-fpm
文件[root@cloudhost ~]# find / -name init.d.php-fpm /root/php-7.2.16/sapi/fpm/init.d.php-fpm
/etc/init.d
目錄下[root@cloudhost ~]# cp /root/php-7.2.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@cloudhost ~]# chmod 755 /etc/init.d/php-fpm
php-fpm
[root@cloudhost ~]# service php-fpm start Gracefully shutting down php-fpm . done
php-fpm
[root@cloudhost ~]# service php-fpm stop Starting php-fpm done
php-fpm
[root@cloudhost ~]# service php-fpm reload Reload service php-fpm done
當嘗試使用php -v
查看PHP版本時,提示命令未找到。sql
[root@cloudhost ~]# php -v -bash: php: 未找到命令
此時,須要將php添加到環境變量中。api
咱們能夠先經過 echo $PATH
查看一下環境變量。bash
[root@cloudhost ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
添加環境變量有3種方法:curl
PATH
就會失效export PATH=$PATH:/usr/local/php/bin
source
命令,將上一種方式的導出操做添加到文件/etc/profile
的末尾。source
命令,優先級高於2
,將方式1的導出操做添加到文件~/.bashrc
的末尾添加了環境變量以後,經過php -v
命令查看PHP版本。socket
[root@cloudhost ~]# php -v PHP 7.2.15 (cli) (built: Apr 10 2019 16:52:28) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies