资讯专栏INFORMATION COLUMN

nginx系列2----从源码安装nginx和echo-nginx-module模块

nihao / 2389人阅读

摘要:下面的参数是根据需要在压缩或解压档案时可选的。备注了解过程省略这里添加了模块生成文件使用系统库没有用到库使用系统库这些路径是要了解的这是配置文件

资源1: 官网: http://nginx.org
资源2: 官方学习资源,     wiki,     nginx安装之wiki介绍
资源3: 编译选项列表
资源4: nginx源码下载列表,当前Stable版本是nginx-1.14.0,
资源5: 官方新手入门
资源6: 内置变量大全(重点掌握),    内置指令大全(重点掌握),    重定向(重点掌握)    核心功能(重点掌握)

安装资源: nginx源码地址(版本1.11.2):http://nginx.org/download/ngi...
安装资源: echo模块安装地址(版本):https://github.com/openresty/...
安装参考: echo模块安装方法

安装时间:2018-09-12

一:从源码安装nginx和echo-nginx-module模块(推荐) 01: 准备nginx和echo-nginx-module模块源码
nginx版本为1.11.2,echo-nginx-module版本为0.61
vagrant@qianjin:~$ wget http://nginx.org/download/nginx-1.11.2.tar.gz
vagrant@qianjin:~$ wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
vagrant@qianjin:~$ ls //查看一下,压缩包在当前目录下
code  Dockerfile  nginx-1.11.2.tar.gz  v0.61.tar.gz
vagrant@qianjin:~$ tar -zxvf nginx-1.11.2.tar.gz
vagrant@qianjin:~$ tar -zxvf v0.61.tar.gz
02: 安装
vagrant@qianjin:~$ cd  nginx-1.11.2 //到解压后的nginx目录中
vagrant@qianjin:~/nginx-1.11.2$ ./configure --prefix=/opt/nginx 
      --add-module=/home/vagrant/echo-nginx-module-0.61
// –prefix为nginx安装位置,–add-module为需要添加的模块路径,这里添加解压后echo-nginx-module路径
// 执行配置后,目录下多了 Makefile文件和 objs目录
vagrant@qianjin:~/nginx-1.11.2$ make -j2

//报错:
src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
         h ^= data[2] << 16;
         ~~^~~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:38:5: note: here
     case 2:
     ^~~~
src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
         h ^= data[1] << 8;
         ~~^~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:40:5: note: here
     case 1:
     ^~~~
原因,是将警告当成了错误处理,打开/home/vagrant/nginx-1.11.2/objs/Makefile,
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g (去年-Werror)
再重新make -j2
-Wall 表示打开gcc的所有警告
-Werror,它要求gcc将所有的警告当成错误进行处理

vagrant@qianjin:~/nginx-1.11.2$ make -j2
vagrant@qianjin:~/nginx-1.11.2$ make install
03: 测试
vagrant@qianjin:~$ sudo /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
// -c 指定配置文件的路径sudo /opt/nginx/sbin/nginx -h了解更多用法
在浏览器中输入 http://192.168.10.10/,如果出现Welcome to nginx!页面表示安装成功
04: 配置 建立链接
sudo ln -s /opt/nginx/sbin/nginx /usr/bin/nginx 
// 建立软链接,/usr/local/bin包含于$PATH当中,不需要额外的设置环境变量了,可以在任何目录运行nginx命令
// 现在可以sudo nginx启动,用sudo nginx -s stop等
开机启动

编译安装需要自己进行设置方可自动启动

# 设置nginx自启动,在/lib/systemd/system/ 目录下创建一个服务文件
vim /lib/systemd/system/nginx.service
内容如下:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
文件说明
[Unit]部分
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务

[Service]部分
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令(需要根据路径适配)
ExecReload为重启命令(需要根据路径适配)
ExecStop为停止命令(需要根据路径适配)
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]部分
服务安装的相关设置,可设置为多用户

