Ngnix服务部署的三种方法说明
1.包管理器安装(推荐新手)
适用于快速安装,适合大多数用户(尤其是新手)。
-
步骤(以 Ubuntu/Debian 为例)
# 更新软件包列表 sudo apt update # 安装 Nginx sudo apt install nginx -y # 启动并启用开机自启 sudo systemctl start nginx sudo systemctl enable nginx # 检查服务状态 # 如果显示 active (running),说明安装成功。 sudo systemctl status nginx # 验证 Nginx 是否正常运行 # 在浏览器或终端中访问本地页面: curl http://localhost # 或直接访问服务器的 IP 地址(需确保防火墙开放 80 端口)。 # 配置防火墙(如需要) # 如果使用 UFW 防火墙,开放 HTTP 端口: sudo ufw allow 'Nginx HTTP' -
步骤(以 CentOS/RHEL/Rocky Linux 为例)
# 安装 EPEL 仓库(部分系统需要) sudo yum install epel-release -y # 安装 Nginx sudo yum install nginx -y # 启动并启用开机自启 sudo systemctl start nginx sudo systemctl enable nginx # 检查服务状态 sudo systemctl status nginx # 配置防火墙 # 如果使用 firewalld,开放 HTTP 端口: sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
2. 源码编译安装(定制化推荐)
以下是针对 Ubuntu 22.04 LTS 系统从源码编译安装 Nginx 的完整步骤:
-
一、环境准备
-
更新系统
sudo apt update && sudo apt upgrade -y -
安装编译工具和依赖库
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -
关闭防火墙或开放端口
# 临时关闭 UFW 防火墙 sudo ufw disable # 或者开放 80 和 443 端口(推荐) sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw status
-
-
二、下载和解压 Nginx 源码
-
下载 Nginx 源码包
cd /usr/local/src sudo wget http://nginx.org/download/nginx-1.28.0.tar.gz -
解压源码包
sudo tar -zxvf nginx-1.28.0.tar.gz cd nginx-1.28.0
-
-
三、创建 Nginx 用户和目录
-
创建 Nginx 用户
sudo useradd -r -s /sbin/nologin nginx -
创建必要目录
sudo mkdir -p /usr/local/nginx/{logs,conf,html} sudo chown -R nginx:nginx /usr/local/nginx
-
-
四、配置编译选项
-
运行
configure脚本sudo ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_sub_module \ --with-pcre \ --with-threads \ --with-stream -
验证配置并执行编译和安装
sudo make && sudo make install
-
-
五、配置 Nginx 服务
-
创建 systemd 服务文件
sudo vim /etc/systemd/system/nginx.service -
添加以下内容
[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target -
重新加载 systemd 配置
sudo systemctl daemon-reload
-
-
六、启动和验证 Nginx
# 启动 Nginx 服务 sudo systemctl start nginx # 设置开机自启 sudo systemctl enable nginx # 检查服务状态 sudo systemctl status nginx # 验证 Nginx 是否运行 curl http://localhost -
七、配置 Nginx 默认页面
-
修改默认配置文件
sudo vim /usr/local/nginx/conf/nginx.conf确保
server块中包含以下内容:server { listen 80; server_name localhost; location / { root /usr/local/nginx/html; index index.html index.htm; try_files $uri $uri/ =404; } } -
创建默认页面
# 切换到root账户后 echo "Hello, Nginx!" > /usr/local/nginx/html/index.html -
重新加载配置
sudo systemctl reload nginx
-
再次重复步骤六进行测试。
-
注意事项:
-
为什么会出现 403 错误?
在步骤六中,您可能遇到以下问题导致 403 错误:
- 权限问题:Nginx 的工作进程用户(如
nobody或nginx)无法访问网站目录(例如/root/webUi/),因为/root目录默认只有 root 用户可访问。 - 索引文件缺失:如果未正确配置
index指令或目录中缺少index.html文件,Nginx 会返回 403 错误。 - 配置错误:
root或alias路径指向错误,或权限不足。
- 权限问题:Nginx 的工作进程用户(如
-
为什么步骤 8 后恢复正常?
在步骤 8 中,:
- 修改了 Nginx 默认页面配置:确保
root指向/usr/local/nginx/html/,并添加了index.html文件。 - 设置了正确的权限:确保 Nginx 用户(如
nginx)有权限访问/usr/local/nginx/html/目录。 - 验证了服务状态:通过
curl http://localhost确认页面能正常显示。
这些操作解决了之前的权限或配置问题,因此 403 错误消失。
- 修改了 Nginx 默认页面配置:确保
-
3. Docker 容器部署(推荐容器化环境)
-
一、安装必要的一些系统工具
sudo apt update sudo apt install ca-certificates curl gnupg运行完该指令后,会提示重启虚拟机。
-
二、信任 Docker 的 GPG 公钥
sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg sudo chmod a+r /etc/apt/keyrings/docker.gpg -
三、写入阿里云的软件源信息
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null -
四、安装docker相关组件
sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -
五、安装指定版本Docker-CE 版本
首先,查找源里所有的版本号
apt-cache madison docker-ce
接着,选择所需要的版本,
安装指定版本的Docker-CE: (VERSION例如上面的5:28.1.1-1ubuntu.22.04jammy)
sudo apt -y install docker-ce=[VERSION]下面可直接复制使用
sudo apt -y install docker-ce=5:28.1.1-1~ubuntu.22.04~jammy -
六、对docker镜像源进行更换(2024年6月后,国外镜像源已无法链接)
-
创建目录(如果已经有该目录,则忽略此步骤
sudo mkdir -p /etc/docker -
对配置文件写入可用镜像源
根据2025.3.16最新测试的可用镜像源,我在此给出命令行的操作方法
特别提醒,不管
daemon.json文件是否存在,均可直接使用,不会报错。直接将下列文本粘贴到终端中,然后回车运行即可。
sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": [ "https://docker.m.daocloud.io", "https://docker.imgdb.de", "https://docker-0.unsee.tech", "https://docker.hlmirror.com", "https://docker.1ms.run", "https://func.ink", "https://lispy.org", "https://docker.xiaogenban1993.com" ] } EOF -
重启docker服务
sudo systemctl daemon-reload && sudo systemctl restart docker -
查看docker版本
sudo docker version
-
-
七、拉取 Nginx 镜像
从 Docker Hub 拉取官方 Nginx 镜像(使用你配置的镜像加速器会更快):
docker pull nginx:latest -
八、创建Nginx配置文件
启动前需要先创建Nginx外部挂载的配置文件( /home/nginx/conf/nginx.conf)
之所以要先创建 , 是因为Nginx本身容器只存在/etc/nginx 目录 , 本身就不创建 nginx.conf 文件
当服务器和容器都不存在 nginx.conf 文件时, 执行启动命令的时候 docker会将nginx.conf 作为目录创建 , 这并不是我们想要的结果# 创建挂载目录 mkdir -p /home/nginx/conf mkdir -p /home/nginx/log mkdir -p /home/nginx/html -
生成默认配置文件
# 生成容器 docker run --name nginx -p 9001:80 -d nginx # 将容器nginx.conf文件复制到宿主机 docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf/nginx.conf # 将容器conf.d文件夹下内容复制到宿主机 docker cp nginx:/etc/nginx/conf.d /home/nginx/conf/conf.d # 将容器中的html文件夹复制到宿主机 docker cp nginx:/usr/share/nginx/html /home/nginx/ -
停止并删除临时容器
# 找到nginx对应的容器id docker ps -a # 关闭该容器 docker stop nginx # 删除该容器 docker rm nginx # 删除正在运行的nginx容器 docker rm -f nginx -
运行 Nginx 容器
docker run \ -p 9002:80 \ --name nginx \ -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /home/nginx/conf/conf.d:/etc/nginx/conf.d \ -v /home/nginx/log:/var/log/nginx \ -v /home/nginx/html:/usr/share/nginx/html \ -d nginx:latest-p 9002:80- 作用:将宿主机的 9002 端口 映射到容器的 80 端口。
--name nginx- 作用:为容器指定名称
nginx,便于后续管理(如重启、删除)。
- 作用:为容器指定名称
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf- 作用:挂载宿主机的
nginx.conf文件到容器内的对应路径。
- 作用:挂载宿主机的
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d- 作用:挂载宿主机的
conf.d目录到容器内,用于存放虚拟主机配置。
- 作用:挂载宿主机的
-v /home/nginx/log:/var/log/nginx- 作用:挂载宿主机目录到容器内日志路径。
-v /home/nginx/html:/usr/share/nginx/html- 作用:挂载宿主机目录到容器内默认网页内容路径。
-d- 作用:后台运行容器,不占用当前终端。
nginx:latest- 作用:使用本地已有的
nginx:latest镜像启动容器。
- 作用:使用本地已有的
\- 作用:Shell 命令换行符,用于将长命令分成多行书写。
-
结果测试



3027

被折叠的 条评论
为什么被折叠?



