1、ngnix安装前准备:
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y
2、安装nginx
mkdir -p /home/oldboy/tools
cd /home/oldboy/tools/
#添加用户
useradd nginx -s /sbin/nologin -M
#下载及解压
#安装 指定的安装路径可以不存在,加个状态模块和ssl模块,创建用户和用户组
[root@jackroo tools]# echo $?
0
#没有问题,执行命令
make
#再执行
make install
#软链接
ln -s /application/nginx1.6.2/ /application/nginx
#检查结果
ll /application/nginx
[root@jackroo application]# netstat -lntup|grep nginx
[root@jackroo application]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4070 root 6u IPv4 17969 0t0 TCP *:http (LISTEN)
nginx 4071 nginx 6u IPv4 17969 0t0 TCP *:http (LISTEN)
#本机验证成功如下所示:
[root@jackroo application]# curl 192.168.1.6
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
ll /application/nginx
3、启动nginx
#检查语法
[root@jackroo application]# /application//nginx/sbin/nginx -t
#启动及验证是否启动
[root@jackroo application]# /application/nginx/sbin/nginx
[root@jackroo application]# netstat -lntup|grep nginx
[root@jackroo application]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4070 root 6u IPv4 17969 0t0 TCP *:http (LISTEN)
nginx 4071 nginx 6u IPv4 17969 0t0 TCP *:http (LISTEN)
#本机验证成功如下所示:
[root@jackroo application]# curl 192.168.1.6
<!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>
4、window浏览器访问问题排查:
# 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;
20 }
21 }
22 }
[root@jackroo conf]# cat nginx.conf
#user nobody;
worker_processes 1; #进程,和cpu相关
#error_log logs/error.log; #日志级别error
#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;
4、window浏览器访问问题排查:
1)【客户端】ping 服务器端IP
ping 10.0.0.7(服务器ip) 排除物理线路
2)【客户端】telnet 服务器端IP 服务端口
telnet 10.0.0.7 80 浏览器到web服务通不通
3)【服务端】wget 127.0.0.1 模拟用户访问,排查http服务自身问题,根据输出在排除。
服务器本地curl 10.0.0.7 Web服务开没开
4)【服务端】看服务日志。error.log 其它未知错误查找。
5、nginx主配置文件nginx.conf
[root@jackroo conf]# egrep -v "#|^$" nginx.conf|cat -n
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 server { #一个server标签代表一个虚拟主机
11 listen 80;
12 server_name localhost;
13 location / {
14 root html;
15 index index.html index.htm;
16 }
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 }
[root@jackroo conf]# cat nginx.conf
#user nobody;
worker_processes 1; #进程,和cpu相关
#error_log logs/error.log; #日志级别error
#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;
# }
#}
}
#配置后同上验证后重启
[root@jackroo nginx]# sbin/nginx -s reload
[root@jackroo nginx]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4070 root 6u IPv4 17969 0t0 TCP *:http (LISTEN)
nginx 4510 nginx 6u IPv4 17969 0t0 TCP *:http (LISTEN)
nginx 4511 nginx 6u IPv4 17969 0t0 TCP *:http (LISTEN)
#配置多个虚拟主机后配置本地宿主机host文件
win+R-->system32-->drivers-->etc-->host
192.168.1.6 www.etiantian.rog bbs.etiantian.org blog.etiantian.org
#管理员启动编辑,然后ping通即可。
Pinging www.etiantian.rog [192.168.1.6] with 32 bytes of data:
Reply from 192.168.1.6: bytes=32 time<1ms TTL=64
Reply from 192.168.1.6: bytes=32 time<1ms TTL=64
Reply from 192.168.1.6: bytes=32 time<1ms TTL=64
Reply from 192.168.1.6: bytes=32 time<1ms TTL=64
Ping statistics for 192.168.1.6:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
#添加状态
[root@jackroo conf]# vi nginx.conf
#status
server {
listen 80;
server_name stauts.etiantian.org;
stub_status on;
access_log off;
}
[root@jackroo conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx1.6.2/conf/nginx.conf test is successful
[root@jackroo conf]# ../sbin/nginx -s reload
[root@jackroo conf]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4070 root 6u IPv4 17969 0t0 TCP *:http (LISTEN)
nginx 4650 nginx 6u IPv4 17969 0t0 TCP *:http (LISTEN)
nginx 4651 nginx 6u IPv4 17969 0t0 TCP *:http (LISTEN)