Sansx's Studio.

centos7 安装Docker + nginx

字数统计: 816阅读时长: 3 min
2018/05/19 Share

安装docker

1. 首先进行更新

执行yum update

保险起见还需要删除docker旧版本防止出错

1
2
3
4
5
6
7
8
9
10
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine

2. 安装依赖

1
2
3
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

3. 设置储存库

官方:

1
2
3
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

国内阿里云镜像:

1
2
>yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
>

4. 安装DOCKER CE

$ sudo yum -y install docker-ce

5. 启动DOCKER CE

systemctl start docker

查看docker的版本信息
docker version

之后为可选操作

5. 系统配置

官方文档

  1. 创建docker组
    $ sudo groupadd docker
  2. 添加组用户
    $ sudo usermod -aG docker $USER
  3. 重新登陆之后就可以直接使用docker命令进行操作
    $ docker run hello-world

如果报错:curl: (6) Could not resolve host
检查并修改DNS地址

1
2
3
4
5
[root@localhost ~]# vim /etc/resolv.conf  

Generated by NetworkManager
nameserver 114.114.114.114
nameserver 8.8.8.8

安装nginx

1. 安装依赖

yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

gcc用作编译源码、nginx的http模块通过pcre解析的正则表达式、zlib库提供了多种压缩和解压缩的方式、OpenSSL用作nginx的https支持(即在ssl协议上传输http)

2. Nginx安装

2.1. 查看需求的Nginx版本

当前稳定版nginx-1.14.0

版本说明:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版

2.2. 安装wget

yum -y install wget

2.3. 下载nginx压缩包

wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

2.4. 进入目录,解压
1
2
3
cd /usr/software/
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
2.5. 安装配置

使用默认配置:
./configure

指定用户、路径和模块配置:

1
2
3
4
5
6
7
./configure \
--user=nginx --group=nginx \ #安装的用户组
--prefix=/usr/local/nginx \ #指定安装路径
--with-http_stub_status_module \ #监控nginx状态,需在nginx.conf配置
--with-http_ssl_module \ #支持HTTPS
--with-http_sub_module \ #支持URL重定向
--with-http_gzip_static_module #静态压缩

2.6. 编译

make && make install

2.6. 全局nginx命令执行
1
2
cd /usr/local/nginx/sbin/
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

3. 基本命令

3.1. 查看版本

nginx -v

3.2. 查看加载的模块

nginx -V

3.3. 启停

启动
nginx
停止
nginx -s stop
nginx -s quit
动态加载
ngins -s reload
测试配置文件nginx.conf
nginx -t

4. 开机自启动

编辑/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
进入配置文件
vim /etc/rc.d/rc.local
在文件中新增一行
/usr/local/nginx/sbin/nginx
设置权限
chmod u+x rc.local

5. 更改默认端口80

编辑配置文件/usr/local/nginx/conf/nginx.conf,修改listen开头那行后面的数字保存退出即可
重新加载
nginx -s reload

6. 访问nginx

6.1 修改防火墙规则(开启默认80端口)

编辑public.xml文件,添加规则

1
2
3
vim /etc/firewalld/zones/public.xml
<port protocol="tcp" port="80"/>
<port protocol="udp" port="80"/>

保存退出后,重启防火墙
firewall-cmd --complete-reload

或者直接关闭防火墙

1
2
3
firewall-cmd --state
systemctl stop firewalld.service
firewall-cmd --state

6.2 访问测试

浏览器输入服务器IP加端口即可
例如:127.0.0.1:80

CATALOG
  1. 1. 安装docker
    1. 1.0.1. 1. 首先进行更新
    2. 1.0.2. 2. 安装依赖
    3. 1.0.3. 3. 设置储存库
    4. 1.0.4. 4. 安装DOCKER CE
    5. 1.0.5. 5. 启动DOCKER CE
    6. 1.0.6. 之后为可选操作
    7. 1.0.7. 5. 系统配置
    8. 1.0.8. 官方文档
  • 2. 安装nginx
    1. 2.0.1. 1. 安装依赖
    2. 2.0.2. 2. Nginx安装
      1. 2.0.2.1. 2.1. 查看需求的Nginx版本
      2. 2.0.2.2. 2.2. 安装wget
      3. 2.0.2.3. 2.3. 下载nginx压缩包
      4. 2.0.2.4. 2.4. 进入目录,解压
      5. 2.0.2.5. 2.5. 安装配置
      6. 2.0.2.6. 2.6. 编译
      7. 2.0.2.7. 2.6. 全局nginx命令执行
    3. 2.0.3. 3. 基本命令
      1. 2.0.3.1. 3.1. 查看版本
      2. 2.0.3.2. 3.2. 查看加载的模块
      3. 2.0.3.3. 3.3. 启停
    4. 2.0.4. 4. 开机自启动
    5. 2.0.5. 5. 更改默认端口80
    6. 2.0.6. 6. 访问nginx
      1. 2.0.6.1. 6.1 修改防火墙规则(开启默认80端口)
      2. 2.0.6.2. 6.2 访问测试