進入下載安裝包目錄javascript
cd /usr/local/src
下載nginx安裝包php
wget http://nginx.org/download/nginx-1.15.3.tar.gz
解包解壓nginx安裝包css
tar -zxvf nginx-1.15.3.tar.gz
進入解壓nginx目錄下html
cd nginx-1.15.3.tar.gz
./configure nginx 完成該操做後執行echo $? 驗證是否有錯java
./configure --prefix=/usr/local/nginx
make和make install 編譯安裝node
[root[@localhost](https://my.oschina.net/u/570656) ~]# make && make install
在/etc/init.d/目錄下,建立nginx啓動腳本,並寫入內容linux
[root[@localhost](https://my.oschina.net/u/570656) ~]# vim /etc/init.d/nginx
寫入內容參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx 內容以下: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
給nginx(啓動腳本) 設定755權限git
[root[@localhost](https://my.oschina.net/u/570656) ~]# chmod 755 /etc/init.d/nginx
添加 nginx服務 到服務列表apache
[root[@localhost](https://my.oschina.net/u/570656) ~]# chkconfig --add nginx
設定 nginx服務 開機啓動
[root[@localhost](https://my.oschina.net/u/570656) ~]# chkconfig nginx on
進入 /usr/local/nginx/conf/
把...conf目錄下的nginx.conf文件重命名
[root@localhost conf]# mv nginx.conf nginx.conf.1
在...conf目錄下,建立新的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_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
檢測nginx配置文件是否有錯
[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
啓動nginx服務
[root@localhost conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ]
建立一個1.php測試文件
vim /usr/local/nginx/html/1.php
使用curl命令測試
[root@localhost conf]# curl 127.0.0.1/1.php welcome to nginx[root@localhost conf]# vim /usr/local/nginx/html/1.php
能夠看到PHP已成功解析。
編輯nginx.conf配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
刪除下面所示的內容
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_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
寫入一行內容 include vhost/*.conf;
pplication/xml; include vhost/*.conf; }
在當前目錄下,建立vhost目錄
[root@localhost conf]# mkdir /usr/local/nginx/conf/vhost
進入/usr/local/nginx/conf/vhost目錄下
建立一個aaa.com.conf文件,並寫入內容
[root@localhost conf]# vim /usr/local/nginx/conf/vhost/aaa.com.conf server { listen 80 default_server; #default_server表示默認虛擬主機 server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; }
建立default目錄
[root@localhost vhost]# mkdir -p /data/wwwroot/default
進入/data/wwwroot/default目錄
[root@localhost vhost]# cd /data/wwwroot/default/
建立一個index.html文件,並寫入內容:
[root@localhost default]# vim index.html [root@localhost default]# cat index.html This is a test page for nginx
檢測nginx配置文件是否有錯
[root@localhost default]# /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
從新加載Nginx
[root@localhost default]# /usr/local/nginx/sbin/nginx -s reload
使用curl測試
[root@localhost default]# curl -x127.0.0.1:80 www.hello.com This is a test page for nginx [root@localhost default]# curl -x127.0.0.1:80 aaa.com This is a test page for nginx
能夠看到測試結果,不管使用什麼域名,只要解析到該主機上就會默認訪問該站點,這就是默認虛擬主機的特權。
進入/usr/local/nginx/conf/vhost目錄下
建立一個test.com.conf文件,並寫入內容
[root@localhost vhost]# vim test.com.conf server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
在/data/wwwroot/test.com目錄下建立一個index.html的文件
在Windows上作一個地址解析,而後經過網頁訪問
說明用戶認證已經配置成功
建立認證用戶和密碼 ,須要有apache的支持
yum install httpd -y
使用htpasswd命令給Nginx建立用戶密碼須要絕對路徑,第二次再建立用戶時就不能加-c了,若是再加-c會把以前的帳戶都覆蓋掉。
[root@localhost vhost]# /usr/local/apache/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lisi New password: Re-type new password: Adding password for user lisi [root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd lisi:$apr1$a3bgoFwN$ubHbITBBewSAU0XJd4/nT.
從新加載一下Nginx
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
驗證
編輯配置文件
[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
將location / 改成location /admin/
從新加載
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
在/data/wwwroot/test.com/下建立admin/目錄
[root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.15.3</center> </body> </html> [root@localhost vhost]# curl -ulisi:lichao@123 -x127.0.0.1:80 test.com/admin/ zhendui mulu zuo yonghu renzheng
一樣,若是是針對單個頁面須要作認證的話,就在location後面寫文件名就能夠了。
進入vhost目錄
[root@localhost vhost]# cd /usr/local/nginx/conf/vhost
說明
在Nginx裏「server_name」 支持跟多個域名;可是Apache「server_name」只能跟一個域名,須要跟多個域名,須要使用Alisa; 在Nginx的conf配置文件裏「server_name 」 設置了多個域名,就會使網站的權重變了,到底須要哪一個域名爲主站點呢 因此須要域名重定向
編輯配置文件
server { listen 80 ; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != ‘test.com’ ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
下面這一段是說明
server { listen 80 ; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host!= ‘test.com’ ) //假如域名,「!=」不等於 test.com,將執行下面的腳本 { rewrite ^/(.*)$ http://test.com/$1 permanent; // ^/(.*)$ 正式寫法 http://$host/(.*)$ 這段能夠直接省略掉的,同時還能夠加上一些規則,「 permanent 」就是301的意思;若是想弄成302,只須要更改成「 redirect 」 } }
使用curl測試
[root@localhost vhost]# curl -x127.0.0.1:80 test1.com -I HTTP/1.1 200 OK Server: nginx/1.15.3 Date: Mon, 03 Sep 2018 19:16:05 GMT Content-Type: text/html Content-Length: 30 Last-Modified: Mon, 03 Sep 2018 17:30:36 GMT Connection: keep-alive ETag: "5b8d6fbc-1e" Accept-Ranges: bytes
能夠看到,原本不存在的test1.com成功的重定向到了test.com