Tim's Note

试问Coding应不好,却道:此心安处是吾乡

0%

CentOS7 编译安装 Nginx

Nginx 在 CentOS 上安装可以使用 yum 或源码安装,推荐使用源码安装的方式,因为 yum 的版本比较旧,而且使用源码可以自定义功能,方便业务的上的使用,源码安装需要提前准备标准的编译器和依赖即可。下面记录一下如何编译安装 Nginx,避免在安装时各种缺少依赖库的情况!

安装 Nginx

1、安装 gcc 编译器

1
yum -y install gcc

2、nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要安装 pcre 库

1
yum install -y pcre pcre-devel

3、 nginx 使用 zlib 对 http 包的内容进行 gzip,所以需要安装 zlib

1
yum install -y zlib zlib-devel

4、openssl 是 nginx 的 https 模块需要的,所以需要安装 openssl

1
yum install -y openssl openssl-devel

5、下载 Nginx 源码包

1
wget http://nginx.org/download/nginx-1.9.9.tar.gz

6、把压缩包解压缩到 /usr/local 下

1
2
tar -zxvf  nginx-1.9.9.tar.gz
mv nginx-1.9.9 /usr/local/

7、进入 /usr/local/nginx-1.9.9

1
cd /usr/local/nginx-1.9.9

8、编译安装

1
2
3
4
5
./configure

make

make install

9、安装完成不想配置环境变量的话可以建立软链接

1
ln -s /usr/local/nginx/sbin/nginx/usr/local/bin/nginx

10、配置文件的修改

1
2
cp /usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf.back
vim /usr/local/nginx/conf/nginx.conf

启动 / 停止

1、启动

1
nginx

2、 快速停止

1
nginx -s stop

3、正常停止

1
nginx -s quit

4、重新加载配置文件

1
nginx -s reload

配置 HTTPS

上面讲述了如何编译安装 Nginx,要想用 HTTPS, 我们只需要在原有的基础上添加 ssl 模块就行了

来到解压目录

1
cd /usr/local/nginx-1.9.9
1
2
3
./configure --with-http_ssl_module

make

停止 Nginx

1
nginx -s stop

把之前的 nginx 先备份一下,然后把新的程序复制过去覆盖之前的即可

1
2
3
cp /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.bak

cp objs/nginx/usr/local/nginx/sbin/nginx

接下来配置证书

mark

解压后得到两个文件,放在服务器的任意目录

mark

接下来就是修改 Nginx 的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#user  nobody;
user root;
worker_processes 2;

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name zouchanglin.cn;
return 301 https://zouchanglin.cn;

}
# HTTPS server
server {
listen 443;
server_name zouchanglin.cn;

ssl on;
ssl_certificate /root/nginx.crt;
ssl_certificate_key /root/nginx.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root /root/hexo/public;
index index.html index.htm;
}
}
}

注意 ssl on; 这个配置一定要加上!

欢迎关注我的其它发布渠道