Ref: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-ubuntu-18-04
Install Redis Server
|
1 2 3 |
sudo add-apt-repository ppa:chris-lea/redis-server sudo apt-get update sudo apt-get install redis-server |
Then modify /etc/redis/redis.conf file. In the file, find the supervised directive. Currently, this is set to no. Since we are running an operating system that uses the systemd init system, we can change this to systemd:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
. . . # 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 . . . |
Next, find the dir directory. This option specifies the directory that Redis will use to dump persistent data. We need to pick a location that Redis will have write permission and that isn’t viewable by normal users.
We will use the /var/lib/redis directory for this, which we will create in a moment:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
. . . # The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir /var/lib/redis . . . |
Save and close the file when you are finished.
Create a Redis systemd Unit File
Next, we can create a systemd unit file so that the init system can manage the Redis process.
Create and open the /etc/systemd/system/redis.service file to get started:
|
1 |
$ sudo gedit /etc/systemd/system/redis.service |
Inside, we can begin the [Unit] section by adding a description and defining a requirement that networking be available before starting this service. In the [Service] section, we need to specify the service’s behavior. For security purposes, we should not run our service as root. We should use a dedicated user and group, which we will call redis for simplicity. We will create these momentarily.
To start the service, we just need to call the redis-server binary, pointed at our configuration. To stop it, we can use the Redis shutdown command, which can be executed with the redis-cli binary. Also, since we want Redis to recover from failures when possible, we will set the Restart directive to “always”. Finally, in the [Install] section, we can define the systemd target that the service should attach to if enabled (configured to start at boot):
<DONT USE THIS SETTING>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[Unit] Description=Redis In-Memory Data Store After=network.target [Service] User=redis Group=redis ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target |
</DONT USE THIS SETTING>
<USE THIS SETTING>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[Unit] Description=Redis In-Memory Data Store After=network.target [Service] Type=forking ExecStart=/usr/bin/redis-server /etc/redis/redis.conf ExecStop=/bin/kill -s TERM $MAINPID PIDFile=/var/run/redis/redis-server.pid ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid" TimeoutStopSec=0 Restart=always User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=2755 [Install] WantedBy=multi-user.target |
</USE THIS SETTING>
Save and close the file when you are finished. After saving and closing the file, I reloaded the systemd manager configuration:
|
1 |
sudo systemctl daemon-reload |
Create the Redis User, Group and Directories
Now, we just have to create the user, group, and directory that we referenced in the previous two files.
Begin by creating the redis user and group. This can be done in a single command by typing:
|
1 2 |
teddy@teddy:~$ sudo adduser --system --group --no-create-home redis The system user `redis' already exists. Exiting. |
Now, we can create the /var/lib/redis directory by typing:
|
1 2 |
teddy@teddy:~$ sudo mkdir /var/lib/redis mkdir: cannot create directory ‘/var/lib/redis’: File exists |
We should give the redis user and group ownership over this directory:
|
1 |
teddy@teddy:~$ sudo chown redis:redis /var/lib/redis |
Adjust the permissions so that regular users cannot access this location:
|
1 |
teddy@teddy:~$ sudo chmod 770 /var/lib/redis |
Start and Test Redis
Now, we are ready to start the Redis server.
Start the Redis Service
|
1 |
sudo systemctl start redis |
Check that the service had no errors by running:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
teddy@teddy:~$ sudo systemctl status redis ● redis.service - Redis In-Memory Data Store Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled) Active: active (running) since Sen 2019-02-11 21:58:44 WIB; 4s ago Process: 23216 ExecStartPost=/bin/sh -c echo $MAINPID > /var/run/redis/redis.pid (code=exited, status=0/SUCCESS Process: 23211 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS) Main PID: 23212 (redis-server) CGroup: /system.slice/redis.service └─23212 /usr/bin/redis-server 127.0.0.1:6379 Peb 11 21:58:44 teddy systemd[1]: Starting Redis In-Memory Data Store... Peb 11 21:58:44 teddy systemd[1]: Started Redis In-Memory Data Store. |
I got the below error previously because the wrong setting in the redis.service file.
|
1 |
sudo systemctl status redis |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
redis.service - Redis In-Memory Data Store Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled) Active: failed (Result: start-limit-hit) since Sen 2019-02-11 21:48:28 WIB; 8s ago Process: 21490 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=203/EXEC) Process: 21488 ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf (code=exited, status=203/EXEC) Main PID: 21488 (code=exited, status=203/EXEC) Peb 11 21:48:28 teddy systemd[1]: redis.service: Unit entered failed state. Peb 11 21:48:28 teddy systemd[1]: redis.service: Failed with result 'exit-code'. Peb 11 21:48:28 teddy systemd[1]: redis.service: Service hold-off time over, scheduling restart. Peb 11 21:48:28 teddy systemd[1]: Stopped Redis In-Memory Data Store. Peb 11 21:48:28 teddy systemd[1]: redis.service: Start request repeated too quickly. Peb 11 21:48:28 teddy systemd[1]: Failed to start Redis In-Memory Data Store. Peb 11 21:48:28 teddy systemd[1]: redis.service: Unit entered failed state. Peb 11 21:48:28 teddy systemd[1]: redis.service: Failed with result 'start-limit-hit'. |
Check it on the terminal
|
1 2 3 |
teddy@teddy:~$ sudo netstat -lnp | grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 23212/redis-server tcp6 0 0 ::1:6379 :::* LISTEN 23212/redis-server |
Or make sure port 6379 only be using by redis
|
1 2 3 |
teddy@teddy:~$ sudo netstat -lnp | grep ':6379' tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 23212/redis-server tcp6 0 0 ::1:6379 :::* LISTEN 23212/redis-server |
Test the Redis Instance Functionality
To test that your service is functioning correctly, connect to the Redis server with the command-line client:
|
1 2 3 4 |
teddy@teddy:~$ redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> |
Check that you can set keys by typing:
|
1 2 3 4 5 6 |
teddy@teddy:~$ redis-cli 127.0.0.1:6379> set test "It's working" OK 127.0.0.1:6379> get test "It's working" 127.0.0.1:6379> exit |
Enable Redis to Start at Boot
If all of your tests worked, and you would like to start Redis automatically when your server boots, you can enable the systemd service.
To do so, type:
|
1 2 |
teddy@teddy:~$ sudo systemctl enable redis Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service. |
Go to your magento root directory and open app/etc/env.php and add the below codes just before the last line.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
'cache' => array ( 'frontend' => array ( 'default' => array ( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => array ( 'server' => '127.0.0.1', 'port' => '6379', 'persistent' => '', 'database' => '0', 'force_standalone' => '0', 'connect_retries' => '1', 'read_timeout' => '10', 'automatic_cleaning_factor' => '0', 'compress_data' => '1', 'compress_tags' => '1', 'compress_threshold' => '20480', 'compression_lib' => 'gzip', ), ), 'page_cache' => array ( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => array ( 'server' => '127.0.0.1', 'port' => '6379', 'persistent' => '', 'database' => '1', 'force_standalone' => '0', 'connect_retries' => '1', 'read_timeout' => '10', 'automatic_cleaning_factor' => '0', 'compress_data' => '0', 'compress_tags' => '1', 'compress_threshold' => '20480', 'compression_lib' => 'gzip', ), ), ), ), |
Restart the Redis server
|
1 |
sudo service redis-server restart |
Check whether Redis server is working or not. Open your website and view some pages then run the command
|
1 |
sudo redis-cli info |
If you see this keyspace. Then your Redis is configured properly. If you do not see anything below Keyspace then Redis have not cached anything. In this case check your configuration
|
1 2 |
# Keyspace db0:keys=196,expires=24,avg_ttl=13891985 |
To flush the Redis cache run the following command
|
1 |
sudo redis-cli flushdb |
Now your site is optimized with Varnish and Redis.
Benchmark
When I open https://fontaineind.test/ for the first time (varnish after fresh restart still caching so the page load still slower and redis also after first restart and flushdb command executed). I got this from redis-cli info:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
teddy@teddy:~$ sudo redis-cli info # Server redis_version:5.0.3 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:45d60903d31a0894 redis_mode:standalone os:Linux 4.15.0-38-generic x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:5.4.0 process_id:23212 run_id:0398df44fdf5d42687203813f129a4ad0753aca8 tcp_port:6379 uptime_in_seconds:2206 uptime_in_days:0 hz:10 configured_hz:10 lru_clock:6395458 executable:/usr/bin/redis-server config_file:/etc/redis/redis.conf # Clients connected_clients:1 client_recent_max_input_buffer:2 client_recent_max_output_buffer:20504 blocked_clients:0 # Memory used_memory:2132688 used_memory_human:2.03M used_memory_rss:7667712 used_memory_rss_human:7.31M used_memory_peak:2988272 used_memory_peak_human:2.85M used_memory_peak_perc:71.37% used_memory_overhead:572870 used_memory_startup:512456 used_memory_dataset:1559818 used_memory_dataset_perc:96.27% allocator_allocated:2655832 allocator_active:3239936 allocator_resident:7098368 total_system_memory:16707932160 total_system_memory_human:15.56G used_memory_lua:37888 used_memory_lua_human:37.00K used_memory_scripts:0 used_memory_scripts_human:0B number_of_cached_scripts:0 maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction allocator_frag_ratio:1.22 allocator_frag_bytes:584104 allocator_rss_ratio:2.19 allocator_rss_bytes:3858432 rss_overhead_ratio:1.08 rss_overhead_bytes:569344 mem_fragmentation_ratio:3.70 mem_fragmentation_bytes:5597904 mem_not_counted_for_evict:0 mem_replication_backlog:0 mem_clients_slaves:0 mem_clients_normal:49694 mem_aof_buffer:0 mem_allocator:jemalloc-5.1.0 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1549899310 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:1269760 aof_enabled:0 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:0 # Stats total_connections_received:81 total_commands_processed:3895 instantaneous_ops_per_sec:0 total_net_input_bytes:3117796 total_net_output_bytes:9256043 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:1897 keyspace_misses:400 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:159 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:master connected_slaves:0 master_replid:928ea24939c31a88a14f9f496bb1822da1d17828 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # CPU used_cpu_sys:1.361721 used_cpu_user:1.720340 used_cpu_sys_children:0.002643 used_cpu_user_children:0.013073 # Cluster cluster_enabled:0 # Keyspace db0:keys=196,expires=24,avg_ttl=13891985 |
Then I refresh the same page (varnish cache is kicking off and redis as well). I noticed the page load faster than only from varnish. But not much. Probably from 10-12 seconds to 8-9 seconds. Here is the output from redis-cli info:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
teddy@teddy:~$ sudo redis-cli info # Server redis_version:5.0.3 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:45d60903d31a0894 redis_mode:standalone os:Linux 4.15.0-38-generic x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:5.4.0 process_id:23212 run_id:0398df44fdf5d42687203813f129a4ad0753aca8 tcp_port:6379 uptime_in_seconds:2249 uptime_in_days:0 hz:10 configured_hz:10 lru_clock:6395501 executable:/usr/bin/redis-server config_file:/etc/redis/redis.conf # Clients connected_clients:1 client_recent_max_input_buffer:2 client_recent_max_output_buffer:0 blocked_clients:0 # Memory used_memory:2132688 used_memory_human:2.03M used_memory_rss:7323648 used_memory_rss_human:6.98M used_memory_peak:2988272 used_memory_peak_human:2.85M used_memory_peak_perc:71.37% used_memory_overhead:572870 used_memory_startup:512456 used_memory_dataset:1559818 used_memory_dataset_perc:96.27% allocator_allocated:2508448 allocator_active:3112960 allocator_resident:6602752 total_system_memory:16707932160 total_system_memory_human:15.56G used_memory_lua:37888 used_memory_lua_human:37.00K used_memory_scripts:0 used_memory_scripts_human:0B number_of_cached_scripts:0 maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction allocator_frag_ratio:1.24 allocator_frag_bytes:604512 allocator_rss_ratio:2.12 allocator_rss_bytes:3489792 rss_overhead_ratio:1.11 rss_overhead_bytes:720896 mem_fragmentation_ratio:3.54 mem_fragmentation_bytes:5253840 mem_not_counted_for_evict:0 mem_replication_backlog:0 mem_clients_slaves:0 mem_clients_normal:49694 mem_aof_buffer:0 mem_allocator:jemalloc-5.1.0 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:12 rdb_bgsave_in_progress:0 rdb_last_save_time:1549899310 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:1269760 aof_enabled:0 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:0 # Stats total_connections_received:118 total_commands_processed:4554 instantaneous_ops_per_sec:0 total_net_input_bytes:3180153 total_net_output_bytes:14480169 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:2473 keyspace_misses:404 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:159 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:master connected_slaves:0 master_replid:928ea24939c31a88a14f9f496bb1822da1d17828 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # CPU used_cpu_sys:1.420002 used_cpu_user:1.755142 used_cpu_sys_children:0.002643 used_cpu_user_children:0.013073 # Cluster cluster_enabled:0 # Keyspace db0:keys=196,expires=24,avg_ttl=14559389 |
I need to analyze these later.