PowerShell Install Nginx

Posted CIAS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerShell Install Nginx相关的知识,希望对你有一定的参考价值。

nginx download

nginxdownload
WinSWdownload
nginx 专栏参考参考
Powershell 使用使用参数参考

前提条件

  • 开启wmi,配置网卡,参考 

一键自动化部署nginx

  • 实现下载nginx,下载WinSW服务,创建nginx 服务,创建WinSW配置文件,创建nginx.conf以下创建好了优化完成的,实际更多功能性参考我的专栏,防火墙端口配置,删除nginx。WinSW 安装包
  • 安装位置"C:\\Program Files\\nginx"
  • 配置更改"C:\\Program Files\\nginx\\nginx.conf"
powershell-install-nginx.ps1
<# Powershell Install nginx
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+  _____                       _____ _          _ _ +
+ |  __ \\                     / ____| |        | | |+
+ | |__) |____      _____ _ _| (___ | |__   ___| | |+
+ |  ___/ _ \\ \\ /\\ / / _ \\ '__\\___ \\| '_ \\ / _ \\ | |+
+ | |  | (_) \\ V  V /  __/ |  ____) | | | |  __/ | |+
+ |_|   \\___/ \\_/\\_/ \\___|_| |_____/|_| |_|\\___|_|_|+
+ +++++++++++++++++++++++++++++++++++++++++++++++++++
                                                                                                              
# Powershell Install nginx
# .\\powershell-install-nginx.ps1
#> 


$drive="c:\\"
$nginx_url="http://nginx.org/download/"
$nginx_zip="nginx-1.22.1.zip"
$nginx_site="C:\\Program Files\\"
$nginx_new="nginx"
$nginx_catalogue="nginx-1.22.1"

$WinSW_url="https://github.com/winsw/winsw/releases/download/v2.12.0/"
$WinSW_exe="WinSW-x64.exe"

Write-Host "download WinSW" -ForegroundColor Green
wget -Uri $WinSW_url$WinSW_exe -UseBasicParsing -OutFile $drive$WinSW_exe

Write-Host "download nginx" -ForegroundColor Green
wget -Uri $nginx_url$nginx_zip -UseBasicParsing -OutFile $drive$nginx_zip

Write-Host "decompression nginx" -ForegroundColor Green
Expand-Archive -Path $drive\\$nginx_zip -DestinationPath $nginx_site

Write-Host "Rename the nginx folder name" -ForegroundColor Green
Rename-Item -Path $nginx_site$nginx_catalogue -NewName $nginx_new

Write-Host "Create nginx environment variables" -ForegroundColor Green
$env:path += ";C:\\Program Files\\nginx"
setx PATH $env:path
setx PATH $env:path /M

Write-Host "View the environment variables protected by path" -ForegroundColor Green
Get-ChildItem env:path

Write-Host "nginx version check" -ForegroundColor Green
nginx -version

Write-Host "nginx xml configuration" -ForegroundColor Green
$functionText = @"
<!-- WinSW-x64.xml -->
<service>
    <id>nginx</id>
    <name>nginx</name>
    <description>nginx</description>
    <logpath>C:\\Program Files\\nginx\\logs\\</logpath>
    <logmode>roll</logmode>
    <depend></depend>
    <executable>C:\\Program Files\\nginx\\nginx.exe</executable>
    <stopexecutable>C:\\Program Files\\nginx\\nginx.exe -s stop</stopexecutable>
</service>
"@

New-Item "$nginx_site$nginx_new\\WinSW-x64.xml" -type file -force -value $functionText

Write-Host "mv WinSW" -ForegroundColor Green
Move-Item $drive$WinSW_exe $nginx_site$nginx_new

Write-Host "Install the nginx service" -ForegroundColor Green
cd $nginx_site$nginx_new
Start-Process -FilePath ".\\WinSW-x64.exe" install -wait -PassThru

Write-Host "nginx.conf configuration" -ForegroundColor Green
$functionText_nginx = @"
#user nobody;
           worker_processes auto; #Custom CPU
           worker_rlimit_nofile 100000;
           error_log  logs/error.log  info;
events 
           worker_connections 1024;
           accept_mutex on;
           multi_accept on;
 

http
          #gzip
          gzip on; #Open compression
          gzip_min_length  1k;
          gzip_buffers     4 32k; #Compressed cache area size
          gzip_http_version 1.1; #Compressed version
          gzip_comp_level 9; #compression ratio
          gzip_types  text/css text/xml application/javascript; #Compression type
          gzip_vary on; #vary header

          server_tokens off; #Closed version
          include       mime.types;
          default_type  application/octet-stream;
          tcp_nopush on;  #Prevent network and disk i/o blocking
          tcp_nodelay on; #Prevent network and disk i/o blocking
          sendfile        on; #Efficient file transfer
          keepalive_timeout  65; #keepalive timeout
          server_names_hash_bucket_size 128; #Multiple domain names
          server_names_hash_max_size 512;
          client_header_timeout 15s;
          client_body_timeout 15s;
          send_timeout 60s;
          client_header_buffer_size 2k; #Buffer size
          large_client_header_buffers 4 4k;
          client_max_body_size 8m;

server 
        listen       80;
        server_name  localhost;
        location / 
        proxy_pass http://127.0.0.1:8082;
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html 
           root   html;
       
    

"@
New-Item "$nginx_site$nginx_new\\conf\\nginx.conf" -type file -force -value $functionText_nginx

Write-Host "start nginx service" -ForegroundColor Green
Start-Service nginx

Write-Host "delete nginx software package" -ForegroundColor Green
Remove-Item $drive$nginx_zip -recurse

Write-Host "firewall nginx port 80" -ForegroundColor Green
New-NetFirewallRule -DisplayName "nginx" -Direction Outbound -profile any -LocalPort 80 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "nginx" -Direction Inbound -profile any -LocalPort 80 -Protocol TCP -Action Allow

执行安装

.\\powershell-install-nginx.ps1

 输出结果

访问测试,我不让版本显示,不用管提示

以上是关于PowerShell Install Nginx的主要内容,如果未能解决你的问题,请参考以下文章

powershell install_chocolatey.ps1

powershell Windows 10 Fresh Install(Chocolatey + Boxstarter)

powershell PS_v5_install.ps1

PowerShell Install Mysql 8

PowerShell Install Mysql 8

PowerShell Install Redis