目录
一、安装Nginx
1.使用Docker下载Nginx镜像
docker pull nginx
2.查看已下载的Nginx镜像
docker images
3.启动Nginx容器
docker run --name docker-nginx -p 8080:80 -d nginx
--name:容器的名字
-p:端口映射,外部端口8080,内部端口80(将容器的80端口映射到主机的8080端口)
-d:设置容器在后台不断运行
4.查看效果
二、配置Nginx
1.在Linux创建要挂载的文件夹
创建好的文件要与容器内的文件做一个挂载操作,这样修改服务器内的文件即可修改容器内的文件。我这里直接用Xftp做了文件创建。
2.将容器内Nginx的默认配置文件拷贝到挂载文件中
docker cp ffe3c800c168:/etc/nginx/nginx.conf /opt/myDocker/nginx/conf
3.运行部署命令
docker run -d -p 8080:80 --name docker-nginx -v /opt/myDocker/nginx/html:/usr/share/nginx/html -v /opt/myDocker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/myDocker/nginx/log:/var/log/nginx -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro nginx
此时修改/opt/myDocker/nginx/html下的index.html文件,页面内容即可更改。
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
三、Nginx配置域名
1.修改nginx.conf文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.mrfei.work;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html;
}
#error_page 404 /404.html;
}
}
server_name:填写域名或localhost
2.使用docker重启Nginx
docker ps
docker restart 容器ID
docker ps 查看docker内容器
docker restart 容器id