Docker安装Nginx

196 阅读1分钟

背景

- os: MacOS 12.3
- 芯片:M1

准备

  1. Docker

安装

  1. 拉取镜像(根据自己需要安装合适的版本)
docker pull nginx:1.23.0
  1. 配置文件准备

    1. 创建临时容器

      docker run -d --name nginx-temp nginx:1.23.0
      
    2. 在主机中创建挂载目录

      # 目录创建
      mkdir -p ~/dev/docker/nginx/conf
      mkdir -p ~/dev/docker/nginx/logs
      mkdir -p ~/dev/docker/nginx/html
      
      # 权限调整
      chomd 777 ~/dev/docker/nginx
      
    3. 拷贝Nginx配置文件至挂载目录(因为后续创建挂载目录的容器时不会生成配置文件)

      docker cp nginx-temp:/etc/nginx/nginx.conf ~/dev/docker/nginx
      docker cp nginx-temp:/etc/nginx/conf.d ~/dev/docker/nginx
      
    • 默认配置文件内容(nginx.conf

      #user  nobody;
      worker_processes  1;
      
      #error_log  logs/error.log;
      #error_log  logs/error.log  notice;
      #error_log  logs/error.log  info;
      
      #pid        logs/nginx.pid;
      
      
      events {
          worker_connections  1024;
      }
      
      
      http {
          include       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  logs/access.log  main;
      
          sendfile        on;
          #tcp_nopush     on;
      
          #keepalive_timeout  0;
          keepalive_timeout  65;
      
          #gzip  on;
      
          server {
              listen       80;
              server_name  localhost;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  root   html;
                  index  index.html index.htm;
              }
      
              #error_page  404              /404.html;
      
              # redirect server error pages to the static page /50x.html
              #
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
      
              # proxy the PHP scripts to Apache listening on 127.0.0.1:80
              #
              #location ~ .php$ {
              #    proxy_pass   http://127.0.0.1;
              #}
      
              # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
              #
              #location ~ .php$ {
              #    root           html;
              #    fastcgi_pass   127.0.0.1:9000;
              #    fastcgi_index  index.php;
              #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
              #    include        fastcgi_params;
              #}
      
              # deny access to .htaccess files, if Apache's document root
              # concurs with nginx's one
              #
              #location ~ /.ht {
              #    deny  all;
              #}
          }
      
      
          # another virtual host using mix of IP-, name-, and port-based configuration
          #
          #server {
          #    listen       8000;
          #    listen       somename:8080;
          #    server_name  somename  alias  another.alias;
      
          #    location / {
          #        root   html;
          #        index  index.html index.htm;
          #    }
          #}
      
      
          # HTTPS server
          #
          #server {
          #    listen       443 ssl;
          #    server_name  localhost;
      
          #    ssl_certificate      cert.pem;
          #    ssl_certificate_key  cert.key;
      
          #    ssl_session_cache    shared:SSL:1m;
          #    ssl_session_timeout  5m;
      
          #    ssl_ciphers  HIGH:!aNULL:!MD5;
          #    ssl_prefer_server_ciphers  on;
      
          #    location / {
          #        root   html;
          #        index  index.html index.htm;
          #    }
          #}
      }
      
    1. 停止并删除临时容器
      docker container stop nginx-temp
      docker container rm nginx-temp
      
  2. 部署容器

    docker run -d -p 80:80 \
     --name nginx \
     -v ~/dev/docker/nginx/html:/usr/share/nginx/html \
     -v ~/dev/docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
     -v ~/dev/docker/nginx/conf.d:/etc/nginx/conf.d \
     -v ~/dev/docker/nginx/logs:/var/log/nginx \
     nginx:1.23.0
    

Nginx 常用命令

nginx                   # 启动
nginx -s reload         # 重新加载配置
nginx -s reopen         # 重启
nginx -s stop           # 停止  快速停止和关闭Nginx
nginx -s quit           # 退出  正常停止或关闭Nginx
nginx -v                # 查看版本
nginx -V                # 查看版本,以及配置文件地址
nginx -c filename       # 指定配置文件
nginx -h                # 帮助