天天看點

Linux環境下使用Docker安裝Nginx

目前環境:

Centos7.6 (3.10.0-1062.1.1.el7.x86_64)

搜尋Nginx鏡像

docker search nginx

擷取官方的鏡像

Linux環境下使用Docker安裝Nginx

下載下傳鏡像到本地

docker pull nginx

不指定

tag

預設是最新版本,本文使用

nginx1.17.6

docker pull nginx:1.17.6

Linux環境下使用Docker安裝Nginx

檢視本地鏡像清單

docker images

Linux環境下使用Docker安裝Nginx

建立Nginx容器外部挂載目錄

mkdir -p /opt/data/nginx/conf
mkdir -p /opt/data/nginx/conf.d
mkdir -p /opt/data/nginx/html
mkdir -p /opt/data/nginx/logs
           

建立配置檔案

/opt/data/nginx/conf/

目錄下建立

nginx.conf

檔案

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
}

           

/opt/data/nginx/conf.d/

目錄下建立

default.conf

檔案

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.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;
    }

}
           

/opt/data/nginx/html/

目錄下建立

index.html

檔案

<!DOCTYPE html>
<html>
	<head>
		<title>Welcome to Website!</title>
	</head>
	<body>
		<h1>Hello Nginx!</h1>
	</body>
</html>

           

運作Nginx容器

docker run -d --name nginx -p 80:80 \
-v /opt/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/data/nginx/logs:/var/log/nginx \
-v /opt/data/nginx/html:/usr/share/nginx/html \
-v /opt/data/nginx/conf.d:/etc/nginx/conf.d \
--restart=always --privileged=true nginx:1.17.6
           

通路下

Linux環境下使用Docker安裝Nginx