Skip to content

Nginx 安装和配置

本安装教程已在Debian、Ubuntu、AlmaLinux等系统上测试成功!

跳转测试

Nginx

安装前准备

创建一个临时目录,把 NginxOpenssl的源码包下载好。

bash
### 创建临时目录
mkdir temp && cd temp

### 下载 nginx和 openssl源码包
wget --no-check-certificate https://nginx.org/download/nginx-1.24.0.tar.gz && tar xzvf nginx-1.24.0.tar.gz && cd nginx-1.24.0
wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1l.tar.gz && tar xzvf openssl-1.1.1l.tar.gz

安装依赖

bash
### ubuntu、debian系列依赖安装
apt-get update -y
apt-get install zlib1g-dev libpcre3 libpcre3-dev automake make -y
apt-get install curl gcc zip vim wget unzip build-essential libtool libssl-dev automake autoconf -y

### centos 系列依赖安装
yum install -y wget gcc gcc-c++ vim libtool zip perl-core zlib-devel pcre* unzip automake autoconf make curl vim

编译安装 Nginx

bash
### 测试
./configure --prefix=/usr/local/nginx-1.24.0 --with-openssl=../openssl-1.1.1l --with-openssl-opt='enable-tls1_3' --with-stream_ssl_preread_module \
--with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module \
--with-http_sub_module --with-stream --with-stream_ssl_module --with-http_mp4_module --with-http_flv_module 

### 编译
make && make install

启动配置

bash
### 启动配置
cat >/etc/systemd/system/nginx-1.24.0.service<<-EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx-1.24.0/sbin/nginx
ExecReload=/usr/local/nginx-1.24.0/sbin/nginx -s reload
ExecStop=/usr/local/nginx-1.24.0/sbin/nginx -s quit
PrivateTmp=true
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=multi-user.target
EOF

### 启动 Nginx
systemctl restart nginx-1.24.0.service

### Nginx 状态
systemctl status nginx-1.24.0.service

### 开机自启动
systemctl enable nginx-1.24.0.service