1. Add Repository
Let’s begin by updating the package lists.
sudo apt update For Ubuntu 20.04 and 18.04 users, install libapache2-mod-fcgid.
sudo apt install libapache2-mod-fcgidUbuntu 16.04 users, install libapache2-mod-fastcgi.
sudo apt install libapache2-mod-fastcgiYou may need software-properties-common in order to add a repository with add-apt-repository.
We will add Ondřej’s PHP repository that will allow us to download co-installable versions of PHP 5.6 and PHP 7.x, then update the package lists again.
sudo add-apt-repository ppa:ondrej/php && sudo apt updateENTER when prompted to add the repository.2. Install PHP
You can now install the version of PHP you require. Ondřej’s repository provides PHP 5.6 and PHP 7.x. In this example we will install PHP 5.6 and PHP 7.4.
sudo apt install php5.6 php5.6-fpmsudo apt install php7.4 php7.4-fpmPress y and ENTER when prompted to install.
Once installed, you should have two new sockets in /var/run/php/.
ls /var/run/php/total 8
-rw-r--r-- 1 root root 4 Feb 17 16:50 php5.6-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 17 16:50 php5.6-fpm.sock
-rw-r--r-- 1 root root 5 Feb 17 16:51 php7.4-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 17 16:51 php7.4-fpm.sockIn Step 3, we will use the <FilesMatch> directive to tell Apache which PHP socket to use.
MySQL Extension
If you intend on using MySQL, you must install the MySQL extension for both PHP 7.x and PHP 5.6.
sudo apt install php5.6-mysqlsudo apt install php7.4-mysqlOther Extensions/Libraries
Note that if you need any other libraries or extensions, they must be installed separately per PHP version. For example, if you need cURL, you would need to install it for both versions.
sudo apt install php5.6-curlsudo apt install php7.4-curl3. Configure Apache
We need to add some Apache modules using a2enmod.
Ubuntu 20.04 / 18.04 users.
sudo a2enmod actions alias proxy_fcgi fcgidUbuntu 16.04 users.
sudo a2enmod actions alias proxy_fcgi fastcgiYou may need to add these two PHP services to the Apache config with:
sudo a2enconf php5.6-fpmsudo a2enconf php7.4-fpmAnd then start them with:
sudo service php5.6-fpm startsudo service php7.4-fpm startRestart Apache.
sudo systemctl restart apache2You can now use either Virtual Hosts or .htaccess to instruct Apache which version of PHP to use.
4. Virtual Hosts Method
Open up your Apache .conf file and add the <FilesMatch> directive to your Virtual Host.
This instructs Apache which PHP socket to use.
PHP 5.6 Example
<VirtualHost *:80>
<FilesMatch \.php> # Apache 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost> Make sure to restart Apache after making changes to the Virtual Hosts.
sudo systemctl restart apache2PHP 7.4 Example
<VirtualHost *:80>
<FilesMatch \.php> # Apache 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost>Make sure to restart Apache after making changes to the Virtual Hosts.
sudo systemctl restart apache25. htaccess Method
You can also add the <FilesMatch> directive to your .htaccess file. Before you do, make sure that AllowOverride is enabled, otherwise Apache will ignore .htaccess.
Open the Apache config file.
sudo nano /etc/apache2/apache2.confScroll down the the following section and make sure that AllowOverride is set to All.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>Save and exit (press CTRL + X, press Y and then press ENTER)
Restart Apache.
sudo systemctl restart apache2Now you can add the <FilesMatch> directive to .htaccess
PHP 5.6 Example
<FilesMatch \.php>
# Apache 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
</FilesMatch>PHP 7.4 Example
<FilesMatch \.php>
# Apache 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>6. Test PHP
To see which version of PHP Apache is serving, create a new file called info.php in your web document root.
In this example, we will create a new file in /var/www/html/
sudo nano /var/www/html/info.phpEnter the following PHP code.
<?php
phpinfo();
?>Save file and exit. (Press CTRL + X, press Y and then press ENTER)
We can now load this file in the browser by going to http://example.com/info.php or http://your_ip/info.php
ไม่มีความคิดเห็น:
แสดงความคิดเห็น