下载安装包,解压编译安装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/