安裝的基本步驟官方文檔都有說明:PHP官網安裝文檔php
這一步實際上已經在:LNMP環境搭建(二):Nginx上完成了html
[root@localhost ~]# yum -y install gcc [root@localhost ~]# yum -y install gcc++ [root@localhost ~]# yum -y install gcc-c++ [root@localhost ~]# yum -y install wget [root@localhost ~]# yum -y install make [root@localhost ~]# yum -y install libxml2 [root@localhost ~]# yum -y install libxml2-devel [root@localhost ~]# yum -y install openssl [root@localhost ~]# yum -y install openssl-devel [root@localhost ~]# yum -y install curl-devel [root@localhost ~]# yum -y install libjpeg-devel [root@localhost ~]# yum -y install libpng-devel [root@localhost ~]# yum -y install freetype-devel [root@localhost ~]# yum -y install bison [root@localhost ~]# yum -y install autoconf [root@localhost ~]# yum -y install automake [root@localhost ~]# yum -y install libtool [root@localhost ~]# yum -y install cmake [root@localhost ~]# yum -y install zlib [root@localhost ~]# yum -y install zlib-devel [root@localhost ~]# yum -y install pcre-devel
[root@localhost ~]# wget http://php.net/distributions/php-7.2.12.tar.gz [root@localhost ~]# tar -zxvf php-7.2.12.tar.gz
[root@localhost ~]# ./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-mysql-sock=/tmp/mysql.sock --with-mhash --with-openssl --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-fast-install
--prefix=/usr/local/php [php安裝的根目錄] --exec-prefix=/usr/local/php [php執行文件所在目錄] --bindir=/usr/local/php/bin [php/bin目錄] --sbindir=/usr/local/php/sbin [php/sbin目錄] --includedir=/usr/local/php/include [php包含文件所在目錄] --libdir=/usr/local/php/lib/php [php/lib目錄] --mandir=/usr/local/php/php/man [php/man目錄] --with-config-file-path=/usr/local/php/etc [php的配置目錄] --with-mysql-sock=/tmp/mysql.sock [php的Unix socket通訊文件] --with-mhash [Mhash是基於離散數學原理的不可逆向的php加密方式擴展庫,其在默認狀況下不開啓] --with-openssl [OpenSSL 是一個安全套接字層密碼庫] --with-mysqli=shared,mysqlnd [php依賴mysql庫] --with-pdo-mysql=shared,mysqlnd [php依賴mysql庫] --with-gd [gd庫] --with-iconv [關閉iconv函數,種字符集間的轉換] --with-zlib [zlib是提供數據壓縮用的函式庫] --enable-zip [打開對zip的支持] --enable-inline-optimization [優化線程] --disable-debug [關閉調試模式] --disable-rpath [關閉額外的運行庫文件] --enable-shared [啓用動態庫] --enable-xml [開啓xml擴展] --enable-bcmath [打開圖片大小調整,用到zabbix監控的時候用到了這個模塊] --enable-shmop [共享內存] --enable-sysvsem [內存共享方案] --enable-mbregex [開啓多字節正則表達式的字符編碼。] --enable-mbstring [開啓多字節字符串函數] --enable-ftp [開啓ftp] --enable-gd-native-ttf [開啓gd庫原有字體] --enable-pcntl [PHP的進程控制支持實現了Unix方式的多進程建立] --enable-sockets [開啓套節字] --with-xmlrpc [打開xml-rpc的c語言] --enable-soap [開啓簡單對象訪問協議簡單對象訪問協議] --without-pear [開啓php擴展與應用庫] --with-gettext [開戶php在當前域中查找消息] --enable-session [容許php會話session] --with-curl [容許curl擴展] --with-jpeg-dir [指定jpeg安裝目錄yum安裝事後不用再次指定會自動找到] --with-freetype-dir [指定freetype安裝目錄yum安裝事後不用再次指定會自動找到] --enable-opcache [開啓使用opcache緩存] --enable-fpm [開啓fpm] --with-fpm-user=nginx [php-fpm的用戶] --with-fpm-group=nginx [php-fpm的用戶組] --without-gdbm [數據庫函數使用可擴展散列和相似於標準UNIX dbm的工做] --enable-fast-install [爲快速安裝優化] --disable-fileinfo [防止低內存VPS出現內存不住的錯誤]
[root@localhost ~]# make && make install
> -r: 添加系統用戶 > -g: 指定要建立的用戶所屬組 > -s: 新賬戶的登陸shell > -d: 新賬戶的主目錄 > -M: 不要建立用戶的主目錄
[root@localhost ~]# groupadd -r php [root@localhost ~]# useradd -r -g php -M php [root@localhost ~]# passwd php [root@localhost ~]# usermod -s /sbin/nologin php ###文件配置
> php.ini #php運行核心配置文件 > www.conf #php-fpm進程服務的擴展配置文件 > php-fpm.conf #php-fpm進程服務的配置文件
[root@localhost ~]# cp php.ini-development /usr/local/php/etc/php.ini [root@localhost ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@localhost ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf [root@localhost ~]# cp sapi/fpm/php-fpm /usr/local/bin
環境變量:輸入命令的時候LINUX會去查找PATH裏面記錄的路徑
[root@localhost ~]# echo $PATH [root@localhost ~]# ln -s /usr/local/php/sbin/php-fpm /usr/local/sbin/php-fpm [root@localhost ~]# ln -s /usr/local/php/bin/php /usr/local/bin/php
在環境變量:/usr/local/bin/ 下建立 /usr/local/php/bin/php的軟鏈接,當咱們在任意目錄輸入 php時,系統去環境變量中查找,經過軟鏈接指向 /usr/local/php/bin/php。php-fpm同理mysql
編譯以前./configure的時候,咱們都已經肯定了php-fpm 模塊使用 nginx 用戶和 nginx 用戶組的身份運行
[root@localhost ~]# ps -ef | grep php-fpm root 75608 1 0 02:32 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php- fpm.conf) nginx 75609 75608 0 02:32 ? 00:00:00 php-fpm: pool www nginx 75610 75608 0 02:32 ? 00:00:00 php-fpm: pool www root 75641 96978 0 02:32 pts/0 00:00:00 grep --color=auto php-fpm
配置 Nginx 使其支持 PHP 應用nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
修改默認的 location 塊,使其支持 .php 文件:c++
location / { root html; index index.php index.html index.htm; }
下一步配置來保證對於 .php 文件的請求將被傳送到後端的 PHP-FPM 模塊, 取消默認的 PHP 配置塊的註釋,並改成如下的內容:web
location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }
重啓 Nginx正則表達式
[root@localhost ~]# sudo /usr/local/nginx/sbin/nginx -s stop [root@localhost ~]# sudo /usr/local/nginx/sbin/nginx
建立測試文件redis
[root@localhost ~]# rm /usr/local/nginx/html/index.html [root@localhost ~]# echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php
訪問服務器的:localhost 能夠看到 phpinfosql
[root@localhost ~]# vim /usr/local/php/etc/php.ini extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/" date.timezone = Asia/Shanghai
查看 php-fpm 進程重啓 php-fpm 查看更改shell
[root@localhost ~]# ps aux | grep php-fpm [root@localhost ~]# kill 22747 [root@localhost ~]# php-fpm
具體文法參考:Systemd 入門教程:實戰篇
在 php-7.2.12.tar.gz 的解壓目錄下已有一個 service 文件了,複製並重命名到 /etc/systemd/system/ 下便可
[root@localhost ~]# find / -name "php-fpm.service" [root@localhost ~]# cp /home/soft/php-7.1.23/sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service
[Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
啓動 php,設置 php 自動啓動
[root@centos7 /]# systemctl start php-fpm [root@centos7 /]# systemctl enable php-fpm
當啓動失敗時能夠嘗試終止進程再次啓動
參考: centos 經過 yum 安裝 php72
[root@centos7 /]# yum -y remove php*
[root@centos7 /]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm [root@centos7 /]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
等同於
[root@centos7 /]# yum install epel-release -y [root@centos7 /]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@centos7 /]# yum -y install php72w [root@centos7 /]# yum -y install php72w-cli [root@centos7 /]# yum -y install php72w-fpm [root@centos7 /]# yum -y install php72w-common [root@centos7 /]# yum -y install php72w-pdo [root@centos7 /]# yum -y install php72w-pdo_mysql [root@centos7 /]# yum -y install php72w-pdo_sqlite [root@centos7 /]# yum -y install php72w-devel
[root@centos7 /]# php -v # PHP 7.2.11 (cli) (built: Oct 11 2018 19:14:35) ( NTS ) # Copyright (c) 1997-2018 The PHP Group # Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies [root@centos7 /]# php -m # 查看安裝的擴展
[root@centos7 /]# systemctl start php-fpm [root@centos7 /]# systemctl enable php-fpm
參考:編譯安裝PHP擴展
添加軟鏈接,使 phpize 能夠全局使用
[root@localhost ~]# echo $PATH [root@localhost ~]# ln -s /usr/local/php/bin/phpize /usr/local/bin/phpize
[root@localhost ~]# php -m
[PHP Modules] bcmath Core ctype curl date dom fileinfo filter ftp gd gettext hash iconv json libxml mbstring mysqlnd openssl pcntl pcre PDO pdo_sqlite Phar posix Reflection session shmop SimpleXML soap sockets SPL sqlite3 standard sysvsem tokenizer xml xmlreader xmlrpc xmlwriter zip zlib [Zend Modules]
PECL 是PHP Extensions的存儲庫,提供了用於下載和開發PHP擴展的全部已知擴展和託管工具的目錄。
[root@localhost ~]# wget http://pecl.php.net/get/redis-4.1.1.tgz [root@localhost ~]# tar -xvzf redis-4.1.1.tgz [root@localhost ~]# phpize [root@localhost ~]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@localhost ~]# make && make install
前面在 php.ini 設置了拓展的安裝路徑,因而此處已自動將 redis.so 加入拓展目錄中,只要在 php.ini,添加 extension = redis.so 便可
[root@localhost ~]# vim /usr/local/php/etc/php.ini extension = redis.so
[root@localhost ~]# php -m | grep redis
根據 PHP 版本選擇對應的 Xdebug
[root@localhost ~]# wget http://pecl.php.net/get/xdebug-2.6.1.tgz [root@localhost ~]# tar -xvzf xdebug-2.6.1.tgz [root@localhost ~]# phpize [root@localhost ~]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@localhost ~]# make && make install
[root@localhost ~]# vim /usr/local/php/etc/php.ini zend_extension = xdebug.so
[root@localhost ~]# php -m | grep xdebug
----------------------------------------------------------DONE------------------------------------------------------------