Skip to content

安装 Nginx

apt-get

bash
apt install -y nginx

Nginx 默认目录

bash
/var/www/html               # 网站文件存放的地方, 默认只有 Nginx 欢迎页面.

/etc/nginx/                 # Nginx 配置文件目录
├── conf.d                  # 可以在这儿管理 server
├── nginx.conf              # Nginx 的主配置文件

/var/log/nginx/             # Nginx 日志文件
├── access.log              # 每一个访问请求都会默认记录在这个文件中
└── error.log               # 任何 Nginx 的错误信息都会记录到这个文件中

Docker

  1. 创建目录结构

    bash
    mkdir -p ~/nginx-home/{conf,html,logs,certs}
    • ~/nginx-home/conf:存放 Nginx 配置文件。
    • ~/nginx-home/html:存放网站文件(如 index.html)。
    • ~/nginx-home/logs:存放 Nginx 日志文件。
    • ~/nginx-home/certs:存放 SSL 证书。
  2. 部署容器

    Nginx 镜像

    bash
    docker network create nginx-network
    bash
    docker container run -d \
                         --name nginx-service \
                         --network nginx-network \
                         -p 80:80 \
                         -p 443:443 \
                         -v ~/nginx-home/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
                         -v ~/nginx-home/html:/usr/share/nginx/html:ro \
                         -v ~/nginx-home/logs:/var/log/nginx \
                         -v ~/nginx-home/certs:/etc/nginx/certs \
                         --restart=always \
                         nginx:latest
    • -v ~/nginx-home/conf/nginx.conf:/etc/nginx/nginx.conf:ro:将宿主机的 nginx.conf 文件映射到容器内的 Nginx 配置文件,并设置为只读(ro)。
    • -v ~/nginx-home/html:/usr/share/nginx/html:ro:将宿主机的 html 目录映射到容器内的网站文件目录,并设置为只读。
    • -v ~/nginx-home/logs:/var/log/nginx:将宿主机的 logs 目录映射到容器内的日志文件目录。

基于 MIT 许可发布