How to set password for Redis?

How to set password for Redis?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to set password for Redis?

Moving a Redis instance from a local development environment to a production server introduces a critical security requirement: authentication. While running locally, convenience often dictates leaving the server unsecured, but a live application demands protection against unauthorized access. Implementing a password involves two distinct steps: configuring the Redis server itself and updating your client code to handle that authentication.

Configuring Authentication in redis.conf

The source of confusion for many developers is locating the correct directive within the configuration file. You do not need to create new files or use obscure flags; the setting resides directly in redis.conf. To enable a password, you must uncomment or add the line defining the requirepass parameter:

# Commented by default
requirepass your-strong-secret-password-here

Once this directive is present and uncommented, Redis will reject all incoming connections that do not provide the correct credentials. It is important to choose a robust password string rather than a simple token like "mypassword". In a production environment, this configuration file should be owned by the redis user and have restrictive permissions (e.g., 600) to prevent unauthorized modification.

If you are using Docker or a managed service, the approach shifts slightly. Some container images expect the password to be passed via an environment variable rather than a config file, but for standard binary installations, editing redis.conf remains the standard procedure. Always restart the Redis service immediately after making changes to apply them.

Updating the Predis Client

On the client side, your PHP code needs to know how to authenticate before attempting any operations. You do not need to call an authentication command manually; the library handles the handshake automatically if you pass the credentials during initialization.

Using the Predis client, you can extend your existing server array to include the password:

$my_server = [
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'database' => 1,
    'password' => 'your-strong-secret-password-here' // Replace with your configured password
];

$client = new Predis\Client($my_server);

By passing the 'password' key in the configuration array, Predis will include the password in the initial connection attempt. If the Redis server requires a password and the client does not provide one, the connection will fail immediately with an authentication error. There is no need to add the password after connecting; it must be part of the connection string parameters.

Managing the Redis Service on Production

Finally, handling service management in a live environment often differs from local setups. The command redis-server stop fails because it attempts to treat "stop" as a configuration file path (hence the error: Fatal error, can't open config file 'stop'). This command syntax is only valid when explicitly specifying a config file, such as redis-server /path/to/redis.conf --daemonize no --shutdown.

On most Linux distributions, the standard way to manage system services without direct binary invocation is through the init scripts or systemd. If your live server uses Systemd (common on Ubuntu 16.04+, CentOS 7+, and modern distros), you should interact with the service directly:

sudo systemctl stop redis-server
sudo systemctl start redis-server

If your environment relies on SysV init scripts, the script is typically located in /etc/init.d/. You can verify availability by listing the scripts in that directory. Attempting to run redis-server directly without flags will only work if you are running it as a background process or if no daemon manager is active. Always ensure your service manager (systemd or init) is configured correctly before relying on standard stop/start commands.

Our network