--- title: 'Redis Server建立過程' disqus: hackmd --- Redis Server建立過程 ===  [TOC] ## 安裝系統 略,可參考Linux筆記或 https://hackmd.io/@3obs7uo2R0ywhgjPUysZZg/HJ91a49CV ## 安裝必要程式 安裝PHP Mysql Redis Nginx ```gherkin= yum -y install git zip unzip nginx mariadb-server mariadb redis gcc libevent-devel yum -y install php72w-devel php72w-fpm php72w-cli php72w-mbstring php72w-mysqlnd php72w-pdo php72w-opcache php72w-sodium php72w-dom php72w-gd php72w-intl php72w-pecl-imagick php72w-pecl-memcached php72w-pecl-apcu php72w-pecl-redis php72w-bcmath ``` ## 手動安裝 ### 如果無法連網需要手動安裝包 Redis release: http://download.redis.io/releases/ 安裝rpm檔案:  ### 如果無法自動安裝PHP 7.2 可能需要安裝epel repo來源 ```gherkin= rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm ``` ### 如果出現zmalloc錯誤 >zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory 解法: >make MALLOC=libc :::info 在Cent OS7 安裝Redis: https://blog.csdn.net/u010177412/article/details/81780844 https://blog.yowko.com/centos7-redis-service/ [在 CentOS7/RHEL7 上架設 Redis Server](http://linux.onlinedoc.tw/2016/06/centos7rhel7-redis-server.html) ::: ## Nginx啟動 ```gherkin= Redis release: http://download.redis.io/releases/ systemctl start nginx systemctl enable nginx ``` ## MariaDB啟動 ```gherkin= systemctl start mariadb systemctl enable mariadb mysql_secure_installation ``` ## 修改防火牆 ```gherkin= firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=public --add-port=443/tcp --permanent firewall-cmd --zone=public --add-port=6379/tcp --permanent firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload ``` 若有其他port要開給redis做多instance,依樣畫葫蘆開port ## Redis設定 連線IP 密碼 ```gherkin= vim /etc/redis.conf port 6379 bind 0.0.0.0 requirepass [密碼] ``` >修改為0.0.0.0代表不限制連入IP,密碼可自訂 ## 查看Redis是否啟動? ```gherkin= redis-cli -h 127.0.0.1 -p 6379 ``` 預設連接埠6379,若要開啟多instance,防火牆也要設定開啟 :star: 注意: 若要redis重啟之後不讀取舊資料(比如dump rdb檔案),要將檔案移除後再重啟,redis重啟之後資料應該是清空的 ## 複製redis設定 ```gherkin= cp /etc/redis/redis.conf redis6380.conf ``` ## 修改Redis設定 ```gherkin= vim /etc/redis6380.conf ``` >daemonize yes >pidfile路徑不變 >port 6380 >bind 0.0.0.0 >logfile /var/log/redis/redis-server6380.log >dbfilename dump6380.rdb >slaveof 127.0.0.1 6379(#注意這條一定要加上#) >requirepass [密碼] :wq存檔跳出 看從節點你是否需要每個instance開不同資料夾,並且把conf, pidfile andredis-server /etc/redis/redis6380.confs服務 ### 修改 /etc/sysctl.conf 加入 >vm.overcommit_memory=1 >net.core.somaxconn= 1024 #連線數 啟用設定 >sysctl vm.overcommit_memory=1 ### 重啟Redis ```gherkin= systemctl start redis systemctl enable redis ``` 如果開啟服務出現問題,檢查redis.conf檔案,可能路徑設置錯誤導致找不到服務 ### 修改檔案與資料夾的權限:chown ``` chown redis:redis /etc/redis/redis.conf chown redis:redis /var/log/redis.log chown -R redis:redis /var/lib/redis/6379/ ``` ## 開啟從節點redis slave服務 ```gherkin= redis-server /etc/redis/redis6380.conf ``` ## 測試redis master設定 ```gherkin= redis-cli -p 6379 ``` 若成功應該會進入命令行畫面: 127.0.0.1:6379> 輸入: >ping 應該會出現 >PONG 的回應結果,代表設置成功 ## 測試redis slave設定 開啟新的terminal視窗 ```gherkin= redis-cli -p 6380 ``` 若成功應該會進入命令行畫面: 127.0.0.1:6380> ## 測試redis master >127.0.0.1:6379>set test 123 >OK > ## 測試redis slave >127.0.0.1:6380>get test >"123" > ## 建議redis.conf配置 ```gherkin= port:63799 //目的:簡單的修改預設埠是最好的防禦攻擊 requirepass //密碼 rdbcompression no //關閉壓縮【硬碟最夠,降低cpu的能耗更利於提升效能】 daemonize yes //開啟守護程序:【master開啟守護,增加穩定性】 protect-mode no //允許他機器訪問 bind 0.0.0.0 logfile pidfile //修改log地址,pid地址和資料儲存地址:【便於維護和安全】 slowlog-log-slower-than 500 //新增慢查詢:【根據業務需求,便於優化】 maxmemory 1024MB //最大記憶體限制:【考慮穩定性和效能,一般不超過最大記憶體的60%】 ``` ## nginx.conf設定範例 ```gherkin= server { listen 80; listen 443 ssl; server_name risk.fitiapp.com; root /usr/share/nginx/html; index index.php; location / { # Redirect everything that isn't a real file to index.php try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_buffer_size 128k; fastcgi_buffers 256 16k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; include fastcgi_params; } # Media: images, icons, video, audio, HTC location ~* \.(?:js|css|png|jpg|jpeg|gif|ico|html|woff|woff2|ttf|svg|eot|otf)$ { expires 1M; access_log off; add_header Cache-Control "public"; } location ~ /\.(ht|svn|git) { deny all; } } ``` 可能還有地方要微調,日後看環境需要修改 ## Appendix and FAQ :::info **Redis優化配置** redis集群实战:https://blog.51cto.com/navyaijm/1706715 [在 CentOS / RHEL 7 上,架設多個 Redis 服務 !!](http://linux.onlinedoc.tw/2017/05/centos-rhel-7-redis.html) ::: Leave a comment! ###### tags: `redis` `php` `nginx` `mariadb` `centos`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up