基于源代码安装Nginx

软件安装5个月前更新 apegeek
127 0

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.2 解压&重命名

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

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

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

2.2 解压&重命名

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

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

#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

3.2 解压&重命名

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

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

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++

3.4 编译安装

进入nginx目录,执行命令

cd /opt/nginx
./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
make & make install

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;
        }
    }}

4. 服务化配置

4.1 编辑脚本

vi /lib/systemd/system/nginx.service

输入以下内容:

[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

完成后保存并退出。

4.2 开机启动

systemctl daemon-reload

systemctl enable nginx.service

4.3 启动服务

systemctl start nginx.service
© 版权声明

相关文章

暂无评论

暂无评论...