yum安装nginx方法

nginx安装步骤总结-故障排查-浏览原理_linux


1 启动并检查安装结果

    安装完nginx后并不能直接对外提供服务需要先启动nginx服务才行具体操作如下

    1启动前检查配置文件语法

[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful

提示在启动服务前检查语法非常重要可以防止因配置错误导致网站重启重新加载配置。

        再次启动nginx提示80端口已被占用

[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

    kill掉nginx进程然后再启动nginx

[root@web01 nginx-1.6.3]# killall nginx

    2再次启动nginx服务

[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx

    3查看nginx服务对应的端口是否成功启动netstat -lutup或者lsof -i都可以

[root@web01 nginx-1.6.3]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5161/nginx
[root@web01 nginx-1.6.3]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   5161 root    6u  IPv4  19215      0t0  TCP *:http (LISTEN)
nginx   5162  www    6u  IPv4  19215      0t0  TCP *:http (LISTEN)

    4检查nginx启动的实际效果

        4.1在windows下通过浏览器检测的方法如下

    打开浏览器输入http://10.0.0.8(10.0.0.8为安装nginx服务器的ip地址)然后回车

如果看到如下内容就表示nginx已经启动了。

nginx安装步骤总结-故障排查-浏览原理_linux_02

            4.2在linux下可使用wget命令检测。

[root@web01 nginx-1.6.3]# wget 127.0.0.1
--2017-08-19 16:11:17--  http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: index.html
100%[=================================>] 612         --.-K/s   in 0s     
2017-08-19 16:11:17 (54.7 MB/s) - index.html saved [612/612]

            4.3在linux下也可以使用curl命令检查如下

[root@web01 nginx-1.6.3]# curl 127.0.0.1
<!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>

        以上3中方法可以检测nginx安装及浏览是否正常。

        提示10.0.0.8为服务器的IP地址也可以通过netstat -lnt|grep 80、lsof -i:80 检查nginx服务端口默认为80来判断nginx是否已启动或通过ps -ef|grep nginx检查nginx服务进程来判断当然最稳妥的方法还是通过URL地址来检查在后面的监控服务中还会提到这个问题。


nginx启动的疑难杂症汇总

nginx安装步骤总结-故障排查-浏览原理_linux_03

        解答第二个可能原因:执行nginx自检报错:

[root@web01 html]# /application/nginx/sbin/nginx -t 
nginx: [emerg] unexpected "}" in /application/nginx-1.6.3//conf/nginx.conf:27
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test failed

        发现有问题:nginx.conf配置文件第27号的{是多余的,删除后再执行检查就ok了。

[root@web01 conf]# vim nginx.conf #增加后的虚拟主机配置文件
   
       worker_connections  1024;
   http {
       include       mime.types;
       default_type  application/octet-stream;
       sendfile        on;
       keepalive_timeout  65;
      server {
          listen       80;
          server_name  ;#基于域名的虚拟主机
          location / {
              root   html/www;    #域名www.etiantian.org对应自己的www站点
              index  index.html index.htm;#首页文件名字,和下面的bbs站点不在一个路径中
          }
          }
      server {
          listen       80;
          server_name  bbs.etiantian.org;#基于域名的虚拟主机
          location / {
              root   html/bbs;    #域名bbs.etiantian.org对应自己的bbs站点
              index  index.html index.htm;#首页文件名字,和上面的www站点不在同一个路径
          }
          }
      }
  }

        配置文件中 }删除后再执行检查就ok了。

[root@web01 conf]# /application/nginx/sbin/nginx -t 
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful

      

nginx安装步骤总结-故障排查-浏览原理_linux_04

nginx安装步骤总结-故障排查-浏览原理_linux_05

        查看nginx版本ls /application/

[root@web01 nginx-1.6.3]# ls /application/
nginx  nginx-1.6.3

        查看nginx编译时候添加了什么组件/application/nginx/sbin/nginx -V

[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
TLS SNI support enabled
configure arguments: --user=www --group=www --with-http_ssl_module --with-http_sub_module --prefix=/application/nginx-1.6.3/

nginx安装步骤总结-故障排查-浏览原理_linux_06

nginx安装步骤总结-故障排查-浏览原理_linux_07

nginx安装步骤总结-故障排查-浏览原理_linux_08

nginx安装步骤总结-故障排查-浏览原理_linux_09

nginx安装步骤总结-故障排查-浏览原理_linux_10

nginx安装步骤总结-故障排查-浏览原理_linux_11