Difference between revisions of "Ubuntu: Install Multiple PHP Version 20.04"

From Catatan IBM
Jump to navigation Jump to search
(Created page with "==Nginx== Here, we will install PHP7.0, PHP7.1, PHP7.2, and PHP7.3 on the server. 1. First, we will add PPA using the command below – # sudo apt install software-properties-common # sudo add-apt-repository ppa:ondrej/php 2. Run the Update command. # sudo apt update 3. Install PHP 7.2 with this command. # sudo apt install php7.2 php7.2-fpm 4. Install PHP extensions using this command – # sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-...")
 
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
Here, we will install PHP7.0, PHP7.1, PHP7.2, and PHP7.3 on the server.
Here, we will install PHP7.0, PHP7.1, PHP7.2, and PHP7.3 on the server.


1. First, we will add PPA using the command below –  
* 1. First, we will add PPA using the command below –  


  # sudo apt install software-properties-common
  # sudo apt install software-properties-common
  # sudo add-apt-repository ppa:ondrej/php
  # sudo add-apt-repository ppa:ondrej/php


2. Run the Update command.  
* 2. Run the Update command.  
 
  # sudo apt update
  # sudo apt update


3. Install PHP 7.2 with this command.
* 3. Install PHP 7.2 with this command.
 
  # sudo apt install php7.2 php7.2-fpm
  # sudo apt install php7.2 php7.2-fpm


4. Install PHP extensions using this command –  
* 4. Install PHP extensions using this command –  
 
  # sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
  # sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl


5. Now, install PHP 7.1 using this command –  
* 5. Now, install PHP 7.1 using this command –  
 
  # sudo apt install php7.1 php7.1-fpm
  # sudo apt install php7.1 php7.1-fpm


6. Install common PHP7.1 extensions.
* 6. Install common PHP7.1 extensions.
 
  # sudo apt install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl
  # sudo apt install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl


You can install PHP 7.0 as we have installed PHP 7.1 and 7.2
You can install PHP 7.0 as we have installed PHP 7.1 and 7.2


7. Install Nginx with this command –  
* 7. Install Nginx with this command –  
 
  # sudo apt install nginx
  # sudo apt install nginx


8. Create two folders to test the website with PHP 7.1 and PHP 7.2.
* 8. Create two folders to test the website with PHP 7.1 and PHP 7.2.
 
  echo "<?php phpinfo();?>" > /var/www/php72/index.php
  echo "<?php phpinfo();?>" > /var/www/php72/index.php
  echo "<?php phpinfo();?>" > /var/www/php71/index.php
  echo "<?php phpinfo();?>" > /var/www/php71/index.php


9. We will create server blocks for both websites on Nginx.
* 9. We will create server blocks for both websites on Nginx.
 
  # sudo nano /etc/nginx/sites-available/php71.example.com
  # sudo nano /etc/nginx/sites-available/php71.example.com


10. Insert the following content for the site with PHP 7.1 –  
* 10. Insert the following content for the site with PHP 7.1 –  
 
  # Application with PHP 7.1  
  # Application with PHP 7.1
  #
  #
   
   
  server {
  server {  
     listen 80;
     listen 80;
     root /var/www/php71;
     root /var/www/php71;
    index index.php;
     server_name php71.test.com;
     server_name php71.test.com;
   
   
    index index.html index.htm index.php;
   
   
    location / {
        try_files $uri $uri/ =404;
    }
   
   
     location ~* \.php$ {
     location ~* \.php$ {
         # With php-fpm unix sockets
         # With php-fpm unix sockets
         fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
         fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
         include        fastcgi_params;
         include        fastcgi_params;
         fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
         fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
         fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
         fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    }
   
   
        location ~ /\.ht {
        deny all;
     }
     }
   
   
  }
  }


11. Now, create a second VirtualHost configuration file to work with PHP 7.2.
* 11. Now, create a second VirtualHost configuration file to work with PHP 7.2.
 
  # sudo nano /etc/nginx/sites-available/php72.example.com
  # sudo nano /etc/nginx/sites-available/php72.example.com


  # Application with PHP 7.2
  # Application with PHP 7.2
  #
  #
   
   
  server {
  server {
     listen 80;
     listen 80;
     root /var/www/php72;
     root /var/www/php72;
    server_name php72.test.com;
   
   
     index index.php;
     index index.html index.htm index.php;
   
   
     server_name php72.test.com;
     location / {
        try_files $uri $uri/ =404;
 
    }
   
   
     location ~* \.php$ {
     location ~* \.php$ {
         # With php-fpm unix sockets
         # With php-fpm unix sockets
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         include        fastcgi_params;
         include        fastcgi_params;
         fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
         fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
         fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
         fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    }
   
   
        location ~ /\.ht {
        deny all;
     }
     }
   
   
  }
  }


