基于源代码安装Nginx

# 1. 安装Pcre

# 1.1 下载

点击这里 (opens new window)或执行下面的命令下载Pcre。

wget -O pcre-8.39.tar.gz https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz/download --no-check-certificate
1

# 1.2 解压&重命名

将压缩包上传至**/opt**目录下,并执行以下命令:

tar -xvf pcre-8.39.tar.gz
1
mv pcre-8.39 pcre
1

# 2. 安装ZLib

# 2.1 下载

点击这里 (opens new window)或执行下面的命令下载ZLib。

wget -O zlib-1.2.11.tar.gz http://prdownloads.sourceforge.net/libpng/zlib-1.2.11.tar.gz?download --no-check-certificate
1

# 2.2 解压&重命名

将压缩包上传至**/opt**目录下,并执行以下命令:

tar -xvf zlib-1.2.11.tar.gz
1
mv zlib-1.2.11 zlib
1

# 3. 安装Nginx

# 3.1 下载

点击这里 (opens new window)或执行下面的命令下载Nginx。

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

# 3.2 解压&重命名

将压缩包上传至**/opt**目录下,并执行以下命令:

tar zvxf nginx-1.16.1.tar.gz
1
mv nginx-1.16.1 nginx
1

# 3.3 依赖库安装

确认如果没有安装以下库,则需要执行安装!

yum install libtool
yum install libtool-ltdl
yum install libtool-ltdl-devel
yum -y install openssl openssl-devel
yum install -y gcc gcc-c++
1
2
3
4
5

# 3.4 编译安装

进入nginx目录,执行命令

cd /opt/nginx
1
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/nginx --conf-path=/opt/nginx/nginx.conf --pid-path=/opt/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre --with-zlib=../zlib
1
make & make install
1

# 3.5 修改配置

根据实际情况,修改nginx.conf文件,调整server节点配置。

#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 {
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 1000m;

    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"';

    access_log  logs/access.log  main;

    gzip  on;

    #vhost_traffic_status_zone;
    #vhost_traffic_status_filter_by_host on;

    proxy_headers_hash_max_size 51200;
    proxy_headers_hash_bucket_size 6400;

    upstream providers {
    	server localhost:8002;
    }

    server {
        listen       8888;
        server_name  localhost;
        location / {
            try_files $uri $uri/ @router;
            index  index.html;
            root /opt/ota/html/;
        }

        location /api/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-For $http_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass   http://providers/;
        }

        location @router {
            rewrite ^.*$ /index.html last;
        }

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

    autoindex on;# 显示目录
    autoindex_exact_size on;# 显示文件大小
    autoindex_localtime on;# 显示文件时间

    server {
        listen 9003;
        charset UTF-8;
        server_name localhost;
        location /video {
            alias /opt/uav/upload/images;
            autoindex on;
        }
    }

    server {
        listen 80;
        charset UTF-8;
        server_name localhost;
        location / {
            alias /opt/uav/wiki;
            autoindex on;
        }
    }

}

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

# 4. 服务化配置

# 4.1 编辑脚本

vi /lib/systemd/system/nginx.service
1

输入以下内容:

[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/nginx
ExecReload=/opt/nginx/nginx -s reload
ExecStop=/opt/nginx/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

1
2
3
4
5
6
7
8
9
10
11
12

完成后保存并退出。

# 4.2 开机启动

systemctl daemon-reload

systemctl enable nginx.service
1
2
3

# 4.3 启动服务

systemctl start nginx.service
1
更新时间: 12/13/2022, 7:50:55 PM