linux 编译安装nginx

前言

  在linux操作系统上安装并且部署一个简单的web页面,本文将为您介绍yum安装和源码包编译安装


一、nginx介绍

1.nginx

   Nginx (engine x) 是一个轻量级,高性能的 HTTP 和 反向代理 服务,也是一个IMAP/POP3/SMTP服务。因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
  Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:单机环境下参考服务器配置。 并发连接数在7000+ -8000左右。 集群模式20000+
  Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到724不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。

2.HTTP返回码(部分)

200 (成功) 服务器已成功处理了请求。 通常,这表示服务器提供了请求的网页。
301 (永久重定向) 请求的网页已永久移动到新位置
302 (临时重定向)服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。
400 (错误请求) 服务器不理解请求的语法。
401 (未授权) 请求要求身份验证。 对于需要登录的网页,服务器可能返回此响应。
402 该状态码是为了将来可能的需求而预留的。
403 (禁止) 服务器拒绝请求。
404 (未找到) 服务器找不到请求的网页。
500 (服务器内部错误) 服务器遇到错误,无法完成请求。
501 (尚未实施) 服务器不具备完成请求的功能。 例如,服务器无法识别请求方法时可能会返回此代码。
502 (错误网关) 服务器作为网关或代理,从上游服务器收到无效响应。
503 (服务不可用) 服务器目前无法使用(由于超载或停机维护)。 通常,这只是暂时状态。
504 (网关超时) 服务器作为网关或代理,但是没有及时从上游服务器收到请求。
505 (HTTP 版本不受支持) 服务器不支持请求中所用的 HTTP 协议版本。

二、安装nginx

2.1.使用yum安装nginx

1.确保你的机器有扩展源,如果没有请安装epel-release

[root@localhost /]# yum -y install nginx

2.安装成功后查看80端口是否启动

[root@localhost /]# ss -ntpl|grep nginx
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=15562,fd=6),("nginx",pid=15561,fd=6))
LISTEN     0      128         :::80                      :::*                   users:(("nginx",pid=15562,fd=7),("nginx",pid=15561,fd=7))

3.使用curl命令在本地访问本机nginx服务,状态返回码是否为 200

[root@localhost /]# curl 127.0.0.1:80 -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Mon, 01 Feb 2021 20:19:08 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes

4.编辑nginx配置文件并设置首页,部分配置文件如下:

vim /etc/nginx/nginx.conf

