【服务器入门】快速配置轻量应用服务器(腾讯云)

服务器基本信息

轻量应用服务器(腾讯云):Ubuntu 18.04.1 LTS

https://console.cloud.tencent.com/lighthouse/instance

网络:

(公)x.x.x.x

(内)x.x.x.x

账号及密码:

root@x.x.x.x/xxxxxxxx

team@x.x.x.x/xxxxxxxx

用户管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建用户
root@VM-4-13-ubuntu:~# adduser team

# 修改密码
team@VM-4-13-ubuntu:~$ sudo passwd root

# 查看用户
root@VM-4-13-ubuntu:~# awk -F':' '{ print $1}' /etc/passwd

# 用户授予和root相同的权限
root@VM-4-13-ubuntu:~# vi /etc/sudoers
team ALL=(ALL:ALL) ALL

# 安装vsftpd
root@VM-4-13-ubuntu:~# apt install vsftpd

远程登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 确认开启端口22
(base) ☁ ~ telnet x.x.x.x 22

# 密码登录,默认情况下,root是禁止远程登录的
(base) ☁ ~ ssh team@x.x.x.x

# 进入服务器,建立密钥对
➜ ~ ssh-keygen -C "xxxxxxx@163.com"

# 安装公钥
➜ .ssh cat id_rsa.pub >> authorized_keys
➜ .ssh chmod 600 authorized_keys
➜ ~ chmod 700 .ssh

# 设置ssh
➜ ~ sudo vi /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
:wq!

# 另外在本配置文件下有如下选项
# 禁止密码登录
PasswordAuthentication no
# 允许root通过ssh登录
PermitRootLogin yes

# 登录本机
➜ ~ ssh localhost

# 远程登录
(base) ☁ ~ ssh -i .ssh/id_rsa team@x.x.x.x

# 重启ssh服务
➜ ~ sudo service sshd restart

安装 ohmyzsh

1
2
3
4
5
6
# 安装zsh
team@VM-4-13-ubuntu:~$ sudo apt install zsh
# 安装git
team@VM-4-13-ubuntu:~$ sudo apt install git
# 安装ohmyzsh
team@VM-4-13-ubuntu:~$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装并配置MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 安装mysql server
root@VM-4-13-ubuntu:~# apt install mysql-server
..............
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
........................
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
..............

# 安装mysql client
root@VM-4-13-ubuntu:~# apt install mysql-client-core-5.7
root@VM-4-13-ubuntu:~# apt install mariadb-client-core-10.1
......
update-alternatives: using /etc/mysql/mariadb.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
........

# 启动/重启/停止/查看mysql服务
root@VM-4-13-ubuntu:~# service mysql start/restart/stop/staus

# 如果mysql服务被屏蔽
root@VM-4-13-ubuntu:~# systemctl unmask mysql.service

# 设置root初始密码
root@VM-4-13-ubuntu:~# vi init.sql
alter user 'root'@'localhost' identified by 'xxxxxxxxx';
:wq!s


# 用户授权
mysql> grant all privileges on *.* to 'root'@'*' identified by 'xxxxxxxxxxxx';
mysql> flush privileges;

# 修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxxxxxx';

# 创建用户
mysql> CREATE USER 'team'@'%' IDENTIFIED WITH mysql_native_password BY 'xxxxxxx';
mysql> grant all privileges on *.* to 'team'@'*' identified by 'xxxxxxxx' with grant option;

# 取消mysql大小写敏感,只需要在配置文件中添加如下配置
[mysqld]
lower_case_table_names=1

# 备份数据库
➜ ~ mysqldump -uteam -pxxxxxxx dbxxxxx > workorders.sql

安装Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# (1)安装docker,源更新
➜ ~ sudo apt update
# (2)安装 apt 依赖包,用于通过HTTPS来获取仓库
➜ ~ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
# (3)添加密钥
➜ ~ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# (4)验证指纹锁
➜ ~ sudo apt-key fingerprint 0EBFCD88
# (5)设置稳定版本库
➜ ~ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
# (6)更新 apt 包索引
➜ ~ sudo apt update
# (7)安装最新的Docker Engine-Community
➜ ~ sudo apt install docker-ce docker-ce-cli containerd.io
# 或者(7)指定安装版本,查看可用版本
➜ ~ apt-cache madison docker-ce
# 或者(8)安装指定版本
➜ ~ sudo apt install docker-ce=5:18.09.9~3-0~ubuntu-xenial docker-ce-cli=5:18.09.9~3-0~ubuntu-xenial containerd.io
# 如果要使用 Docker 作为非 root 用户,则应考虑使用类似以下方式将用户添加到 docker 组
➜ ~ sudo usermod -aG docker team
➜ ~ newgrp docker
# 配置阿里云镜像加速
➜ ~ sudo tee /etc/docker/daemon.json <<-'EOF'
heredocd> {
"registry-mirrors": ["https://8th48jkz.mirror.aliyuncs.com"]
}
heredocd> EOF
{
"registry-mirrors": ["https://8th48jkz.mirror.aliyuncs.com"]
}
➜ ~ cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://8th48jkz.mirror.aliyuncs.com"]
}
➜ ~ sudo systemctl daemon-reload
➜ ~ sudo systemctl restart docker.service
➜ ~ docker info
.......
Registry Mirrors:
https://8th48jkz.mirror.aliyuncs.com/
.......

# 拉镜像
➜ ~ docker pull mysql:5.7
➜ ~ docker pull nginx:1.10
➜ ~ docker pull redis:4.0
➜ ~ docker pull tomcat:8.0

