当前位置: 首页 > 后端技术 > PHP

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

时间:2023-03-29 18:50:51 PHP

来自源码资源一:官网:http://nginx.org资源二:官方学习资源,wiki,wiki介绍nginx安装资源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-121:从源码安装nginx和echo-nginx-module模块(推荐)01:准备nginx和echo-nginx-module模块源码nginx版本为1.11.2,echo-nginx-模块版本是0.61vagrant@qianjin:~$wgethttp://nginx.org/download/nginx-1.11.2.tar.gzvagrant@qianjin:~$wgethttps://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gzvagrant@qianjin:~$ls//查看一下,压缩包在当前目录codeDockerfilenginx-1.11.2.tar.gzv0.61.tar.gzvagrant@qianjin:~$tar-zxvfnginx-1.11.2.tar.gzvagrant@qianjin:~$tar-zxvfv0.61.tar.gz02:installvagrant@qianjin:~$cdnginx-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//Error:src/core/ngx_murmurhash.c:Infunction'ngx_murmur_hash2':src/core/ngx_murmurhash.c:37:11:错误:此语句可能会失败[-Werror=implicit-fallthrough=]h^=data[2]<<16;~~^~~~~~~~~~~~~~~~src/core/ngx_murmurhash.c:38:5:注意:这里是案例2:^~~~src/core/ngx_murmurhash.c:39:11:错误:这条语句可能会通过[-Werror=implicit-fallthrough=]h^=data[1]<<8;~~^~~~~~~~~~~~~~src/core/ngx_murmurhash.c:40:5:注意:这里是case1:^~~~reason,把警告当作错误,打开/home/vagrant/nginx-1.11.2/objs/Makefile,CFLAGS=-pipe-O-W-Wall-Wpointer-arith-Wno-unused-parameter-Werror-g(Lastyear-Werror)re-make-j2-Wall意思是打开gcc-Werror的所有警告,需要gcc把所有的警告都当成错误vagrant@qianjin:~/nginx-1.11.2$make-j2vagrant@qianjin:~/nginx-1.11.2$makeinstall03:testvagrant@qianjin:~$sudo/opt/nginx/sbin/nginx-c/opt/nginx/conf/nginx.conf//-c指定配置文件路径sudo/opt/nginx/sbin/nginx-h更多用法,在浏览器中输入http://192.168.10.10/,如果出现Welcometonginx!页面出现,安装成功。04:配置并建立链接sudoln-s/opt/nginx/sbin/nginx/usr/bin/nginx//创建软链接,/usr/local/bin包含在$PATH中,无需额外设置环境变量,可以在任意目录下运行nginx命令//现在可以启动sudonginx,使用sudonginx-sstop等启动编译安装,需要自己设置才可以自动启动#设置nginx为自动启动,在/lib/systemd/system/目录下创建服务文件vim/lib/systemd/system/nginx。服务内容如下:[Unit]Description=nginx-highperformancewebserverAfter=network.targetremote-fs.targetnss-lookup.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx-sreloadExecStop=/usr/local/nginx/sbin/nginx-sstop[Install]WantedBy=多用户。targetfiledescription[Unit]PartDescription:描述服务After:Dependency,当依赖服务启动后,再启动自定义服务[Service]partType=forking为后台运行形式ExecStart为具体运行命令服务(需要根据路径进行适配)ExecReload为重启命令(需要根据路径进行适配)ExecStop为停止命令(需要根据路径进行适配)PrivateTmp=True表示给服务分配一个独立的临时空间注意:所有启动、重启、停止命令都需要使用绝对路径[Install]部分服务安装相关设置可以设置为多用户#设置自启动后,在任意目录下执行systemctlenablenginx.service#启动nginx服务systemctlstartnginx.service#设置自动启动systemctlenablenginx.service#停止自动启动systemctldisablenginx.service#查看状态systemctlstatusnginx.service#重启服务systemctlrestartnginx.service#查看所有服务systemctllist-units--type=service//我还是比较喜欢用下面的方法vagrant@qianjin:~$sudoservicenginxstopvagrant@qianjin:~$sudoservicenginxstar//其实下面的代码就少了。vagrant@qianjin:~$sudonginx#Startvagrant@qianjin:~$sudonginx-sstop停止最简单的nginx配置文件(只支持静态html)events{}http{server{server_namesymfony.cc;位置/{root/var/www/study/symfony;indexindex.htmlindex.htm;}}}最简单的nginx配置文件(支持静态html和php)前提条件:定义在hosts168.192.10.10symfony.ccevents{}http{server{server_namesymfony.cc;根/var/www/study/symfony;位置/{我indexindex.phpindex.htmlindex.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:9000fastcgi_pass127.0.0.1:9000;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;包括fastcgi_params;}}}//适用于检查nginx是否正常,当配置有问题时,使用这个配置,然后在此基础上修改。必须明白,nginx只是一个web服务器。当它遇到php文件时,它不会解析它。交给php-fpm处理。php-fpm处理的数据会交给nginx来了解工作流程。进程master进程,4个worker进程,master进程的pid会动态记录在/opt/nginx/logs/nginx.pid文件中,其余4个worker进程的pid值自增1,如主进程的pid如果是3082,那么其他工作进程的pid分别是3083、3084、3085、3086。主进程负责监听端口,协调工作进程的工作状态,分配工作任务;工作进程负责任务处理vagrant@qianjin:~$ps-ef|grepnginxroot30821003:56?00:00:00nginx:主进程nginx-c/opt/nginx/conf/2018091201.confnobody30833082003:56?00:00:00nginx:workerprocessnobody30843082003:56?00:00:00nginx:workerprocessnobody30853082003:56?00:00:00nginx:工人30863082003:56?00:00:00nginx:workerprocessvagrant30892311003:57pts/000:00:00grep--color=autonginx2:测试echo模块(key)作用范围:locationblockExampleevents{}http{服务器{server_name本地主机;根/var/www/study/symfony;location/{indexindex.phpindex.htmlindex.htm;}location~\.php${fastcgi_pass127.0.0.1:9000;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;包括fastcgi_params;回声“你好,单词”;呼应长沙;回声$document_root;,网页中原有内容没有显示,只有hello,wordchangsha/var/www/study/symfony使用单独的位置放置echo语句,不影响网页事件{}http{server{server_namelocalhost;根/var/www/study/symfony;location/{indexindex.phpindex.htmlindex.htm;}location/wang{echo"hello,word";echo-n长沙;#-n表示这一行不变,紧跟在echo之后,其他地方忽略echo$document_root;回声冲洗;}location~\.php${fastcgi_pass127.0.0.1:9000;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;包括fastcgi_params;}}}输入http://192.168.10.10/wang会是...hello,wordChangsha/var/www/study/symfonyExample3:location/chen{echo_duplicate1$echo_client_request_headers;回声“\r”;echo_read_request_body;回声$request_body;}输出GET/chenHTTP/1.1Host:192.168.10.10Connection:keep-aliveCache-Control:max-age=0Upgrade-Insecure-Requests:1User-Agent:Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/67.0.3396.99Safari/537.36Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding:gzip,deflateAccept-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:usearchives名字,记住,这个parameter是最后一个参数,后面只有文件名。备注2:了解OS+Linux4.15.0-32-genericx86_64检查C编译器的配置过程...发现+使用GNUC编译器+gcc版本:7.3.0(Ubuntu7.3.0-16ubuntu3)检查gcc-管道开关...发现省略了对getaddrinfo()的检查...发现在/home/vagrant/echo-nginx-module-0.61中添加模块配置附加模块PCRElibrary...发现正在检查PCREJIT支持...foundcheckingforzliblibrary...foundcreatingobjs/Makefile//GeneratefileMakefileConfigurationsummary+usingsystemPCRElibrary使用系统PCRElibrary+OpenSSLlibraryisnotuseused+usingsystemzliblibraryuseSystemzliblibrary//这些路径都是nginx路径前缀:"/opt/nginx"nginx二进制文件:"/opt/nginx/sbin/nginx"nginx模块路径:"/opt/nginx/modules"nginx配置前缀:"/opt/nginx/conf"nginx配置文件:"/opt/nginx/conf/nginx.conf”这是配置文件nginxpid文件:“/opt/nginx/logs/nginx.pid”nginx错误日志文件:“/opt/nginx/logs/error.log”nginxhttp访问日志文件:“/opt/nginx/logs/access.log”nginxhttp客户端请求正文临时文件:“client_body_temp”nginxhttp代理临时文件文件:“proxy_temp”nginxhttpfastcgi临时文件:“fastcgi_temp”nginxhttpuwsgi临时文件:“uwsgi_temp”nginxhttpscgi临时文件:“scgi_temp”