DEV Community

Maksim N Epikhin (PIKHTA)
Maksim N Epikhin (PIKHTA)

Posted on

Setting up different PHP versions in Ubuntu on Apache2

This is a machine translation of the article "Configuring different versions of PHP in Ubuntu on Apache2" from my personal blog.

In this article, I will tell you my experience of setting up PHP of different versions on the same machine running Ubuntu Linux in conjunction with Apache2, xdebug and PHPStorm. It's not new, but I've had problems that PHPStorm didn't catch Debug on different versions, but preferred one.

Installing prerequisites

First you need to completely remove the existing PHP, and then install the required versions (we will install PHP 7.0 and 7.4) from the ppa:ondrej/php repository:

sudo apt purge 'php*'
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update -y

# for version 7.0

sudo apt-get install php7.0 php7.0-fpm  libapache2-mod-php7.0 php7.0-mbstring php7.0-pgsql php7.0-xdebug libapache2-mod-fcgid -y

# for version 7.4

sudo apt-get install php7.4 php7.4-fpm  libapache2-mod-php7.4 php7.4-mbstring php7.4-pgsql php7.4-xdebug libapache2-mod-fcgid -y
Enter fullscreen mode Exit fullscreen mode

Here we have installed the following packages:

  • php7.X is a metapackage used to run PHP applications.
  • php7.X-fpm provides a Fast Process Manager interpreter that runs as a daemon and accepts Fast/CGI requests.
  • libapahce2-mod-php7.X provides a PHP module for the Apache web server.
  • libapache2-mod-fcgid contains mod_fcgid, which runs multiple instances of the CGI program to handle concurrent requests.
  • php7.X-mbstring module for working with multibyte strings
  • php7.X-pgsql links PHP to PostgreSQL database
  • php7.X-xdebug module for debugging

Setting up xdebug debugging

Now we need to configure xdebug itself in the system and PHPStorm. To do this, go along the path /etc/php/7.X/fpm/ and add the section to php.ini:

# for version xdebug < 3

[xdebug] 
zend_extension=xdebug.so
xdebug.default_enable=1
xdebug.client_port=9000
xdebug.remote_autostart=1
xdebug.remote_enable=1 
xdebug.remote_host=127.0.0.1 
xdebug.remote_handler="dbgp"
xdebug.remote_port=9000

# for version xdebug >= 3

[xdebug] 
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=yes
Enter fullscreen mode Exit fullscreen mode

In PHPStorm itself, the settings are made in Settings -> PHP:

  • Select the desired version and CLI interpreter
  • In the Debug section, uncheck Ignore external connections through unregistered server configurations and Break at first line in PHP scripts
  • Set Debug port to 9000 for xdebug version < 3 and 9003 for xdebug version >= 3.

You may need to add your local site address to the Servers tab, but this may not be necessary. Just in case, I will specify the setting:

  • In Name we write any name
  • In Host we write the address without a protocol to your site, for example, mysite.local

PHPStorm PHP Debug settings

Setting up virtual hosts in Apache2

You need to create a virtual host configuration file:

<VirtualHost *:80>     
  ServerName domain.local
  ServerAlias www.domain.local
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/directory

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

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

  ErrorLog ${APACHE_LOG_DIR}/domain-error.log
  CustomLog ${APACHE_LOG_DIR}/domain-access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Pay attention to the line SetHandler "proxy:unix:/run/php/php7.X-fpm.sock|fcgi://localhost". You need to replace X with the PHP version. In our case, 0 or 4.

How to create and activate sites, I think you know. If not, there are plenty of guides on the internet on this subject.

Reloading services

At the final stage, you need to overload the services and enable the necessary modules:

sudo systemctl start php7.0-fpm
sudo systemctl start php7.4-fpm
sudo a2enmod actions fcgid alias proxy_fcgi
Enter fullscreen mode Exit fullscreen mode

That's it, now you have 2 versions of PHP on different local sites and xdebug will work on both sites.

Top comments (2)

Collapse
 
leslieeeee profile image
Leslie

If you are a macOS user, ServBay.dev is worth to try. You don't need to spend some time or couple of days to setup anything. Just download it and you can use it immediately. You can run multiple PHP versions simultaneously and switch between them effortlessly.
Honestly, this tool has greatly simplified my PHP development and is definitely worth trying!

Collapse
 
maksimepikhin profile image
Maksim N Epikhin (PIKHTA)

I agree. There are many solutions that are much simpler. This manual is made for manual configuration. Most often, this is used on Ubuntu servers.