1.安装主MySQL
添加MariaDB软件源
在/etc/yum.repos.d/目录中添加MariaDB.repo文件
1
2
3
4
5
6
7
8
9
10
11
12sudo vi /etc/yum.repos.d/MariaDB.repo
添加以下内容:
MariaDB 10.3 CentOS repository list - created 2018-11-24 14:39 UTC
http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
安装MariaDB(1分钟)
1
sudo yum install MariaDB-server MariaDB-client mariadb-devel MariaDB-shared -y
修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16vim /etc/my.cnf.d/server.cnf
在 [mysqld] 标签下方添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
vim /etc/my.cnf.d/client.cnf
在 [client] 标签下方添加
default-character-set=utf8
vim /etc/my.cnf.d/mysql-clients.cnf
在 [mysql] 标签下方添加
default-character-set=utf8启动
1
2
3
4设置开机自启
sudo systemctl enable mariadb
启动mariadb
sudo systemctl start mariadb设置root账号
1
2
3
4
5mysql_secure_installation
初始密码为空
修改密码为: mysql
后面的设置一路yes即可
登录测试: mysql -uroot -pPython客户端依赖
1
2需要使用mysql的python客户端库mysqlclient,安装依赖包
sudo yum install python-devel zlib-devel openssl-devel -y
2. 使用docker安装从MySQL
安装Docker
1
2
3
4
5
6
7
8
9
10
11
12
13安装docker依赖
yum install -y yum-utils device-mapper-persistent-data lvm2 -y
添加docker yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
下载并安装docker (2-3分钟)
yum install docker-ce docker-ce-cli containerd.io -y
启动docker
sudo systemctl start docker
测试docker
sudo docker run hello-world使用Docker安装MySQL(1分钟)
1
sudo docker pull mariadb:10.3.13
配置MySQL主从同步
配置MySQL master
1
2
3
4
5master 配置文件: /etc/my.cnf.d/server.cnf
vim /etc/my.cnf.d/server.cnf
在 [mysqld] 标签下方添加
log-bin=mysql-bin
server-id=1重启主mysql
1
systemctl restart mariadb
配置MySQL slave
1
2
3
4
5
6
7
8把master的配置拷过去作为从的配置
sudo cp -r /etc/my.cnf.d/ /opt/my.cnf.d-slave
修改从的配置
vim /opt/my.cnf.d-slave/server.cnf
在 [mysqld] 标签下方修改
server-id=10
read_only=on
port=8306启动docker容器mysql slave
1
sudo docker run --name mariadb -v /opt/my.cnf.d-slave:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=mysql -v /opt/mysql-slave-data:/var/lib/mysql -d --network=host mariadb:10.3.13
为mysql master添加用于同步复制的账号
1
2
3mysql -uroot -p
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' identified by 'slave';
FLUSH PRIVILEGES;查看mysql master状态
1
show master status;
设置mysql slave
1
2mysql -uroot -p -h 127.0.0.1 -P 8306
change master to master_host='127.0.0.1', master_user='slave', master_password='slave',master_log_file='mysql-bin.000001', master_log_pos=637;注意
1
2
3
4master_log_file要改成show master status中的文件名
master_log_pos要改成show master status中的Position值
start slave;
show slave status\G
从虚拟机中导出数据
1
mysqldump -uroot -pmysql --databases toutiao >/home/python/toutiao.sql
导入头条数据
1
2
3
4上传sql文件到服务器中(5分钟)
scp toutiao.sql root@39.108.69.171:/home/python
从sql文件中导入数据
mysql -uroot -p < toutiao.sql
3.安装redis
从虚拟机中上传redis压缩文件到服务器中
1
scp redis-4.0.13.tar.gz root@120.77.33.51:/root/
解压
1
tar -zxvf redis-4.0.13.tar.gz
安装
1
2
3
4
5
6安装gcc。 编译redis,需要用到gcc编译器
sudo yum install gcc -y
再装redis
cd redis-4.0.13
sudo make
sudo make install4.配置redis集群
从虚拟机中上传redis集群配置
1
2
3
4在服务器中创建redis目录, 用于存放redis配置
mkdir -p /etc/redis
在虚拟机中上传文件
scp /etc/redis/* root@120.77.33.51:/etc/redis安装Ruby
1
2
3
4
5
6
7
8
9
10下载ruby (1分钟)
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.gz
tar -zxvf ruby-2.6.1.tar.gz
cd ruby-2.6.1
配置(1分钟)
./configure
make 比较耗时(4分钟)
sudo make
sudo make install
gem install redis从服务器中上传redis启动脚本
1
scp /opt/redis-* root@39.108.69.171:/opt
创建存放redis数据库的文件夹
1
2
3
4
5
6
7
8mkdir -p /var/lib/redis/7000
mkdir -p /var/lib/redis/7001
mkdir -p /var/lib/redis/7002
mkdir -p /var/lib/redis/7003
mkdir -p /var/lib/redis/7004
mkdir -p /var/lib/redis/7005
mkdir -p /var/lib/redis/6381
mkdir -p /var/lib/redis/6380启动redis集群实例
1
2
3
4先给脚本执行权限
chmod +x /opt/redis-cluster-start.sh
执行命令
/opt/redis-cluster-start.sh启动cluster集群
1
2
3
4拷贝redis-trib.rb命令到搜索路径下
sudo cp /home/python/redis-4.0.13/src/redis-trib.rb /usr/local/bin/
创建集群
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005测试redis集群
1
redis-cli -c -p 7000
5.redis主从
使用脚本启动redis主从
1
2
3
4添加执行权限
chmod +x /opt/redis-replication-start.sh
执行脚本,启动主从redis、哨兵
/opt/redis-replication-start.sh测试
1
redis-cli -p 6381
6.安装rabbitmq
安装erlang
1
2
3
4
5
6
7
8
9
10
11
12
13
14在/etc/yum.repos.d/目录中添加rabbitmq_erlang.repo文件
sudo vim /etc/yum.repos.d/rabbitmq_erlang.repo
在文件中添加
In /etc/yum.repos.d/rabbitmq_erlang.repo
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/21/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
执行安装(测试时安装1分钟,实际安装,需要换源)
sudo yum install erlang -y安装RabbitMQ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
在/etc/yum.repos.d/目录中添加rabbitmq_server.repo文件
vim /etc/yum.repos.d/rabbitmq_server.repo
在文件中添加
In /etc/yum.repos.d/rabbitmq_server.repo
[bintray-rabbitmq-server]
name=bintray-rabbitmq-rpm
baseurl=https://dl.bintray.com/rabbitmq/rpm/rabbitmq-server/v3.7.x/el/7/
gpgcheck=0
repo_gpgcheck=0
enabled=1
执行安装(实际安装1分钟)
sudo yum install rabbitmq-server -y启动
1
2sudo chkconfig rabbitmq-server on
sudo systemctl start rabbitmq-server.service头条项目账号设置
1
2
3
4sudo rabbitmqctl add_user python rabbitmqpwd
sudo rabbitmqctl add_vhost toutiao
sudo rabbitmqctl set_user_tags python administrator
sudo rabbitmqctl set_permissions -p toutiao python ".*" ".*" ".*"
7.安装Elasticsearch5
安装wget
1
sudo yum install wget -y
下载jdk 官网密码: 13423682841_qinzhiWang
1
2
3
4下载页:
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
下载前得先创建账号
选中xxx-linux-x64.rpm 或者 xxx-linux-x64.tar.gz安装
1
rpm -ivh /home/python/jdk-8u221-linux-x64.rpm
安装后查看java版本
1
java -version
设置python登录时的环境变量
1
2
3vim ~/.bash_profile
在最后添加
export JAVA_HOME=/usr/java/default安装Elasticsearch
1
2
3
4
5
6
7
8
9
10
11
12
13rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在/etc/yum.repos.d/ 中编辑elasticsearch5x.repo
vim /etc/yum.repos.d/elasticsearch5x.repo
添加以下内容
[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md执行安装(7分钟, 实测30秒)
1
sudo yum install elasticsearch -y
启动
1
2
3
4设置开机启动
sudo systemctl enable elasticsearch.service
启动elasticsearch
sudo systemctl start elasticsearch.service测试
1
curl localhost:9200/_cluster/health?pretty
8.安装中文分词器
上传ik文件
1
scp elasticsearch-analysis-ik-5.6.16.zip root@120.77.33.51:/root
安装
1
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///home/python/elasticsearch-analysis-ik-5.6.16.zip
重新启动elasticsearch
1
sudo systemctl restart elasticsearch
测试
1
2
3
4
5
6
7
8
9
10
11curl -X GET 127.0.0.1:9200/_analyze?pretty -d '
{
"analyzer": "standard",
"text": "我是&中国人"
}'
curl -X GET 127.0.0.1:9200/_analyze?pretty -d '
{
"analyzer": "ik_max_word",
"text": "我是&中国人"
}'9.安装git
安装
1
yum install git -y
拉取项目代码(python)
1
git clone ****
10.Logstach安装(换成脚本导入import_es_data.py)
安装Logstach
1
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
添加Logstach yum源
1
2
3
4
5
6
7
8
9
10vi /etc/yum.repos.d/logstash.repo
输入内容
[logstash-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md安装 (超慢,提前安装)
1
sudo yum install logstash -y
安装插件
1
2
3cd /usr/share/logstash/bin/
sudo ./logstash-plugin install logstash-input-jdbc
sudo ./logstash-plugin install logstash-output-elasticsearch上传mysql-connector到服务器
1
scp mysql-connector-java-8.0.13.tar.gz python@120.78.184.139:/home/python
解压
1
tar -zxvf mysql-connector-java-8.0.13.tar.gz
执行脚本导入数据到elasticsearch
1
/home/python/toutiao-backend/scripts/import_elasticsearch_data.sh
使用import_es_data.py导入es数据
1
2
3workon toutiao
cd /home/python/toutiao-backend
python scripts/import_es_data.py
11.安装虚拟环境
安装虚拟环境(使用python 用户)
1
2sudo pip install virtualenv
sudo pip install virtualenvwrapper配置虚拟环境命令环境
1
2
3
4
5
6创建目录用来存放虚拟环境
mkdir $HOME/.virtualenvs
打开~/.bash_profile文件,并添加如下
vim ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh测试
1
2重连
workon安装python3
下载
1
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
解压
1
2xz -d Python-3.6.4.tar.xz
tar -xf Python-3.6.4.tar安装sqlite3(一定要先装,再装python,不然会报各种未知错误)
1
yum install sqlite-devel -y
编译&安装
1
2
3
4
5
6cd Python-3.6.4
配置
./configure prefix=/usr/local/python3
编译&安装(2分钟)
make
sudo make install配置python3命令可以搜索
1
2
3vi ~/.bash_profile
添加以下内容
export PATH=$PATH:/usr/local/python3/bin/
创建头条虚拟环境
1
mkvirtualenv -p python3 toutiao
安装依赖
1
2
3
4workon toutiao
cd /home/python/toutiao-backend
大概(5分钟)
pip install -r requirements.txt
12.配置项目代码
git配置永久保存密码
1
git config --global credential.helper store
创建logs日志文件夹
1
mkdir -p /home/python/logs
运行代码测试
1
2
3export FLASK_APP=toutiao.main
export FLASK_ENV=production
flask run测试
1
curl localhost:5000
使用脚本运行代码
1
2
3
4sh /home/python/toutiao-backend/scripts/toutiao_app.sh
加上可执行权限
chmod +x /home/python/toutiao-backend/scripts/toutiao_app.sh
/home/python/toutiao-backend/scripts/toutiao_app.sh
14.安装supervisord
安装
1
pip install supervisor
生成默认配置文件
1
echo_supervisord_conf > /etc/supervisord.conf
修改/etc/supervisord.conf 文件
1
2
3
4
5
6
7
8
9vim /etc/supervisord.conf
;[include]
;files = relative/directory/*.ini
改成
[include]
files = /home/python/etc/supervisor/*.conf
创建存放supervisor配置文件夹
mkdir -p /home/python/etc/supervisor/启动supervisord(使用python用户)
1
supervisord -c /etc/supervisord.conf
配置项目由supervisord管理
1
2
3
4
5
6
7
8创建toutiao_app_supervisor软链接
ln -s /home/python/toutiao-backend/scripts/toutiao_app_supervisor.conf /home/python/etc/supervisor/toutiao_app_supervisor.conf
创建im_supervisor软链接
ln -s /home/python/toutiao-backend/scripts/im_supervisor.conf /home/python/etc/supervisor/im_supervisor.conf
创建apscheduler_supervisor软链接
ln -s /home/python/toutiao-backend/scripts/apscheduler_supervisor.conf /home/python/etc/supervisor/apscheduler_supervisor.conf给命令加上执行权限
1
2
3chmod +x /home/python/toutiao-backend/scripts/im.sh
chmod +x /home/python/toutiao-backend/scripts/toutiao_app.sh
chmod +x /home/python/toutiao-backend/scripts/apscheduler.sh重新加载配置
1
supervisorctl reload
测试
1
curl localhost:5000
15.nginx
安装
1
yum install nginx -y
启动
1
nginx
创建nginx日志文件夹(python用户)
1
mkdir -p /home/python/logs/nginx
配置反向代理访问项目
添加toutiao配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18vim /etc/nginx/conf.d/toutiao.conf
upstream toutiao-api-backend {
server localhost:5000;
}
server {
listen 80;
server_name toutiao.com;
access_log /home/python/logs/nginx/toutiao.com.access.log main;
location / {
client_max_body_size 5M;
proxy_pass http://toutiao-api-backend;
proxy_set_header X-Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header Connection '';
proxy_http_version 1.1;
}
}添加im配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19vim /etc/nginx/conf.d/im.conf
upstream im-backend {
server localhost:7777;
}
server {
listen 80;
server_name toutiao_im.com;
access_log /home/python/logs/nginx/toutiao_im.com.access.log main;
location /socket.io/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header User-Agent $http_user_agent;
proxy_pass http://im-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}重新加载配置
1
nginx -s reload
配置本地hosts指向服务器
1
2
3vim /etc/hosts
120.77.33.51 toutiao.com
120.77.33.51 toutiao_im.com测试 - 关注测试
1
toutiao.com
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 zoubinbf@163.com