Image

Knowledge base → Setting up two php-fpm services with different php versions in different folders on the same domain

[Virtual servers]
Date of publication: 24.11.2022

The task is to deploy two applications with different php versions on the same nginx web server and on the same domain name at the same time. In our example, this will be a customer relationship management (CRM) website and application.

The task is to make the site open:

  • domain.tld - (main domain)
  • domain.tld/nested-app (application)

To ensure security, the application folder must be located at the same level as the site, so that in the event of a site hack, an attacker does not gain access to the application files and vice versa. In our example, we are setting up on Centos 7.

  • /var/www/domain.tld/site - (site folder)
  • /var/www/domain.tld/nested-app - (application folder)

As we can see, they are on the same level in the hierarchy. Since the applications use different php versions, we will need to install and configure the second php-fpm service, along with php and configure it all on nginx. 


1. Install php 8 and php-fpm service

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget https://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm

Enable php 8 in the remi repository settings and install the necessary packages:

apt install php php-fpm php-mysql php-common php-cli php-json php-opcache php-readline php-mbstring php-xml php-gd php-curl

2. Install php 7 and second php-fpm service

yum install centos-release-scl
yum install rh-php73 rh-php73-php-fpm
scl enable rh-php73 bash
semanage port -a -t http_port_t -p tcp 9003
systemctl start rh-php73-php-fpm
systemctl enable rh-php73-php-fpm

Everything is ready for configuration, in this configuration we have added the service to autostart.


3. Install and configure nginx.

yum install nginx

The configuration for our domain will be as follows:

/etc/nginx/conf.d/domain_tld.conf
server { 

listen xx.x.xx.x:443 ssl;
listen [ipv6]:443 ssl; 
server_name domain.tld www.domain.tld; 
index index.php index.html;

 set $base /var/www/domain.tld;
 root $base/site;
 ssl_certificate /etc/ssl/domain_tld/cert23.crt;
 ssl_certificate_key /etc/ssl/domain_tld/cert23.key;
 client_body_timeout 5s;
 client_header_timeout 5s;
 client_max_body_size 32m;
 ssl_session_timeout 5m;

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.2;
 ssl_ciphers HIGH:!aNULL:!MD5;
 ssl_prefer_server_ciphers on;

 add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; ";
 ssl_stapling on;
 ssl_stapling_verify on;

 gzip on;
 gzip_disable "msie6";

 gzip_types text/plain text/css application/json application/x-javascript text/xml 
application/xml application/xml+rss text/javascript application/javascript;

# -- www to no www --

 if ($host ~* ^www.domain.tld$) {
 rewrite ^(.*)$ https://domain.tld$1 permanent;
 }
# -- www to no www --

location / {
 try_files $uri $uri/ /index.php?$query_string;   }

location ^~ /nested-app {alias $base/nested-app;

        if (!-e $request_filename) {
            rewrite ^ /nested-app/index.php last;        }

location ~ \.php$ {
            if (!-f $request_filename) {rewrite ^
            /nested-app/index.php last;
            } include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9003;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            }
  }
location ~ \.php$ { include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}


In our case:

  • domain.tld - running on the port 9000
  • domain.tld/nested-app - running on the port 9003


Depending on which php you need, specify the port in the settings of each, since both have a port specified by default 9000.

This is done in the configuration file:

  • For the main /etc/php-fpm.d/www.conf
  • For additional with php 7 /etc/opt/rh/rh-php73/php-fpm.d/www.conf

To apply the configuration, you must restart all services

systemctl restart rh-php73-php-fpm
systemctl restart php-fpm
systemctl restart nginx




No Comments Yet