centos安裝多個(gè)php的方法:首先為yum引入EPEL庫(kù)和REMI庫(kù);然后通過(guò)命令“yum-config-manager –enable remi-php71”啟用PHP源“remi-php71”;接著安裝并配置好“php56”即可。
centos中安裝多版本php并同時(shí)用于nginx
在新建的虛擬機(jī)中安裝了php7, 安好了才發(fā)現(xiàn)一些老一點(diǎn)的項(xiàng)目跑不了了, 由于php7版本較php5版本有了較大修改, 很多函數(shù)已經(jīng)不是廢棄, 而是移除了, 導(dǎo)致很多問(wèn)題. 只好再安裝一個(gè)php版本了, 我要安裝的是php5.6, 在網(wǎng)上搜了linux中的php多版本管理, 推薦了phpenv, 搞了一通, 沒(méi)有結(jié)果, 只好換個(gè)方法了, 直到我發(fā)現(xiàn)了這篇文章, 直接解決. 下面給大家介紹安裝及配置過(guò)程.
推薦:《centos教程》
這種情況下其實(shí)可以通過(guò)直接在一個(gè)linux系統(tǒng)中通過(guò)yum等工具安裝好不同的PHP版本, 分別注冊(cè)PHP-FPM服務(wù), 配置到服務(wù)器中即可.
實(shí)驗(yàn)環(huán)境
CENTOS7
Nginx v1.12.2
PHP7(設(shè)置為系統(tǒng)默認(rèn)PHP版本)和PHP5.6
服務(wù)器IP 192.168.56.100
安裝PHP7與PHP5.6
首先為yum引入兩個(gè)庫(kù): EPEL與REMI, 因?yàn)檫@個(gè)兩個(gè)庫(kù)為我們提供最新的PHP版本源, CENTOS 自帶的yum庫(kù)中PHP版本都太老舊了.
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
安裝php71
# yum-config-manager --enable remi-php71 [Default] # yum install php php-common php-fpm # yum install php-mysql php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo
第一句用于啟用PHP源remi-php71
安裝php56
# yum install php56 php56-php-common php56-php-fpm # yum install php56-php-mysql php56-php-pecl-memcache php56-php-pecl-memcached php56-php-gd php56-php-mbstring php56-php-mcrypt php56-php-xml php56-php-pecl-apc php56-php-cli php56-php-pear php56-php-pdo
在linux中執(zhí)行php -v, 驗(yàn)證一下, 當(dāng)前的php版本應(yīng)該是7.1
安裝好之后, 下面就要配置php-fpm與php56-php-fpm, 他們是php的Fastcgi進(jìn)程管理器, linux中web服務(wù)器調(diào)用php處理就是通過(guò)他們.
好了,開(kāi)始配置吧.
兩個(gè)php版本分別對(duì)應(yīng)的配置文件為
php-fpm (default 7.1) – /etc/php-fpm.d/www.conf php56-php-fpm – /opt/remi/php56/root/etc/php-fpm.d/www.conf
(很神奇, 安裝php56版本的目錄是在opt目錄下)
打開(kāi)兩個(gè)配置文件, 更改如下代碼
listen = 127.0.0.1:9000[php-fpm] listen = 127.0.0.1:9001[php56-php-fpm]
如果是通過(guò)socket通信方式調(diào)用php-fpm的情況, 則更改代碼如下
listen = /var/run/php-fpm/php-fpm.sock[php-fpm] listen = /opt/remi/php56/root/var/run/php-fpm/php-fpm.sock[php56-php-fpm]
分別注冊(cè)并啟用兩個(gè)版本的php-fpm服務(wù)
# systemctl enable nginx # systemctl start nginx # systemctl enable mariadb # systemctl start mariadb ---------------- PHP 7.1 ---------------- # systemctl enable php-fpm # systemctl start php-fpm ---------------- PHP 5.6 ---------------- # systemctl enable php56-fpm # systemctl start php56-php-fpm
使用php7的nginx服務(wù)器配置
server { listen 80; server_name example1.com www.example1.com; root /var/www/html/example1.com/; index index.php index.html index.htm; #charset koi8-r; access_log /var/log/nginx/example1.com/example1_access_log; error_log /var/log/nginx/example1.com/example1_error_log error; location / { try_files $uri $uri/ /index.php?$query_string; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { root /var/www/html/example1.com/; fastcgi_pass 127.0.0.1:9000;#set port for php-fpm to listen on fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include /etc/nginx/fastcgi_params; } } `
使用php56的nginx服務(wù)器中配置
server { listen 80; server_name example2.com www.example2.com; root /var/www/html/example2.com/; index index.php index.html index.htm; #charset koi8-r; access_log /var/log/nginx/example2.com/example2_access_log; error_log /var/log/nginx/example2.com/example2_error_log error; location / { try_files $uri $uri/ /index.php?$query_string; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { root /var/www/html/example2.com/; fastcgi_pass 127.0.0.1:9001;#set port for php56-php-fpm to listen on fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include /etc/nginx/fastcgi_params; } }
添加測(cè)試網(wǎng)頁(yè)文件
# echo "<?php phpinfo(); ?>" > /var/www/html/example1.com/info.php # echo "<?php phpinfo(); ?>" > /var/www/html/example2.com/info.php
測(cè)試
之后訪問(wèn)example1.com/info.php 與 example2.com/info.php測(cè)試即可.
如果你是在本地虛擬機(jī)中配置的, 別忘了在本地host文件中添加
192.168.56.100 example1.com example1 192.168.56.100 example2.com example2