Sure, here’s a comprehensive step-by-step tutorial to set up Redis on an EC2 instance and connect it to a Laravel application.
- Log in to your AWS Management Console and navigate to the EC2 Dashboard.
- Launch a new instance:
- Choose an Amazon Machine Image (AMI): Select an Ubuntu Server AMI (e.g., Ubuntu Server 20.04 LTS).
- Choose an Instance Type: Select an instance type (e.g., t2.micro for testing).
- Configure Instance: Configure according to your needs.
- Add Storage: Keep the default settings or modify as needed.
- Add Tags: Optionally add tags.
- Configure Security Group: Create a new security group or select an existing one. Ensure you allow inbound traffic on port 22 (SSH) and port 6379 (default Redis port).
- Launch the instance and download the key pair (.pem file) if you don't already have one.
- Open your terminal (or use PuTTY on Windows).
- Change permissions for your key pair file:
chmod 400 your-key-pair.pem
- Connect to your instance:
ssh -i "your-key-pair.pem" ubuntu@your-ec2-public-dns
- Update the package list:
sudo apt-get update
- Install Redis:
sudo apt-get install redis-server
- Verify the installation:
redis-server --version
- Open the Redis configuration file:
sudo nano /etc/redis/redis.conf
- Configure Redis:
- Change the
bind
address to0.0.0.0
to allow connections from any IP address (or specify your IP range for more security):bind 0.0.0.0
- Set
supervised
tosystemd
:supervised systemd
- Change the
- Save and exit the file (
Ctrl+X
, thenY
, andEnter
).
- Restart the Redis service:
sudo systemctl restart redis.service
- Enable Redis to start on boot:
sudo systemctl enable redis.service
- Connect to Redis CLI:
redis-cli
- Run a simple test:
You should see:
set test "Redis is running" get test
"Redis is running"
Ensure your EC2 security group allows inbound traffic on port 6379:
- Go to the EC2 Dashboard > Instances > Select your instance.
- Click on the Security Group associated with the instance.
- Edit Inbound Rules to add a rule allowing TCP traffic on port 6379 from your IP address or range.
- Connect to your Laravel application's server and install the Redis PHP extension.
sudo apt-get update sudo apt-get install php-redis
- Restart your web server (e.g., Apache or Nginx) to load the new PHP extension.
sudo systemctl restart apache2 # or for Nginx sudo systemctl restart nginx
-
Open your Laravel configuration file for Redis located at
config/database.php
. -
Locate the Redis array configuration section and update it to match your Redis server settings:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_CACHE_DB', 1), ], ],
-
Update your environment file (
.env
) to include the Redis configuration:REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
-
Create a test route in
routes/web.php
:use Illuminate\Support\Facades\Redis; Route::get('/redis-test', function () { $redis = Redis::connection(); $redis->set('test_key', 'Redis is connected!'); return $redis->get('test_key'); });
-
Access the route in your browser by navigating to
http://your-laravel-app/redis-test
. You should seeRedis is connected!
.
-
Run Tinker:
php artisan tinker
-
Test Redis Commands:
$redis = Redis::connection(); $redis->set('test_key', 'Redis is connected via Tinker!'); echo $redis->get('test_key');
You should see the output:
Redis is connected via Tinker!
You have now set up Redis on an EC2 instance and configured your Laravel application to connect to it. This setup allows your Laravel application to utilize Redis for caching, session storage, and other purposes, providing improved performance and scalability.