You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目迁移至虚拟主机后Redis连接超时问题求助

Troubleshooting Redis Connection Timeout in Laravel on Virtual Host

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 bind directive—if it’s set to 127.0.0.1, that should work, but some virtual hosts use isolated network environments where 127.0.0.1 doesn’t point to the Redis server’s context.
  • Ensure protected-mode is set to no (temporarily, for testing) and restart Redis with sudo 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:

  1. Find your Redis socket path (usually /var/run/redis/redis.sock or /tmp/redis.sock—check redis.conf for the unixsocket directive).
  2. Update your Laravel .env file to use the socket instead of TCP:
    REDIS_SCHEME=unix
    REDIS_PATH=/var/run/redis/redis.sock
    REDIS_PASSWORD=null
    REDIS_DB=0
    
  3. Make sure your config/database.php has 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),
        ],
    ],
    
  4. Clear Laravel’s config cache with php artisan config:cache and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:43:08