资讯专栏INFORMATION COLUMN

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

mylxsw / 555人阅读

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

资源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模块源码

</>复制代码

  1. nginx版本为1.11.2echo-nginx-module版本为0.61

</>复制代码

  1. vagrant@qianjin:~$ wget http://nginx.org/download/nginx-1.11.2.tar.gz
  2. vagrant@qianjin:~$ wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
  3. vagrant@qianjin:~$ ls //查看一下,压缩包在当前目录下
  4. code Dockerfile nginx-1.11.2.tar.gz v0.61.tar.gz
  5. vagrant@qianjin:~$ tar -zxvf nginx-1.11.2.tar.gz
  6. vagrant@qianjin:~$ tar -zxvf v0.61.tar.gz
02: 安装

</>复制代码

  1. vagrant@qianjin:~$ cd nginx-1.11.2 //到解压后的nginx目录中
  2. vagrant@qianjin:~/nginx-1.11.2$ ./configure --prefix=/opt/nginx
  3. --add-module=/home/vagrant/echo-nginx-module-0.61
  4. // –prefix为nginx安装位置,–add-module为需要添加的模块路径,这里添加解压后echo-nginx-module路径
  5. // 执行配置后,目录下多了 Makefile文件和 objs目录
  6. vagrant@qianjin:~/nginx-1.11.2$ make -j2
  7. //报错:
  8. src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:
  9. src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
  10. h ^= data[2] << 16;
  11. ~~^~~~~~~~~~~~~~~~
  12. src/core/ngx_murmurhash.c:38:5: note: here
  13. case 2:
  14. ^~~~
  15. src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
  16. h ^= data[1] << 8;
  17. ~~^~~~~~~~~~~~~~~
  18. src/core/ngx_murmurhash.c:40:5: note: here
  19. case 1:
  20. ^~~~
  21. 原因,是将警告当成了错误处理,打开/home/vagrant/nginx-1.11.2/objs/Makefile,
  22. CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g (去年-Werror)
  23. 再重新make -j2
  24. -Wall 表示打开gcc的所有警告
  25. -Werror,它要求gcc将所有的警告当成错误进行处理
  26. vagrant@qianjin:~/nginx-1.11.2$ make -j2
  27. vagrant@qianjin:~/nginx-1.11.2$ make install
03: 测试

</>复制代码

  1. vagrant@qianjin:~$ sudo /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
  2. // -c 指定配置文件的路径sudo /opt/nginx/sbin/nginx -h了解更多用法
  3. 在浏览器中输入 http://192.168.10.10/,如果出现Welcome to nginx!页面表示安装成功
04: 配置 建立链接

</>复制代码

  1. sudo ln -s /opt/nginx/sbin/nginx /usr/bin/nginx
  2. // 建立软链接,/usr/local/bin包含于$PATH当中,不需要额外的设置环境变量了,可以在任何目录运行nginx命令
  3. // 现在可以sudo nginx启动,用sudo nginx -s stop等
开机启动

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

</>复制代码

  1. # 设置nginx自启动,在/lib/systemd/system/ 目录下创建一个服务文件
  2. vim /lib/systemd/system/nginx.service
  3. 内容如下:
  4. [Unit]
  5. Description=nginx - high performance web server
  6. After=network.target remote-fs.target nss-lookup.target
  7. [Service]
  8. Type=forking
  9. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  10. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  11. ExecStop=/usr/local/nginx/sbin/nginx -s stop
  12. [Install]
  13. WantedBy=multi-user.target
  14. 文件说明
  15. [Unit]部分
  16. Description:描述服务
  17. After:依赖,当依赖的服务启动之后再启动自定义的服务
  18. [Service]部分
  19. Type=forking是后台运行的形式
  20. ExecStart为服务的具体运行命令(需要根据路径适配)
  21. ExecReload为重启命令(需要根据路径适配)
  22. ExecStop为停止命令(需要根据路径适配)
  23. PrivateTmp=True表示给服务分配独立的临时空间
  24. 注意:启动、重启、停止命令全部要求使用绝对路径
  25. [Install]部分
  26. 服务安装的相关设置,可设置为多用户
  27. # 设置了自启动后,任意目录下执行
  28. systemctl enable nginx.service
  29. # 启动nginx服务
  30. systemctl start nginx.service
  31. # 设置开机自动启动
  32. systemctl enable nginx.service
  33. # 停止开机自动启动
  34. systemctl disable nginx.service
  35. # 查看状态
  36. systemctl status nginx.service
  37. # 重启服务
  38. systemctl restart nginx.service
  39. # 查看所有服务
  40. systemctl list-units --type=service
  41. // 我还是比较喜欢用下面的方式
  42. vagrant@qianjin:~$ sudo service nginx stop
  43. vagrant@qianjin:~$ sudo service nginx star
  44. // 实际下面敲的代码少
  45. vagrant@qianjin:~$ sudo nginx #启动
  46. vagrant@qianjin:~$ sudo nginx -s stop 停止
