在windows上安裝nginx並注冊
一、前言
最近自己也嘗試了一下在windows上安裝nginx,其實非常的簡單,這里算是備忘一下。
二、在windows下面安裝
首先需要到nginx的官網上下載最新版的nginx:http://nginx.org/en/download.html
找到最新版本下載即可:

至於后面的pgp,其實是一種密鑰機制(pretty good privacy)。
下載之后,我們發現這個文件是不用安裝的,也就是不需要在windows的注冊表中注冊的,因此我們可以直接使用,如果大家有使用過masm之類的匯編軟件,相信大家會理解的,之后,我們就可以直接在cmd中運行了,這個時候可能會出現問題,其中一個問題是閃退。
閃退的原因有很多種,我們要注意查看日志文件:
如果大家遇到下圖中我畫出的問題:

1 2017/12/13 12:44:23 [emerg] 7260#2152: CreateFile() "C:\Users\愛好妹子的朱\Desktop\nginx-1.13.7/conf/nginx.conf" failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)
那么原因其實很簡單,我們讀了日志之后就能理解,不能映射編碼,說明我們肯定是在路徑中有中文字符,也就是我們的文件存放目錄一定不能是中文的,要不然肯定會閃退的。
解決辦法就是把下載的文件放到全英文的目錄之中。
還有一個錯誤是:

綁定端口異常,那肯定呀,80端口是默認讓http協議使用的,我們這里肯定不能占用,同時我們也可以回憶一下常用的端口,比如HTTPS(443),ftp(20,21)等等。
解決辦法也很簡單,只需要在NGINX中修改一下配置即可:

我們在sublime中打開該文件:

就可以看到相應的配置信息,比如IP地址和端口,這里筆者已經改成了一個大於1024的端口了(其他任意)。
讓我們仔細看一下配置文件,我們以后的很多操作都可以從中配置:
1 #user nobody; 2 worker_processes 1; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; 13 } 14 15 16 http { 17 include mime.types; 18 default_type application/octet-stream; 19 20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 # '$status $body_bytes_sent "$http_referer" ' 22 # '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 #access_log logs/access.log main; 25 26 sendfile on; 27 #tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; 31 32 #gzip on; 33 34 server { 35 listen 31000; 36 server_name localhost; 37 38 #charset koi8-r; 39 40 #access_log logs/host.access.log main; 41 42 location / { 43 root html; 44 index index.html index.htm; 45 } 46 47 #error_page 404 /404.html; 48 49 # redirect server error pages to the static page /50x.html 50 # 51 error_page 500 502 503 504 /50x.html; 52 location = /50x.html { 53 root html; 54 } 55 56 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 57 # 58 #location ~ \.php$ { 59 # proxy_pass http://127.0.0.1; 60 #} 61 62 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 63 # 64 #location ~ \.php$ { 65 # root html; 66 # fastcgi_pass 127.0.0.1:9000; 67 # fastcgi_index index.php; 68 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 69 # include fastcgi_params; 70 #} 71 72 # deny access to .htaccess files, if Apache's document root 73 # concurs with nginx's one 74 # 75 #location ~ /\.ht { 76 # deny all; 77 #} 78 } 79 80 81 # another virtual host using mix of IP-, name-, and port-based configuration 82 # 83 #server { 84 # listen 8000; 85 # listen somename:8080; 86 # server_name somename alias another.alias; 87 88 # location / { 89 # root html; 90 # index index.html index.htm; 91 # } 92 #} 93 94 95 # HTTPS server 96 # 97 #server { 98 # listen 443 ssl; 99 # server_name localhost; 100 101 # ssl_certificate cert.pem; 102 # ssl_certificate_key cert.key; 103 104 # ssl_session_cache shared:SSL:1m; 105 # ssl_session_timeout 5m; 106 107 # ssl_ciphers HIGH:!aNULL:!MD5; 108 # ssl_prefer_server_ciphers on; 109 110 # location / { 111 # root html; 112 # index index.html index.htm; 113 # } 114 #} 115 116 }
三、將nignx注冊成服務
如果我們經常使用,那么在cmd之中啟動是非常不方便的,因此,我們可以將其注冊成服務。
首先我們下載Windows Service Wrapper(比如winsw-1.8-bin.exe)服務注冊工具,然后將這個工具放到ngnix安裝目錄之下,然后新建一個文件,這個文件的文件名需要和我們的服務注冊工具一致,也就是說如果把服務注冊工具重命名,那么這個文件也需要重命名(winsw-1.8-bin.xml):
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <service> 3 <id>nginx</id> 4 <name>zyrNginx Service</name> 5 <description>High Performance Nginx Service 朱彥榮 zyr</description> 6 <executable>D:\nginx-1.14.0\nginx.exe</executable> 7 <logpath>D:\nginx-1.14.0\</logpath> 8 <logmode>roll</logmode> 9 <depend></depend> 10 <startargument>-p D:\nginx-1.14.0</startargument> 11 <stopargument>-p D:\nginx-1.14.0 -s stop</stopargument> 12 </service>
注意上面的文件我們要修改nginx存放的目錄,以及相關的配置。

然后我們在命令行(管理員模式,不然權限不夠)中,將該服務注冊到windows系統之中:

最后我們打開任務管理器中的服務,可以發現我們自己命名的服務已經出現了:


