130 lines
3.3 KiB
Markdown
130 lines
3.3 KiB
Markdown
---
|
||
title: "Nginx 安装"
|
||
date: 2023-03-01T18:07:00+08:00
|
||
lastmod: 2023-03-01T18:07:00+08:00
|
||
tags: ["nginx"]
|
||
categories: ["web"]
|
||
---
|
||
|
||
## 环境
|
||
- 操作系统 CentOS7.9
|
||
|
||
## 包管理器直接安装
|
||
- 创建 yum 源文件 /etc/yum.repos.d/nginx-nju.repo,内容如下
|
||
```ini
|
||
echo '[nginx-stable]
|
||
name=nginx stable repo
|
||
#baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
|
||
baseurl=https://mirrors.nju.edu.cn/nginx/rhel/$releasever/$basearch/
|
||
gpgcheck=0
|
||
enabled=1
|
||
gpgkey=https://nginx.org/keys/nginx_signing.key
|
||
module_hotfixes=true
|
||
```
|
||
|
||
- 安装 nginx
|
||
```bash
|
||
yum makecache
|
||
yum install nginx
|
||
```
|
||
|
||
## 编译安装
|
||
- 下载 stable 版本的 nginx 源码包,下载地址: [https://nginx.org/en/download.html](https://nginx.org/en/download.html)
|
||
- 解压源码包,进入源码包根目录下
|
||
- 检查依赖
|
||
```bash
|
||
./configure \
|
||
--prefix=$HOME/nginx \
|
||
--with-threads \
|
||
--with-compat \
|
||
--with-http_addition_module \
|
||
--with-http_auth_request_module \
|
||
--with-http_gunzip_module \
|
||
--with-http_gzip_static_module \
|
||
--with-http_random_index_module \
|
||
--with-http_realip_module \
|
||
--with-http_secure_link_module \
|
||
--with-http_slice_module \
|
||
--with-http_ssl_module \
|
||
--with-http_stub_status_module \
|
||
--with-http_sub_module \
|
||
--with-http_v2_module \
|
||
--with-stream \
|
||
--with-stream_realip_module \
|
||
--with-stream_ssl_module \
|
||
--with-stream_ssl_preread_module \
|
||
--with-http_dav_module \
|
||
--with-http_flv_module \
|
||
--with-http_mp4_module
|
||
```
|
||
|
||
- 在 centos 7.9 容器中编译时需要安装如下几个包
|
||
```
|
||
gcc
|
||
make
|
||
openssl-devel
|
||
pcre-devel
|
||
```
|
||
|
||
- 编译安装
|
||
```bash
|
||
make
|
||
make install
|
||
```
|
||
|
||
- 清空 nginx 配置文件($HOME/nginx/conf/nginx.conf),替换内容如下
|
||
```
|
||
#user nobody;
|
||
worker_processes auto;
|
||
error_log logs/error.log;
|
||
pid logs/nginx.pid;
|
||
worker_rlimit_nofile 65535;
|
||
|
||
events {
|
||
worker_connections 8192;
|
||
}
|
||
|
||
http {
|
||
default_type application/octet-stream;
|
||
log_format main '$remote_addr [$time_local] "$request" '
|
||
'$status $body_bytes_sent "$http_referer" '
|
||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||
access_log logs/access.log main;
|
||
gzip on;
|
||
sendfile on;
|
||
#tcp_nopush on;
|
||
keepalive_timeout 65;
|
||
include mime.types;
|
||
include http.d/*.conf;
|
||
}
|
||
|
||
stream {
|
||
log_format main '$remote_addr [$time_local] '
|
||
'$protocol $status $bytes_sent $bytes_received '
|
||
'$session_time "$upstream_addr" "$upstream_bytes_sent" '
|
||
'"$upstream_bytes_received" "$upstream_connect_time"';
|
||
access_log logs/stream.log main;
|
||
include stream.d/*.conf;
|
||
}
|
||
```
|
||
|
||
- 创建 nginx 配置子目录,清空 html 目录
|
||
```bash
|
||
mkdir -p $HOME/nginx/conf/{http.d,stream.d}
|
||
rm -f $HOME/nginx/html/*
|
||
```
|
||
|
||
- 创建一个简单的 80 端口配置文件($HOME/nginx/conf/http.d/80.conf),内容如下
|
||
```
|
||
server {
|
||
listen 80;
|
||
return 200 "Test nginx 80\n";
|
||
}
|
||
```
|
||
|
||
- 启动 nginx
|
||
```bash
|
||
$HOME/nginx/sbin/nginx
|
||
```
|
||
|