Docker上部署Nginx的方法步驟
1.從 docker 下載 Nginx 鏡像
docker pull nginx
2.創(chuàng)建掛載目錄
之后的文件就放這里面,對(duì) docker 里 Nginx 對(duì)應(yīng)的目錄進(jìn)行映射,就不用改文件進(jìn)到容器里了
mkdir -p /data/nginx/{conf,conf.d,html,logs}
3.為了保證文件的正確性,建議先進(jìn)入容器把對(duì)應(yīng)的文件給復(fù)制出來(lái)
不方便的可以開兩個(gè)窗口,一個(gè)進(jìn)到容器里,左邊復(fù)制到右邊這樣,這是為了保證文件正確
#啟動(dòng)容器 docker run -itd nginx /bin/bash #進(jìn)入容器 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 |
首頁(yè)文件夾html路徑 | html文件夾 | /data/nginx/html | /usr/share/nginx/html |
日志文件 | log文件夾 | /data/nginx/logs | /var/log/nginx |
這是對(duì)應(yīng)的掛載目錄,把 nginx.conf 文件和 conf.d 里的 default.conf 復(fù)制到對(duì)應(yīng)文件夾放好,后面就是修改了
4.接下來(lái)修改下 default.conf 文件就好了
這里我最多就改改端口號(hào),訪問路徑之類的
server { ? ? ? #端口號(hào) ? ? 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; ? ? #} }
這里測(cè)試用的 1.html 自己寫的
<html> <head> <title>Mynginx</title> </head> <body> <h1> 歡迎使用nginx! </h1> </body> </html>
5.接下來(lái)就可以啟動(dòng)容器了
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
掛載路徑一定要對(duì)好,別寫錯(cuò)了
-p 8089:80 這里把 80 端口映射到主機(jī)的 8089 端口,這樣訪問就是 8089 端口了,不用去改 nginx 的默認(rèn)端口
接下來(lái)就可以看下容器是否正常啟動(dòng)
docker ps
要是沒有看到容器那說明啟動(dòng)有問題,看看是配置文件寫的不對(duì),還是掛載路徑不對(duì)之類的
啟動(dòng)后就可以直接瀏覽器 localhost:8089 看到剛才寫的 1.index 頁(yè)面了
6.不停止 nginx 更新配置文件
當(dāng)我們修改配置文件后要更新配置文件,這個(gè)時(shí)候開兩窗口就很爽
#進(jìn)入容器 docker exec -it xxxxxxxxxxx /bin/bash ? #測(cè)試配置文件是否有問題 nginx -t ? #要是顯示 successful 就可以更新了 nginx -s reload
到此這篇關(guān)于Docker上部署Nginx的方法步驟的文章就介紹到這了,更多相關(guān)Docker部署Nginx內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
docker-compose使用volumes注意項(xiàng)介紹
這篇文章主要介紹了docker-compose使用volumes注意項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03一次Docker中Redis連接暴增的問題排查實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了一次Docker中Redis連接暴增的問題排查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06docker?run容器運(yùn)行的方法實(shí)現(xiàn)
本文主要介紹了docker?run容器運(yùn)行的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06docker私庫(kù)Harbor的架構(gòu)與組件說明
今天小編就為大家分享一篇關(guān)于docker私庫(kù)Harbor的架構(gòu)與組件說明,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Docker 部署HAProxy v2.2.29 并暴露指標(biāo)接口的問題解決
haproxy提供高可用性、負(fù)載均衡以及基于TCP(第四層)和HTTP(第七層)應(yīng)用的代理,支持虛擬主機(jī),它是免費(fèi)、快速并且可靠的一種解決方案,這篇文章主要介紹了Docker 部署HAProxy v2.2.29 并暴露指標(biāo)接口,需要的朋友可以參考下2023-04-04