Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

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>

Wednesday, October 30, 2013

Find Base Path using PHP

Find base url path and absolute URL

define('ABSPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');

$tempPath1 = explode('/', str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])));

for ($i = count($tempPath2); $i < count($tempPath1); $i++)
    array_pop ($tempPath3);

$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);

if ($urladdr{strlen($urladdr) - 1}== '/')
    define('URLADDR', 'http://' . $urladdr);
else
    define('URLADDR', 'http://' . $urladdr . '/');

unset($tempPath1, $tempPath2, $tempPath3, $urladdr);

Wednesday, October 9, 2013

Filter an array of objects using object's property

Iterate through the array and return true or false based on the condition

$new_array = array_filter($array, function($obj){
    if (isset($obj->admins)) {
        foreach ($obj->admins as $admin) {
            if ($admin->member == 11) return false;
        }
    }
    return true;
});

Monday, May 27, 2013

Delete all files inside a folder in PHP

$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
}

Finding Extension of a File in PHP

Simply use pathinfo() function

$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);

Sunday, March 17, 2013

MySQL - If Exists update else insert Query

INSERT INTO component_psar (tbl_id, row_nr, col_1, col_2, col_3, col_4, col_5, col_6, unit, add_info, fsar_lock)
VALUES('2', '1', '1', '1', '1', '1', '1', '1', '1', '1', 'N')
ON DUPLICATE KEY UPDATE col_1 = VALUES(col_1), col_2 = VALUES(col_2), col_3 = VALUES(col_3), col_4 = VALUES(col_4), col_5 = VALUES(col_5), col_6 = VALUES(col_6), unit = VALUES(unit), add_info = VALUES(add_info), fsar_lock = VALUES(fsar_lock)
The Checking key should be UNIQUE. Only if this query will work.
Refer : http://stackoverflow.com/questions/12639407/sql-if-exists-update-else-insert-syntax-error 

Save array as multiple fields in DB

Set id as an auto_increment primary key column in your table.
create table email (
    id int unsigned primary key auto_increment,
    cus_id int unsigned,
    email varchar(50)
);

insert into email (cus_id, email) values ('$cusid', '$email');
id will then take care of itself, all you have to do is leave it alone when inserting.
PHP code to do this in a loop:
foreach ($test as $cusid => $email) {
    $querystring = "insert into users (cus_id, email) values ('$cusid', '$email')";
    myqsli_query($querystring);
}