Cara Menginstall Multiple php di centos 7

Membuat banyak aplikasi pada sebuah server terkadang menjadi kendala ketika setiap aplikasi membutuhkan environment berbeda-beda. Misalnya pada sebuah server terdapat 3 aplikasi dimana bahasa pemograman yang di butuhkan hanya satu tetapi dengan versi berbeda-beda. Contoh :

Aplikasi 1 : PHP 7.3

Aplikasi 2 : PHP 7.0

Aplikasi 3 : PHP 5.6

Dengan kondisi ini, Pada instalasi dengan repository default, instalasi php hanya bisa satu versi saja. Untuk itu perlu repository dan konfigurasi tambahan agar sever dapat melakukan instalasi serta menggunakan 3 versi bahasa pemrograman php.

Langkah-langkah nya sbb:

  1. Masuk ke terminal console dan samakan presepsi user yang kita gunakan saat ini

      - whoami

      - [users@localhost ~]$ whoami users

      - [users@localhost ~]$

  2. Pastikan user nya Super User (root). Ketikkan user root di terminal dan juga password root

      - su

      - [users@localhost ~]$ su

        Password:

      - [root@localhost ~]#

  3. Cek apakah user sudah berubah menjadi root

  4. Pindah ke directory /root

      - cd ~

      - pwd

      - [root@localhost ~]# pwd /root

      - [root@localhost ~]#

  5. Kemudian Install Scl Utils

      - yum install scl-utils -y

  6. Lalu Install epel realese

      - yum install epel-release -y

  7. Install repository remi

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

  8. Selanjutnya Instalasi Setiap versi PhP

      1. yum install php56 php56-php php56-php-cli php56-php-mbstring php56-php-mcryp php56-php-cli php56-php-mysql php56-php-pdo php56-php-xml php56-php-common php56-php-fpm php56-php-devel -y

      2. yum install php70 php70-php php70-php-cli php70-php-mbstring php70-php-mcryp php70-php-cli php70-php-mysql php70-php-pdo php70-php-xml php70-php-common php70-php-fpm php70-php-devel -y

      3. yum install php73 php73-php php73-php-cli php73-php-mbstring php73-php-mcryp php73-php-cli php73-php-mysql php73-php-pdo php73-php-xml php73-php-common php73-php-fpm php73-php-devel -y

  9. Test aplikasi melalui terminal

     1. Buat script test.php yg didalam untuk mengecek versi php

         - 

     2. Jalankan dengan setiap versi

         - php56 tes.php | grep "PHP Version"

         - php70 tes.php | grep "PHP Version"

         - php73 tes.php | grep "PHP Version"

Jika versi setiap php keluarnya sesuai dengan kompiler maka instalasi berhasil.

Selanjutnya coba untuk mengecek versi php dengan cara mengetik php -v pada console terminal. Jika hasil yang keluar -bash: php:command not found

Ini bukan berarti ada yang salah dalam proses instalasi. Hanya saja ada satu versi php yang telah di install belum dijadikan default kompiler, cara untuk merubahnya adalah :

     1. Lakukan dengan tool scl (misal : php56 sebagai default)

        - scl enable php56 bash

     2. Cek dengan cara

        - php -v

Jika hasil nya

 PHP 5.6.40 (cli) (built: Mar 12 2019 15:08:24) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Maka php70 sudah bejalan sebagai default di system. Jika dilakukan tes dengan cara menjalankan file tes.php yang telah dibuat (php tes.php | grep "PHP Version"), maka hasil nya php56

 [root@localhost ~]# php tes.php | grep "PHP Version"
PHP Version => 5.6.40
PHP Version => 5.6.40
[root@localhost ~]#

