rap2搭建,mysql,redis,nginx安裝,node環境安裝,rap2安裝

所需環境

1、mysql
2、redis
3、nginx安裝
4、npm/nodejs環境
5、rap2-delos端安裝
6、客戶端dolores環境搭建

1、mysql安裝

CentOS7的yum源中默認好像是沒有mysql的。爲了解決這個問題,我們要先下載mysql的repo源。

1. 下載mysql的repo源

$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2. 安裝mysql-community-release-el7-5.noarch.rpm包

   $ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

安裝這個包後,會獲得兩個mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。

3. 安裝mysql

$ sudo yum install mysql-server

根據步驟安裝就可以了,不過安裝完成後,沒有密碼,需要重置密碼。

4. 重置密碼

重置密碼前,首先要登錄

$ mysql -u root

登錄時有可能報這樣的錯:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),原因是/var/lib/mysql的訪問權限問題。下面的命令把/var/lib/mysql的擁有者改爲當前用戶:

$ sudo chown -R root:root /var/lib/mysql

5、修改my.cnf
vim /etc/my.cnf,修改的內容是:

[mysqld]
basedir=/home/mysql
datadir=/home/mysql/data
port=3306
skip-grant-tables
character_set_server=utf8
init_connect='SET NAMES utf8'
max_connections=500
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

max_allowed_packet=256M

[client]
default-character-set=utf8

#[mysqld_safe]
#log-error=/home/mysql/log/mariadb.log
#pid-file=/home/mysql/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

然後,重啓服務:

$ service mysqld restart

6、接下來登錄重置密碼:

$ mysql -u root      (直接Enter進入)
修改root的密碼
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:		2
Current database:	mysql
Current user:		root@
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.21 MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/tmp/mysql.sock
Uptime:			4 min 43 sec

Threads: 1  Questions: 42  Slow queries: 0  Opens: 127  Flush tables: 1  Open tables: 122  Queries per second avg: 0.148
--------------

mysql>

修改完成密碼之後將/etc/my.cnf中的skip-grant-tables註釋掉
然後重啓mysql,執行命令:service mysqld restart

7. 需要更改權限才能實現遠程連接MYSQL數據庫

可以通過以下方式來確認:

mysql -u root –p     #接下來輸入密碼:123456
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

如果在上面步驟出現類似如下的錯誤:

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

解決辦法是:

service mysqld stop
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

mysql -u root mysql
mysql>use mysql ;
mysql> UPDATE user SET Password=PASSWORD('123456') where USER='root' and host='127.0.0.l' or host='bigdata1' or host='localhost';//把空的用戶密碼都修改成非空的密碼就行了。(我將此處的newpassword 改成了123456)
mysql> FLUSH PRIVILEGES;
mysql> quit

