Docker上如何部署Nginx

1.從 docker 下載 nginx 鏡像

docker?pull?nginx

2.創建掛載目錄

之后的文件就放這里面,對 docker 里 nginx 對應的目錄進行映射,就不用改文件進到容器里了

mkdir?-p?/data/nginx/{conf,conf.d,html,logs}

3.為了保證文件的正確性,建議先進入容器把對應的文件給復制出來

不方便的可以開兩個窗口,一個進到容器里,左邊復制到右邊這樣,這是為了保證文件正確

#啟動容器 docker?run?-itd?nginx?/bin/bash #進入容器 docker?attach?xxxxxxxxxx
說明 文件 掛載路徑 nginx路徑
配置文件 nginx.conf /data/nginx/conf/nginx.conf /etc/nginx/nginx.conf
配置文件文件夾 conf.d文件夾 /data/nginx/conf.d /etc/nginx/conf.d
首頁文件夾html路徑 html文件夾 /data/nginx/html /usr/share/nginx/html
日志文件 log文件夾 /data/nginx/logs /var/log/nginx

這是對應的掛載目錄,把 nginx.conf 文件和 conf.d 里的 default.conf 復制到對應文件夾放好,后面就是修改了

4.接下來修改下 default.conf 文件就好了

這里我最多就改改端口號,訪問路徑之類的

server?{ ? ????#端口號 ????listen???????80; ????#定義使用?localhost?訪問 ????server_name??localhost; ? ????#charset?koi8-r; ????#access_log??/var/log/nginx/host.access.log??main; ? ????location?/?{ ????????#根目錄位置 ????????root???/usr/share/nginx/html; ????????#index?文件位置 ????????index??1.html; ????} ? ????#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; ????#} }

這里測試用的 1.html 自己寫的

  <title>Mynginx</title><h2> 歡迎使用nginx! </h2>  

5.接下來就可以啟動容器了

docker?run??--name?myNginx?-d?-p?8089:80?-v?/data/nginx/html:/usr/share/nginx/html?-v?/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf?-v?/data/nginx/conf.d:/etc/nginx/conf.d??-v?/data/nginx/logs:/var/log/nginx?nginx

掛載路徑一定要對好,別寫錯了

-p 8089:80 這里把 80 端口映射到主機的 8089 端口,這樣訪問就是 8089 端口了,不用去改 nginx 的默認端口

接下來就可以看下容器是否正常啟動

docker?ps

要是沒有看到容器那說明啟動有問題,看看是配置文件寫的不對,還是掛載路徑不對之類的

啟動后就可以直接瀏覽器 localhost:8089 看到剛才寫的 1.index 頁面了

6.不停止 nginx 更新配置文件

當我們修改配置文件后要更新配置文件,這個時候開兩窗口就很爽

#進入容器 docker?exec?-it?xxxxxxxxxxx?/bin/bash ? #測試配置文件是否有問題 nginx?-t ? #要是顯示?successful?就可以更新了 nginx?-s?reload

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享