一、進入src目錄,下載源碼包,在官網nginx.org下載:javascript
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# ls mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz mysql-test-5.7.18-linux-glibc2.5-x86_64.tar.gz php-5.6.32 php-5.6.32.tar.bz2 [root@localhost src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2018-06-10 11:50:38-- http://nginx.org/download/nginx-1.14.0.tar.gz 正在解析主機 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ... 正在鏈接 nginx.org (nginx.org)|95.211.80.227|:80... 已鏈接。 已發出 HTTP 請求,正在等待迴應... 302 Found 位置:http://120.198.248.38/cache/nginx.org/download/nginx-1.14.0.tar.gz?ich_args2=913-10115011025885_c68489af48a342d4d96e3d5442d9961a_10001002_9c896624d1c3f3d79433518939a83798_dce717bbf01ac9d1291c27e56e2c7bd4 [跟隨至新的 URL] --2018-06-10 11:50:38-- http://120.198.248.38/cache/nginx.org/download/nginx-1.14.0.tar.gz?ich_args2=913-10115011025885_c68489af48a342d4d96e3d5442d9961a_10001002_9c896624d1c3f3d79433518939a83798_dce717bbf01ac9d1291c27e56e2c7bd4 正在鏈接 120.198.248.38:80... 已鏈接。 已發出 HTTP 請求,正在等待迴應... 200 OK 長度:1016272 (992K) [application/octet-stream] 正在保存至: 「nginx-1.14.0.tar.gz」 100%[====================================================================================================================================>] 1,016,272 2.43MB/s 用時 0.4s 2018-06-10 11:50:38 (2.43 MB/s) - 已保存 「nginx-1.14.0.tar.gz」 [1016272/1016272])
二、解壓源碼包:php
[root@localhost src]# tar xzvf nginx-1.14.0.tar.gz nginx-1.14.0/ nginx-1.14.0/auto/ nginx-1.14.0/conf/ nginx-1.14.0/contrib/ nginx-1.14.0/src/ nginx-1.14.0/auto/cc/msvc nginx-1.14.0/auto/cc/name nginx-1.14.0/auto/cc/owc nginx-1.14.0/auto/cc/sunc
三、進入源碼包目錄,配置編譯文件css
//能夠根據需求增長配置項,例如支持https的配置 [root@localhost src]# cd nginx-1.14.0 [root@localhost nginx-1.14.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found checking for gcc variadic macros ... found checking for uintptr_t ... uintptr_t found checking for PCRE JIT support ... found checking for zlib library ... found creating objs/Makefile Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@localhost nginx-1.14.0]# echo $? 0
四、編譯及安裝nginx:html
[root@localhost nginx-1.14.0]# make && make install make -f objs/Makefile make[1]: 進入目錄「/usr/local/src/nginx-1.14.0」 cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_log.o \ src/core/ngx_log.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_palloc.o \ cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: 離開目錄「/usr/local/src/nginx-1.14.0」 [root@localhost nginx-1.14.0]# echo $? 0
五、安裝成功,查看nginx目錄結構及核心文件:java
//分別是配置文件目錄、網站目錄、日誌目錄、進程目錄 [root@localhost nginx-1.14.0]# cd /usr/local/nginx/ [root@localhost nginx]# ls conf html logs sbin //核心二進制文件 [root@localhost nginx]# ls -l sbin/nginx -rwxr-xr-x 1 root root 3746616 6月 10 11:57 sbin/nginx
六、修改、檢查配置文件:node
//用本身的配置文件,不用自帶的 [root@localhost nginx]# mv conf/nginx.conf conf/nginx.conf.bak [root@localhost nginx]# cd conf/ [root@localhost conf]# ls fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf.bak scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@localhost conf]# vim nginx.conf user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } } [root@localhost conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
七、編輯啓動腳本:mysql
[root@localhost conf]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL //修改成755權限 [root@localhost conf]# chmod 755 /etc/init.d/nginx [root@localhost conf]# ls -l !$ ls -l /etc/init.d/nginx -rwxr-xr-x 1 root root 1133 6月 10 13:08 /etc/init.d/nginx
八、加入開機啓動列表,設置開機啓動:linux
[root@localhost conf]# chkconfig --add nginx [root@localhost conf]# chkconfig nginx on
九、啓動nginx服務:nginx
[root@localhost conf]# /etc/init.d/n netconsole network nginx [root@localhost conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ]
十、查看進程:訪問主頁驗證sql
[root@localhost conf]# ps aux |grep nginx root 4096 0.0 0.0 20548 628 ? Ss 13:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4097 0.0 0.1 22992 3216 ? S 13:10 0:00 nginx: worker process nobody 4098 0.0 0.1 22992 3216 ? S 13:10 0:00 nginx: worker process [root@localhost conf]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4096/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 897/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1256/master tcp6 0 0 :::22 :::* LISTEN 897/sshd tcp6 0 0 ::1:25 :::* LISTEN 1256/master tcp6 0 0 :::3306 :::* LISTEN 1138/mysqld //200狀態碼,訪問正常 [root@localhost conf]# curl localhost -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 05:11:57 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Sun, 10 Jun 2018 03:57:01 GMT Connection: keep-alive ETag: "5b1ca18d-264" Accept-Ranges: bytes
一、把主配置文件下的server段註釋或刪除掉,增長include段
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf http { # server # { # listen 80; # server_name localhost; # index index.html index.htm index.php; # root /usr/local/nginx/html; # location ~ \.php$ # { # include fastcgi_params; # fastcgi_pass unix:/tmp/php-fcgi.sock; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; # } # } include vhost/*.conf; //指定虛擬主機配置文件目錄在vhost/ 下 }
二、建立虛擬主機配置文件,一個虛擬主機就有一個配置文件。
[root@localhost conf]# mkdir vhost/ [root@localhost conf]# cd vhost/ [root@localhost vhost]# ls [root@localhost vhost]# vim aaa.com.conf //aaa.com網站的虛擬主機配置文件 server { listen 80 default_server; //有default_server標誌的,表明是默認虛擬主機,只要訪問沒有指定的域名過來,就會默認訪問aaa.com虛擬主機 server_name aaa.com; index index.html index.php; root /data/wwwroot/aaa.com; }
三、檢查配置文件、從新加載配置文件:
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
四、建立網站目錄,進行訪問驗證:
[root@localhost ~]# mkdir -p /data/wwwroot/aaa.com/ [root@localhost ~]# cd !$ cd /data/wwwroot/aaa.com/ [root@localhost aaa.com]# ls [root@localhost aaa.com]# vim index.html [root@localhost aaa.com]# curl -x127.0.0.1:80 bbb.com echo "this is nginx testpage!" [root@localhost aaa.com]# curl -x127.0.0.1:80 bbb.com -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 05:54:50 GMT Content-Type: text/html Content-Length: 31 Last-Modified: Sun, 10 Jun 2018 05:54:34 GMT //200狀態碼,由於是默認的虛擬主機,因此其餘域名bbb.com也能訪問過來。
一、在vhost目錄下載增長另一個虛擬主機配置文件
[root@localhost ~]# cp /usr/local/nginx/conf/vhost/aaa.com.conf /usr/local/nginx/conf/vhost/test.com.conf
二、設置虛擬主機配置文件,加入用戶認證配置段
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com; index index.html index.php; root /data/wwwroot/test.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } [root@localhost ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
三、用apache的htpasswd工具,生成密碼文件
//可單獨安裝htpasswd工具 [root@localhost ~]# yum install -y httpd 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.tongji.edu.cn * extras: mirrors.163.com * updates: mirrors.163.com 正在解決依賴關係 --> 正在檢查事務 ---> 軟件包 httpd.x86_64.0.2.4.6-80.el7.centos 將被 安裝 --> 正在處理依賴關係 httpd-tools = 2.4.6-80.el7.centos,它被軟件包 httpd-2.4.6-80.el7.centos.x86_64 須要 --> 正在處理依賴關係 /etc/mime.types,它被軟件包 httpd-2.4.6-80.el7.centos.x86_64 須要 --> 正在處理依賴關係 libaprutil-1.so.0()(64bit),它被軟件包 httpd-2.4.6-80.el7.centos.x86_64 須要 --> 正在處理依賴關係 libapr-1.so.0()(64bit),它被軟件包 httpd-2.4.6-80.el7.centos.x86_64 須要 --> 正在檢查事務 ---> 軟件包 apr.x86_64.0.1.4.8-3.el7_4.1 將被 安裝 ---> 軟件包 apr-util.x86_64.0.1.5.2-6.el7 將被 安裝 ---> 軟件包 httpd-tools.x86_64.0.2.4.6-80.el7.centos 將被 安裝 ---> 軟件包 mailcap.noarch.0.2.1.41-2.el7 將被 安裝 --> 解決依賴關係完成 依賴關係解決 ============================================================================================================================================================================== Package 架構 版本 源 大小 ============================================================================================================================================================================== 正在安裝: httpd x86_64 2.4.6-80.el7.centos base 2.7 M 爲依賴而安裝: apr x86_64 1.4.8-3.el7_4.1 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-80.el7.centos base 89 k mailcap noarch 2.1.41-2.el7 base 31 k 事務概要 ============================================================================================================================================================================== 安裝 1 軟件包 (+4 依賴軟件包) 總下載量:3.0 M 安裝大小:10 M Downloading packages: (1/5): apr-1.4.8-3.el7_4.1.x86_64.rpm | 103 kB 00:00:00 (2/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00 (3/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00 (4/5): httpd-2.4.6-80.el7.centos.x86_64.rpm | 2.7 MB 00:00:07 (5/5): httpd-tools-2.4.6-80.el7.centos.x86_64.rpm | 89 kB 00:00:15 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 總計 192 kB/s | 3.0 MB 00:00:16 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安裝 : apr-1.4.8-3.el7_4.1.x86_64 1/5 正在安裝 : apr-util-1.5.2-6.el7.x86_64 2/5 正在安裝 : httpd-tools-2.4.6-80.el7.centos.x86_64 3/5 正在安裝 : mailcap-2.1.41-2.el7.noarch 4/5 正在安裝 : httpd-2.4.6-80.el7.centos.x86_64 5/5 驗證中 : httpd-tools-2.4.6-80.el7.centos.x86_64 1/5 驗證中 : apr-1.4.8-3.el7_4.1.x86_64 2/5 驗證中 : mailcap-2.1.41-2.el7.noarch 3/5 驗證中 : httpd-2.4.6-80.el7.centos.x86_64 4/5 驗證中 : apr-util-1.5.2-6.el7.x86_64 5/5 已安裝: httpd.x86_64 0:2.4.6-80.el7.centos 做爲依賴被安裝: apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-80.el7.centos mailcap.noarch 0:2.1.41-2.el7 完畢! //lgs爲用戶名,回車後輸入兩次密碼,設置成功 [root@localhost ~]# htpasswd -c /usr/local/nginx/conf/htpasswd lgs New password: Re-type new password: Adding password for user lgs [root@localhost ~]# cat /usr/local/nginx/conf/htpasswd lgs:$apr1$/XnF945H$/hM8VWuy4u1eNDmchA39s.
四、驗證用戶認證:
[root@localhost ~]# mkdir -p /data/wwwroot/test.com/ [root@localhost ~]# vim /data/wwwroot/test.com/1.txt //報401錯誤,提示須要認證 [root@localhost ~]# curl -x127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 06:15:56 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth"
一、設置虛擬主機配置文件,加入域名重定向配置段
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com test1.com test2.com; //支持多個域名,與apache不一樣,因此要域名重定向,跳轉到test.com上,不影響他的搜索權重 index index.html index.php; root /data/wwwroot/test.com; # location / # { # auth_basic "Auth"; # auth_basic_user_file /usr/local/nginx/conf/htpasswd; # } if ($host != 'test.com') { rewrite ^/(.*)$ http://test.com/$1 permanent; } //使用rewrite模塊實現。redirect是臨時重定向,302狀態碼。 } [root@localhost ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
二、驗證重定向:
[root@localhost ~]# curl -x127.0.0.1:80 test2.com/1.txt -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 06:26:13 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/1.txt //301狀態碼,重定向到test.com去。