12. Create a symbolic link for both config files under the /etc/nginx/sites-enabled directory.  
* 12. Create a symbolic link for both config files under the /etc/nginx/sites-enabled directory.  
 
  # sudo ln -s /etc/nginx/sites-available/php71.example.com /etc/nginx/sites-enabled/
  # sudo ln -s /etc/nginx/sites-available/php71.example.com /etc/nginx/sites-enabled/
  # sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/
  # sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/


13. Let us restart the Nginx.
* 13. Then, unlink the default configuration file from the /sites-enabled/ directory:
# sudo unlink /etc/nginx/sites-enabled/default


* 14. Let us restart the Nginx.
  # sudo systemctl restart nginx.service
  # sudo systemctl restart nginx.service


14. You can access both websites in the browser and see the different versions for each.
* 15. You can access both websites in the browser and see the different versions for each.
 
 
==Apache==
 
 
===Step 1 — Installing PHP Versions 7.2 and 7.3 with PHP-FPM===
 
Execute the apt-get command to install software-properties-common:
sudo apt-get install software-properties-common -y
 
add the ondrej/php repository to your system
sudo add-apt-repository ppa:ondrej/php
 
Update the repository:
sudo apt-get update -y
 
 
Next, install php7.2, php7.2-fpm, php7.2-mysql, libapache2-mod-php7.2, and libapache2-mod-fcgid with the following commands:
sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 libapache2-mod-fcgid -y
*php7.2 is a metapackage used to run PHP applications.
*php7.2-fpm provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.
*php7.2-mysql connects PHP to the MySQL database.
*libapache2-mod-php7.2 provides the PHP module for the Apache webserver.
*libapache2-mod-fcgid contains a mod_fcgid that starts a number of CGI program instances to handle concurrent requests.
 
Now repeat the process for PHP version 7.3. Install php7.3, php7.3-fpm, php7.3-mysql, and libapache2-mod-php7.3.
sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y
 
After installing both PHP versions, start the php7.2-fpm service:
sudo systemctl start php7.2-fpm
sudo systemctl status php7.2-fpm
 
Repeating this process, now start the php7.3-fpm service:
sudo systemctl start php7.3-fpm
sudo systemctl status php7.3-fpm
 
Lastly, you must enable several modules so that your Apache2 service can work with multiple PHP versions:
sudo a2enmod actions fcgid alias proxy_fcgi
*actions is used for executing CGI scripts based on media type or request method.
*fcgid is a high performance alternative to mod_cgi that starts a sufficient number of instances of the CGI program to handle concurrent requests.
*alias provides for the mapping of different parts of the host filesystem in the document tree, and for URL redirection.
*proxy_fcgi allows Apache to forward requests to PHP-FPM.
 
Now restart the Apache service to apply your changes:
sudo systemctl restart apache2
 
 
===Step 2 — Creating Directory Structures for Both Websites===
 
First, create document root directories for both site1.your_domain and site2.your_domain:
sudo mkdir /var/www/site1.your_domain
sudo mkdir /var/www/site2.your_domain
 
By default, the Apache webserver runs as a www-data user and www-data group. To ensure that you have the correct ownership and permissions of your website root directories, execute the following commands:
sudo chown -R www-data:www-data /var/www/site1.your_domain
sudo chown -R www-data:www-data /var/www/site2.your_domain
sudo chmod -R 755 /var/www/site1.your_domain
sudo chmod -R 755 /var/www/site2.your_domain
 
Next you will create an info.php
sudo nano /var/www/site1.your_domain/info.php
===ISI===
<?php phpinfo(); ?>
 
Copy to site2:
sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/info.php
 
 
===Step 3 — Configuring Apache for Both Websites===
 
First create a new virtual host configuration file for the website site1.your_domain. Here you will direct Apache to render content using php7.2:
sudo nano /etc/apache2/sites-available/site1.your_domain.conf
 
