Nginx 系列02(版本选择、下载与安装)

目录

版本选择

Linux 系统

使用包管理器安装(以 Ubuntu 为例)

使用包管理器安装(以 CentOS 为例)

从源代码安装

Windows 系统

1. 下载安装包

2. 解压文件

3. 修改配置文件(可选)

4. 启动 Nginx

5. 检查是否启动

macOS 系统

Docker 环境


版本选择

  • Nginx 开源版:分为主线版本(Mainline)和稳定版本(Stable)。主线版本一般为奇数版本号,是主要开发版本,包含最新特性和功能,但可能稳定性稍差,适合测试环境和开发环境用于测试新特性和参与开发。稳定版本通常为偶数版本号,有最新的性能优化和安全修复,适合生产环境。
  • Nginx Plus 商业版:相比开源版增加了更多功能和技术支持,适合对功能和支持要求较高的企业用户,但需要付费。
  • OpenResty:将 Nginx 框架用 Lua 语言同步开发实现,兼具高性能和高开发效率,适合需要使用 Lua 进行开发或对 Nginx 功能有扩展需求的场景。
  • Tengine:由阿里基于 Nginx 主干版本开发,在阿里生态下经过严格考验,但无法与 Nginx 官方版本同步升级,一般不推荐普通用户使用。

Linux 系统

使用包管理器安装(以 Ubuntu 为例)
# 1. 更新软件源
sudo apt update

# 2. 安装Nginx
sudo apt install nginx -y

# 3. 检查Nginx服务状态
sudo systemctl status nginx
使用包管理器安装(以 CentOS 为例)
# 1. 更新系统软件包列表
sudo yum update -y

# 2. 安装EPEL存储库(如果需要)
sudo yum install epel-release -y

# 3. 安装Nginx
sudo yum install nginx -y

# 4. 检查Nginx服务状态
sudo systemctl status nginx
从源代码安装
# 1. 安装必要的编译工具和依赖库
sudo yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

# 2. 进入/usr/local目录
cd /usr/local

# 3. 下载Nginx源代码,这里以nginx-1.24.0为例
wget https://nginx.org/download/nginx-1.24.0.tar.gz

# 4. 解压下载的安装包
tar xvf nginx-1.24.0.tar.gz

# 5. 进入解压后的目录
cd nginx-1.24.0

# 6. 配置编译选项
# --prefix 指定安装路径
# --with-http_stub_status_module 启用状态监控模块
# --with-http_ssl_module 启用SSL模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

# 7. 编译
make

# 8. 安装
sudo make install

# 9. 添加环境变量
# 编辑/etc/profile文件
sudo bash -c 'echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile'

# 10. 重新加载环境变量
source /etc/profile

Windows 系统

1. 下载安装包

手动访问nginx 官方网站的下载页面,选择适合 Windows 系统的稳定版本(通常是.zip文件格式,如nginx-1.22.1.zip)进行下载。

2. 解压文件

假设将文件解压到C:\nginx目录,可使用以下 PowerShell 命令(前提是下载的文件在Downloads目录):

Expand-Archive -Path "$env:USERPROFILE\Downloads\nginx-1.22.1.zip" -DestinationPath "C:\"
3. 修改配置文件(可选)

以下是使用 PowerShell 简单修改配置文件的示例(假设要修改监听端口为 8080):

# 定义配置文件路径
$configPath = "C:\nginx\conf\nginx.conf"

# 读取配置文件内容
$configContent = Get-Content -Path $configPath

# 替换监听端口
$newConfigContent = $configContent -replace "listen       80;", "listen       8080;"

# 将修改后的内容写回配置文件
$newConfigContent | Set-Content -Path $configPath
4. 启动 Nginx
# 进入Nginx安装目录
Set-Location -Path "C:\nginx"

# 启动Nginx
Start-Process -FilePath ".\nginx.exe"
5. 检查是否启动
# 查看端口监听情况
Get-NetTCPConnection -LocalPort 8080  # 如果前面修改为8080端口

macOS 系统

# 1. 安装Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/homebrew/install/HEAD/install.sh)"

# 2. 更新Homebrew
brew update

# 3. 安装Nginx
brew install nginx

# 4. 启动Nginx服务
brew services start nginx

# 5. 验证安装
curl http://localhost:8080

Docker 环境

# 1. 拉取Nginx镜像
docker pull nginx

# 2. 创建配置和数据目录
mkdir -p /data/nginx/{conf,logs,html}

# 3. 编写Nginx配置示例(创建一个简单的nginx.conf文件)
cat << EOF > /data/nginx/conf/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  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}
EOF

# 4. 启动Nginx容器
docker run -d -p 80:80 -p 443:443 --name nginx \
  -v /data/nginx/html:/usr/share/nginx/html \
  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /data/nginx/logs:/var/log/nginx \
  nginx

# 5. 验证部署
curl http://localhost

以上代码示例涵盖了不同操作系统和环境下 Nginx 的下载、安装和基本使用步骤。在实际使用中,可根据具体需求进行调整。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪九天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值