# 设置了自启动后,任意目录下执行
systemctl enable nginx.service
# 启动nginx服务
systemctl start nginx.service
# 设置开机自动启动
systemctl enable nginx.service
# 停止开机自动启动
systemctl disable nginx.service
# 查看状态
systemctl status nginx.service
# 重启服务
systemctl restart nginx.service
# 查看所有服务
systemctl list-units --type=service
// 我还是比较喜欢用下面的方式
vagrant@qianjin:~$ sudo service nginx stop
vagrant@qianjin:~$ sudo service nginx star
// 实际下面敲的代码少
vagrant@qianjin:~$ sudo nginx #启动
vagrant@qianjin:~$ sudo nginx -s stop 停止
最简单的nginx配置文件(只支持静态html)
events{
}
http {
    server {
        server_name  symfony.cc;
        location / {
            root   /var/www/study/symfony;
            index  index.html index.htm;
       }
    }
}
最简单的nginx配置文件(支持静态html和php)
前提:在hosts中定义了168.192.10.10 symfony.cc
events{
}
http {
    server {
        server_name  symfony.cc;
        root /var/www/study/symfony;
        location / {
            index index.php index.html index.htm;
       }
       location ~ .php$ {
             # 在/etc/php/7.0/fpm/pool.d/www.conf的listen值与fastcgi_pass的值要相对应
             # ;listen = /run/php/php7.0-fpm.sock
             # listen = 127.0.0.1:9000       
            fastcgi_pass 127.0.0.1:9000;     
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
       }
     }
}
//适合用来检查nginx是否正常,当配置出问题是,就用此配置,再在这个基础上修改,一定要明白的是,nginx只是个web服务器,碰到php文件,它也不解析,它交给php-fpm来处理,php-fpm处理完的数据再交给nginx
了解工作进程worker_process

配置位置在:在最外面
语法: worker_processes 4;
值:默认为1,一般设为cpu数量x2
说明:当设为4时,1个主进程master process,4个工作进程worker process,master process的pid会动态记录在/opt/nginx/logs/nginx.pid文件中,其余4个工作进程的pid值是递加1,如主进程的pid为3082,那么别外几个工作进程的pid分别为3083,3084,3085,3086,主进程负责监控端口,协调工作进程的工作状态,分配工作任务;工作进程负责进行任务处理

vagrant@qianjin:~$ ps -ef|grep nginx
root      3082     1  0 03:56 ?        00:00:00 nginx: master process nginx -c /opt/nginx/conf/2018091201.conf
nobody    3083  3082  0 03:56 ?        00:00:00 nginx: worker process
nobody    3084  3082  0 03:56 ?        00:00:00 nginx: worker process
nobody    3085  3082  0 03:56 ?        00:00:00 nginx: worker process
nobody    3086  3082  0 03:56 ?        00:00:00 nginx: worker process
vagrant   3089  2311  0 03:57 pts/0    00:00:00 grep --color=auto nginx
二:测试echo模块(重点)

作用范围:location块和location if说句中
例子

events{
} 
http {
    server {
        server_name  localhost;
        root /var/www/study/symfony;
        location / {
            index index.php index.html index.htm;
       }
       location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            echo "hello,word";
            echo Changsha; 
            echo $document_root;
            echo_flush;
       }
     }
}

浏览器中输入http://192.168.10.10/a.php, 网页中原来的内容不显示了,只

hello,word
Changsha
/var/www/study/symfony

使用多带带的location来放echo语句,不影响网页

