前言: 使用docker安裝了nginx容器,很久才成功跑起來,對安裝過程做下記錄
linux系統:centos7.4
docker安裝不闡述,直接記錄安裝創建nginx容器的過程
1. 拉取nginx的鏡像,此處拉取的最新版
docker pull nginx
2. 創建nginx容器之前需要先確認下要掛載的文件,進入到自己想要的放置掛載文件的目錄下,此處我的為/usr/fordocker,并進入。
3. 創建容器
docker run -p 80:80 --name nginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -v $PWD/conf/conf.d:/etc/nginx/conf.d -d nginx
-v 參數后面代表的是宿主機文件路徑和容器文件路徑
-v $PWD/www:/www? 意思為 將當前目錄下的www文件掛載到 容器的www目錄下
? ? 此處掛載了www目錄,nginx.conf文件(修改配置),logs日志文件,conf.d文件(用來存放*.conf文件)
4. 執行完3,容器創建完成,這個時候我們需要配置下nginx.conf文件,只需要修改剛剛配置的在宿主機的nginx.conf即可
#user nobody;
worker_processes 1;error_log /wwwlogs/error.log; #pid 日志路徑 此處的路徑均為容器內路徑
#error_log logs/error.log notice;
#error_log logs/error.log info;pid /www/nginx.pid; #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 /wwwlogs/access.log main; # access_log路徑sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;gzip on;include /etc/nginx/conf.d/*.conf; #引入的conf文件存放路徑
}
配置完成后進入剛剛配置的conf.d文件中創建test.conf,并配置如下
server {listen 80;server_name www.itryfirst.top;root /www/webapps/test;index index.php index.html index.htm;location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}location / {try_files $uri $uri/ /index.php?$query_string;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~ .*\.(js|css)?${expires 1h;}
}
最后進入上面配置的/www/webapps/test路徑中創建index.html文件,并在其中輸入
<html><p>hello world</p>
</html>
訪問域名,頁面出現hello world, 容器安裝成功!
?