# 运行nginx容器:端口映射,将本地 8081 端口映射到容器内部的 80 端口
➜ ~ docker run --name nginx-server -p 8081:80 -d nginx
➜ ~ docker run --name nginx -d -p 8081:80 \
-v /usr/docker/nginx/log:/var/log/nginx \
-v /usr/docker/nginx/html:/usr/share/nginx/html \
-v /usr/docker/nginx/conf.d:/etc/nginx/conf.d nginx:1.10
➜ ~ docker run --name nginx -d -p 8081:80 \
-v /usr/docker/nginx/log:/var/log/nginx \
-v /usr/docker/nginx/conf.d:/etc/nginx/conf.d nginx:1.10
# 启动kylin
➜ ~ docker run --name kylin3.0.1 -d -m 8G -p 7070:7070 -p 8088:8088 -p 50070:50070 -p 8032:8032 -p 8042:8042 -p 16010:16010 apachekylin/apache-kylin-standalone:3.0.1

# 安装python模块,可以解决Memoryerror问题
(env3.7) ➜ ~ pip --no-cache-dir install tensorflow -i https://pypi.douban.com/simple/

配置容器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 启动MYSQL
➜ ~ sudo docker run -p 3306:3306 --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
# mysql管理
mysql> create user 'team' identified by 'xxxxxx';
mysql> grant all privileges on *.* to 'team'@'%' identified by 'xxxxxx';
# 拷贝数据
➜ ~ sudo docker cp /home/team/projects/workorders.sql mysql:/

# 启动redis
➜ ~ sudo docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -d redis:4.0 redis-server --appendonly yes

# 启动nginx
➜ ~ sudo docker run -p 80:80 --name nginx -v /mydata/nginx/html:/usr/share/nginx/html -v /mydata/nginx/logs:/var/logs/nginx -v /mydata/nginx/nginx:/etc/nginx -d nginx:1.10

# 启动tomcat
➜ ~ sudo docker run -p 8080:8080 --name tomcat -v /mydata/tomcat/webapps:/usr/local/tomcat/webapps -v /mydata/tomcat/logs:/usr/local/tomcat/logs -d tomcat:8.0

➜ ~ sudo docker run -p 8080:8080 --name tomcat -d tomcat:8.5.54
# 拷贝容器中的conf文件
➜ ~ sudo docker container cp tomcat:/usr/local/tomcat/conf /mydata/tomcat/conf
# 修改配置再次启动
➜ ~ sudo docker run -p 8080:8080 --name tomcat -v /mydata/tomcat/webapps:/usr/local/tomcat/webapps -v /mydata/tomcat/logs:/usr/local/tomcat/logs -v /mydata/tomcat/conf:/usr/local/tomcat/conf -d tomcat:8.0

➜ ~ sudo docker run -p 8080:8080 --name tomcat -v /mydata/tomcat:/usr/local/tomcat -d tomcat:8.5.54

# dockerfile 配置镜像
➜ tomcat docker build -f Dockerfile -t tomcat:new .

# 启动maxwell
➜ ~ sudo docker run -it --rm zendesk/maxwell bin/maxwell --user='root' --password='xxxxxx' --host='x.x.x.x' --producer=stdout

安装Nginx

1
2
3
4
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
1
2
3
4
5
6
7
8
9
10
11
➜  ~ docker pull nginx
➜ ~ docker run --name nginx-demo -d -p 80:80 nginx:latest
➜ ~ docker exec -it nginx-demo /bin/bash
root@6b14f12532e1:/# ls /var/log/nginx/
root@6b14f12532e1:/# ls /etc/nginx/
root@6b14f12532e1:/# ls /usr/share/nginx/html/
➜ docker mkdir -p nginx/log nginx/conf nginx/html
➜ docker docker run --name nginx-demo -d -p 80:80 \
> -v /home/team/docker/nginx/log:/var/log/nginx\
> -v /home/team/docker/nginx/nginx:/etc/nginx\
> -v /home/team/docker/nginx/html:/usr/share/nginx/html nginx:latest

安装 Java

1
2
3
4
5
6
7
8
9
10
11
12
13
# 官网下载jdk包,上传至服务器
➜ tools ls -la jdk-8u291-linux-x64.tar.gz
# 解压
local sudo tar -xvf ~/tools/jdk-8u291-linux-x64.tar.gz
# 创建软链接
local sudo ln -s jdk1.8.0_291 jdk1.8
# 添加环境变量
➜ ~ vi .zshrc
export JAVA_HOME=/usr/local/jdk1.8
export PATH=$JAVA_HOME/bin:$PATH
:wq!

➜ ~ source .zshrc

搭建 Jenkins + GitHub

安装 Jenkins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 更新source list
➜ tools sudo vi /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu xenial-security main restricted
deb http://mirrors.aliyun.com/ubuntu xenial-security universe
deb http://mirrors.aliyun.com/ubuntu xenial-security multiverse
:wq!

# 更新
➜ tools sudo apt update
# 下载镜像
➜ tools wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
# 将Debian包存储库地址附加到服务器的sources.list
➜ tools echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
# 更新
➜ tools sudo apt update
# 错误处理:W: GPG error: https://pkg.jenkins.io/debian-stable binary/ Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY FCEF32E745F2C3D5
➜ tools sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys FCEF32E745F2C3D5
# 添加Java环境变量
root@VM-4-13-ubuntu:~# ln -s /usr/local/jdk1.8/bin/java /usr/bin/java
# 安装
➜ tools sudo apt install -y jenkins

参考文献

坚持原创技术分享,您的支持将鼓励我继续创作!