Monday, February 3, 2020

PHP-FPM, Apache virtual host from different mounts

I was stuck in scenario where I had to inter-operate both Ubuntu and Windows.

Most of my PHP projects were in Ubuntu and some few in Windows. I found it difficult because if you want to switch to the other project, then you have to switch the OS.

To mitigate this, I created a folder which is a common one accessible from both OS. From Windows its a D drive and all I could do was edit some apache config and it loads the projects from this web root.

But in case of Ubuntu, there was the apache already with default /var/www
It did not like moving out of the document root to a different point that too along with php-fpm.
I badly needed this as I was running different projects based on 5.6. 7.1 and 7.2.

After hitting Google a large number of times, finally managed to get this work.

In order to help myself and anyone who needs this future, this is a little note.

  • Setup virtual host in apache as similar to other ones
  • Change the apache default user and group to your system user and group:
  • Change the php-fpm user and group similar to apache:
    • /etc/php/7.1/fpm/pool.d/www.conf
    • /etc/php/7.1/fpm/php-fpm.conf
    • systemctl reload php7.1-fpm.service
  • And finally the virtual host example:
<VirtualHost *:80>
        ServerAdmin webmaster@kauripizzeria.test
        ServerName kauripizzeria.test
        ServerAlias kauripizzeria.test
        #DocumentRoot /var/www/html/raas
        DocumentRoot /media/bubi/SHARED/workspace/raas/kauri-pizzeria/public
        ErrorLog /media/bubi/SHARED/workspace/raas/error.log
        CustomLog /media/bubi/SHARED/workspace/raas/access.log combined

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

        <Proxy fcgi://kauripizzeria.test>
            ProxySet connectiontimeout=5 timeout=240
        </Proxy>

        <directory "/media/bubi/SHARED/workspace/raas/kauri-pizzeria/public">
        #<directory "/var/www/html/appexert/sekure/crm">
            DirectoryIndex index.php index.html
            Options Indexes FollowSymLinks Includes ExecCGI
            AllowOverride All
            Allow from All
            Require all granted
        </directory>
</VirtualHost>