Nginx 安装

Nginx 安装和简单使用

Nginx 下载地址nginx v1.24.0 下载链接nginx 源码编译配置文档

注:安装环境为 Centos 7

  • 下载 nginx 源码,解压源码包
1
2
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxf nginx-1.24.0.tar.gz
  • 创建 nginx 运行账户
1
useradd -M -s /sbin/nologin nginx
  • 安装编译依赖

    gcc 编译时依赖 gcc 环境,pcre 提供 nginx 支持重写功能

    zlib 压缩 / 解压工具,openssl 安全套接字层密码库,通信加密

1
yum install -y gcc gcc-c++ automake make openssl openssl-devel pcre pcre-devel zlib zlib-devel
  • 检查安装环境,编译,安装
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 进入 nginx 源码解压目录
cd nginx-1.24.0

# 检查安装环境
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module

# 编译 nginx 源码
make

# 安装
make install

参数说明:

参数 说明
–prefix=path 定义保存服务文件目录,默认目录 /usr/local/nginx
–with-http_ssl_module 构建时添加 HTTPS 模块支持
–with-http_stub_status_module 该模块提供 nginx 基本状态信息的访问

编辑 nginx systemd 服务配置 /usr/lib/systemd/system/nginx.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[Unit]
Description=nginx - high performance web server
After=network.target

[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
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.target

注:默认 nginx 配置文件路径为 prefix/conf/nginx.conf ,这里就是 /usr/local/nginx/conf/nginx.conf

nginx 配置生成网站

主配置文件 config/nginx.conf

 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
39
40
41
42
43
44
45
46
47
48
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        access_log  logs/host.access.log  main;

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

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}

默认配置文件名为 nginx.conf,放置在目录 /usr/local/nginx/conf/etc/nginx/usr/local/etc/nginx 中。

指定具体配置文件启动:

1
nginx -c /config/path/nginx.conf

启动、停止和重新加载配置。 nginx 启动后,可以通过使用 -s 参数调用可执行文件来控制。

1
nginx -s signal

singal (信号)参数列表:

  • stop — 快速关闭 (fast shutdown)
  • quit — 优雅的关闭(graceful shutdown)
  • reload — 重新加载配置文件(reloading the configuration file)
  • reopen — reopening the log files

示例:要停止 nginx 进程并等待工作进程完成当前请求的服务,可以执行以下命令:

1
nginx -s quit

该命令应在启动 nginx 的同一用户下执行

相关内容