设置私有服务器(VPS)备忘录
Linux Distribution OS
CentOS 7
必要的安全设置
保持服务器系统更新
自动更新安全更新
- CentOS 使用 yum-cron 自动更新
- Fedora 使用 dnf-automatic
- Debian Ubuntu 使用 unattended upgrade
添加访问受限的用户账号
- 创建用户 (以用户名 devop 为例, 可以替换成你想创建的用户名)
1 | useradd devop && passwd devop |
- 添加该用户到wheel组
1 | usermod -aG wheel devop |
SSH 守护进程配置
拒绝Root用户通过SSH登录
修改配置文件/etc/ssh/sshd_config
1 | # Authentication: |
1 | # Change to no to disable tunnelled clear text passwords |
然后重启sshd服务
1 | sudo systemctl restart sshd |
设置fail2ban
1 | yum install fail2ban |
开启服务:
1 | systemctl start fail2ban |
配置服务:
1 | cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local |
CentOS 或者是 Fedora 需要修改jail.local配置文件配置:
1 | backend = systemd |
并注释掉下面这些配置
1 | [sshd] |
更多fail2ban配置:A Tutorial for Using Fail2ban to Secure Your Server
更多服务器安全配置:linode 服务器安全配置
必要的软件
系统、开发相关
- EPEL
1 | sudo yum install epel-release |
- Python3 virtualenv
1 | sudo yum install python34 python-pip |
然后紧接着升级pip和安装 virtualenv
1 | sudo pip install -U pip |
在CentOS7上使用源码编译安装Python3.6
1 | sudo yum install gcc |
1 | cd /usr/src |
1 | cd .. |
- MySQL
首先更新yum
1 | sudo yum update |
然后下载安装MySQL社区版仓库
1 | wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm |
然后安装、重启MySQL
1 | sudo yum install mysql-server |
然后安全配置MySQL
1 | sudo mysql_secure_installation |
- Redis
添加EPEL仓库,更新yum
1 | sudo yum install epel-release |
安装Redis
1 | sudo yum install redis |
启动并配置随机自启
1 | sudo systemctl start redis |
- Nginx
首先配置EPEL和yum更新
1 | sudo yum install epel-release |
配置启动Nginx
1 | systemctl enable nginx.service |
- centOS 防火墙打开对http/https协议端口限制
1 | $ sudo firewall-cmd --add-service=http --permanent |
查看防火墙支持协议:
1 | $ sudo firewall-cmd --list-all |
docker
- 设置yum 仓库地址
1
2
3
4
5$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo- 安装docker
1
$ sudo yum install docker
- 配置docker为linux service
1
2$ sudo systemctl start docker
$ sudo systemctl enable docker如果没有提示错误,应该就是已经成功安装,如果不是很放心可以使用下面命令检查服务状态。
1
$ sudo systemctl status docker
- 运行一个helloworld
1
$ sudo docker run hello-world
成功运行容器会有下面的信息提示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/Git 服务器
配置链接
Git Hook 配置Git自动部署
How To Set Up Automatic Deployment with Git with a VPSjenkins
- 使用Docker安装
选择jenkins/jenkins镜像,注意不是docker官方镜像(官方镜像版本较老,并且长时间未更新) - 获取镜像
1
sudo docker pull jenkins/jenkins
- 创建jenkins需要挂载的host volume目录,并赋予docker用户(uid = 1000)的访问权限
mkdir /path/to/jekins/home
- 创建并启动jenkins容器
1
sudo docker run -p 8080:8080 -p 50000:50000 -v /path/to/jekins/home:/var/jenkins_home --name=jenkins jenkins/jenkins:lts
- 在浏览器中对
http://<host_ip>:8080
或者http://<jenkins_domain>
(需要使用反向代理upstream 端口为8080的jenkins服务) 进行后续的jenkins设置
- 使用Docker安装
Redis docker 容器
1
sudo docker pull redis
- 创建host本地持久化文件
mkdir /path/to/redis/home
touch /path/to/redis/home/redis.conf
- 创建并启动redis容器
1
docker run -p 6379:6379 -v /path/to/redis/home/:/usr/local/etc/redis/ --name redis-latest redis redis-server /usr/local/etc/redis/redis.conf
Ansible
An Introduction to Ansible: Installing Jenkins on your server
Deployment with Ansible, Docker, Jenkins and Git
Automated Servers and Deployments with Ansible & JenkinsApacheBench(ab)
1 | sudo yum install httpd-tools |
用法
1 | ab -c 并发数 -n 请求数 URL |