Add the following content. Make sure the website directory path, server name, and PHP version match your setup:
<VirtualHost *:80>
      ServerAdmin [email protected]_domain
      ServerName site1.your_domain
      DocumentRoot /var/www/site1.your_domain
      DirectoryIndex info.php
      <Directory /var/www/site1.your_domain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
      </Directory>
    <FilesMatch \.php$>
        # From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
          SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>
      ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
      CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>
 
 
Next, create a new virtual host configuration file for the website site2.your_domain. You will specify this subdomain to deploy php7.3:
sudo nano /etc/apache2/sites-available/site2.your_domain.conf
 
Add the following content. Again, make sure the website directory path, server name, and PHP version match your unique information:
<VirtualHost *:80>
      ServerAdmin [email protected]_domain
      ServerName site2.your_domain
      DocumentRoot /var/www/site2.your_domain
      DirectoryIndex info.php
      <Directory /var/www/site2.your_domain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
      </Directory>
    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
          SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
    </FilesMatch>
      ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
      CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>
 
check the Apache configuration file for any syntax errors:
sudo apachectl configtest
===OUTPUT===
Output
Syntax OK
 
Next, enable both virtual host configuration files with the following commands:
sudo a2ensite site1.your_domain
sudo a2ensite site2.your_domain
 
Now disable the default site, since you won’t need it.:
sudo a2dissite 000-default.conf
 
Finally, restart the Apache service to implement your changes:
sudo systemctl restart apache2
 
 
===Step 4 — Testing Both Websites===
Open your web browser and visit both sites http://site1.your_domain and http://site2.your_domain.

Latest revision as of 18:23, 3 October 2023

Nginx

Here, we will install PHP7.0, PHP7.1, PHP7.2, and PHP7.3 on the server.

  • 1. First, we will add PPA using the command below –
# sudo apt install software-properties-common
# sudo add-apt-repository ppa:ondrej/php
  • 2. Run the Update command.
# sudo apt update
  • 3. Install PHP 7.2 with this command.
# sudo apt install php7.2 php7.2-fpm
  • 4. Install PHP extensions using this command –
# sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
  • 5. Now, install PHP 7.1 using this command –
# sudo apt install php7.1 php7.1-fpm
  • 6. Install common PHP7.1 extensions.
# sudo apt install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl

You can install PHP 7.0 as we have installed PHP 7.1 and 7.2

  • 7. Install Nginx with this command –
# sudo apt install nginx
  • 8. Create two folders to test the website with PHP 7.1 and PHP 7.2.
echo "<?php phpinfo();?>" > /var/www/php72/index.php
echo "<?php phpinfo();?>" > /var/www/php71/index.php
  • 9. We will create server blocks for both websites on Nginx.
# sudo nano /etc/nginx/sites-available/php71.example.com
  • 10. Insert the following content for the site with PHP 7.1 –
# Application with PHP 7.1 
#

