下載安裝包,解壓編譯安裝javascript
[root@test-a etc]# cd /usr/local/src/ [root@test-a src]# wget http://nginx.org/download/nginx-1.14.1.tar.gz [root@test-a src]# tar -zxvf nginx-1.14.1.tar.gz [root@test-a nginx-1.14.1]# ./configure --prefix=/usr/local/nginx [root@test-a nginx-1.14.1]# make [root@test-a nginx-1.14.1]# make install
編寫啓動腳本,啓動php
啓動腳本內容:css
#!/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
更改啓動腳本權限,設置系統開機啓動等html
[root@test-a nginx-1.14.1]# vim /etc/init.d/nginx [root@test-a nginx-1.14.1]# chmod 755 /etc/init.d/nginx [root@test-a nginx-1.14.1]# chkconfig --add nginx [root@test-a nginx-1.14.1]# chkconfig nginx on [root@test-a nginx-1.14.1]# ls /usr/local/nginx/ conf html logs sbin [root@test-a nginx-1.14.1]# /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
新建配置文件,內容以下java
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; # tcp方式 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
啓動node
[root@test-a conf]# mv nginx.conf nginx.conf.orignal [root@test-a conf]# vim nginx.conf # 添加上面的nginx內容 [root@test-a 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@test-a conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ OK ]
測試訪問 [root@test-a conf]# curl localhostnginx
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@test-a conf]# cd ../html/ [root@test-a html]# vim index.php [root@test-a html]# cat index.php <?php echo "Hello,nginx"; [root@test-a html]# curl localhost/index.php Hello,nginx[root@test-a html]#
先把nginx.conf中的server部份內容去掉,換成include 虛擬主機配置,參考以下web
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; include "vhost/*.conf"; }
建立虛擬主機配置目錄及文件apache
[root@test-a conf]# mkdir vhost [root@test-a conf]# cd vhost/ [root@test-a vhost]# vim test.com.conf # 添加以下內容 server { listen 80 default_server; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/default; }
建立站點目錄,添加文件,測試vim
[root@test-a conf]# mkdir -p /data/wwwroot/default [root@test-a conf]# cd /data/wwwroot/default/ [root@test-a default]# vim index.html [root@test-a default]# cat index.html This is nginx default site. [root@test-a 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 [root@test-a default]# /usr/local/nginx/sbin/nginx -s reload [root@test-a vhost]# curl localhost This is nginx default site.
添加虛擬站點配置,生成認證帳戶
[root@test-a vhost]# vim abc.com.conf [root@test-a vhost]# cat abc.com.conf # 添加以下內容 server { listen 80; server_name abc.com; index index.html index.htm index.php; root /data/wwwroot/abc.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/vhost/nginx-htpasswd; } } # 若是沒有htpasswd, 能夠yum install -y httpd安裝便可 [root@test-a vhost]# /usr/local/apache2.4/bin/htpasswd -c nginx-htpasswd test New password: Re-type new password: Adding password for user test [root@test-a vhost]# ls abc.com.conf nginx-htpasswd test.com.conf [root@test-a vhost]# cat nginx-htpasswd test:$apr1$p3836SLL$Atra.KlKSpLnEWb8lKDNh1 [root@test-a vhost]# /usr/local/apache2.4/bin/htpasswd nginx-htpasswd test1 New password: Re-type new password: Adding password for user test1
建立站點目錄及文件,從新加載服務器配置
[root@test-a vhost]# mkdir /data/wwwroot/abc.com [root@test-a vhost]# cd /data/wwwroot/abc.com [root@test-a abc.com]# vim index.html [root@test-a abc.com]# cat index.html This is abc.com site. [root@test-a vhost]# /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@test-a vhost]# /usr/local/nginx/sbin/nginx -s reload
訪問測試
[root@test-a abc.com]# curl -x127.0.0.1:80 abc.com <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.14.1</center> </body> </html> [root@test-a abc.com]# curl -utest1:test1 -x127.0.0.1:80 abc.com This is abc.com site.
若是訪問返回403狀態碼,有幾種可能:
- 用戶名密碼不正確
- 祕鑰文件配置不正確
- 沒有配置索引頁,即index index.html
- 站點目錄沒有對應的索引頁
- 索引頁及相應的目錄權限不正確
- nginx啓動的用戶和站點用戶不一致
配置,從新加載配置
[root@test-a vhost]# vim abc.com.conf [root@test-a vhost]# cat abc.com.conf server { listen 80; server_name abc.com ab.com a.com; # 這裏和apache的ServerName配置有點差異,apache不能配置多個,配置了也只能識別第一個,只能經過ServerAlias來配置。 index index.html index.htm index.php; root /data/wwwroot/abc.com; if ($host != 'abc.com'){ rewrite ^/(.*)$ http://abc.com/$1 permanent; # permanent 301, redirect 302 } } [root@test-a 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@test-a nginx]# ./sbin/nginx -s reload
訪問測試
[root@test-a nginx]# curl -x127.0.0.1:80 ab.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.1 Date: Mon, 26 Nov 2018 22:59:41 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://abc.com/ [root@test-a nginx]# curl -x127.0.0.1:80 a.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.1 Date: Mon, 26 Nov 2018 22:59:55 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://abc.com/