1. 程式人生 > 實用技巧 >CentOS8本地安裝Redash中文版,並且配置為生產環境

CentOS8本地安裝Redash中文版,並且配置為生產環境

Centos8內建的Python為3.6.8版本,以下是在內建Python3.6.8基礎上的安裝步驟。由於安裝多版本Python會導致系統底層庫需要下載原始碼重新編譯,比較麻煩,不建議在多版本Python環境下安裝Redash中文版。

本地安裝Redash中文版

1、初始化

改國內源:
cd /etc/yum.repos.d/
sudo rm -f CentOS-Base.repo CentOS-AppStream.repo CentOS-PowerTools.repo CentOS-centosplus.repo CentOS-Extras.repo
sudo curl -o CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
sudo yum makecache

更新系統包:
sudo yum update

安裝常用包:
sudo yum -y install gcc gcc-c++ kernel-devel make zlib zlib-devel libffi-devel openssl-devel git

當前使用者新增到root組:
sudo usermod -aG root 當前centos使用者名稱

2、安裝PostgreSql9.6

安裝PostgreSql9.6初始化並配置為系統啟動時自動啟動:
sudo dnf install @postgresql:9.6
sudo dnf install postgresql-contrib
sudo yum install postgresql-devel
sudo postgresql-setup initdb
sudo systemctl enable --now postgresql

登入Postgresql:
sudo -u postgres psql

更改postges密碼:
alter user postgres with password '密碼';

建立角色:
create role 當前centos使用者名稱;

賦予登入許可權:
alter role 當前centos使用者名稱 login;

退出postgresql終端:
\q

3、安裝Redis

sudo yum install redis
sudo systemctl start redis
sudo systemctl enable redis.service 

4、安裝Nodejs

sudo yum install nodejs
sudo npm config set registry https://registry.npm.taobao.org
sudo npm install n -g
sudo /usr/local/bin/n stable

5、安裝Redash中文版

1、下載程式碼:
   git clone https://github.com/dazdata/redash.git
   cd redash
2、前端安裝依賴包:npm install
3、前端打包:npm run build
4、配置pip國內源:sudo cp pip.conf /etc/pip.conf
5、安裝Python虛擬環境:sudo pip3 install virtualenv
6、建立Python虛擬環境:virtualenv venv
7、啟用Python虛擬環境:source venv/bin/activate
8、安裝Pip包:
  pip install -r requirements.txt -r requirements_dev.txt -r requirements_bundles.txt
9、初始化資料庫表結構:./manage.py database create_tables
10、退出虛擬環境,安裝完成:deactivate

6、啟動

分別開啟三個終端,都執行cd redash進入目錄後分別執行下列三個命令之一:
source venv/bin/activate
./manage.py runserver --debugger --reload
和
source venv/bin/activate
./manage.py rq worker
和
source venv/bin/activate
./manage.py rq scheduler

開啟瀏覽器,輸入地址:http://localhost:5000

配置為生產環境

在此基礎上升級為Nginx+Supervisor+uWSGI的生產環境部署

一、安裝和測試uWSGI

1、安裝:
sudo yum -y install python36-devel
sudo pip3 install uwsgi

2、配置:
進入專案目錄:cd ~/redash
建立配置檔案:sudo nano uwsgi.ini
[uwsgi]
http=:5000
chdir=/home/當前centos使用者名稱/redash/
wsgi-file=redash/wsgi.py
callable=app
master=true
virtualenv=/home/當前centos使用者名稱/redash/venv/                       
pythonpath=/home/當前centos使用者名稱/redash/
processes=1
threads=2
儲存退出,先配置成http方式,便於從瀏覽器訪問,後續配合nginx將改為socket方式。
注意這只是基本配置,高階配置請參考uWSGI官網中文文件
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html

3、啟動:
uwsgi uwsgi.ini
進入瀏覽器輸入http://localhost:5000即可訪問,
注意其它worker和scheduler還是以終端方式進虛擬環境啟動。
執行無問題,證明已用uwsgi啟動了redash,先Ctrl+C停止uwsgi服務繼續進行後續配置。

二、安裝和測試Supervisor

1、安裝SuperVisor:
sudo pip3 install supervisor 

2、配置:
進入專案目錄:cd ~/redash
先用命令生成預設配置檔案:echo_supervisord_conf > supervisord.conf 
開啟配置檔案:sudo nano supervisord.conf 修改其中86/87行,去掉行頭註釋符;
[program:redash] 
command = uwsgi uwsgi.ini
在檔案結尾插入下列內容,用於啟動redash的worker和scheduler
[program:worker]
directory=/home/當前centos使用者名稱/redash/
command=venv/bin/python3 ./manage.py rq worker

[program:scheduler]
directory=/home/當前centos使用者名稱/redash/
command=venv/bin/python3 ./manage.py rq scheduler
儲存退出,注意這只是基本配置,高階配置請參考http://supervisord.org/

3、啟動
supervisord -c supervisord.conf
進入瀏覽器輸入http://localhost:5000即可訪問,注意這時worker和scheduler也已用supervisor進行管理,
不需要另外啟動。可用supervisorctl status檢視三個程序,執行無問題,
證明已用supervisor啟動了含uwsgi的redash全部程序。
先執行supervisorctl stop all停止程序(最好重啟系統),繼續後續配置。

三、安裝Nginx並全部啟動

1、安裝Nginx:
sudo yum install nginx

2、修改配置:sudo nano /etc/nginx/nginx.conf 將location /加入如下內容
location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:5000;
}
儲存退出

3、改uwsgi協議:
進入專案目錄:cd ~/redash
修改配置檔案:sudo nano uwsgi.ini的第2行並新增第3行
socket=127.0.0.1:5000
chmod-socket=666
儲存退出

4、啟動:
supervisord -c supervisord.conf
systemctl start nginx
進入瀏覽器輸入http://127.0.0.1:80能夠正常訪問即為安裝成功。