最简单的nginx配置文件(只支持静态html)

</>复制代码

  1. events{
  2. }
  3. http {
  4. server {
  5. server_name symfony.cc;
  6. location / {
  7. root /var/www/study/symfony;
  8. index index.html index.htm;
  9. }
  10. }
  11. }
最简单的nginx配置文件(支持静态html和php)

</>复制代码

  1. 前提:在hosts中定义了168.192.10.10 symfony.cc

</>复制代码

  1. events{
  2. }
  3. http {
  4. server {
  5. server_name symfony.cc;
  6. root /var/www/study/symfony;
  7. location / {
  8. index index.php index.html index.htm;
  9. }
  10. location ~ .php$ {
  11. # 在/etc/php/7.0/fpm/pool.d/www.conf的listen值与fastcgi_pass的值要相对应
  12. # ;listen = /run/php/php7.0-fpm.sock
  13. # listen = 127.0.0.1:9000
  14. fastcgi_pass 127.0.0.1:9000;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. include fastcgi_params;
  17. }
  18. }
  19. }
  20. //适合用来检查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,主进程负责监控端口,协调工作进程的工作状态,分配工作任务;工作进程负责进行任务处理

</>复制代码

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

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

</>复制代码

  1. events{
  2. }
  3. http {
  4. server {
  5. server_name localhost;
  6. root /var/www/study/symfony;
  7. location / {
  8. index index.php index.html index.htm;
  9. }
  10. location ~ .php$ {
  11. fastcgi_pass 127.0.0.1:9000;
  12. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  13. include fastcgi_params;
  14. echo "hello,word";
  15. echo Changsha;
  16. echo $document_root;
  17. echo_flush;
  18. }
  19. }
  20. }

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

</>复制代码

  1. hello,word
  2. Changsha
  3. /var/www/study/symfony

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

</>复制代码

  1. events{
  2. }
  3. http {
  4. server {
  5. server_name localhost;
  6. root /var/www/study/symfony;
  7. location / {
  8. index index.php index.html index.htm;
  9. }
  10. location /wang {
  11. echo "hello,word";
  12. echo -n Changsha; # -n表示这个不换行,它紧接在echo后面,其它地方忽略
  13. echo $document_root;
  14. echo_flush;
  15. }
  16. location ~ .php$ {
  17. fastcgi_pass 127.0.0.1:9000;
  18. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  19. include fastcgi_params;
  20. }
  21. }
  22. }

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

</>复制代码

  1. hello,word
  2. Changsha/var/www/study/symfony

例3:

</>复制代码

  1. location /chen {
  2. echo_duplicate 1 $echo_client_request_headers;
  3. echo "
  4. ";
  5. echo_read_request_body;
  6. echo $request_body;
  7. }

输出

</>复制代码

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

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

</>复制代码

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

备注2:了解configure过程

</>复制代码

  1. checking for OS
  2. + Linux 4.15.0-32-generic x86_64
  3. checking for C compiler ... found
  4. + using GNU C compiler
  5. + gcc version: 7.3.0 (Ubuntu 7.3.0-16ubuntu3)
  6. checking for gcc -pipe switch ... found
  7. 省略
  8. checking for getaddrinfo() ... found
  9. configuring additional modules
  10. adding module in /home/vagrant/echo-nginx-module-0.61 //这里添加了 echo模块
  11. + ngx_http_echo_module was configured
  12. checking for PCRE library ... found
  13. checking for PCRE JIT support ... found
  14. checking for zlib library ... found
  15. creating objs/Makefile //生成文件Makefile
  16. Configuration summary
  17. + using system PCRE library 使用系统PCRE库
  18. + OpenSSL library is not used 没有用到OpenSSL库
  19. + using system zlib library 使用系统zlib库
  20. // 这些路径是要了解的
  21. nginx path prefix: "/opt/nginx"
  22. nginx binary file: "/opt/nginx/sbin/nginx"
  23. nginx modules path: "/opt/nginx/modules"
  24. nginx configuration prefix: "/opt/nginx/conf"
  25. nginx configuration file: "/opt/nginx/conf/nginx.conf" 这是配置文件
  26. nginx pid file: "/opt/nginx/logs/nginx.pid"
  27. nginx error log file: "/opt/nginx/logs/error.log"
  28. nginx http access log file: "/opt/nginx/logs/access.log"
  29. nginx http client request body temporary files: "client_body_temp"
  30. nginx http proxy temporary files: "proxy_temp"
  31. nginx http fastcgi temporary files: "fastcgi_temp"
  32. nginx http uwsgi temporary files: "uwsgi_temp"
  33. nginx http scgi temporary files: "scgi_temp"

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

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

相关文章

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

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

    nihao 评论0 收藏0
  • Nginx基础

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

    Thanatos 评论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条评论

mylxsw

|高级讲师

TA的文章

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