Appearance
Nginx 安装教程
1. 源码安装
1.1. 概述
源码安装需要 GCC 和 Automake 工具。另外 Nginx 的一些模块需要依赖第三方库,比如 pcre(支持 rewrite),zlib(支持 gzip 模块)和 openssl(支持 ssl 模块)等。
在 Unix/Linux 环境下,如果使用源码安装软件的话,一般会经过三个步骤:
configure:这一步一般是用来生成 Makefile 文件,为下面的make做准备。一般情况下,configure后面会带一些参数,对编译和安装进行控制。比如说,一般会有一个prefix参数,用于控制程序的安装路径;make:这一步就是对程序中的源文件进行编译,生成可执行文件。这个命令其实就是执行第一步生成的 Makefile 文件,按照文件的规则自动的编译源文件;make install:这个命令是执行 Makefile 文件中的install标签内容。用来安装上一步生成的可执行文件;
1.2. 安装步骤
1.2.1. 安装依赖
1.2.1.1. RHEL
安装 gcc:
Bash$ yum install gcc-c++安装 PCRE:
Bash$ yum install -y pcre pcre-devel安装 zlib:
Bash$ yum install -y zlib zlib-devel安装 OpenSSL:
Bash$ yum install -y openssl openssl-devel
1.2.1.2. Debian
安装 gcc:
Bash$ apt install -y build-essentialNote:Debian 可以通过安装
build-essential来安装 GCC 编译器。安装 PCRE:
Bash$ apt install -y libpcre3-dev安装 zlib:
Bash$ apt install -y zlib1g-dev安装 OpenSSL:
Bash$ apt install -y openssl libssl-dev
1.2.2. 下载 Nginx
进入
usr/local/src目录:Bash$ cd /usr/local/src下载 tar 包,到 Nginx 的下载页面,右键点击 nginx-1.24.0,选择复制链接地址,然后通过 wget 下载源码:
Bash$ wget http://nginx.org/download/nginx-1.24.0.tar.gz解压 tar:
Bash$ tar -xf nginx-1.24.0.tar.gz
1.2.3. vim 语法高亮 可选
ls查看文件夹(/usr/local/src/nginx-1.24.0)内容:Bash$ ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src1
2把
contrib/vim目录里面所有的文件复制到/root/.vim目录下:Bash$ cp -a contrib/vim/* /root/.vim/后续再编辑
nginx.conf文件时,便支持语法高亮;
1.2.4. 编译 & 安装
创建 nginx 用户(名称可以改):
Bash$ useradd -s /sbin/nologin -M nginxNote
-s /sbin/nologin:指定用户登录的 shell 为/sbin/nologin,表示该用户不允许登录系统;-M:表示不需要创建用户的家目录;
配置:
configure的参数可以参考官方 Nginx 配置文档,也可以通过configure --help来查看完整的命令参数。以下是参考的配置选项:Bash$ ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_body_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --with-threads \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_ssl_preread_module \ --with-pcre1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25如果没有输出错误信息则表示成功的创建了 Makefile 文件。
Note
如果直接使用以上配置文件则需先创建
/var/cache/nginx文件夹:Bash$ mkdir /var/cache/nginx编译:
Bash$ make安装:
Bash$ make install授权:
Bash$ chown -R nginx /usr/local/nginx注册 & 启动服务:
创建
nginx.service文件:Bash$ vim /usr/lib/systemd/system/nginx.service输入以下内容:
INI[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target1
2
3
4
5
6
7
8
9
10
11
12
13重新加载服务配置文件:
Bash$ systemctl daemon-reload开机时启动 Nginx:
Bash$ systemctl enable nginx启动 Nginx 服务:
Bash$ systemctl start nginx
2. 安装包安装
官方文档:https://nginx.org/en/linux_packages.html。
官方文档安装教程中,在执行 yum install nginx / apt install nginx 之前会有很多命令,建议参照一步步执行。这些命令的作用是确保你从官方的 Nginx 仓库安装最新的 Nginx 版本,而不是从 Debian 或 Ubuntu 的默认仓库中安装可能较旧的版本。
3. 测试
使用 curl http://localhost/ 命令测试是否搭建成功:
Bash
$ curl http://localhost/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
4. 示例:静态网站
创建 web 站点文件夹:
Bash$ mkdir -p /var/webroot/yoursitename创建
index.html:Bash$ vim /var/webroot/yoursitename/index.htmlindex.html内容:HTML<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> Hello Nginx </body> </html>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15授权:
Bash$ chown -R nginx /var/webroot/配置:
Bash$ vim /usr/local/nginx/conf/nginx.confNginx# ... http { # ... server { listen 80; server_name *.yourdomain.com; location / { root /var/webroot/yoursitename; index index.html index.htm; } # ... } # ... }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19Note:关于
nginx.conf详细的配置说明文档,请参考 Nginx 官方文档。