How to configure Laravel to use PhpRedis? - laravel

PHP 7.3
Laravel 5.8
Until now I was using Predis for my cache in the Laravel project. Now I want to switch to PhpRedis. I've read it's really simple (just config changes), but I have a lot of problems. I don't know what to begin with, so I'll write all what I know.
My hosting provider claims that PhpRedis is enabled.
The code below executed in a controller (Predis is set) works fine - I receive the set value.
$redis = new \Redis();
$redis->connect( 'socket path', 0 );
$redis->set('test', 'testValue');
print_r( $redis->get('test') );
However, the same code in the raw PHP file executed via SSH returns "Uncaught Error: Class 'Redis' not found in..."
Let's go to changes in config/database.php. Here is my configuration:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'/*'phpredis'*/),
'cluster' => true,
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'/*'redis'*/),
'prefix' => Str::slug(env('APP_NAME'), '_').'_',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],
'default' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_CACHE_DB', 0)
],
(...) // other
],
When I change values to these in comments, my website shows just a blank page - any errors in the mailbox.
Furthermore, when I run for example "php73 artisan config:clear" in the SSH, console returns "Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension." in the Illuminate/Redis/Connectors/PhpRedisConnector.php.
When I change the alias in config/app.php from "Redis" to "RedisManager" and try again it returns
Uncaught Error: Class 'Redis' not found in /path/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:70.
What's going on? How to set Laravel's configuration to use PhpRedis? Maybe it's my hosting provider issue? Thanks in advance for every advice.
If I missed some important code, give me a sign - I will add it.

The PHPRedis libraries are not installed by default in a shared hosting environment, and are generally not part of a PHP installation by default. You would have to ask your host to install these libraries within their shared hosting platform.

Related

Laravel memcached not working in shared hosting

I've been trying to change the cache driver (CACHE_DRIVER) from file to memcached in production (shared host) where Memcached is set correctly and available according to phpinfo. Running still on Laravel 7.4.x
So far I've done the following:
On the .env file I've changed the CACHE_DRIVER=file to CACHE_DRIVER=memcached;
Checked the configuration in the config/cache.php as below:
<?PHP
'stores' => [
'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'), // not required I think?...
'sasl' => [
env('MEMCACHED_USERNAME'), // not required I think?...
env('MEMCACHED_PASSWORD'), // not required I think?...
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000, // Ok
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'), // set like in phpinfo
'port' => env('MEMCACHED_PORT', 11211), // not required I think?...
'weight' => 100, // Ok
],
],
],
// ....
],
Ran php artisan config:clear and php artisan cache:clear;
Checked for existing cache tag. e.g. cache()->has('countries') and its false;
Changed back to CACHE_DRIVER=file and started over. Result for cache()->has('countries') and its true
Either probably I'm missing something in the process, yet in local it's running on Memcached and for performance improvement, I'm trying to change it in production.
Thanks in advance for any inputs that might help me to solve this issue.
I won't delete the question (answering here) as it may help others facing the same problem on shared hostings.
As #apokryfos mentioned the extension may be there but the Memcached server might not be available in all shared hostings, which is my case.
If you have your projects on shared hosting and want to use Memcached, contact their support and ask if your plan contemplates the Memcached server. Otherwise, use other cache systems, namely and ultimately the file cache.

Laravel redis works, but list still empty

i'm currently using redis in laravel everything works fine, unless i can't check my redis's keys like normally
I went to "redis-cli " and type "keys *" it shows -empty list or set-
But my cache worked. I tested it with laravel debugbar.
i'm not sure, where and how to check all my redis's keys via command line,
here is my config/database for redis
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', 'helper_'),
],
thanks
...
Assuming you are using default configuration, and have CACHE_DRIVER set as redis, then you are in fact writing to Redis. However you are by default writing to the database with index 1
You can see these entries by calling SELECT 1 followed by KEYS * in redis-cli.
You can specify which database to write to in a connection in config/database.php. You can then select which connection to use by default in config/cache.php.
This is why changing to default works in Nazmus Shakib's answer, because the default connection outlined in config/database.php uses database 0 rather than database 1. Alternatively, you could change REDIS_CACHE_DB to 0 in .env. Or, you could just use database 1.
Need to modify the file: config/cache.php
Change the stores > redis > connection
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
],
to
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
And make sure your .env file has CACHE_DRIVER=redis

