понедельник, 28 октября 2019 г.

Linux Ubuntu 18.04 Use redis for cache pages and PHP sessions storage

apt update -y && apt install redis-server php-redis

/etc/php/7.2/fpm/pool.d/user.conf
php_admin_value[session.save_handler] = redis
php_admin_value[session.save_path] = "tcp://127.0.0.1:6379?persistent=1&weight=1&database=2"

phpinfo should display redis info on the page.
There is link https://github.com/phpredis/phpredis#php-session-handler for save_path parameters

The checking script is 

<?php

  $redisinstance = new Redis();
  $redisinstance->connect("127.0.0.1", 6379);
  $result = $redisinstance->get("test");

  if ($result) {
      echo $result;
  } else {
      echo "No matching key found. Refresh the browser to add it!";
      $redisinstance->set("test", "Successfully retrieved the data!") or die("Couldn't save anything to redis...");
  }
?>
After refreshing page

redis-cli
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> keys "*"
1) "test"
127.0.0.1:6379> get "test"
"Successfully retrieved the data!"

пятница, 25 октября 2019 г.

Nginx inclusive locations processing

https://habr.com/ru/company/oleg-bunin/blog/313666/

Short explanation afterwards

https://www.zagirov.name/post/nginx-config/

manual - http://nginx.org/ru/docs/http/ngx_http_core_module.html#location

Example: how to open phpinfo.php only for 1.2.3.4 address

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/dev/shm/php-user72.sock;                
                deny all;
        }

        location ^~ /phpinfo {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/dev/shm/php-user72.sock;

                allow 1.2.3.4;
                deny all;
        }

Prefix location with ^~ disables first regexp location.

четверг, 24 октября 2019 г.

Linux Ububntu 18.04 Use memcached for PHP session storage

https://devdocs.magento.com/guides/v2.3/config-guide/memcache/memcache_ubuntu.html

apt update -y && apt install php-memcached memcached
/etc/php/7.2/fpm/php.ini
session.save_handler = "memcached"
session.save_path = "127.0.0.1:11211"

or /etc/php/7.2/fpm/pool.d/user.conf
php_admin_value[session.save_path] = '127.0.0.1:11211'
php_admin_value[session.save_handler] = 'memcached'

phpinfo should display memcached info on the page.

Check working with a script cache-test.php

PHP Nginx error "No input file specified." display on a page, and FastCGI sent in stderr: "Unable to open primary script: *.php (Operation not permitted)" in nginx log

/var/log/nginx/error.log

FastCGI sent in stderr: "Unable to open primary script: /var/www/html/phpinfo.php (Operation not permitted)

Don't forget to check open_basedir parameter

fastcgi_param PHP_ADMIN_VALUE   "open_basedir=/var/www/html/:/tmp/";

среда, 23 октября 2019 г.

Nginx testing proxy_cache_valid parameter

Firstly, I created proxy pass for the site. Test location is there for testing cache.

# the site
http {
    ...
    proxy_cache_path /tmp/nginx-cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
}
server {
    listen 8080;
    ...
    access_log /var/log/nginx/access.log;
    location /test {
        return 200 “It’s now $time_local\n”;
    }
}
# the proxy
server {
    listen 80 default_server;
    root /var/www/;
    index index.html index.htm;

    server_name example.com;

    charset utf-8;

    location / {
        include proxy_params;
        proxy_pass http://localhost:8080;
        proxy_cache my_cache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 3;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;
        proxy_cache_valid 200 15s;
    }
}
proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

The cache server is already confgured to cache responses with status code 200 for 15 seconds. This typically results in cache updates every 15 or 16 seconds.
proxy_cache_valid 200 15s;
Once per second, we send an HTTP request to the address. The response does not change until the caches on the server expire. This happens every 15 or 16 seconds

$ while sleep 1; do curl http://example.com/test; done
...
It’s now 23/Oct/2019:15:11:12 +0000
It’s now 23/Oct/2019:15:11:12 +0000
It’s now 23/Oct/2019:15:11:12 +0000
It’s now 23/Oct/2019:15:11:28 +0000
It’s now 23/Oct/2019:15:11:28 +0000
It’s now 23/Oct/2019:15:11:28 +0000
^C

We can also inspect the logs on the origin server to confrm that it is receiving
a request only every 15 or 16 seconds

/var/log/nginx/access.log
...
127.0.0.1 - - [23/Oct/2019:15:11:12 +0000] "GET /test HTTP/1.0" 200 38 "-" "curl/7.58.0"
127.0.0.1 - - [23/Oct/2019:15:11:28 +0000] "GET /test HTTP/1.0" 200 38 "-" "curl/7.58.0"

Apache2.4 with PHP7.2-FPM on Ubuntu 18.04

I have installed PHP7.2-FPM on Ubuntu 18.04.
For enabling it in Apache2.4, I run those commands.

a2enmod proxy_fcgi
a2enconf php7.2-fpm

PHP sock file is /dev/shm/php-user7.2.sock
So FilesMatch block have to be added to virtualhost.

<FilesMatch \.php$>
  SetHandler "proxy:unix:/dev/shm/php-user7.2.sock|fcgi://localhost"
</FilesMatch>

or replaced it in /etc/apache2/conf-available/php7.2-fpm.conf

<FilesMatch ".+\.ph(ar|p|tml)$">
  SetHandler "proxy:unix:/dev/shm/php-user72.sock|fcgi://localhost"
</FilesMatch>