主题
安装
Ubuntu
bash
apt update
apt install -y nginx启动并设置开机自启:
bash
systemctl enable --now nginx查看状态:
bash
systemctl status nginx常见路径:
| 路径 | 作用 |
|---|---|
/usr/sbin/nginx | Nginx 主程序 |
/etc/nginx | 配置目录 |
/etc/nginx/nginx.conf | 主配置文件 |
/etc/nginx/conf.d | 额外配置目录 |
/etc/nginx/sites-available | 可用站点配置 |
/etc/nginx/sites-enabled | 已启用站点配置 |
/var/www/html | 默认网页根目录 |
/var/log/nginx | 日志目录 |
Docker Compose
创建部署目录:
bash
mkdir -p ~/apps/nginx/{conf,html,logs,certs}
cd ~/apps/nginx创建 conf/nginx.conf:
nginx
events {}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
}
}创建 compose.yml:
yaml
services:
nginx:
image: nginx:stable
container_name: nginx-service
networks:
- nginx-network
ports:
- '80:80'
- '443:443'
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf:ro
- ./html:/usr/share/nginx/html:ro
- ./logs:/var/log/nginx
- ./certs:/etc/nginx/certs:ro
restart: unless-stopped
networks:
nginx-network:启动服务:
bash
docker compose up -d说明
conf/nginx.conf映射到容器主配置。html/放静态站点文件。logs/保存访问日志和错误日志。certs/放 HTTPS 证书,建议只读挂载。- 生产环境建议固定镜像版本,不直接使用
latest。
