Nginx安装

Linux 源码安装Nginx

  • 下载稳定版nginx:http://nginx.org/en/download.html
  • 安装编译工具及依赖库
    yum -y install gcc gcc-c++ autoconf automake make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
    说明:
  • PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库
    注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
  • zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
  • OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
# wget http://nginx.org/download/nginx-1.20.0.tar.gz
# tar -zxvf nginx-1.20.0.tar.gz
# cd nginx-1.20.0
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module  --with-http_realip_module --with-http_flv_module --with-http_mp4_module
# make
# make install
# cd /usr/local/nginx/
# mkdir temp
# sbin/nginx -V
# sbin/nginx

设置Nginx开机启动

  • 查看nginx服务状态:service nginx status
    nginx-status

Windows 压缩包安装Nginx

  • 下载Windows最新稳定版nginx
  1. 解压后在 nginx-1.20.0 目录下新建启动,重启,关闭脚本
  • 新建 startup.bat
@echo off
title "startup nginx"
echo "nginx is starting on port 80"
start "" "nginx.exe"
rem 等待5秒后退出
CHOICE /T 5 /C ync /CS /D y /n
exit
  • 新建 restart.bat
@echo off
title "restart nginx"
tasklist | findstr /i "nginx.exe"
echo "nginx is running, stopping..."
tskill nginx
echo "stop nginx"
nginx.exe -t
nginx.exe -v
start nginx.exe
echo "nginx is starting on port 80"
rem 等待3秒后退出
rem CHOICE /T 3 /C ync /CS /D y /n
timeout /nobreak /t 3
exit
  • 新建 stop.bat
@echo off
title "stop nginx"
tasklist | findstr /i "nginx.exe"
echo "nginx is running, stopping..."
TASKKILL /F /IM nginx.exe /T
echo "stop ok"
  • 查看nginx服务状态:tasklist | findstr /i "nginx.exe"
    nginx-tasklist

Nginx 参考配置


#user  nobody;
worker_processes  auto;
#配置Nginx worker进程最大打开文件数
worker_rlimit_nofile 65535;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    # Linux下多路复用IO接口select/poll的增强版本,使用epoll模式,增加nginx系统并发连接能力
    use epoll;
    multi_accept on;
    worker_connections 20480;
    #「惊群问题」当一个新连接到达时,如果激活了accept_mutex,那么多个Worker将以串行方式来处理,其中有一个Worker会被唤醒,其他的Worker继续保持休眠状态;
    # 如果没有激活accept_mutex,那么所有的Worker都会被唤醒,不过只有一个Worker能获取新连接,其它的Worker会重新进入休眠状态
    accept_mutex on;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    # 隐藏版本号的作用:通过你所用的版本,找其漏洞,进行攻击
    server_tokens off;
    add_header X-Frame-Options SAMEORIGIN;
     
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64K;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
    gzip_vary on;
    #配置代理参数
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 10;
    proxy_send_timeout 65;
    proxy_read_timeout 65;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    #缓存配置
    proxy_cache_key '$host:$server_port$request_uri';
    proxy_temp_file_write_size 64m;
    proxy_temp_path temp/proxy_temp_path;
    proxy_cache_path temp/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
     
    #FastCGI优点是把动态语言解析和HTTP服务器分离开来
    #针对504错误修改参数
    send_timeout 60;
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 128k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    #针对504错误 增加超时时间
    fastcgi_connect_timeout 120;
    fastcgi_send_timeout 120;
    fastcgi_read_timeout  120;
    
    #从哪个header头检索出所要的IP地址
    real_ip_header X-Forwarded-For;
    #真实服务器上一级代理的IP地址或者IP段,可以写多行
    set_real_ip_from 0.0.0.0/0;
    #递归的去除所配置中的可信IP。排除set_real_ip_from里面出现的IP。如果出现了未出现这些IP段的IP,那么这个IP将被认为是用户的IP。
    real_ip_recursive on;
    
    #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;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #location = / {
        #  rewrite ^/(.*)$ https://www.baidu.com/$1 break;
        #}
        # 查看Nginx的一些状态信息
        location /status {
            stub_status;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

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

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

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

}
### 不同操作系统上的 Nginx 安装方法 #### Ubuntu 和 Debian 系统 对于基于 Debian 的 Linux 发行版,可以使用 APT 包管理器来安装 Nginx。 ```bash sudo apt update sudo apt install nginx ``` 这会自动下载并安装最新稳定版本的 Nginx 及其依赖项[^1]。 #### CentOS, RHEL 或 Fedora 系统 在 Red Hat 类似发行版中,YUM 是默认的包管理系统。可以通过 YUM 来安装 Nginx: ```bash sudo yum install epel-release sudo yum install nginx ``` 上述命令先启用了 EPEL 仓库,因为官方源可能不包含最新的 Nginx 版本;接着通过 `yum` 下载并安装 Nginx[^4]。 #### 麒麟操作系统 (Kylin OS) 针对麒麟操作系统的环境,有特定的方式来进行 Nginx 的离线安装。首先需要获取到 Nginx 的 RPM 文件,之后再执行本地安装过程: ```bash yum -y install --downloadonly --downloaddir=/home/nginx_install nginx # 获取离线安装包 rpm -ivh /home/nginx_install/nginx*.rpm # 执行RPM安装 ``` 完成安装后,可通过如下方式启动服务以及验证是否正常工作: ```bash systemctl start nginx # 启动Nginx服务 curl http://localhost # 测试访问 ``` 另外,在银河麒麟系统中也可以直接进入解压后的目录并通过脚本来启动 Nginx 实例: ```bash cd nginx/sbin ./nginx # 使用自带脚本启动Nginx ``` 此时可以在浏览器里输入服务器 IP 地址来确认 Web 页面能否被正确展示出来[^3]。 #### macOS 环境下 Homebrew 方式 如果是在 Mac 上,则推荐利用 Homebrew 工具简化整个流程: ```bash brew install nginx # 利用Homebrew安装Nginx ``` 以上就是在多种主流平台上部署 Nginx 的基本指导说明。每种平台都有各自的特点和最佳实践方案可供选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值