前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Nginx】Windows下安装Nginx`的最简单的方法

【Nginx】Windows下安装Nginx`的最简单的方法

作者头像
DDGarfield
发布2022-06-23 19:36:49
1.6K0
发布2022-06-23 19:36:49
举报
文章被收录于专栏:加菲的博客加菲的博客

Nginx是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。去年时间听说Igor Sysoev被俄罗斯警方带走了,不知道释放。博主是经常使用nginx的,比如博主的博客网站,公司前后端分离项目等等。

如果服务器是Windows系统,怎么安装与配置nginx;博主之前是使用的如下方法,把nginx安装为windows服务:

从前

1.Windows Service Wrapper

借助Windows Service Wrapper工具,下载该工具后,将其放在 Nginx安装目录下,并重命名为nginx-service.exe,创建配置文件nginx-service.xml(名字要和工具名一样),创建nginx-service.exe.config(为支持NET 4.0 runtime,默认只支持NET 2.0 runtime)

目录如下:

2.nginx-service.xml

nginx-service.xml

代码语言:javascript
复制
<service>
  <id>nginx</id>
  <name>Nginx Service</name>
  <description>Nginx Service</description>
  <logpath>D:\xampp\nginx\logs</logpath>
  <log mode="roll-by-size">
    <sizeThreshold>10240</sizeThreshold>
    <keepFiles>8</keepFiles>
  </log>
  <executable>D:\xampp\nginx\nginx.exe</executable>
  <startarguments>-p D:\xampp\nginx</startarguments>
  <stopexecutable>D:\xampp\nginx\nginx.exe</stopexecutable>
  <stoparguments>-p D:\xampp\nginx -s stop</stoparguments>
</service>

3.nginx-service.exe.config

nginx-service.exe.config

代码语言:javascript
复制
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" />
    <supportedRuntime version="v4.0" />
  </startup>
  <runtime>
    <generatePublisherEvidence enabled="false"/> 
  </runtime>
</configuration>

4.安装服务

最后powershell中运行命令:

代码语言:javascript
复制
nginx-service.exe install

更方便的方法

上面的方法一步步还是比较麻烦的,下面还是介绍一下博主个人认为最简单的方法,如果您还没有安装windows包管理器chocolatey,请参考博文【Tool】Windows软件包管理器——chocolatey

1.下载安装

代码语言:javascript
复制
choco install nginx

这里有两点需要注意:

  1. 通过choco安装nginx,会配套安装NSSM,这是个好东西,可以把nginx注册成windows服务,相当于linux下的SupervisorPM2
  2. 通过choco安装nginx,在命令行中,最后一次需要您同意选择y[Yes]之前,注意命令行窗口回写的内容,大致意思:即将执行C:\ProgramData\chocolatey\lib\nginx\tools\chocolateyInstall.ps1脚本:
代码语言:javascript
复制
$toolsDir = Split-Path -parent $MyInvocation.MyCommand.Definition
. "$toolsDir\helpers.ps1"
   
$pp = Get-PackageParameters
   
$arguments = @{
   packageName = $env:chocolateyPackageName
   file        = "$toolsDir\nginx-1.17.8.zip"
   destination = if ($pp.installLocation) { $pp.installLocation } else { Get-ToolsLocation }
   port        = if ($pp.Port) { $pp.Port } else { 80 }
   serviceName = if ($pp.NoService) { $null } elseif ($pp.serviceName) { $pp.serviceName } else { 'nginx' }
}
   
if (-not (Assert-TcpPortIsOpen $arguments.port)) {
   throw 'Please specify a different port number...'
}
   
Install-Nginx $arguments

注意看port那行,没错,80端口,nginx默认,所以在继续之前,请检查下80端口是否被占用,一般都是被占用的,毕竟windows服务器的IIS默认就把80端口占了,只要被占用就会安装失败。

那么怎么办?这里算是一点骚操作:您只需要编辑上面的这个脚本,把80修改为一个未被占用的端口。然后回到刚才没有选择Y的命令窗口,选择Y,继续执行,就能安装成功。

2.修改脚本

比如博主修改内容:

代码语言:javascript
复制
$toolsDir = Split-Path -parent $MyInvocation.MyCommand.Definition
. "$toolsDir\helpers.ps1"
   
$pp = Get-PackageParameters
   
$arguments = @{
   packageName = $env:chocolateyPackageName
   file        = "$toolsDir\nginx-1.17.8.zip"
   destination = if ($pp.installLocation) { $pp.installLocation } else { Get-ToolsLocation }
   port        = if ($pp.Port) { $pp.Port } else { 81 }
   serviceName = if ($pp.NoService) { $null } elseif ($pp.serviceName) { $pp.serviceName } else { 'nginx' }
}
   
if (-not (Assert-TcpPortIsOpen $arguments.port)) {
   throw 'Please specify a different port number...'
}
   
Install-Nginx $arguments

安装好的nginxC:\tools

nginx的配置文件也会默认如下面的初始化配置:

代码语言:javascript
复制
#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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

3.安装成功

是不是很简单?

------------------- End -------------------

See Also:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 加菲的博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 从前
    • 1.Windows Service Wrapper
      • 2.nginx-service.xml
        • 3.nginx-service.exe.config
          • 4.安装服务
          • 更方便的方法
            • 1.下载安装
              • 2.修改脚本
                • 3.安装成功
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档