CentOS7安装Nginx
# CentOS7 安装 Nginx
记一次 CentOS7 安装 Nginx 过程
- 安装 gcc
- 查看版本:
gcc -v
- 安装命令:
yum -y install gcc
- 安装 pcre、pcre-devel
nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel
- zlib 安装
yum install -y zlib zlib-devel
- 安装 openssl
web 安全通信的基石
yum install -y openssl openssl-devel
- 安装 Nginx
查看 Nginx 版本 (opens new window)
选择一个稳定版本
- 切换到 /usr/local 目录:
cd /usr/local
wget -c https://nginx.org/download/nginx-1.18.0.tar.gz
- 解压:
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
- 执行以下 3 个命令
# 使用默认配置
./configure
# 编译安装
make
make install
1
2
3
4
5
6
2
3
4
5
6
查找安装路径:
whereis nginx
- 默认都是这个路径 /usr/local/nginx
切换到 nginx 安装目录
cd /usr/local/nginx/sbin/
./nginx # 启动
./nginx -s stop # 停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit # 退出停止,等待nginx进程处理完任务再进行停止
./nginx -s reload # 重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效
# 重启nginx,建议先停止,再启动
./nginx -s stop
./nginx
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 查看 nginx 服务是否启动成功:
ps -ef | grep nginx
# 安装上传下载命令 rz 和 sz
yum install lrzsz -y
# 安装 unzip
yum install -y unzip zip
# 添加 ssl 证书
以阿里云为例
- 阿里云免费购买地址 (opens new window)
- 点 "选购证书" 进入购买页面

- 进入证书管理 (opens new window)页面,选择下载 Nginx 版本

- 先关闭 Nginx:
/usr/local/nginx/sbin/nginx -s stop
- 下载后将会得到
.key
和.pem
两个文件,将证书上传到服务器/usr/local/nginx/conf/ssl
下(例子)
https 默认端口为 443,在server
添加如下内容
server {
listen 80;
listen 443 ssl default;
server_name localhost;
ssl_certificate ./ssl/5393413_coderly.cn.pem; # 上传的 .pem 文件(相对路径或绝对路径)
ssl_certificate_key ./ssl/5393413_coderly.cn.key; # 上传的 .key 文件(相对路径或绝对路径)
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root /usr/web/html/blog; # 选择你网站根目录
index index.html index.htm;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- 重新启动 Nginx:
/usr/local/nginx/sbin/nginx
# 问题
- the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
提示
nginx 缺少 http_ssl_module 模块,编译安装的时候带上–with-http_ssl_module 配置就行了
- 执行命令:
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments:
# arguments 参数为空, 所以报错
1
2
3
4
2
3
4
优先关闭 nginx:
/usr/local/nginx/sbin/nginx -s stop
cd nginx-1.18.0
重新执行新配置信息
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
重新启动:
/usr/local/nginx/sbin/nginx
如果上述操作之后还是报错
- 备份 nginx 配置
- 删除 nginx
- 重新 安装
上次更新: 2021/03/29, 09:49:46