...  
  include /etc/nginx/conf.d/*.conf;
    server {
    
    
        listen       80 default_server;
        listen       [::]:80 default_server;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        
        location / {
    
    
    root /html;     #网站根目录
    index index.html index.htm;   #默认页面      
    }
...

[root@localhost nginx]# echo "hello word" > /html/index.html

5.不停止服务 重新加载配置文件

[root@localhost nginx]# systemctl reload nginx

6.访问页面

[root@localhost nginx]# curl 127.0.0.1   #默认使用80端口访问
hello word

或者直接在浏览器输入ip直接访问

2.2.使用源码包编译安装nginx

1、安装编译环境

[root@localhost /]# yum -y install gcc gcc-c++

2、安装pcre软件包(使nginx支持http rewrite模块)

[root@localhost /]# yum install -y pcre pcre-devel gd-devel

3、安装openssl-devel(使nginx支持ssl)

[root@localhost /]# yum install -y openssl openssl-devel 

4、安装zlib

[root@localhost /]# yum install -y zlib zlib-devel

5、创建nginx用户

[root@localhost /]# useradd nginx 
[root@localhost /]# passwd nginx

6.安装nginx

[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@localhost ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/src
[root@localhost ~]# cd /usr/local/nginx-1.16.0/
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@localhost nginx-1.16.0]# make && make install

7.nginx编译参数

[root@localhost ~]#/usr/local/nginx/sbin/nginx -V   #chakannginx安装的模块
--prefix=/usr/local/nginx                        //指向安装目录
--conf-path=/etc/nginx/nginx.conf                //指定配置文件
--http-log-path=/var/log/nginx/access.log        //指定访问日志
--error-log-path=/var/log/nginx/error.log        //指定错误日志
--lock-path=/var/lock/nginx.lock                 //指定lock文件
--pid-path=/run/nginx.pid                        //指定pid文件   查看当前服务是否删除不可删

--http-client-body-temp-path=/var/lib/nginx/body    //设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     //设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy         //设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi           //设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         //设定http uwsgi临时文件路径

--with-debug                                        //启用debug日志
--with-pcre-jit                                     //编译PCRE包含“just-in-time compilation”
--with-ipv6                                         //启用ipv6支持
--with-http_ssl_module                              //启用ssl支持
--with-http_stub_status_module                      //获取nginx自上次启动以来的状态
--with-http_realip_module                 //允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module           //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
...
8.修改配置文件 /etc/nginx/nginx.conf
# 全局参数设置
user nginx;  #设置nginx使用的用户

#设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同,新版本支持 auto ,Nginx 将自动检测CPU核心数,并将worker process 数量设置成等同CPU核心数量
worker_processes  auto;          
error_log  logs/error.log;    #指定错误日志 
worker_rlimit_nofile 1024;  #设置一个nginx进程能打开的最大文件数  设置不一定生效 
pid        /var/run/nginx.pid; 
events {
    
     
    worker_connections  1024; #设置一个进程的最大并发连接数 
}
# http 服务相关设置 
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  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
    sendfile          on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载 
    # sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝
    gzip              on;      #是否开启gzip压缩,将注释去掉开启 
    keepalive_timeout  65;     #设置长连接的超时时间
# 虚拟服务器的相关设置 
    server {
    
     
        listen      80;        #设置监听的端口 
        server_name  localhost;        #设置绑定的主机名、域名或ip地址 
        charset koi8-r;        # 设置编码字符 
        location / {
    
     
            root  /var/www/nginx;           #设置服务器默认网站的根目录位置,需要手动创建
            index  index.html index.htm;    #设置默认打开的文档 
            } 
        error_page  500 502 503 504  /50x.html; #设置错误信息返回页面 
        location = /50x.html {
    
     
            root  html;        #这里的绝对位置是/usr/local/nginx/html
        } 
    } 
 }
nginx文件结构
...              #全局块

events {
    
             #events块
   ...
}

http      #http块
{
    
    
    ...   #http全局块
    server        #server块
    {
    
     
        ...       #server全局块
        location [PATTERN]   #location块
        {
    
    
            ...
        }
        location [PATTERN] 
        {
    
    
            ...
        }
    }
    server
    {
    
    
      ...
    }
    ...     #http全局块
}
1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放 路径,日志存放路径,配置文件引入,允许生成worker process数等。 
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。 
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。 
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。   
5、location块:配置请求的路由,以及各种页面的处理情况。

9.检查配置文件是否正确

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

10.启动nginx服务

[root@localhost ~]# /usr/local/nginx/sbin/nginx

nginx命令 参考

nginx -s stop            	 # 快速停止nginx生效
nginx -s reload  		     # 修改配置后重新加载
nginx -s quit				 # 完整有序的停止nginx

11.使用curl命令访问页面

[root@localhost /]# curl 127.0.0.1 -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Tue, 02 Feb 2021 12:12:29 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 28 Jan 2021 07:27:56 GMT
Connection: keep-alive
ETag: "6012677c-e"
Accept-Ranges: bytes

补充

添加环境变量
[root@localhost /]# vim /etc/profile
#在最后一行添加   
PATH=$PATH:/usr/local/nginx/sbin
export PATH
[root@localhost /]# source /etc/profile   #刷新环境变量,生效
[root@localhost /]# nginx      #直接启动nginx 或者  nginx -s stop

链接

nginx官网 : http://nginx.org/
nginx源码: http://nginx.org/download/

总结

   nginx除了简单的web服务器之外,还可以用来做正方向代理,负载均衡等服务,后续将陆续做出相关参考文档。如果想进一步学习,需要参考配置文件,欢迎一起来学习交流!!!

猜你喜欢

转载自blog.csdn.net/weixin_52099680/article/details/113525732