Laravel项目迁移至虚拟主机后Redis连接超时问题求助
Hey Yeasir, this connection timeout error is super common when moving Laravel projects to virtual hosts—even if your Redis service is running, there are a handful of environment-specific roadblocks that could be blocking the connection. Let’s walk through the most likely fixes step by step:
1. Verify Redis is actually listening on 127.0.0.1:6379
First, let’s confirm Redis is accepting connections on the expected address/port from your virtual host’s environment. SSH into your virtual host and run these commands:
# Test TCP connection to Redis telnet 127.0.0.1 6379 # Or use netcat if telnet isn't installed nc -zv 127.0.0.1 6379
If either command fails, Redis isn’t listening on that port. Check your redis.conf file:
- Look for the
binddirective—if it’s set to127.0.0.1, that should work, but some virtual hosts use isolated network environments where127.0.0.1doesn’t point to the Redis server’s context. - Ensure
protected-modeis set tono(temporarily, for testing) and restart Redis withsudo systemctl restart redis-server.
2. Check if your virtual host blocks TCP connections to Redis
Many shared or managed virtual hosts disable TCP access to local services like Redis for security reasons. Instead, they require using a Unix socket to connect. To test this:
- Find your Redis socket path (usually
/var/run/redis/redis.sockor/tmp/redis.sock—checkredis.conffor theunixsocketdirective). - Update your Laravel
.envfile to use the socket instead of TCP:REDIS_SCHEME=unix REDIS_PATH=/var/run/redis/redis.sock REDIS_PASSWORD=null REDIS_DB=0 - Make sure your
config/database.phphas the socket configuration enabled in the Redis section:'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'default' => [ 'scheme' => env('REDIS_SCHEME', 'tcp'), 'path' => env('REDIS_PATH', null), 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], ], - Clear Laravel’s config cache with
php artisan config:cacheand test your site again.
3. Confirm the PHP Redis extension is installed and working
Even if it works locally, your virtual host’s PHP environment might be missing the Redis extension. Run this command to check:
php -m | grep redis
If you don’t see redis in the output, install the extension (contact your host if you’re on a shared plan, or use sudo apt install php-redis on Ubuntu-based VPS). You can also test it directly in Laravel with:
php artisan tinker > Redis::ping();
If this returns PONG, the extension is working; if not, you’ll need to resolve the extension issue first.
4. Check for virtual host process isolation
Some managed hosts use containers or virtualization where your Laravel app runs in a separate environment from the Redis service. In this case, 127.0.0.1 won’t point to the Redis server—you’ll need to use the internal IP address or hostname provided by your host for Redis. Reach out to your virtual host’s support team to get the correct connection details if this is the case.
5. Verify Redis credentials and permissions
Double-check that your .env file has the correct REDIS_PASSWORD (if you set one) and that the Redis user has permission to access the database you’re using. You can test this by connecting to Redis directly:
redis-cli > AUTH your-redis-password (if set) > SELECT 0 (or your database number) > KEYS *
If this works, the credentials are correct—so the issue is likely network-related.
内容的提问来源于stack exchange,提问作者Yeasir Arafat Majumder

