Cài đặt nhiều phiên bản php cho linux centos

1.Cấu hình firewall,cài đặt apache và các repo cần thiết:

yum install httpd
yum install epel-release
yum install yum-utils

Disable SELinux

sudo setenforce 0

Cấu hình firewall

firewall-cmd --permanent --add-service=http
firewall-cmd --reload

2.Cài đặt nhiều phiên bản php

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Setup php56

yum install php56 php56-php-cli.x86_64 php56-php-common.x86_64 php56-php-gd.x86_64 php56-php-imap.x86_64 php56-php-mbstring.x86_64 php56-php-mysqlnd.x86_64 php56-php-pdo.x86_64 php56-php-pecl-jsonc.x86_64 php56-php-pecl-zip.x86_64 php56-php-xml.x86_64 php56-php-pgsql.x86_64 php56-php-soap.x86_64 php56-php-devel.x86_64 php56-php-pear.noarch php56-php-ldap.x86_64

Setup php72

yum install php72 php72-php-cli.x86_64 php72-php-common.x86_64 php72-php-gd.x86_64 php72-php-imap.x86_64 php72-php-mbstring.x86_64 php72-php-mysqlnd.x86_64 php72-php-pdo.x86_64 php72-php-pecl-jsond.x86_64 php72-php-pecl-zip.x86_64 php72-php-xml.x86_64 php72-php-pgsql.x86_64 php72-php-soap.x86_64 php72-php-devel.x86_64 php72-php-pear.noarch php72-php-ldap.x86_64

Setup php73

yum install php73 php73-php-cli.x86_64 php73-php-common.x86_64 php73-php-gd.x86_64 php73-php-imap.x86_64 php73-php-mbstring.x86_64 php73-php-mysqlnd.x86_64 php73-php-pdo.x86_64 php73-php-pecl-jsond.x86_64 php73-php-pecl-zip.x86_64 php73-php-xml.x86_64 php73-php-pgsql.x86_64 php73-php-soap.x86_64 php73-php-devel.x86_64 php73-php-pear.noarch php73-php-ldap.x86_64

Setup php74

yum install php74 php74-php-cli.x86_64 php74-php-common.x86_64 php74-php-gd.x86_64 php74-php-imap.x86_64 php74-php-mbstring.x86_64 php74-php-mysqlnd.x86_64 php74-php-pdo.x86_64 php74-php-pecl-jsond.x86_64 php74-php-pecl-zip.x86_64 php74-php-xml.x86_64 php74-php-pgsql.x86_64 php74-php-soap.x86_64 php74-php-devel.x86_64 php74-php-pear.noarch php74-php-ldap.x86_64

Setup fpm

yum install php56-php-fpm
yum install php72-php-fpm
yum install php73-php-fpm
yum install php74-php-fpm

Stop

systemctl stop php56-php-fpm
systemctl stop php72-php-fpm
systemctl stop php73-php-fpm
systemctl stop php74-php-fpm

3.Cấu hình php-fpm
Mặc định fpm chạy ở port mặc định là 9000 cho nên muốn chạy nhiều version php thì ta phải cấu hình port như sau:

sed -i 's/:9000/:9056/' /etc/opt/remi/php56/php-fpm.d/www.conf
sed -i 's/:9000/:9072/' /etc/opt/remi/php72/php-fpm.d/www.conf
sed -i 's/:9000/:9073/' /etc/opt/remi/php73/php-fpm.d/www.conf
sed -i 's/:9000/:9074/' /etc/opt/remi/php74/php-fpm.d/www.conf

Run fpm

systemctl restart php56-php-fpm
systemctl restart php72-php-fpm
systemctl restart php73-php-fpm
systemctl restart php74-php-fpm

Tiếp theo cấu hình như sau:

cat > /var/www/cgi-bin/php56.fcgi << EOF
#!/bin/bash
exec /bin/php56-cgi
EOF
cat > /var/www/cgi-bin/php72.fcgi << EOF
#!/bin/bash
exec /bin/php72-cgi
EOF
cat > /var/www/cgi-bin/php73.fcgi << EOF
#!/bin/bash
exec /bin/php73-cgi
EOF
cat > /var/www/cgi-bin/php74.fcgi << EOF
#!/bin/bash
exec /bin/php74-cgi
EOF

Chmod

sudo chmod 755 /var/www/cgi-bin/php56.fcgi
sudo chmod 755 /var/www/cgi-bin/php72.fcgi
sudo chmod 755 /var/www/cgi-bin/php73.fcgi
sudo chmod 755 /var/www/cgi-bin/php74.fcgi

4.Cấu hình Apache
Tạo file test.conf ỏ /etc/httpd/conf.d/

vi /etc/httpd/conf.d/test.conf

Cấu hình file test.conf như sau:

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

<Directory /var/www/html/php56>
DirectoryIndex index.php
AllowOverride all
Require all granted
AddHandler php56-fcgi .php
Action php56-fcgi /cgi-bin/php56.fcgi
</Directory>

<Directory /var/www/html/php72>
DirectoryIndex index.php
AllowOverride all
Require all granted
AddHandler php72-fcgi .php
Action php72-fcgi /cgi-bin/php72.fcgi
</Directory>

<Directory /var/www/html/php73>
DirectoryIndex index.php
AllowOverride all
Require all granted
AddHandler php73-fcgi .php
Action php73-fcgi /cgi-bin/php73.fcgi
</Directory>

<Directory /var/www/html/php74>
DirectoryIndex index.php
AllowOverride all
Require all granted
AddHandler php74-fcgi .php
Action php74-fcgi /cgi-bin/php74.fcgi
</Directory>

Tiếp theo edit file httpd.conf

vi /etc/httpd/conf/httpd.conf

Thêm dòng sau vào cuối file httpd.conf

AddType application/x-httpd-php .php

Tiếp theo tạo file test:

mkdir -p /var/www/html/php56
mkdir -p /var/www/html/php72
mkdir -p /var/www/html/php73
mkdir -p /var/www/html/php74
echo "<?php phpinfo(); ?>" > /var/www/html/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/html/php72/index.php
echo "<?php phpinfo(); ?>" > /var/www/html/php73/index.php
echo "<?php phpinfo(); ?>" > /var/www/html/php74/index.php

5.Chạy service và test:

systemctl enable httpd
systemctl enable php56-php-fpm
systemctl enable php72-php-fpm
systemctl enable php73-php-fpm
systemctl enable php74-php-fpm
systemctl restart httpd
systemctl restart php56-php-fpm
systemctl restart php72-php-fpm
systemctl restart php73-php-fpm
systemctl restart php74-php-fpm

Test
Truy cập link test theo ip server của bạn

http://127.0.0.1/php56
http://127.0.0.1/php72
http://127.0.0.1/php73
http://127.0.0.1/php74

Bạn nào dùng virtual host thì cấu hình như sau:

NameVirtualHost *:80

<VirtualHost *:80>
ServerName gacoder.info
ServerAlias www.gacoder.info
ServerAdmin gacoder@gmail.com
DocumentRoot /var/www/html/php56
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory /var/www/html/php56>
DirectoryIndex index.php
AllowOverride all
Require all granted
AddHandler php56-fcgi .php
Action php56-fcgi /cgi-bin/php56.fcgi
</Directory>

ErrorLog /var/www/html/gacoder.info-error.log
CustomLog /var/www/html/gacoder.info-access.log combined
</VirtualHost>

0 Shares

Leave a Reply

avatar
  Subscribe  
Notify of