# 单机部署
::: tip
完全抽象出来的本地部署方式,不理解Docker的同学可以使用本文的进行参考
部署视频参考:
视频会在业余时间持续录制,更多视频可以参考Up主空间:https://space.bilibili.com/388245257/
- [部署安装教程](https://www.bilibili.com/video/BV1BL4y1a7TU/)
- [快速了解视频](https://www.bilibili.com/video/BV1rp4y1v7fa/)
- [二次开发教程](https://www.bilibili.com/video/BV1Sy4y137md/)
:::
## 环境准备
> 本地部署方式,采用pm2进行守护,这是针对一些不想玩Docker的同学准备的,如果你想容器你就分布式部署aaaaa!!!!!
> 另外这里没写域名管理是怎么部署的, 如果需要参考分部署部署!!!!
**建议配置**
- 系统: CentOS7+
- CPU: 4Core+
- 内存: 8G+
- 磁盘: 50G+
**基础环境**
- 版本约束
- Python3.6
- Redis3.2
- MySQl5.7
- RabbitMQ
- node.js
**优化系统**
注意:
- 如果你的系统是新的,我们建议你先优化下系统,同样我们也提供了[优化系统脚本](https://github.com/opendevops-cn/opendevops/tree/master/scripts/system_init_v1.sh)
- 以下基础环境中,若你的系统中已经存在可跳过,直接配置,建议使用我们推荐的版本
`Tips: 内部域名不要修改,不要修改,不要修改,都是内部通信!!!!!!!!! 不会真的有人不听劝???? ????`
**环境变量**
>创建项目目录
```
$ mkdir -p /opt/codo/ && cd /opt/codo/
```
> 以下内容贴入到`vim /opt/codo/env.sh`文件,主要修改配置地址和密码信息
```shell
echo -e "\033[31m 注意:token_secret一定要做修改,防止网站被攻击!!!!!!! \033[0m"
echo -e "\033[32m 注意:token_secret一定要做修改,防止网站被攻击!!!!!!! \033[0m"
echo -e "\033[33m 注意:token_secret一定要做修改,防止网站被攻击!!!!!!! \033[0m"
#部署的IP地址
export LOCALHOST_IP="10.10.10.12"
#设置你的MYSQL密码
export MYSQL_PASSWORD="m9uSFL7duAVXfeAwGUSG"
### 设置你的redis密码
export REDIS_PASSWORD="cWCVKJ7ZHUK12mVbivUf"
### RabbitMQ用户密码信息
export MQ_USER="ss"
export MQ_PASSWORD="5Q2ajBHRT2lFJjnvaU0g"
#codo-admin用到的cookie和token
export cookie_secret="nJ2oZis0V/xlArY2rzpIE6ioC9/KlqR2fd59sD=UXZJ=3OeROB"
# 这里codo-admin和gw网关都会用到,一定要修改。可生成随意字符
export token_secret="pXFb4i%*834gfdh963df718iodGq4dsafsdadg7yI6ImF1999aaG7"
##如果要进行读写分离,Master-slave主从请自行建立,一般情况下都是只用一个数据库就可以了
# 写数据库
export DEFAULT_DB_DBHOST="10.10.10.12"
export DEFAULT_DB_DBPORT='3306'
export DEFAULT_DB_DBUSER='codo'
export DEFAULT_DB_DBPWD=${MYSQL_PASSWORD}
#export DEFAULT_DB_DBNAME=${mysql_database}
# 读数据库
export READONLY_DB_DBHOST='10.10.10.12'
export READONLY_DB_DBPORT='3306'
export READONLY_DB_DBUSER='codo'
export READONLY_DB_DBPWD=${MYSQL_PASSWORD}
#export READONLY_DB_DBNAME=${mysql_database}
# 消息队列
export DEFAULT_MQ_ADDR='10.10.10.12'
export DEFAULT_MQ_USER=${MQ_USER}
export DEFAULT_MQ_PWD=${MQ_PASSWORD}
# 缓存
export DEFAULT_REDIS_HOST='10.10.10.12'
export DEFAULT_REDIS_PORT=6379
export DEFAULT_REDIS_PASSWORD=${REDIS_PASSWORD}
```
==**最后一定不要忘记source**:== `source /opt/codo/env.sh`
**关闭SELINUX**
- 若已关闭请跳过
```shell
#临时关闭
$ setenforce 0
#或修改配置文件关闭,需要重启
$ vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
设置后需要重启才能生效
```
**清空防火墙规则**
```shell
#只清空filter链即可
$ iptables -F
```
**安装Python3**
> 建议使用Python36,若你的系统里面已经存在Python36可以跳过此步骤。
```shell
echo -e "\033[32m [INFO]: Start install python3 \033[0m"
yum update -y
yum groupinstall Development tools -y
yum -y install zlib-devel
yum install -y openssl-devel libxslt-devel libxml2-devel libcurl-devel
yum install python3 -y
```
**安装MySQL**
> 一般来说一个MySQL实例即可,如果有需求可以自行搭建主从,微服务每个服务都可以有自己的数据库
- 必须使用MySQL 5.7,我这个安装下来应该是mysql5.6
所以需要你改一下安装好的mysql配置文件
```shell
source /opt/codo/env.sh #变量文件
yum install -y wget
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
```
```shell
cat >/etc/yum.repos.d/mysql-community.repo <<EOF
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Note: MySQL 5.7 is currently in development. For use at your own risk.
# Please read with sub pages: https://dev.mysql.com/doc/relnotes/mysql/5.7/en/
[mysql57-community-dmr]
name=MySQL 5.7 Community Server Development Milestone Release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF
```
```shell
yum install mysql-community-server -y
chkconfig mysqld on
service mysqld start
echo "info: start mysql grant user."
mysql -e "CREATE USER \"codo\"@\"localhost\" IDENTIFIED BY \"${MYSQL_PASSWORD}\";CREATE USER \"codo\"@\"%\" IDENTIFIED BY \"${MYSQL_PASSWORD}\""
mysql -e " GRANT ALL PRIVILEGES ON *.* TO \"codo\"@\"localhost\" IDENTIFIED BY \"${MYSQL_PASSWORD}\";GRANT ALL PRIVILEGES ON *.* TO \"codo\"@\"%\" IDENTIFIED BY \"${MYSQL_PASSWORD}\";GRANT ALL PRIVILEGES ON *.* TO \"codo\"@\"${HOSTNAME}\" IDENTIFIED BY \"${MYSQL_PASSWORD}\"; " 2>&1
```
- 测试 `mysql -h127.0.0.1 -ucodo -p${MYSQL_PASSWORD}`
**安装Redis**
- 若有请跳过
```shell
yum install redis -y
source /opt/codo/env.sh #变量文件
echo "requirepass ${REDIS_PASSWORD}" >> /etc/redis.conf
sed -i "s/^daemonize.*$/daemonize yes/g" /etc/redis.conf
#bind 127.0.0.1 如果是开启的请关闭掉
systemctl start redis
systemctl status redis
```
- 测试 `redis-cli -h 127.0.0.1 -p 6379 -a ${REDIS_PASSWORD}`
**安装RabbitMQ**
```shell
echo -e "\033[32m [INFO]: Start install rabbitmq \033[0m"
source /opt/codo/env.sh #变量文件
# echo $LOCALHOST_IP opendevops >> /etc/hosts
# echo opendevops > /etc/hostname
# export HOSTNAME=opendevops
yum install -y rabbitmq-server
rabbitmq-plugins enable rabbitmq_management
systemctl start rabbitmq-server
rabbitmqctl add_user ${MQ_USER} ${MQ_PASSWORD}
rabbitmqctl set_user_tags ${MQ_USER} administrator
rabbitmqctl set_permissions -p '/' ${MQ_USER} '.' '.' '.'
systemctl restart rabbitmq-server
systemctl enable rabbitmq-server
systemctl status rabbitmq-server
# rabbitmq-server -detached
status=`systemctl status rabbitmq-server | grep "running" | wc -l`
if [ $status == 1 ];then
echo -e "\033[32m [INFO]: rabbitmq install success. \033[0m"
else
echo -e "\033[31m [ERROR]: rabbitmq install faild \033[0m"
exit -5
fi
```
**安装DNS**
- 注意,这里如果你内部有自己DNS,你也可以选择使用你自己的, 若没有请继续安装
> 部署内部DNS dnsmasq 用于服务间内部通信,API网关需要配置,切记
```shell
echo -e "\033[32m [INFO]: Start install dnsmasq \033[0m"
yum install dnsmasq -y
# 设置上游DNS,毕竟你的D