server { 
    listen 80;
    root /var/www/php71;
    server_name php71.test.com;

    index index.html index.htm index.php;

    location / {
       try_files $uri $uri/ =404;
   }

    location ~* \.php$ {
        # With php-fpm unix sockets
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

        location ~ /\.ht {
       deny all;
    }

}
  • 11. Now, create a second VirtualHost configuration file to work with PHP 7.2.
# sudo nano /etc/nginx/sites-available/php72.example.com
# Application with PHP 7.2
#

server {
    listen 80;
    root /var/www/php72;
    server_name php72.test.com;

    index index.html index.htm index.php;

    location / {
       try_files $uri $uri/ =404;
    }

    location ~* \.php$ {
        # With php-fpm unix sockets
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

        location ~ /\.ht {
        deny all;
    }

}
  • 12. Create a symbolic link for both config files under the /etc/nginx/sites-enabled directory.
# sudo ln -s /etc/nginx/sites-available/php71.example.com /etc/nginx/sites-enabled/
# sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/
  • 13. Then, unlink the default configuration file from the /sites-enabled/ directory:
# sudo unlink /etc/nginx/sites-enabled/default
  • 14. Let us restart the Nginx.
# sudo systemctl restart nginx.service
  • 15. You can access both websites in the browser and see the different versions for each.


Apache

Step 1 — Installing PHP Versions 7.2 and 7.3 with PHP-FPM

Execute the apt-get command to install software-properties-common:

sudo apt-get install software-properties-common -y

add the ondrej/php repository to your system

sudo add-apt-repository ppa:ondrej/php

Update the repository:

sudo apt-get update -y


Next, install php7.2, php7.2-fpm, php7.2-mysql, libapache2-mod-php7.2, and libapache2-mod-fcgid with the following commands:

sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 libapache2-mod-fcgid -y
  • php7.2 is a metapackage used to run PHP applications.
  • php7.2-fpm provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.
  • php7.2-mysql connects PHP to the MySQL database.
  • libapache2-mod-php7.2 provides the PHP module for the Apache webserver.
  • libapache2-mod-fcgid contains a mod_fcgid that starts a number of CGI program instances to handle concurrent requests.

Now repeat the process for PHP version 7.3. Install php7.3, php7.3-fpm, php7.3-mysql, and libapache2-mod-php7.3.

sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y

After installing both PHP versions, start the php7.2-fpm service:

sudo systemctl start php7.2-fpm
sudo systemctl status php7.2-fpm

Repeating this process, now start the php7.3-fpm service:

sudo systemctl start php7.3-fpm
sudo systemctl status php7.3-fpm

Lastly, you must enable several modules so that your Apache2 service can work with multiple PHP versions:

sudo a2enmod actions fcgid alias proxy_fcgi
  • actions is used for executing CGI scripts based on media type or request method.
  • fcgid is a high performance alternative to mod_cgi that starts a sufficient number of instances of the CGI program to handle concurrent requests.
  • alias provides for the mapping of different parts of the host filesystem in the document tree, and for URL redirection.
  • proxy_fcgi allows Apache to forward requests to PHP-FPM.

Now restart the Apache service to apply your changes:

sudo systemctl restart apache2


Step 2 — Creating Directory Structures for Both Websites

First, create document root directories for both site1.your_domain and site2.your_domain:

sudo mkdir /var/www/site1.your_domain
sudo mkdir /var/www/site2.your_domain

By default, the Apache webserver runs as a www-data user and www-data group. To ensure that you have the correct ownership and permissions of your website root directories, execute the following commands:

sudo chown -R www-data:www-data /var/www/site1.your_domain
sudo chown -R www-data:www-data /var/www/site2.your_domain
sudo chmod -R 755 /var/www/site1.your_domain
sudo chmod -R 755 /var/www/site2.your_domain

Next you will create an info.php

sudo nano /var/www/site1.your_domain/info.php

===ISI===
<?php phpinfo(); ?>

Copy to site2:

sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/info.php


Step 3 — Configuring Apache for Both Websites

First create a new virtual host configuration file for the website site1.your_domain. Here you will direct Apache to render content using php7.2:

sudo nano /etc/apache2/sites-available/site1.your_domain.conf

Add the following content. Make sure the website directory path, server name, and PHP version match your setup:

<VirtualHost *:80>
     ServerAdmin [email protected]_domain
     ServerName site1.your_domain
     DocumentRoot /var/www/site1.your_domain
     DirectoryIndex info.php

     <Directory /var/www/site1.your_domain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
        # From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
         SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
     CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>


Next, create a new virtual host configuration file for the website site2.your_domain. You will specify this subdomain to deploy php7.3:

sudo nano /etc/apache2/sites-available/site2.your_domain.conf

Add the following content. Again, make sure the website directory path, server name, and PHP version match your unique information:

<VirtualHost *:80>
     ServerAdmin [email protected]_domain
     ServerName site2.your_domain
     DocumentRoot /var/www/site2.your_domain
     DirectoryIndex info.php

     <Directory /var/www/site2.your_domain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
         SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
     CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>

check the Apache configuration file for any syntax errors:

sudo apachectl configtest

===OUTPUT===
Output
Syntax OK

Next, enable both virtual host configuration files with the following commands:

sudo a2ensite site1.your_domain
sudo a2ensite site2.your_domain

Now disable the default site, since you won’t need it.:

sudo a2dissite 000-default.conf

Finally, restart the Apache service to implement your changes:

sudo systemctl restart apache2


Step 4 — Testing Both Websites

Open your web browser and visit both sites http://site1.your_domain and http://site2.your_domain.