centos7 上 用docker部署nginx服务

1、下载一个nginx镜像

[root@Docker ~]# docker pull nginx:1.16.1
1.16.1: Pulling from library/nginx
b8f262c62ec6: Pull complete 
00b0a9251451: Pull complete 
7cc4a8bdb72c: Pull complete 
Digest: sha256:0d0af9bc6ca2db780b532a522a885bef7fcaddd52d11817fc4cb6a3ead3eacc0
Status: Downloaded newer image for nginx:1.16.1
docker.io/library/nginx:1.16.1

2、创建挂载的目录

[root@bogon ~]# mkdir -p /app/nginx/{conf,conf.d,html,logs}

3、编写主配置文件

cat > /app/nginx/conf/nginx.conf << EOF
##############################Nginx主配置文件
#设置运行的用户
user root;
#设置nginx要开启的子进程数量,一般设置为和CPU数量相等值
work_processes 1;
#设置全局错误日志位置和级别
error_log /var/log/nignx/error.log error;
#设置进程id的存放位置
pid /var/run/nigx/pid;
events{
  #设置线程轮询的方案,如果是linux2.6+,使用epoll,如果是BSD如Mac请使用Kqueue
  use epoll;
  #设置单个work_processes的最大的并发连接数
  worker_connections 1024;
}
http{
  #设置mime类型,类型由mime.type文件定义
  include mime.types;
  default_type  application/octet-stream;
  #设置一个名为main的日志格式
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  #设置访问日志的位置,使用main的格式
  access_log  /var/log/nginx/access.log  main;
  #设置连接超时时间
  keepalive_timeout  65;
  #设置gzip功能为开启
  gzip on;
  gzip_disable "msie6";
  #设置请求缓冲
  client_header_buffer_size    128k;
  large_client_header_buffers  4 128k;
  #设置nginx是否使用sendfile函数(普通应用设on,而下载等磁盘重负载应用为设off)
  sendfile on;
  #设置当前配置文件包含另一个子配置文件
  include /etc/nginx/conf.d/*.conf;
}
EOF

4、编写虚拟主机域名www.92fuge.com的配置

cat > /app/nginx/conf.d/www.92fuge.com.conf << EOF
server {
    listen       80;
    server_name  www.92fuge.com;
    #charset koi8-r;
    access_log  /var/log/nginx/www.92fuge.com.log  main;
    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/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;
    #}
}
EOF

5、启动容器

[root@bogon html]# docker run -d --name CentOS_nginx --mount type=bind,src=/app/nginx/conf/nginx.conf,dst=/etc/nginx/nginx.conf:ro --mount type=bind,src=/app/nginx/conf.d,dst=/etc/nginx/conf.d  --mount type=bind,src=/app/nginx/html/,dst=/usr/share/nginx/html  --mount type=bind,src=/app/nginx/logs,dst=/var/log/nginx -p 80:80 nginx
3f105fbfa09c1dca62c057fa96609cbee66cca3c68d7c07b50f7562f7b0dd393

(其它说明:默认容器对目录有可读写权限,可以通过指定ro,将权限改为只读,ro应该是read only)

启动一个nginx容器。

-p表示将服务器端口左80映射到容器内端口右80,

-d表示在后台运行, 

–name表示启动后的容器名称,

第一个mount表示将本地自定义目录的自写义主配置文件映射为容器的主配置文件,

第二个mount表示将本地自定义的子配置文件目录映射为容器的子配置文件目录,

第三个mount表示将本地自定义的项目代码目录映射为容器的项目代码目录。

第四个mount表示将本地自定义的日志目录映射为容器的日志目录。

6、检查 docker 是否启动成功

 [root@Docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
dab83792fa0f        nginx               "nginx -g 'daemon of…"   43 seconds ago      Up 42 seconds       0.0.0.0:80->80/tcp   CentOS_nginx

7、简单的测试访问

[root@bogon html]# echo "172.17.0.2 www.92fuge.com" >> /etc/hosts
[root@bogon html]# echo "www.92fuge.com" >>/app/nginx/html/1.html
[root@bogon html]# curl http://www.92fuge.com/1.html
www.92fuge.com

------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容