How to Configure Multiple PHP Versions with Apache on Ubuntu 如何在ubuntu为apache安装不同版本的PHP

Installing Apache2

Install Apache web server from the official repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of the Apache webserver.

sudo apt update 
sudo apt install apache2 libapache2-mod-fcgid

Now enable a few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with the Apache server.

sudo a2enmod actions fcgid alias proxy_fcgi

Installing PHP Versions

For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system.sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php

For this tutorial, we are using PHP 8.1 and PHP 7.4 to configure with the Apache webserver. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Let’s install the following packages on your system.sudo apt update

sudo apt install php8.1 php8.1-fpm
sudo apt install php7.4 php7.4-fpm

After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.

sudo systemctl status php8.1-fpm 
sudo systemctl status php7.4-fpm

Configuring Apache Virtual Hosts

Get ready for the configuration of websites on your Apache server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server.

sudo mkdir /var/www/php81 
sudo mkdir /var/www/php74

Create an index.php file containing the phpinfo() function.

echo "" | sudo tee -a /var/www/php81/index.php 
echo "" | sudo tee -a /var/www/php74/index.php

Let’s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf. Create a file for the first virtual host and edit in your favorite text editor.

sudo vim /etc/apache2/sites-available/php81.example.com.conf

Add the following content. Make sure to use the correct ServerName and directory path according to your setup. This website is configured to work with PHP 8.1.

<VirtualHost *:80> 
ServerName php81.example.com 
DocumentRoot /var/www/php81 
<Directory /var/www/php81> 
Options -Indexes +FollowSymLinks +MultiViews 
AllowOverride All 
Require all granted 
</Directory> 
<FilesMatch \.php$> # Apache 2.4.10+ can proxy to unix socket SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost" 
</FilesMatch>
</VirtualHost>

Similarly, create a second VirtualHost configuration file to work with PHP 7.4. Edit configuration file in text editor:

sudo vim /etc/apache2/sites-available/php74.example.com.conf

Add the following content to file with proper ServerName and DocumentRoot.

<VirtualHost *:80> 
ServerName php74.example.com 
DocumentRoot /var/www/php74 
<Directory /var/www/php74> 
Options -Indexes +FollowSymLinks +MultiViews 
AllowOverride All 
Require all granted 
</Directory> 
<FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost" </FilesMatch> 
</VirtualHost>

Both of the websites are configured now. But they are still not active. Apache keeps active sites under /etc/apache2/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use the below command to do the same.

sudo a2ensite php81.example.com 
sudo a2ensite php74.example.com

After making all the changes restart Apache to reload new settings changes.

sudo systemctl restart apache2

Your setup has been completed now. Go to the next step to test your setup.

Test Setup

Edit /etc/hosts file on your local system and make an entry like below. This will resolve temprory names to localhost IP address.

sudo vim /etc/hosts

Add following entry to end of file

127.0.0.1 php81.example.com 
127.0.0.1 php74.example.com

Open a web browser and visit both of the sites. You will see that php81.example.com shows the version PHP 8.1 and php74.example.com is showing the PHP 7.4 as the configuration.

http://php81.example.com

PHP 8.1 with Apache

http://php74.example.com

PHP 7.4 with Apache

Conclusion

This tutorial helped you to install and configure multiple PHP versions with Apache on the Ubuntu Linux system. That provides you with a cost-effective solution to deploy different PHP version applications on a single web server.

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
下一篇