centos7 docker部署Nginx

122 阅读1分钟
  1. 拉取nginx镜像

docker pull nginx:1.21.5

  1. 在/root目录下创建nginx目录用于存储nginx数据信息

mkdir -p /root/nginx/conf

  1. 在/root/nginx/conf下创建nginx.conf文件:vim nginx.conf,如果第一步和我一样就可以直接粘贴下面的代码来执行,然后看第4步。

如果版本不一样,配置文件里面的内容也可能不同。这时有两种解决方法:

第一种是下载和我一样的nginx镜像版本,我这里nginx镜像的版本是:1.21.5

第二种是可以先创建并打开一个nginx容器:docker run -d --name nginx-test -p 8081:80 nginx ,然后复制/ect/nginx/nginx.conf中的内容,替换下面的代码。

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;

    include /etc/nginx/conf.d/*.conf;
}
  1. 回退到nginx目录,粘贴下面的命令并运行
docker run -id --name=c_nginx \
-p 80:80 \
-v  $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v  $PWD/logs:/var/log/nginx \
-v  $PWD/html:/usr/share/nginx/html \
nginx
  1. 进入html目录,创建index.html文件:vim index.html ,编写网页内容, 例如:<h1>hello nginx docker<h2>

  2. 直接输入宿主机网址查看网页,成功显示!

3.png