然後service mysqld start
再次輸入以下,發現已經需要輸入密碼了
mysql -uroot -p
[root@bigdata1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> use mysql
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
mysql> flush privileges;

可以查看密碼信息

mysql> select host, user, password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| bigdata1  | root | 123456                                    |
| 127.0.0.1 | root | 123456                                    |
| ::1       | root | 123456                                    |
| localhost |      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| bigdata1  |      |                                           |
| %         | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------+-------------------------------------------+
7 rows in set (0.00 sec)

mysql>

至此mysql安裝完畢

2、安裝redis

參考地址:https://www.cnblogs.com/zuidongfeng/p/8032505.html

3、nginx安裝

安裝方式:https://blog.csdn.net/tototuzuoquan/article/details/78155700
3.1 安裝所需環境
Nginx 是 C語言 開發,建議在 Linux 上運行,當然,也可以安裝 Windows 版本,本篇則使用 CentOS 7 作爲安裝環境。
一. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

yum install gcc-c++

二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

yum install -y pcre pcre-devel

三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。

yum install -y zlib zlib-devel

四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
3.2 官網下載
1.直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html
在這裏插入圖片描述
2.使用wget命令下載(推薦)。
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
在這裏插入圖片描述
我下載的是1.10.1版本,這個是目前的穩定版。
3.3 解壓
依然是直接命令:

tar -zxvf nginx-1.10.1.tar.gz -C /home/bigdata/installed/
cd nginx-1.10.1

3.4 配置
其實在 nginx-1.10.1 版本中你就不需要去配置相關東西,默認就可以了。當然,如果你要自己配置目錄也是可以的。
1.使用默認配置

./configure

2.自定義配置(不推薦)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:將臨時文件目錄指定爲/var/temp/nginx,需要在/var下創建temp及nginx目錄
3.5 編譯安裝

make
make install

查找安裝路徑:

whereis nginx

在這裏插入圖片描述
3.6 啓動、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit:此方式停止步驟是待nginx進程處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
查詢nginx進程:
ps aux|grep nginx
3.7 重啓 nginx
1.先停止再啓動(推薦):
對 nginx 進行重啓相當於先停止再啓動,即先執行停止命令再執行啓動命令。如下:
./nginx -s quit
./nginx
2.重新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效需要重啓 nginx,使用-s reload不用先停止 ngin x再啓動 nginx 即可將配置信息在 nginx 中生效,如下:
./nginx -s reload
啓動成功後,在瀏覽器可以看到這樣的頁面:
在這裏插入圖片描述
3.8 開機自啓動
即在rc.local增加啓動代碼就可以了。
vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx (這裏在實際的機器上已經註釋掉了)
設置執行權限:
chmod 755 rc.local
在這裏插入圖片描述
到這裏,nginx就安裝完畢了,啓動、停止、重啓操作也都完成了,當然,你也可以添加爲系統服務,我這裏就不在演示了。

4、安裝npm/nodejs環境

參考地址:https://blog.csdn.net/micarlxm/article/details/81091284
下載的版本是:wget https://nodejs.org/dist/latest-v8.x/node-v8.15.1-linux-x64.tar.gz

5、安裝rap2-delos

1、下載服務端代碼
git clone https://github.com/thx/rap2-delos.git

配置文件

目錄:rap2-delos/src/config
文件:config.dev.ts;config.local.ts;config.prod.ts 請把三個文件都修改  請看下面註釋
修改:config.dev.ts文件中db對象中username,password參數與本地或者開發環境的數據庫信息匹配

上面三個 配置文件 config.*.tx 都要修改 主要是針對 mysql 做修改

import { IConfigOptions } from "../types";

let config: IConfigOptions = {
  version: '2.3',
  serve: {
    port: 8080,
  },
  keys: ['some secret hurr'],
  session: {
    key: 'rap2:sess',
  },
  db: {
    dialect: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'root',
    password: '123456',
    database: 'db_rap2_delos_app',
    pool: {
      max: 5,
      min: 0,
      idle: 10000,
    },
    logging: false,
  },
  redis: {
    host: '127.0.0.1',
    port: 6379
  }
}

export default config

三個文件都按照上面的文件進行配置

2、創建數據庫
自己根據上面定義的數據名字創建 我的是 RAP2_DELOS_APP

CREATE DATABASE IF NOT EXISTS db_rap2_delos_app
DEFAULT CHARSET utf8
COLLATE utf8_general_ci;

3、安裝依賴包

進入項目根目錄
# cd rap2-delos
安裝項目所需依賴
# npm install
全局安裝PM2   這是用來啓動服務端代碼的
# npm install -g pm2
安裝 TypeScript 編譯包
# npm install typescript -g

4、初始化數據庫

執行編譯  
# npm run build
如果上面編譯報錯  rimraf: command not found  則執行 npm install rimraf --save-dev -g  其他錯誤根據具體去處理

初始化數據庫  如果不執行上面的編譯 直接執行初始化數據庫會報錯
# npm run create-db
如果上面報錯 sh: cross-env: command not found  則先執行 npm install --save-dev cross-env -g

5、代碼規範檢查

# npm run check
如果上面報錯 sh: tslint: command not found  則先執行 npm install -g tslint typescript

6、啓動項目 下面兩種不同的啓動方式

開發模式 啓動開發模式的服務器 監視並在發生代碼變更時自動重啓 (第一次運行比較慢,請耐心等待)
# npm run dev

生產模式 啓動生產模式服務器
# npm start

沒報錯代表啓動了起來   如果啓動失敗,請確認node 已經是10版本以上,如果不是 請先切換版本  nvm use v10.15.0

7、查看是否正常啓動

1、執行命令 如下顯示則代表數據庫連接正常
pm2 log

DATABASE √
HOST localhost
PORT 3306
DATABASE RAP2_DELOS_APP

2、瀏覽器查看 ip:8080 請記得開放 8080 端口[下面代表啓動正常]

1、執行命令 如下顯示則代表數據庫連接正常
pm2 log

DATABASE √
HOST localhost
PORT 3306
DATABASE RAP2_DELOS_APP

2、瀏覽器查看 ip:8080 請記得開放 8080 端口[下面代表啓動正常]

RAP2後端服務已啓動,請從前端服務(rap2-dolores)訪問。 RAP2 back-end server is started, please visit via front-end service (rap2-dolores).

8、常見問題

1、執行npm run create-db命令,提示Unable to connect to the database:{ SequelizeAccessDeniedError: Access denied for user 'root'@'localhost' (using password:NO)}

    原因:未修改rap2-delos/src/config目錄下數據庫配置文件,或者是與文件中的數據庫信息與之連接的數據庫信息不匹配
解決方法:修改config.dev.ts文件數據庫配置信息

    如果修改正確無誤後,執行npm run create-db依舊出錯,那麼查看該項目中是否已經存在dist目錄,如果有,請按照如上修改對應的數據庫配置信息

2、執行npm run dev命令,提示Error: listen EADDRINUSE :::8080
    原因:8080 端口被佔用
    解決方法:殺掉佔用 8080 端口的應用


3、執行npm install 命令,提示hiredis 編譯無法通過
    原因:無權限操作rap2-delos/node_modules/hiredis路徑
    解決方法:sudo npm install
    如果提示sudo: npm: command not found,請參考 stackoverflow-npm,stackoverflow-node

4、執行npm run dev可以正常啓動,npm start命令無法正常啓動服務
    原因:請使用pm2 logs查看日誌具體定位
    
5、示例:由於 Redis 的安全模式,不能正常使用

    ReplyError: Ready check failed: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 

    1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
    2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
    3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
    4) Setup a bind address or an authentication password. 
    NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
