nginx安装及部署配置详解

本文详细介绍了在Linux环境下安装NGINX的过程,包括必要的软件包安装、用户创建、编译安装步骤以及如何通过软链接设置默认路径。此外,还提供了启动、重启NGINX服务的方法,并解释了如何通过修改配置文件nginx.conf来调整服务参数,如工作进程数、事件处理能力、HTTP设置等。最后,文章给出了排查Windows浏览器访问问题的步骤,以及如何配置多个虚拟主机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值