Send Mail Using Lumen

Mail.php
return [
'driver' =>'smtp',
'host' => 'smtp.gmail.com',
//'port' => 587,
'port' =>465,
//'encryption' =>'tls',
'encryption' =>'ssl',
'username' => 'xxxxxxxx#gmail.com',
'password' => 'xxxxxxx',
// 'sendmail' => '/usr/sbin/sendmail -bs',
'sendmail' => '/usr/sbin/sendmail -t',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Controller
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Error
Swift_TransportException Connection could not be established with host
smtp.gmail.com [A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
I Tried...
Change ssl / tls
Change the ports
Add "guzzlehttp/guzzle": "~5.3|~6.0" in composer.json
Add a new line in StreamBuffer.php
$options = array_merge($options, array('ssl' => array('verify_peer' =>
false,'verify_peer_name' => false,'allow_self_signed' => true )));
Please help .
Thank you.
1. Require illuminate/mail
Make sure you’re using the same version as your underlying framework (i.e. if you’re on Lumen v. 5.3, use composer require illuminate/mail "5.3.*").
composer require illuminate/mail "5.5.*"
2. Set up Lumen bootstrap/app.php
First, open your bootstrap.php and uncomment the following lines:
$app->withFacades();
$app->register(App\Providers\AppServiceProvider::class);
Also, add the following line below the last line you uncommented:
$app->configure('services');
This will allow you to define a ‘services’ config file and setup your mail service. Now I know that normally configuration is done in the .env file with Lumen, and we’ll use that shortly, but first we’ll need to write a small config file to map to the .env file.
3. Create your configuration files
Create a new folder at the root level of your install called config(if it doesn’t already exist). In the config folder, create two new files, one named services.php and another named **mail.php**.
In the services.php file paste the following:
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
];
Lastly, add the following to your .env file:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=<your-mailgun-domain>
MAILGUN_SECRET=<your-mailgun-api-key>
Make sure you replace those sneaky placeholders with your actual key and domain. If you’re not using Mailgun, you can always use the other mail providers Mail comes with; have a look at the docs if you plan on using a different provider, they are all easy to set up once you’re at this point.
4. Send Email!
To send an email, use one of the following in your classes (depending on your preference):
use Illuminate\Support\Facades\Mail;
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Finally, don’t forget to read the Laravel Mail docs for more info on how to use this great library.
Have you turned on 2 layers security in your Google account (email address you config in .env file) which uses to send email.

AWS ElastiCache Redis can't connect from Laravel nad from redis-cli

I'm having a problem connecting to ElastiCache Redis from Laravel application installed on EC2 instance or even using redis-cli from EC2 instance.
Laravel
I tried to use predis with configurations in database.php like
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => -1,
'timeout' => 0
],
],
and got 'Error while reading line from the server. [tcp:server here]'
I tried with phpRedis extension with same configurations only change 'client' => 'phpredis' and got error read error on connection {"exception":"[object] (RedisException(code: 0): read error on connection at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:69)
Redis cli
Using redis cli redis-cli -h host_here -p 6379 -a password_here I see prompt like host:6379> but typing any command throws error Error: Connection reset by peer
ElastiCache Redis configurations
My EC2 and elastic cache are in the same VPC and using telnet I can connect to redis instance
~$ telnet host 6379
Trying 172.31.23.113...
Connected to host.
Escape character is '^]'.
Thanks for any help!
I know this is pretty old but I was having the same issue myself. If anyone encounters this issue then see the solution here and here.
It seems that when you enable Encryption in-transit in AWS Elasticache it prevents you from using redis-cli as it doesn't support TLS connections. Switching to another client should work. This answer has a list of TLS enabled clients.
Edit:
Did some more digging and found that using stunnel you can wrap your connection of redis-cli with ssl. Here is a guide for doing it.
Related: Laravel + Redis Cache via SSL?
To which I've answered here: https://stackoverflow.com/a/48876398/663058
Relevant details below:
Since you have clustering and TLS then you'll need a different config entirely:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
// Note! for single redis nodes, the default is defined here.
// keeping it here for clusters will actually prevent the cluster config
// from being used, it'll assume single node only.
//'default' => [
// ...
//],
// #pro-tip, you can use the Cluster config even for single instances!
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
],
'options' => [ // Clustering specific options
'cluster' => 'redis', // This tells Redis Client lib to follow redirects (from cluster)
]
],
'options' => [
'parameters' => [ // Parameters provide defaults for the Connection Factory
'password' => env('REDIS_PASSWORD', null), // Redirects need PW for the other nodes
'scheme' => env('REDIS_SCHEME', 'tcp'), // Redirects also must match scheme
],
'ssl' => ['verify_peer' => false], // Since we dont have TLS cert to verify
]
]
Explaining the above:
'client' => 'predis': This specifies the PHP Library Redis driver to use (predis).
'cluster' => 'redis': This tells Predis to assume server-side clustering. Which just means "follow redirects" (e.g. -MOVED responses). When running with a cluster, a node will respond with a -MOVED to the node that you must ask for a specific key.
If you don't have this enabled with Redis Clusters, Laravel will throw a -MOVED exception 1/n times, n being the number of nodes in Redis cluster (it'll get lucky and ask the right node every once in awhile)
'clusters' => [...]: Specifies a list of nodes, but setting just a 'default' and pointing it to the AWS 'Configuration endpoint' will let it find any/all other nodes dynamically (recommended for Elasticache, because you don't know when nodes are comin' or goin').
'options': For Laravel, can be specified at the top-level, cluster-level, and node option. (they get combined in Illuminate before being passed off to Predis)
'parameters': These 'override' the default connection settings/assumptions that Predis uses for new connections. Since we set them explicitly for the 'default' connection, these aren't used. But for a cluster setup, they are critical. A 'master' node may send back a redirect (-MOVED) and unless the parameters are set for password and scheme it'll assume defaults, and that new connection to the new node will fail.
If you are using predis as client.
Then you can change Redis connection in config/database.php
'redis' => [
'client' => 'predis',
'default' => [
'scheme' => 'tls',
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
You can add 'scheme' in that.
I am using Laravel 5.8 And then everything works great within Laravel.
But yes, as Redis doesn't provide TLS connection so redis-cli will still not work.

Configure Memcached in Laravel

I've tried to set up memcached with my Laravel
Set Driver
/config/app.php
'default' => 'memcached',
Configure Memcache Server
'stores' => [
'database' => [
'driver' => 'database',
'table' => 'caches',
'connection' => null,
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
],
],
]
],
Cache
Route::get('cache/users', function() {
return Cache::remember('users', 60, function() {
return User::all();
});
});
How do I know I configure my caching with memcache properly ?
How do I see what I stored ?
First, you can use Cache::has('users') and Cache::get('users') in php artisan tinker or on a test route of some sort to see if stuff's being saved to the cache (and what the current contents are).
Second, you can connect to memcached (as well as other drivers like redis) via telnet and check directly.
telnet 127.0.0.1 11211
Once in, you can issue commands, like:
get users
which should spit out the contents of that cache key.
With the code you've shown, a non-working cache should also result in an exception. You can test this by turning off the memcached instance and refreshing the page - it should error out.
For the case when Memcache is on separate machine named memcached, i.e. docker with corresponding service: in .env set MEMCACHED_HOST=memcached

Resources