events{
}
http {
    server {
        server_name  localhost;
        root /var/www/study/symfony;
        location / {
            index index.php index.html index.htm;
        }
        location /wang {
            echo "hello,word";
            echo -n Changsha; # -n表示这个不换行,它紧接在echo后面,其它地方忽略
            echo $document_root;
            echo_flush;
        }    
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;      
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

输入 http://192.168.10.10/wang就会...

hello,word
Changsha/var/www/study/symfony

例3:

        location /chen {
            echo_duplicate 1 $echo_client_request_headers;
            echo "
";
            echo_read_request_body;
            echo $request_body;
        }

输出

GET /chen HTTP/1.1
Host: 192.168.10.10
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9

备注1: tar参数说明(熟悉的略过)

-c: 建立压缩档案
-x:解压(解压时要用到这个)
-t:查看内容
-r:向压缩归档文件末尾追加文件
-u:更新原压缩包中的文件
这5个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。
下面的参数是根据需要在压缩或解压档案时可选的。
-z:有gzip属性的
-j:有bz2属性的
-Z:有compress属性的
-v:显示所有过程
-O:将文件解开到标准输出
下面的参数-f是必须的
-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。

备注2:了解configure过程

checking for OS
 + Linux 4.15.0-32-generic x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 7.3.0 (Ubuntu 7.3.0-16ubuntu3) 
checking for gcc -pipe switch ... found
省略
checking for getaddrinfo() ... found
configuring additional modules
adding module in /home/vagrant/echo-nginx-module-0.61 //这里添加了 echo模块
 + ngx_http_echo_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile //生成文件Makefile

Configuration summary
  + using system PCRE library 使用系统PCRE库
  + OpenSSL library is not used 没有用到OpenSSL库
  + using system zlib library 使用系统zlib库
  // 这些路径是要了解的
  nginx path prefix: "/opt/nginx"
  nginx binary file: "/opt/nginx/sbin/nginx"
  nginx modules path: "/opt/nginx/modules"
  nginx configuration prefix: "/opt/nginx/conf"
  nginx configuration file: "/opt/nginx/conf/nginx.conf"  这是配置文件
  nginx pid file: "/opt/nginx/logs/nginx.pid"
  nginx error log file: "/opt/nginx/logs/error.log"
  nginx http access log file: "/opt/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40114.html

相关文章

  • nginx系列2----源码安装nginxecho-nginx-module模块

    摘要:下面的参数是根据需要在压缩或解压档案时可选的。备注了解过程省略这里添加了模块生成文件使用系统库没有用到库使用系统库这些路径是要了解的这是配置文件 资源1: 官网: http://nginx.org资源2: 官方学习资源,     wiki,     nginx安装之wiki介绍资源3: 编译选项列表资源4: nginx源码下载列表,当前Stable版本是nginx-1.14.0,资源5...

    mylxsw 评论0 收藏0
  • Nginx基础

    摘要:安装及相关基础插件将以下所需要的支持包上传至服务器中程序源代码信息输出缓存清除负载均衡模块将所有的开发包解压缩到目录中新建文件目录用于保存的相关配置进入的源码目录编译与安装配置编译相关编译项 安装Nginx及相关基础插件 1 . 将以下所需要的支持包上传至服务器中 nginx-1.11.3.tar.gz : Nginx程序源代码 echo-nginx-module-0.59.ta...

    Thanatos 评论0 收藏0
  • Nginx基础

    摘要:安装及相关基础插件将以下所需要的支持包上传至服务器中程序源代码信息输出缓存清除负载均衡模块将所有的开发包解压缩到目录中新建文件目录用于保存的相关配置进入的源码目录编译与安装配置编译相关编译项 安装Nginx及相关基础插件 1 . 将以下所需要的支持包上传至服务器中 nginx-1.11.3.tar.gz : Nginx程序源代码 echo-nginx-module-0.59.ta...

    ethernet 评论0 收藏0
  • 在ubuntu中自定义安装nginx

    摘要:在中,如果使用的方式,这些都已经配置好了,默认情况下,就是开机自启动,使用自启动启动查看状态设置为系统默认启动 安装时间 2018-10-01 安装环境 win10+virtualbox+ubuntu server 16,安装在虚拟机ubuntu server中 安装 下面定义成一句命令,适合在docker 中使用 # 安装库 sudo apt-get update && su...

    孙淑建 评论0 收藏0
  • Nginx 一点一滴 03 - 架构机制

    摘要:服务器架构模块化结构服务器的开发完全遵循模块化设计思想什么是模块化开发单一职责原则,一个模块只负责一个功能将程序分解,自顶向下,逐步求精高内聚,低耦合的模块化结构核心模块最基本最核心的服务,如进程管理权限控制日志记录标准模块服务器的标准功能 Nginx服务器架构 模块化结构 Nginx 服务器的开发完全遵循模块化设计思想 什么是模块化开发? 单一职责原则,一个模块只负责一个功能 将程...

    Anleb 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<