解決方法: 使用--protected-mode no方式啓動
--------------------- 
作者:清悟 
來源:CSDN 
原文:https://blog.csdn.net/qq_16142851/article/details/85198925 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

7、客戶端dolores環境搭建

1、獲取源代碼

git clone https://github.com/thx/rap2-dolores.git
執行不成功 多執行幾次,或者直接在瀏覽器打開 https://github.com/thx/rap2-dolores.git 去下載壓縮包回來

2、配置文件

目錄:rap2-dolores/src/config
文件:config.dev.ts; 其中 dev,表示開發環境,其他同理
修改:config.dev.ts文件,serve地址是 服務端 rap2-delos 部署成功後的地址,默認:'http://localhost:8080'   可以不做修改

最終修改如下 配置文件如下 請注意 ip 那裏 不能使用 127.0.0.1 請使用服務器ip (可以嘗試使用localhost試試)
module.exports = {
serve: ‘http://ip:8080’,
keys: [‘some secret hurr’],
session: {
key: ‘koa:sess’
}
}
3、安裝項目依賴包

# npm install  【如果報下面錯誤】

錯誤

gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/data/wwwroot/rap2-dolores/node_modules/node-sass/.node-gyp'

錯誤解決

1、在項目根目錄創建.npmrc文件,複製下面代碼到該文件
phantomjs_cdnurl=http://cnpmjs.org/downloads
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
registry=https://registry.npm.taobao.org

2、刪除之前安裝node-sass包
# npm uninstall node-sass
3、重新安裝
# npm install -g node-sass

如果上面還是不成功還是報錯 
ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/data/wwwroot/rap2-dolores/node_modules/node-sass/.node-gyp'

安裝淘寶 npm
# npm install -g cnpm --registry=https://registry.npm.taobao.org
使用cnpm 安裝node-sass
# cnpm install -g node-sass

執行成功後 再執行npm install

4、編譯啓動項目

01、開發模式 自動監視改變後重新編譯
# npm run dev
備註:測試用例
# npm run test

02、生產模式 編譯 React 生產包
# npm run build

5、配置服務器 我使用的是nginx

使用 nginx 服務器路由到編譯產出的 build 文件夾作爲靜態服務器即可
 配置如下   8181 是我的前端頁面端口
 server {
    listen 8181;
    server_name _;
    
    root /data/wwwroot/rap2-dolores/build;
    index index.html index.htm;
 
    location / {
        try_files $uri /index.html;
    }
 
    error_page 500 502 503 504 /500.html;
}

重新加載配置文件
# systemctl reload nginx

訪問服務 ip:8181  成功

如果不成功 一直有圈圈在轉 請查看 rap2-dolores/src/config/config.dev.ts 文件 ip 必須是服務器的ip 不能是127.0.0.1

module.exports = {
  serve: 'http://ip:8080',
  keys: ['some secret hurr'],
  session: {
    key: 'koa:sess'
  }
}

配置完成後 重新運行 npm run build  即可
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章