Kemudian kita testing via web. Disini kita coba dengan webserver nginx. Web server NGINX untuk dapat membaca php dibutuhkan modul php-fpm yang sudah di install di awal tadi pada tiap versi php. Langkah - langkah nya sbb :

  1. Kita samakan kembali user dan direktori saat ini. Pastikan dalam berada dalam user root dan direktori /root.

  2. Buat folder web didalam direktori /home/web

    - mkdir /home/web

  3. Masuk ke dalam direktori /home/web

    - cd /home/web

  4. Buat folder app1, app2, app3

    - mkdir app1 app2 app3

    - ls -l

      [root@localhost web]# ls -l

      total 8

      drwxr-xr-x. 2 root root 6 Apr 5 17:00 app1

      drwxr-xr-x. 2 root root 6 Apr 5 17:00 app2

      drwxr-xr-x. 2 root root 6 Apr 5 17:00 app3

  5. Konfigurasi setiap versi php untuk dapat diakses via web

     1. Buka file www.conf di php56

        - vi /etc/opt/remi/php56/php-fpm.d/www.conf

     2. Ubah pada baris 23, user = apache dan baris 25, group = apache menjadi user = nginx dan group = nginx

        ; Unix user/group of processes

        ; Note: The user is mandatory. If the group is not set, the default user's group

        user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

     3. Pada baris ke 39, Listen = 127.0.0.1:9000 ubah menjadi listen = 127.0.0.1:9056.

        ; Note: This value is mandatory.

        listen = 127.0.0.1:9056

 

        ; Set listen(2) backlog.

        ; Default Value: 65535

        ;listen.backlog = 65535

     4. Save dan keluar

     5. Buka file www.conf di php70

        - vi /etc/opt/remi/php73/php-fpm.d/www.conf

     6. Ubah pada baris 23, user = apache dan baris 25,group =apache menjadi user=nginx dan group = nginx

         ; Unix user/group of processes

         ; Note: The user is mandatory. If the group is not set, the default user's group

         ; will be used.

         ; RPM: apache user chosen to provide access to the same directories as httpd user = nginx

         user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

     7. Pada baris ke 39, listen = 127.0.0.1:9000 ubah menjadi listen = 127.0.0.1:9070.

         ; Note: This value is mandatory.
          listen = 127.0.0.1:9070


         ; Set listen(2) backlog.
         ; Default Value: 65535
         ;listen.backlog = 65535

     8. Save dan keluar

     9. Buka file www.conf di php73

         vi /etc/opt/remi/php70/php-fpm.d/www.conf

    10. Ubah pada baris 23, user = apache dan baris 25, group = apache menjadi user = nginx dan group = nginx

         ; Unix user/group of processes
         ; Note: The user is mandatory. If the group is not set, the default user's group
         ; will be used.
         ; RPM: apache user chosen to provide access to the same directories as httpd user = nginx
         user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

     11. Pada baris ke 39, listen = 127.0.0.1:9000 ubah menjadi listen = 127.0.0.1:9073.

         ; Note: This value is mandatory.
          listen = 127.0.0.1:9073

         ; Set listen(2) backlog.
         ; Default Value: 65535
         ;listen.backlog = 65535

     12. Save dan keluar

  6. Ubah settingan linux dari enforcing menjadi permissive

     setenforce 0

  7. Jalankan service php-fpm

     1. service php56-php-fpm start

        Redirecting to /bin/systemctl start php56-php-fpm.service

     2. service php70-php-fpm start

        Redirecting to /bin/systemctl start php56-php-fpm.service

     3. service php73-php-fpm start

        Redirecting to /bin/systemctl start php56-php-fpm.service

  8. Instalasi nginx

     - yum install nginx -y

  9. Konfigurasi file nginx.conf

     1. Setting nginx pada bagian loop server { .... }. Seperti di bawah ini :

         server {
         listen 80 default_server;
         listen [::]:80 default_server;
         server_name _;
         root /root;

         # Load configuration files for the default server block.
         include /etc/nginx/default.d/*.conf;

         location / {
         }
         location /app1 {
         location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass 127.0.0.1:9056;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
         }
         }
         location /app2 {
         location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass 127.0.0.1:9070;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
         }
         }
         location /app3 {
         location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass 127.0.0.1:9073;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
         }
         }

      2. Save dan keluar

  10. Cek apakah konfigurasi nginx benar

      - nginx -t

       nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
      nginx: configuration file /etc/nginx/nginx.conf test is successful
      [root@localhost web]#

  11. Restart service nginx

       service nginx restart

  12. Copy file tes.php ke direktori app1, app2, app3 dengan nama index.php

       1. cp -a /root/tes.php /home/web/app1/index.php

       2. cp -a /root/tes.php /home/web/app2/index.php

       3. cp -a root/tes.php /home/web/app3/index.php

   13. Ubah hak kepemilikan folder dan isi dari app1, app2, app3 yang sebelumnya root:root menjadi nginx:nginx

       - chown nginx:nginx /home/web -R

   14. Cek apakah hak kepemilikan sudah berubah

       - ls -l

         [root@localhost web]# ls -l
         total 8
         drwxr-xr-x. 2 nginx nginx 23 Apr 5 22:48 app1
         drwxr-xr-x. 2 nginx nginx 23 Apr 5 22:48 app2
         drwxr-xr-x. 2 nginx nginx 23 Apr 5 22:48 app3

   15. Tambahkan allow akses http pada firewalld

        1. firewall-cmd --zone=public --add-service=http --permanent

            [root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanent success
        2. firewall-cmd --reload

            [root@localhost ~]# firewall-cmd --reload
            success

   16. Testing Aplikasi via browser, disini ip centosnya adalah 10.251.2.141

        1. http://10.251.2.141/app1/index.php

        2. http://10.251.2.141/app2/index.php

        3. http://10.251.2.141/app3/index.php

Jika app1 infonya php56, app2 infonya php70 dan app3 infonya adalah php73 maka penggunaan setiap versi php dengan nginx telah berhasil.

Sumber : http://hanangpriambodo.com/2019/04/05/install-multiple-php-di-centos-7/

 

Related Articles

Comments