--- slideOptions: transition: slide theme: night tags: Redis, Configure, Tutorial type: title: Install redis --- <style> .reveal section img { background: transparent!important; border: none!important; box-shadow: none!important; } </style> # Install ## On MacOS Type below: ```bash= brew update brew install redis ``` To have launchd start redis now and restart at login: ```bash= brew services start redis ``` to stop it, just run: ```bash= brew services stop redis ``` Or, if you don't want/need a background service you can just run: ```bash= redis-server /usr/local/etc/redis.conf ``` Test if Redis server is running: ```bash= redis-cli ping ``` If it replies “PONG”, then it’s good to go! Location of Redis configuration file. >/usr/local/etc/redis.conf Uninstall Redis and its files. ```bash= brew uninstall redis rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist ``` ---- ## On Windows server <b><u>Note</u></b>: The Redis project does not officially support Windows. However, the Microsoft Open Tech group develops and maintains this Windows port targeting Win64. Official is [here](https://redis.io/download) You can choose to download different versions or the latest version of Redis is [here](https://github.com/microsoftarchive/redis/releases) 1. Download either .msi or .zip file, this tutorial will let you download latest zip file [Redis-x64-3.2.100.zip](https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.zip) 2. Extract the zip file to prepared directory ![](https://i.imgur.com/EATtVPz.png) 3. Run <b>redis-server.exe</b>, you can either directly run redis-server.exe by clicking or run via command prompt ![](https://i.imgur.com/Ab8aFIj.png) 4. Run <b>redis-cli.exe</b>, after successfully running the redis-server. You can access it and test commands by running redis-cli.exe ![](https://i.imgur.com/nEjjD1Z.png) <b>PING</b> command is used to test if a connection is still alive. ![](https://i.imgur.com/jcGvrB5.png) You can now start using Redis , please refer for more [commands in official documentations](https://redis.io/commands) ---- ## On Ubuntu ### Step 1 — Installing and Configuring Redis In order to get the latest version of Redis, we will use ```apt``` to install it from the official Ubuntu repositories. Update your local ```apt``` package cache and install Redis by typing: ```bash= sudo apt update sudo apt install redis-server ``` This will download and install Redis and its dependencies. Following this, there is one important configuration change to make in the Redis configuration file, which was generated automatically during the installation. Open this file with your preferred text editor: ```bash= sudo nano /etc/redis/redis.conf ``` Inside the file, find the ```supervised``` directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The ```supervised``` directive is set to ```no``` by default. Since you are running Ubuntu, which uses the systemd init system, change this to ```systemd```: ```bash= . . . # If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: # supervised no - no supervision interaction # supervised upstart - signal upstart by putting Redis into SIGSTOP mode # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET # supervised auto - detect upstart or systemd method based on # UPSTART_JOB or NOTIFY_SOCKET environment variables # Note: these supervision methods only signal "process is ready." # They do not enable continuous liveness pings back to your supervisor. supervised systemd . . . ``` That’s the only change you need to make to the Redis configuration file at this point, so save and close it when you are finished. Then, restart the Redis service to reflect the changes you made to the configuration file: ```bash= sudo systemctl restart redis.service ``` With that, you’ve installed and configured Redis and it’s running on your machine. Before you begin using it, though, it’s prudent to first check whether Redis is functioning correctly. ### Step 2 — Testing Redis As with any newly-installed software, it’s a good idea to ensure that Redis is functioning as expected before making any further changes to its configuration. We will go over a handful of ways to check that Redis is working correctly in this step. Start by checking that the Redis service is running: ```bash= sudo systemctl status redis ``` If it is running without any errors, this command will produce output similar to the following: ```bash= Output ● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2018-06-27 18:48:52 UTC; 12s ago Docs: http://redis.io/documentation, man:redis-server(1) Process: 2421 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS) Process: 2424 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS) Main PID: 2445 (redis-server) Tasks: 4 (limit: 4704) CGroup: /system.slice/redis-server.service └─2445 /usr/bin/redis-server 127.0.0.1:6379 . . . ``` Here, you can see that Redis is running and is already enabled, meaning that it is set to start up every time the server boots. <div style="background-color:#f4f2d4; text-align:left; vertical-align: middle; padding:15px 20px 15px 15px;"> <b>Note</b>: This setting is desirable for many common use cases of Redis. If, however, you prefer to start up Redis manually every time your server boots, you can configure this with the following command: ```bash= sudo systemctl disable redis ``` </div> To test that Redis is functioning correctly, connect to the server using the command-line client: ```bash= redis-cli ``` In the prompt that follows, test connectivity with the ```ping``` command: ```bash= 127.0.0.1:6379> ping ``` ```bash= Output PONG ``` This output confirms that the server connection is still alive. Next, check that you’re able to set keys by running: ```bash= 127.0.0.1:6379> set test "It's working!" ``` ```bash= Output OK ``` Retrieve the value by typing: ```bash= 127.0.0.1:6379> get test ``` ```bash= Output "It's working!" ``` After confirming that you can fetch the value, exit the Redis prompt to get back to the shell: ```bash= 127.0.0.1:6379> exit ``` As a final test, we will check whether Redis is able to persist data even after it’s been stopped or restarted. To do this, first restart the Redis instance: ```bash= sudo systemctl restart redis ``` Then connect with the command-line client once again and confirm that your test value is still available: ```bash= redis-cli ``` ```bash= 127.0.0.1:6379>get test ``` The value of your key should still be accessible: ```bash= Output "It's working!" ``` Exit out into the shell again when you are finished: ```bash= 127.0.0.1:6379> exit ``` With that, your Redis installation is fully operational and ready for you to use. However, some of its default configuration settings are insecure and provide malicious actors with opportunities to attack and gain access to your server and its data. The remaining steps in this tutorial cover methods for mitigating these vulnerabilities, as prescribed by the [Official Redis website](http://redis.io/topics/security). Although these steps are optional and Redis will still function if you choose not to follow them, it is <i>strongly</i> recommended that you complete them in order to harden your system’s security. # Binding to localhost By default, Redis is only accessible from localhost. However, if you installed and configured Redis by following a different tutorial than this one, you might have updated the configuration file to allow connections from anywhere. This is not as secure as binding to localhost. To correct this, open the Redis configuration file for editing: ```bash= /etc/redis/redis.conf ``` * On MacOS: ```vi /etc/redis/redis.conf``` * On Ubuntu: ```sudo nano /etc/redis/redis.conf``` Locate this line and make sure it is uncommented (remove the # if it exists): ```bind 127.0.0.1 ::1``` Save and close the file when finished Then, restart the service to ensure that systemd reads your changes: * On MacOS: ```brew services restart redis``` * On Ubuntu: ```sudo systemctl restart redis``` ---- # Configuring a Redis Password Configuring a Redis password enables one of its two built-in security features — the ```auth``` command, which requires clients to authenticate to access the database. The password is configured directly in Redis’s configuration file, ```/etc/redis/redis.conf```, so open that file again with your preferred editor: ```bash= /etc/redis/redis.conf ``` Scroll to the ```SECURITY``` section and look for a commented directive that reads: ```bash= # requirepass foobared ``` Uncomment it by removing the ```#```, and change ```foobared``` to a secure password. <div style="background-color:#f4f2d4; text-align:left; vertical-align: middle; padding:15px 20px 15px 15px;"> <b>Note</b>: Above the ```requirepass``` directive in the ```redis.conf``` file, there is a commented warning: ```bash= # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # ``` Thus, it’s important that you specify a very strong and very long value as your password. Rather than make up a password yourself, you can use the ```openssl``` command to generate a random one, as in the following example. By piping the output of the first command to the second ```openssl``` command, as shown here, it will remove any line breaks produced by that the first command: ```bash= openssl rand 60 | openssl base64 -A ``` Your output should look something like: ```bash= Output RBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE ``` After copying and pasting the output of that command as the new value for requirepass, it should read: ```bash= /etc/redis/redis.conf requirepass RBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE ``` </div> After setting the password, save and close the file, then restart Redis: * On MacOS: ```brew services restart redis``` * On Ubuntu: ```sudo systemctl restart redis``` To test that the password works, access the Redis command line: ```bash= redis-cli ``` The following shows a sequence of commands used to test whether the Redis password works. The first command tries to set a key to a value before authentication: ```bash= 127.0.0.1:6379> set key1 10 ``` That won’t work because you didn’t authenticate, so Redis returns an error: ```bash= Output (error) NOAUTH Authentication required. ``` The next command authenticates with the password specified in the Redis configuration file: ```bash= 127.0.0.1:6379> auth your_redis_password ``` Redis acknowledges: ```bash= Output OK ``` After that, running the previous command again will succeed: ```bash= 127.0.0.1:6379> set key1 10 ``` ```bash= Output OK ``` ```get key1``` queries Redis for the value of the new key. ```bash= 127.0.0.1:6379> get key1 10 ``` ```bash= Output "10" ``` After confirming that you’re able to run commands in the Redis client after authenticating, you can exit the ```redis-cli```: ```bash= 127.0.0.1:6379> quit ``` ---- # Renaming Dangerous Commands The other security feature built into Redis involves renaming or completely disabling certain commands that are considered dangerous. When run by unauthorized users, such commands can be used to reconfigure, destroy, or otherwise wipe your data. Like the authentication password, renaming or disabling commands is configured in the same ```SECURITY``` section of the ```/etc/redis/redis.conf``` file. Some of the commands that are considered dangerous include: **FLUSHDB**, **FLUSHALL**, **KEYS**, **PEXPIRE**, **DEL**, **CONFIG**, **SHUTDOWN**, **BGREWRITEAOF**, **BGSAVE**, **SAVE**, **SPOP**, **SREM**, **RENAME**, and **DEBUG**. This is not a comprehensive list, but renaming or disabling all of the commands in that list is a good starting point for enhancing your Redis server’s security. Whether you should disable or rename a command depends on your specific needs or those of your site. If you know you will never use a command that could be abused, then you may disable it. Otherwise, it might be in your best interest to rename it. To enable or disable Redis commands, open the configuration file once more: ```bash= /etc/redis/redis.conf ``` <div style="background-color:#fedee1; text-align:left; vertical-align: middle; padding:15px 20px 15px 15px;"> **Warning**: The following steps showing how to disable and rename commands are examples. You should only choose to disable or rename the commands that make sense for you. You can review the full list of commands for yourself and determine how they might be misused at [redis.io/commands](https://redis.io/commands). </div> To disable a command, simply rename it to an empty string (signified by a pair of quotation marks with no characters between them), as shown below: ```bash= . . . # It is also possible to completely kill a command by renaming it into # an empty string: # rename-command FLUSHDB "" rename-command FLUSHALL "" rename-command DEBUG "" . . . ``` To rename a command, give it another name as shown in the examples below. Renamed commands should be difficult for others to guess, but easy for you to remember: ```bash= . . . # rename-command CONFIG "" rename-command SHUTDOWN SHUTDOWN_MENOT rename-command CONFIG ASC12_CONFIG . . . ``` Save your changes and close the file. After renaming a command, apply the change by restarting Redis: * On MacOS: ```brew services restart redis``` * On Ubuntu: ```sudo systemctl restart redis``` ```bash= redis-cli ``` ```bash= 127.0.0.1:6379> auth your_redis_password ``` ```bash= Output OK ``` ```bash= 127.0.0.1:6379> config get requirepass ``` ```bash= Output (error) ERR unknown command 'config' ``` Calling the renamed command, however, will be successful. It is not case-sensitive: ```bash= 127.0.0.1:6379> asc12_config get requirepass ``` ```bash= Output 1) "requirepass" 2) "your_redis_password" ``` Finally, you can exit from ```redis-cli```: ```bash= 127.0.0.1:6379> exit ``` Note that if you’re already using the Redis command line and then restart Redis, you’ll need to re-authenticate. Otherwise, you’ll get this error if you type a command: <div style="background-color:#fedee1; text-align:left; vertical-align: middle; padding:15px 20px 15px 15px;"> Regarding the practice of renaming commands, there’s a cautionary statement at the end of the ```SECURITY``` section in ```/etc/redis/redis.conf``` which reads: ```bash= . . . # Please note that changing the name of commands that are logged into the # AOF file or transmitted to replicas may cause problems. . . . ``` **Note**: *The Redis project chooses to use the terms “master” and “slave,” while DigitalOcean generally prefers the alternatives “primary” and “secondary.” In order to avoid confusion we’ve chosen to use the terms used in the Redis documentation here*. That means if the renamed command is not in the AOF file, or if it is but the AOF file has not been transmitted to slaves, then there should be no problem. So, keep that in mind when you’re trying to rename commands. The best time to rename a command is when you’re not using AOF persistence, or right after installation, that is, before your Redis-using application has been deployed. When you’re using AOF and dealing with a master-slave installation, consider[this answer from the project’s GitHub issue page](https://github.com/redis/redis/issues/2783). The following is a reply to the author’s question: >The commands are logged to the AOF and replicated to the slave the same way they are sent, so if you try to replay the AOF on an instance that doesn’t have the same renaming, you may face inconsistencies as the command cannot be executed (same for slaves). </div>