centos docker 安装 nginx

1、首先安装docker

下面是一键安装脚本

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

也可以依次执行,推荐用官方一键安装脚本

sudo yum install -y yum-utils

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2、创建挂载目录

下面是常用的几个目录

mkdir -p /appdata/nginx/conf

mkdir -p /appdata/nginx/html

mkdir -p /appdata/nginx/log

mkdir -p /appdata/nginx/certs

3、将配置文件和证书拷贝到相应的目录

注意:配置文件里面的路径都是容器内的路径,因为我们进行了挂载,所以会映射到挂载路径

比如:证书的路径。我们写“certs/private.key” 是相对路径,绝对路径是容器内/etc/nginx/certs/private.key的目录,应为我们进行了挂载,所以这里会映射到:/appdata/nginx/certs/private.key


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
	
    server {
	listen        443 ssl http2;
	listen       [::]:443 ssl http2;
 	server_name  _;
	root         /usr/share/nginx/html;

	ssl_certificate "certs/public.crt";
    ssl_certificate_key "certs/private.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
        root   /usr/share/nginx/html;
	    try_files $uri $uri/ /index.html;
        index  index.html index.htm;
        }
        
    }

    include /etc/nginx/conf.d/*.conf;
}

4、启动容器

docker run -p 443:443 --restart=always --name nginx -v /appdata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /appdata/nginx/log:/var/log/nginx -v /appdata/nginx/html:/usr/share/nginx/html -v /appdata/nginx/certs:/etc/nginx/certs -d nginx:latest

其中:

--restart=always 自动启动

-d 后台运行

-p 映射端口,443:443,前面是宿主机端口,后面是容器端口

你可能感兴趣的:(系统配置,软件开发,docker,centos,nginx)