Nginx是經常使用Web服務器,市場佔有率直追老牌Web服務器軟件Apache。Nginx除了做爲Web服務器之外,還能夠做爲郵件服務器,反向代理服務器。下面咱們介紹源代碼安裝和簡單配置Nginx。javascript
#下載源代碼壓縮包,以nginx-1.11.11爲例 dell@dell:~$ wget http://nginx.org/download/nginx-1.11.11.tar.gz #解壓文件 dell@dell:~$ tar -zxvf nginx-1.11.11.tar.gz
下載PCRE,前面博文中已有下載和安裝過程。php
#PCRE官方網站是 https://sourceforge.net/projects/pcre/files/pcre/
安裝pcre, zlib,前者通常是爲了重寫rewrite,後者通常是爲了gzip壓縮。css
PCRE目前(2017-3-24)有兩個版本,分別是PCRE與PCRE2,這裏咱們選擇下載PCRE。html
PCRE最新版本下載地址是https://sourceforge.net/projects/pcre/files/pcre/8.40/。java
選擇文件下載、解壓與安裝。linux
#下載源代碼 dell@dell:~$ wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz #解壓文件 dell@dell:~$ tar -zxvf pcre-8.40.tar.gz dell@dell:~$ cd pcre-8.40/ #配置安裝文件 dell@dell:~/pcre-8.40$ ./configure --prefix=/usr/local/pcre dell@dell:~/pcre-8.40$ make dell@dell:~/pcre-8.40$ sudo make install
安裝zlib。nginx
zlib官方網站是 http://zlib.net/ 。web
#可在官方網站下載,或去https://sourceforge.net/projects/libpng/files/zlib/ 下載 dell@dell:~$ wget http://zlib.net/zlib-1.2.11.tar.gz dell@dell:~$ tar -zxvf zlib-1.2.11.tar.gz dell@dell:~$ cd zlib-1.2.11/ dell@dell:~/zlib-1.2.11$ ./configure --prefix=/usr/local/zlib1.2 Checking for gcc... Checking for shared library support... Building shared library libz.so.1.2.11 with gcc. Checking for size_t... Yes. Checking for off64_t... Yes. Checking for fseeko... Yes. Checking for strerror... Yes. Checking for unistd.h... Yes. Checking for stdarg.h... Yes. Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf(). Checking for vsnprintf() in stdio.h... Yes. Checking for return value of vsnprintf()... Yes. Checking for attribute(visibility) support... Yes. dell@dell:~/zlib-1.2.11$ make dell@dell:~/zlib-1.2.11$ sudo make install
安裝OpenSSL。 OpenSSL官方網站https://www.openssl.org/ 。下載頁面 https://www.openssl.org/source/ 。apache
# dell@dell:~$ wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz dell@dell:~$ tar -zxvf openssl-1.1.0e.tar.gz dell@dell:~$ cd openssl-1.1.0e/ dell@dell:~/openssl-1.1.0e$ ./config ############################################ 示例配置代碼 ############################################ dell@dell:~/openssl-1.1.0e$ ./config --prefix=/usr/local/openssl1.1 Operating system: x86_64-whatever-linux2 Configuring for linux-x86_64 Configuring OpenSSL version 1.1.0e (0x1010005fL) no-asan [default] OPENSSL_NO_ASAN no-crypto-mdebug [default] OPENSSL_NO_CRYPTO_MDEBUG no-crypto-mdebug-backtrace [default] OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE no-ec_nistp_64_gcc_128 [default] OPENSSL_NO_EC_NISTP_64_GCC_128 no-egd [default] OPENSSL_NO_EGD no-fuzz-afl [default] OPENSSL_NO_FUZZ_AFL no-fuzz-libfuzzer [default] OPENSSL_NO_FUZZ_LIBFUZZER no-heartbeats [default] OPENSSL_NO_HEARTBEATS no-md2 [default] OPENSSL_NO_MD2 (skip dir) no-msan [default] OPENSSL_NO_MSAN no-rc5 [default] OPENSSL_NO_RC5 (skip dir) no-sctp [default] OPENSSL_NO_SCTP no-ssl-trace [default] OPENSSL_NO_SSL_TRACE no-ssl3 [default] OPENSSL_NO_SSL3 no-ssl3-method [default] OPENSSL_NO_SSL3_METHOD no-ubsan [default] OPENSSL_NO_UBSAN no-unit-test [default] OPENSSL_NO_UNIT_TEST no-weak-ssl-ciphers [default] OPENSSL_NO_WEAK_SSL_CIPHERS no-zlib [default] no-zlib-dynamic [default] Configuring for linux-x86_64 CC =gcc CFLAG =-Wall -O3 -pthread -m64 -DL_ENDIAN -Wa,--noexecstack SHARED_CFLAG =-fPIC -DOPENSSL_USE_NODELETE DEFINES =DSO_DLFCN HAVE_DLFCN_H NDEBUG OPENSSL_THREADS OPENSSL_NO_STATIC_ENGINE OPENSSL_PIC OPENSSL_IA32_SSE2 OPENSSL_BN_ASM_MONT OPENSSL_BN_ASM_MONT5 OPENSSL_BN_ASM_GF2m SHA1_ASM SHA256_ASM SHA512_ASM RC4_ASM MD5_ASM AES_ASM VPAES_ASM BSAES_ASM GHASH_ASM ECP_NISTZ256_ASM PADLOCK_ASM POLY1305_ASM LFLAG = PLIB_LFLAG = EX_LIBS =-ldl APPS_OBJ = CPUID_OBJ =x86_64cpuid.o UPLINK_OBJ = BN_ASM =asm/x86_64-gcc.o x86_64-mont.o x86_64-mont5.o x86_64-gf2m.o rsaz_exp.o rsaz-x86_64.o rsaz-avx2.o EC_ASM =ecp_nistz256.o ecp_nistz256-x86_64.o DES_ENC =des_enc.o fcrypt_b.o AES_ENC =aes-x86_64.o vpaes-x86_64.o bsaes-x86_64.o aesni-x86_64.o aesni-sha1-x86_64.o aesni-sha256-x86_64.o aesni-mb-x86_64.o BF_ENC =bf_enc.o CAST_ENC =c_enc.o RC4_ENC =rc4-x86_64.o rc4-md5-x86_64.o RC5_ENC =rc5_enc.o MD5_OBJ_ASM =md5-x86_64.o SHA1_OBJ_ASM =sha1-x86_64.o sha256-x86_64.o sha512-x86_64.o sha1-mb-x86_64.o sha256-mb-x86_64.o RMD160_OBJ_ASM= CMLL_ENC =cmll-x86_64.o cmll_misc.o MODES_OBJ =ghash-x86_64.o aesni-gcm-x86_64.o PADLOCK_OBJ =e_padlock-x86_64.o CHACHA_ENC =chacha-x86_64.o POLY1305_OBJ =poly1305-x86_64.o BLAKE2_OBJ = PROCESSOR = RANLIB =ranlib ARFLAGS = PERL =/usr/bin/perl SIXTY_FOUR_BIT_LONG mode Configured for linux-x86_64. dell@dell:~/openssl-1.1.0e$ ############################################ 示例配置代碼結束 ############################################ dell@dell:~/openssl-1.1.0e$ make dell@dell:~/openssl-1.1.0e$ sudo make install dell@dell:~/openssl-1.1.0e$ cd #檢查OpenSSL安裝位置 dell@dell:~$ which openssl /usr/local/bin/openssl dell@dell:~$ whereis openssl openssl: /usr/bin/openssl /usr/local/bin/openssl /usr/share/man/man1/openssl.1ssl.gz dell@dell:~$
開始安裝Nginx。json
dell@dell:~$ tar -zxvf nginx-1.11.11.tar.gz dell@dell:~$ cd nginx-1.11.11/ dell@dell:~/nginx-1.11.11$ ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src dell@dell:~/nginx-1.11.11$ ./configure --prefix=/usr/local/nginx1.11 --with-pcre=/usr/local/pcre --with-zlib=/usr/local/zlib1.2 dell@dell:~/nginx-1.11.11$ make make -f objs/Makefile make[1]: Entering directory '/home/dell/nginx-1.11.11' cd /usr/local/pcre \ && if [ -f Makefile ]; then make distclean; fi \ && CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \ ./configure --disable-shared /bin/sh: 3: ./configure: not found objs/Makefile:1170: recipe for target '/usr/local/pcre/Makefile' failed make[1]: *** [/usr/local/pcre/Makefile] Error 127 make[1]: Leaving directory '/home/dell/nginx-1.11.11' Makefile:8: recipe for target 'build' failed make: *** [build] Error 2 dell@dell:~/nginx-1.11.11$ #解決該問題一個方法 #參考連接 http://kexiaoke.blog.51cto.com/5530023/1375299 #指定PCRE和zlib源代碼位置,複製文件時請執行make distclean dell@dell:~$ sudo mkdir /usr/local/pcre/src dell@dell:~$ sudo mkdir /usr/local/zlib1.2/src dell@dell:~$ sudo cp -r zlib-1.2.11/* /usr/local/zlib1.2/src/ dell@dell:~$ sudo cp -r pcre-8.40/* /usr/local/pcre/src/ dell@dell:~/nginx-1.11.11$ make clean
請檢查PCRE是否正在運行。有相關進程先結束掉。推測多是PHP或者Apache致使PCRE某些文件不能寫入致使Nginx編譯不成功。重啓電腦可能會解決該問題。
dell@dell:~/nginx-1.11.11$ whereis pcre pcre: /usr/local/pcre dell@dell:~/nginx-1.11.11$ which pcre dell@dell:~/nginx-1.11.11$ ./configure --prefix=/usr/local/nginx1.11 --with-pcre=/home/dell/pcre-8.40 --wit h-zlib=/home/dell/zlib-1.2.11 #開始安裝 dell@dell:~/nginx-1.11.11$ sudo make install [sudo] dell 的密碼: make -f objs/Makefile install make[1]: Entering directory '/home/dell/nginx-1.11.11' test -d '/usr/local/nginx1.11' || mkdir -p '/usr/local/nginx1.11' test -d '/usr/local/nginx1.11/sbin' \ || mkdir -p '/usr/local/nginx1.11/sbin' test ! -f '/usr/local/nginx1.11/sbin/nginx' \ || mv '/usr/local/nginx1.11/sbin/nginx' \ '/usr/local/nginx1.11/sbin/nginx.old' cp objs/nginx '/usr/local/nginx1.11/sbin/nginx' test -d '/usr/local/nginx1.11/conf' \ || mkdir -p '/usr/local/nginx1.11/conf' cp conf/koi-win '/usr/local/nginx1.11/conf' cp conf/koi-utf '/usr/local/nginx1.11/conf' cp conf/win-utf '/usr/local/nginx1.11/conf' test -f '/usr/local/nginx1.11/conf/mime.types' \ || cp conf/mime.types '/usr/local/nginx1.11/conf' cp conf/mime.types '/usr/local/nginx1.11/conf/mime.types.default' test -f '/usr/local/nginx1.11/conf/fastcgi_params' \ || cp conf/fastcgi_params '/usr/local/nginx1.11/conf' cp conf/fastcgi_params \ '/usr/local/nginx1.11/conf/fastcgi_params.default' test -f '/usr/local/nginx1.11/conf/fastcgi.conf' \ || cp conf/fastcgi.conf '/usr/local/nginx1.11/conf' cp conf/fastcgi.conf '/usr/local/nginx1.11/conf/fastcgi.conf.default' test -f '/usr/local/nginx1.11/conf/uwsgi_params' \ || cp conf/uwsgi_params '/usr/local/nginx1.11/conf' cp conf/uwsgi_params \ '/usr/local/nginx1.11/conf/uwsgi_params.default' test -f '/usr/local/nginx1.11/conf/scgi_params' \ || cp conf/scgi_params '/usr/local/nginx1.11/conf' cp conf/scgi_params \ '/usr/local/nginx1.11/conf/scgi_params.default' test -f '/usr/local/nginx1.11/conf/nginx.conf' \ || cp conf/nginx.conf '/usr/local/nginx1.11/conf/nginx.conf' cp conf/nginx.conf '/usr/local/nginx1.11/conf/nginx.conf.default' test -d '/usr/local/nginx1.11/logs' \ || mkdir -p '/usr/local/nginx1.11/logs' test -d '/usr/local/nginx1.11/logs' \ || mkdir -p '/usr/local/nginx1.11/logs' test -d '/usr/local/nginx1.11/html' \ || cp -R html '/usr/local/nginx1.11' test -d '/usr/local/nginx1.11/logs' \ || mkdir -p '/usr/local/nginx1.11/logs' make[1]: Leaving directory '/home/dell/nginx-1.11.11' dell@dell:~/nginx-1.11.11$ dell@dell:~$ whereis nginx nginx: /usr/local/nginx1.11 #運行Nginx dell@dell:/usr/local/nginx1.11/sbin$ sudo /usr/local/nginx1.11/sbin/nginx #檢查Nginx是否佔用80端口 dell@dell:/usr/local/nginx1.11/sbin$ sudo netstat -lpn|grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8166/nginx dell@dell:/usr/local/nginx1.11/sbin$ sudo netstat -lpn|grep :8080 dell@dell:/usr/local/nginx1.11/sbin$ sudo /usr/local/apache2.4/bin/apachectl start dell@dell:/usr/local/nginx1.11/sbin$ sudo netstat -lpn|grep :8080 tcp6 0 0 :::8080 :::* LISTEN 8203/httpd dell@dell:/usr/local/nginx1.11/sbin$
安裝成功截圖以下。
編輯Nginx配置文件。
dell@dell:~$ sudo vim /usr/local/nginx1.11/conf/nginx.conf
該文件能夠修改服務器http監聽端口、服務器名稱等多項設置。
#重啓服務 dell@dell:~$ sudo /usr/local/nginx1.11/sbin/nginx -s reload #中止服務 dell@dell:~$ sudo /usr/local/nginx1.11/sbin/nginx -s stop #啓動服務 dell@dell:~$ sudo /usr/local/nginx1.11/sbin/nginx #測試服務,請檢查-t和-T區別 dell@dell:~$ sudo /usr/local/nginx1.11/sbin/nginx -t nginx: the configuration file /usr/local/nginx1.11/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.11/conf/nginx.conf test is successful dell@dell:~$ sudo /usr/local/nginx1.11/sbin/nginx -T nginx: the configuration file /usr/local/nginx1.11/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.11/conf/nginx.conf test is successful # configuration file /usr/local/nginx1.11/conf/nginx.conf: #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 80; 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; #} } # 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; # } #} } # configuration file /usr/local/nginx1.11/conf/mime.types: types { text/html html htm shtml; text/css css; text/xml xml; image/gif gif; image/jpeg jpeg jpg; application/javascript js; application/atom+xml atom; application/rss+xml rss; text/mathml mml; text/plain txt; text/vnd.sun.j2me.app-descriptor jad; text/vnd.wap.wml wml; text/x-component htc; image/png png; image/tiff tif tiff; image/vnd.wap.wbmp wbmp; image/x-icon ico; image/x-jng jng; image/x-ms-bmp bmp; image/svg+xml svg svgz; image/webp webp; application/font-woff woff; application/java-archive jar war ear; application/json json; application/mac-binhex40 hqx; application/msword doc; application/pdf pdf; application/postscript ps eps ai; application/rtf rtf; application/vnd.apple.mpegurl m3u8; application/vnd.ms-excel xls; application/vnd.ms-fontobject eot; application/vnd.ms-powerpoint ppt; application/vnd.wap.wmlc wmlc; application/vnd.google-earth.kml+xml kml; application/vnd.google-earth.kmz kmz; application/x-7z-compressed 7z; application/x-cocoa cco; application/x-java-archive-diff jardiff; application/x-java-jnlp-file jnlp; application/x-makeself run; application/x-perl pl pm; application/x-pilot prc pdb; application/x-rar-compressed rar; application/x-redhat-package-manager rpm; application/x-sea sea; application/x-shockwave-flash swf; application/x-stuffit sit; application/x-tcl tcl tk; application/x-x509-ca-cert der pem crt; application/x-xpinstall xpi; application/xhtml+xml xhtml; application/xspf+xml xspf; application/zip zip; application/octet-stream bin exe dll; application/octet-stream deb; application/octet-stream dmg; application/octet-stream iso img; application/octet-stream msi msp msm; application/vnd.openxmlformats-officedocument.wordprocessingml.document docx; application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx; application/vnd.openxmlformats-officedocument.presentationml.presentation pptx; audio/midi mid midi kar; audio/mpeg mp3; audio/ogg ogg; audio/x-m4a m4a; audio/x-realaudio ra; video/3gpp 3gpp 3gp; video/mp2t ts; video/mp4 mp4; video/mpeg mpeg mpg; video/quicktime mov; video/webm webm; video/x-flv flv; video/x-m4v m4v; video/x-mng mng; video/x-ms-asf asx asf; video/x-ms-wmv wmv; video/x-msvideo avi; } dell@dell:~$