四章——Nginx网站服务(应用——linux防护与群集)

代码 代码 1673 人阅读 | 0 人回复

<

目次
1、Nginx效劳
1、装置及运转掌握
2、设置文件nginx.conf
 2.1齐局设置   注释:
 2.2  I/O变乱设置   注释:
 2.3 HTTP设置    注释:
 3、会见形态统计及假造主机使用
2、LNMP架构及使用安排
 1、拆建LNMP网站仄台
1.1、装置MYSQL数据库(三章———Mysql数据库体系3.1)
1.2、装置PHP剖析情况
1.3设置nginx撑持PHP情况
2、正在LNMP仄台中安排web使用
2.1安排法式代码
温习题

1、Nginx效劳

Nginx (engine x) 是一款沉量级的HTTP效劳器硬件,长处:不变性好、丰硕的功用散、俭朴的设置文件战低体系资本的耗损,和占据内乱存少,并收才能强(单台物理效劳器可撑持30000~50000个并收恳求)正果云云,大批供给交际收集、消息资讯、电子商务的企业纷繁挑选 Nginx去供给效劳    例:百度、京东、新浪、网易、腾讯、淘宝等
1、装置及运转掌握

  1. [root@C7--01 ~]# yum -y install pcre-devel zlib-devel       #装置撑持硬件  (供给响应的库 战头文件)
  2. .........
  3. ....
  4. [root@C7--01 ~]# useradd -M -s /sbin/nologin nginx          #创立运转用户,组
  5. [root@C7--01 ~]# tar xf nginx-1.12.0.tar.gz -C /usr/src/    #解压nginx
  6. [root@C7--01 ~]# cd /usr/src/nginx-1.12.0/                  #进进目次
  7. [root@C7--01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
  8. ............
  9. .....
复造代码
  装置目次设置为/usr/loca/nginx ,运转用户,组设置为nginx,启用  --with-http_stub_status_module   撑持形态统计,便于检察效劳器的毗连疑息
 成绩:正在停止剖析装置时呈现毛病

  1. [root@C7--01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
  2. checking for OS
  3. + Linux 3.10.0-693.el7.x86_64 x86_64
  4. checking for C compiler ... not found
  5. ./configure: error: C compiler cc is not found
复造代码
打点办法:装置 gcc  openssl-devel
  1. [root@C7--01 nginx-1.12.0]# yum -y install gcc pcre-devel zlib-devel openssl-devel         
复造代码

  1. [root@C7--01 nginx-1.12.0]# make && make install     #编译装置
  2. [root@C7--01 nginx-1.12.0]# ls /usr/local/nginx/     #考证装置
  3. conf  html  logs  sbin
复造代码
劣化施行(便利nginx 运转)
  1. [root@C7--01 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/    #硬毗连
  2. [root@C7--01 nginx-1.12.0]# ls -l /usr/local/sbin/
  3. 总用量 0
  4. lrwxrwxrwx 1 root root 27 8月  28 00:13 nginx -> /usr/local/nginx/sbin/nginx
复造代码
检察nginx协助号令 
  1. [root@C7--01 ~]# nginx -h
  2. nginx version: nginx/1.12.0
  3. Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
  4. Options:
  5.   -?,-h         : this help
  6.   -v            : show version and exit
  7.   -V            : show version and configure options then exit
  8.   -t            : test configuration and exit
  9.   -T            : test configuration, dump it and exit
  10.   -q            : suppress non-error messages during configuration testing
  11.   -s signal     : send signal to a master process: stop, quit, reopen, reload
  12.   -p prefix     : set prefix path (default: /usr/local/nginx/)
  13.   -c filename   : set configuration file (default: conf/nginx.conf)
  14.   -g directives : set global directives out of configuration file
复造代码
 1.1启动、截至
  1. [root@C7--01 ~]# nginx       #启动nginx效劳
  2. [root@C7--01 ~]# netstat -utpln | grep nginx
  3. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      44886/nginx: master
复造代码
 会见测试
115115wwdexhx8gofhdh06.jpg

 正在字符界里可使用文本阅读器检察
    -dump:将HTML文档以杂文本的方法挨印到尺度输出装备
  1. [root@C7--01 ~]# yum  -y install elinks         #装置文本阅读器
  2. [root@C7--01 ~]# elinks  --dump  http://192.168.1.1
  3.                                Welcome to nginx!
  4.    If you see this page, the nginx web server is successfully installed and
  5.    working. Further configuration is required.
  6.    For online documentation and support please refer to [1]nginx.org.
  7.    Commercial support is available at [2]nginx.com.
  8.    Thank you for using nginx.
  9. References
  10.    Visible links
  11.    1. http://nginx.org/
  12.    2. http://nginx.com/
复造代码
   主法式Nginx撑持尺度的历程旌旗灯号,经由过程kill或killall号令收收HUP旌旗灯号暗示重载设置,QUIT旌旗灯号暗示退出历程,KILL旌旗灯号暗示杀逝世历程,最小化装置的centos 体系默许出有装置killall号令,需求先装置
  1. [root@C7--01 ~]# yum -y install psmisc          #装置killall号令
  2. ...........
  3. .....
  4. [root@C7--01 ~]# killall -s HUP  nginx          # 重载设置 相称于  killall  -1  nginx
  5. [root@C7--01 ~]# netstat -utpln | grep nginx
  6. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46095/nginx: master
  7. [root@C7--01 ~]# killall -s QUIT  nginx         # 截至效劳  相称于 killall -3 nginx   (退出历程)
  8. [root@C7--01 ~]# netstat -utpln | grep nginx
复造代码
   编写nginx效劳剧本利用 systemctl 东西停止办理
  1. [root@C7--01 ~]# vi /etc/init.d/nginx
  2. #!/bin/bash
  3. # chkconfig: - 99 20
  4. # description: Nginx Server Control Script
  5. NP="/usr/local/nginx/sbin/nginx"
  6. NPF="/usr/local/nginx/logs/nginx.pid"
  7. case "$1" in
  8.   start)
  9.     $NP;
  10.     if [ $? -eq 0 ]
  11.     then
  12.       echo "nginx is starting!! "
  13.     fi
  14.   ;;
  15.   stop)
  16.     kill -s QUIT $(cat $NPF)
  17.     if [ $? -eq 0 ]
  18.     then
  19.     echo "nginx is stopping!! "
  20.     fi
  21.   ;;
  22.   restart)
  23.     $0 stop
  24.     $0 start
  25.   ;;
  26.   reload)
  27.     kill -s HUP $(cat $NPF)
  28.     if [ $? -eq 0 ]
  29.     then
  30.       echo "nginx config file is reload! "
  31.     fi
  32.   ;;
  33.   *)
  34.     echo "Usage: $0 {start|stop|restart|reload}"
  35.     exit 1
  36. esac
  37. exit 0
  38. [root@C7--01 ~]# chmod +x /etc/init.d/nginx    #付与施行权限
  39. [root@C7--01 ~]# chkconfig --add nginx         #增加为体系效劳
  40. [root@C7--01 ~]# systemctl status nginx        #检察nginx 效劳形态
  41. ● nginx.service - SYSV: Nginx Server Control Script
  42.    Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
  43.    Active: inactive (dead)
  44.      Docs: man:systemd-sysv-generator(8)
复造代码
2、设置文件nginx.conf

       Nginx效劳器主设置文件:/usr/local/nginx/conf/nginx.conf;有三年夜块内乱容:齐局设置、I/O变乱设置、HIIP设置;设置语句的格局为“枢纽字 值;”(开端以分号暗示完毕),“#”开端为正文
 2.1齐局设置   注释:

  1. [root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. #user  nobody;                           #运转用户
  3. worker_processes  1;                     #事情历程数目;可参考CPU中心总数指定事情历程数,网站会见量越年夜,历程数设置越多
  4. #error_log  logs/error.log;              #毛病日记文件的地位
  5. #error_log  logs/error.log  notice;
  6. #error_log  logs/error.log  info;
  7. #pid        logs/nginx.pid;              #PID文件地位
  8. ......
  9. ..
复造代码
 2.2  I/O变乱设置   注释:

  1. events {                       #利用events界定标识表记标帜指定nginx历程的I/O呼应模子,每一个历程毗连数等
  2.     use epoll;                 #利用epoll模子,进步机能
  3.     worker_connections  4096;  #每一个历程处置4096个毗连(默许为1024:每一个历程的毗连数目普通正在50000以下按照实践状况设置)
  4. }
复造代码
 留意:如事情历程数是 8;每一个历程处理惩罚4096 个毗连,则Nginx 供给效劳的毗连数是(4096X8);具体看效劳器硬件战收集带宽等物理前提机能等
 2.3 HTTP设置    注释:

  1. [root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. http {
  3.     include       mime.types;
  4.     default_type  application/octet-stream;
  5.     #log_format  main  &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
  6.     #                  &#39;$status $body_bytes_sent "$http_referer" &#39;
  7.     #                  &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;
  8.     #access_log  logs/access.log  main;            #会见日记地位
  9.     sendfile        on;                            #开启下效传输文件形式
  10.     #tcp_nopush     on;
  11.     #keepalive_timeout  0;
  12.     keepalive_timeout  65;                         #毗连连结超时
  13.     #gzip  on;
  14.     server {                                       #web效劳的监听设置
  15.         listen       80;                           #监听地点及端心
  16.         server_name  localhost;                    #网站称号
  17.         #charset koi8-r;                           #网页的默许字符散
  18.         #access_log  logs/host.access.log  main;
  19.         location / {                               #根目次设置
  20.             root   html;                           #网站根目次的地位,相对装置目次
  21.             index  index.html index.htm;           #默许尾页(搜引页)
  22.         }
  23.         #error_page  404              /404.html;
  24.         # redirect server error pages to the static page /50x.html
  25.         #
  26.         error_page   500 502 503 504  /50x.html;   #内乱部毛病的反应页里
  27.         location = /50x.html {                     #毛病页里设置
  28.             root   html;
  29.         }
  30. .........
  31. ....
  32.     }
复造代码
nginx中的location语法:location [  = / ~ / ~* / ^~]   /uri/  { … }
=开首暗示精确婚配
~开首暗示辨别大小写的正则婚配
~*开首暗示没有辨别大小写的正则婚配
!~战!~*别离为辨别大小写没有婚配及没有辨别大小写没有婚配 的正则
/通用婚配,任何恳求城市婚配到
^~开首暗示uri以某个通例字符串开首,了解为婚配 url途径便可。nginx不合错误url做编码,因而恳求为/static/20%/aa,能够被划定规矩^~ /static/ /aa婚配到(留意是空格)
婚配挨次仅供参考:起首婚配 =,其次婚配^~, 其次是按文件中挨次的正则婚配,最初是交给 /通用         婚配。当有婚配胜利时分,截至婚配,按当前婚配划定规矩处理惩罚恳求
 3、会见形态统计及假造主机使用

   编纂设置文件:把本设置文件修正名字为nginx.conf.bak
  1. [root@C7--01 ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak                        
复造代码
新建nginx.conf设置文件停止编纂
  1. [root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. worker_processes  1;
  3. events {
  4.     use epoll;
  5.     worker_connections  4096;
  6. }
  7. http {
  8.     include       mime.types;
  9.     default_type  application/octet-stream;
  10.     log_format  main  &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
  11.                       &#39;$status $body_bytes_sent "$http_referer" &#39;
  12.                       &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;
  13.     access_log  logs/access.log  main;
  14.     sendfile        on;
  15.     keepalive_timeout  65;
  16.     server {
  17.         listen       80;
  18.         server_name  www.aaa.com;
  19.         charset utf-8;
  20.         location / {
  21.             root   html;
  22.             index  index.html index.php;
  23.         }
  24.         location /status {                        #会见地位:/status
  25.                 stub_status on;                   #翻开形态统计功用
  26.                 access_log off;                   #封闭此地位的日记记载
  27.         }               
  28.         error_page   500 502 503 504  /50x.html;
  29.         location = /50x.html {
  30.             root   html;
  31.         }
  32.     }
  33. }
  34. [root@C7--01 ~]# nginx -t    # 查抄设置文件
  35. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  36. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  37. [root@C7--01 ~]# systemctl start nginx           #启动nginx
  38. [root@C7--01 ~]# systemctl status nginx          #检察形态
  39. ● nginx.service - SYSV: Nginx Server Control Script
  40.    Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
  41.    Active: active (exited) since 六 2021-08-28 00:36:18 CST; 19h ago
  42.      Docs: man:systemd-sysv-generator(8)
  43.   Process: 46093 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
复造代码
   会见测试:IE阅读器  http://192.168.1.1/status
当前的形态统计疑息
Active conmections暗示当前的举动毗连数     1
server accepts handled requests 暗示曾经处理惩罚的毗连疑息
三个数顺次是:已处理惩罚的毗连数(1)、胜利的TCP握脚次数(1)、已处理惩罚的恳求数(1)
 Reading: 0 Writing: 1 Waiting: 0读与形态  写进形态   等待形态
  1. [root@C7--01 ~]# elinks --dump http://192.168.1.1/status
  2.    Active connections: 1 server accepts handled requests 1 1 1 Reading: 0
  3.    Writing: 1 Waiting: 0
复造代码
 图形化测试
115115ckgs2ykcgkwszvks.jpg

3.1安排NGINX假造主机
办法一:设置DNS效劳
  1. [root@C7--01 ~]# yum -y install bind    #装置dns效劳
  2. .......
  3. ...
  4. [root@C7--01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33   #进进网卡
  5. ......
  6. ..
  7. DNS1=192.168.1.1       #增加dns地点
  8. 保留退出
  9. [root@C7--01 ~]# systemctl restart network        #重启网卡
  10. [root@C7--01 ~]# cat /etc/resolv.conf             #检察dns
  11. # Generated by NetworkManager
  12. search 1
  13. nameserver 192.168.1.1
复造代码
修正主设置文件
  1. [root@C7--01 ~]# vim /etc/named.conf
  2. options {
  3.         listen-on port 53 { 192.168.1.1; };                   #修正成地点为192.168.1.1
  4.         listen-on-v6 port 53 { ::1; };
  5.         directory       "/var/named";
  6.         dump-file       "/var/named/data/cache_dump.db";
  7.         statistics-file "/var/named/data/named_stats.txt";
  8.         memstatistics-file "/var/named/data/named_mem_stats.txt";
  9.         allow-query     { any; };                            #许可局部网段会见
  10. .......
  11. ....
  12. zone "." IN {
  13.         type hint;
  14.         file "named.ca";
  15. };
  16. zone "mac.com" IN {          #增加mac.com
  17.         type master;
  18.         file "mac";          #文件 mac
  19. };
  20. zone "aaa.com" IN {          #增加aaa.com
  21.       type master;
  22.       file "aaa";            #文件aaa
  23. };
  24. 保留退出
复造代码
编纂地区设置文件
  1. [root@C7--01 ~]# cd /var/named
  2. [root@C7--01 named]# cp named.localhost aaa    #复造named.localhost,称号修正为aaa
  3. [root@C7--01 named]# cp named.localhost mac    #复造named.localhost,称号修正为mac
  4. [root@C7--01 named]# vim mac              #修正mac地区设置文件
  5. $TTL 1D
  6. @       IN SOA  mac.com. admin.mac.com. (
  7.                                         2021082101      ; serial
  8.                                         1D      ; refresh
  9.                                         1H      ; retry
  10.                                         1W      ; expire
  11.                                         3H )    ; minimum
  12.         NS      www.mac.com.
  13.         MX  10  mail.mac.com.
  14. www     IN   A  192.168.1.1
  15. 保留退出
  16. [root@C7--01 named]# vim aaa             #修正aaa地区文件
  17. $TTL 1D
  18. @       IN SOA  aaa.com. admin.aaa.com. (
  19.                                         2021080902      ; serial
  20.                                         1D      ; refresh
  21.                                         1H      ; retry
  22.                                         1W      ; expire
  23.                                         3H )    ; minimum
  24.         NS      www.aaa.com.
  25.         MX  10  mail.aaa.com.
  26. www     IN   A  192.168.1.1
复造代码
 变动地区文件战主设置文件的权限
  1. [root@C7--01 named]# chown named:named aaa     #修正属主:属组
  2. [root@C7--01 named]# chown named:named mac     #修正属主:属组
  3. [root@C7--01 named]# chown named:named /etc/named.conf  #修正属主:属组
  4. [root@C7--02 ~]# systemctl start named           #启动dns效劳
复造代码
测试:剖析dns两个域名胜利皆是一个ip地点
  1. C:\Users\wrzs0> nslookup  
  2. 默许效劳器:  UnKnown
  3. Address:  192.168.1.1
  4. >
  5. > www.aaa.com
  6. 效劳器:  UnKnown
  7. Address:  192.168.1.1
  8. 称号:    www.aaa.com
  9. Address:  192.168.1.1
  10. > www.mac.com
  11. 效劳器:  UnKnown
  12. Address:  192.168.1.1
  13. 称号:    www.mac.com
  14. Address:  192.168.1.1
复造代码
 办法两:进进hosts文件增加(hosts文件是卖力ip地点取域名快速剖析的文件
  1. [root@C7--01 ~]# vim /etc/hosts
  2. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.1.1 www.aaa.com
  5. 192.168.1.1 www.mac.com
复造代码
  筹办网站目次战测试文件
  1. [root@C7--01 ~]# cd /usr/local/nginx/html      #进进目次
  2. [root@C7--01 html]# ls
  3. 50x.html  index.html
  4. [root@C7--01 html]# mkdir aaa                  #新建html根目次aaa
  5. [root@C7--01 html]# mkdir mac                  #新建html根目次mac
  6. [root@C7--01 html]# ls
  7. 50x.html  aaa  index.html  mac
复造代码
编纂html尾页
  1. [root@C7--01 html]# vim aaa/index.html
  2. aaa <h1>明天气候实好<h1/>
  3. [root@C7--01 html]# vim mac/index.html
  4. mac <h3>欢送去到王者峡谷<h3/>
复造代码
 调解nginx.conf设置文件(倡议删除会见形态统计设置
  1. [root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. worker_processes  1;
  3. events {
  4.     use epoll;
  5.     worker_connections  4096;
  6. }
  7. http {
  8.     include       mime.types;
  9.     default_type  application/octet-stream;
  10.     log_format  main  &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
  11.                       &#39;$status $body_bytes_sent "$http_referer" &#39;
  12.                       &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;
  13.     access_log  logs/access.log  main;
  14.     sendfile        on;
  15.     keepalive_timeout  65;
  16.     server {
  17.         listen       80;
  18.         server_name  www.aaa.com;                   #修正网站称号
  19.         charset utf-8;
  20.         location / {
  21.             root   /usr/local/nginx/html/aaa;       #修正网站根目次
  22.             index  index.html index.php;
  23.         }
  24.      }
  25.     server {
  26.         listen       80;
  27.         server_name  www.mac.com;                   #修正网站称号
  28.         charset utf-8;
  29.         location / {
  30.             root   /usr/local/nginx/html/mac;       #修正网站根目次
  31.             index  index.html index.php;
  32.         }
  33.      }
  34. }
  35. [root@C7--01 ~]# nginx -t                         #查抄设置文件
  36. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  37. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  38. [root@C7--01 ~]# elinks --dump http://www.aaa.com   #字符界里会见
  39.    aaa
  40. 明天气候实好
  41. [root@C7--01 ~]# elinks --dump http://www.mac.com
  42.    mac
  43.   欢送去到王者峡谷
复造代码
 图形化会见
115115al36e0f8p0380z90.jpg

 
115116ri92bezebe92yteb.jpg

2、LNMP架构及使用安排

  LNMP仄台的组成:Linux效劳器、MySQL数据库、PHP剖析情况、Nginx (战LAMP一样需求Linux效劳器、MySQL数据库、PHP剖析情况)
  LNMP战LAMP的区分:正在Nginx取PHP的合作设置上
 1、拆建LNMP网站仄台

1.1、装置MYSQL数据库三章———Mysql数据库体系3.1

装置步调根据:三章———Mysql数据库体系3.1 装置
1.2、装置PHP剖析情况

编译装置PHP
  1. [root@C7--02 ~]# yum -y install libxml2-devel gd zlib-devel libjpeg-devel libpng-devel
  2. 已减载插件:fastestmirror
  3. aaa                                                                                                 | 3.6 kB  00:00:00  
  4. ......
  5. ...
  6. [root@C7--02 ~]# tar xf php-5.5.38.tar.gz -C /usr/src
  7. [root@C7--02 ~]# cd /usr/src/php-5.5.38/
  8. [root@C7--02 php-5.5.38]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib           
  9. ......
  10. ...
  11. [root@C7--02 php-5.5.38]# make && make install
  12. .........
  13. .....
  14. [root@C7--02 php-5.5.38]# ls /usr/local/php5/
  15. bin  etc  include  lib  php  sbin  var
复造代码
装置后调解
  1. [root@C7--02 php-5.5.38]# cp php.ini-development /usr/local/php5/php.ini               
  2. [root@C7--02 ~]# ln -s /usr/local/php5/bin/* /usr/local/bin/
  3. [root@C7--02 ~]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
复造代码
装置ZendGuardLoader
  1. [root@C7--02 ~]# tar xf zend-loader-php5.5-linux-x86_64_update1.tar.gz   #解压
  2. [root@C7--02 ~]# cd zend-loader-php5.5-linux-x86_64
  3. [root@C7--02 zend-loader-php5.5-linux-x86_64]# ls                        #检察
  4. opcache.so  README.txt  ZendGuardLoader.so
  5. [root@C7--02 zend-loader-php5.5-linux-x86_64]# cp ZendGuardLoader.so /usr/local/php5/lib/php/                    
  6. [root@C7--02 zend-loader-php5.5-linux-x86_64]# cd
  7. [root@C7--02 ~]# vim /usr/local/php5/php.ini                            #进进php.ini文件
  8. .........
  9. .....
  10. ;curl.cainfo =
  11. ; Local Variables:
  12. ; tab-width: 4
  13. ; End:
  14. zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so      #正在最上面增加
  15. zend_loader.enable=1                                            #正在最上面增加
复造代码
1.3设置nginx撑持PHP情况

   启用php-fpm历程(默许端标语为9000)
  1. [root@C7--02 ~]# cd /usr/local/php5/etc/
  2. [root@C7--02 etc]# useradd -M -s /sbin/nologin php     #创立用户php
  3. [root@C7--02 etc]# vim php-fpm.conf                    #创立新设置文件
  4. [global]
  5. pid = run/php-fpm.pid        #确认pid文件地位
  6. [www]
  7. listen = 127.0.0.1:9000
  8. user = php                   #运转用户
  9. group = php                  #运转组
  10. pm = dynamic
  11. pm.max_children = 50         #最多闲暇历程数
  12. pm.start_servers = 20        #启动时开启的历程数
  13. pm.min_spare_servers = 5     #起码闲暇历程数
  14. pm.max_spare_servers = 35
  15. 保留退出
  16. [root@C7--02 etc]# /usr/local/php5/sbin/php-fpm      #启动历程
  17. [root@C7--02 etc]# netstat -utpln | grep php         #检察
  18. tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      104914/php-fpm: mas
  19. [root@C7--02 ~]# killall -9 php-fpm         #封闭历程
  20. [root@C7--02 ~]# netstat -utpln | grep php  #检察
复造代码
  新建LNMP启动剧本:能够正在启动或截至 nginx 效劳器时php-fpm历程也能够启动或截至
  1. [root@C7--02 ~]# vim /etc/init.d/lnmp
  2. #!/bin/bash
  3. # chkconfig: 35 95 30
  4. # description: This script is for LNMP Management!
  5. NGF=/usr/local/nginx/sbin/nginx
  6. NGP=/usr/local/nginx/logs/nginx.pid
  7. FPMF=/usr/local/php5/sbin/php-fpm
  8. FPMP=/usr/local/php5/var/run/php-fpm.pid
  9. case $1 in
  10.    start)
  11.       $NGF &&echo "nginx is starting! "
  12.       $FPMF && echo "php-fpm is starting! "
  13.    ;;
  14.    stop)
  15.       kill -QUIT $(cat $NGP) &&echo "nginx is stoped! "
  16.       kill -QUIT $(cat $FPMP) &&echo "php-fpm is stoped! "
  17.    ;;
  18.    restart)
  19.       $0 stop
  20.       $0 start
  21.    ;;
  22.    reload)
  23.       kill -HUP $(cat $NGP)
  24.       kill -HUP $(cat $FPMP)
  25.    ;;
  26.    status)
  27.       netstat -utpln |grep nginx &>/dev/null
  28.       if [  $? -eq 0 ]
  29.       then
  30.          echo "nginx is running! "
  31.       else
  32.          echo "nginx is not running! "
  33.       fi
  34.       netstat -upltn |grep php-fpm &>/dev/null
  35.       if [ $? -eq 0 ]
  36.       then
  37.          echo "php-fpm is runing! "
  38.       else
  39.          echo "php-fpm is not running! "
  40.       fi
  41.    ;;
  42.    *)
  43.       echo "Usage $0 {start|stop|status|restart}"
  44.       exit 1
  45.    ;;
  46. esac
  47. exit 0
  48. 保留退出
  49. [root@C7--02 ~]# chmod +x /etc/init.d/lnmp     #付与施行权限
  50. [root@C7--02 ~]# chkconfig --add lnmp          #参加到体系效劳
复造代码
  确认php-fpm、nginx效劳已截至
  1. [root@C7--02 ~]# netstat -anput | grep php-fpm  #检察
  2. [root@C7--02 ~]# nginx -s stop                  #截至nginx效劳
  3. [root@C7--02 ~]# netstat -naput | grep nginx    #检察
复造代码
   同时启动php-fpm、nginx效劳
  1. [root@C7--02 ~]# systemctl start lnmp     #同时启动
  2. [root@C7--02 ~]# systemctl status lnmp    #检察形态
  3. ● lnmp.service - SYSV: This script is for LNMP Management!
  4.    Loaded: loaded (/etc/rc.d/init.d/lnmp; bad; vendor preset: disabled)
  5.    Active: active (running) since 日 2021-08-29 22:50:39 CST; 21s ago
  6.      Docs: man:systemd-sysv-generator(8)
  7.   Process: 122085 ExecStart=/etc/rc.d/init.d/lnmp start (code=exited, status=0/SUCCESS)
  8.    CGroup: /system.slice/lnmp.service
  9.            ├─122087 nginx: master process /usr/local/nginx/sbin/nginx
  10.            ├─122089 nginx: worker process
  11.            ├─122090 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)
  12.            ├─122091 php-fpm: pool www
  13.            ├─122092 php-fpm: pool www
  14.            ├─122093 php-fpm: pool www
  15.            ├─122094 php-fpm: pool www
  16.            ├─122095 php-fpm: pool www
  17.            ├─122096 php-fpm: pool www
  18.            ├─122097 php-fpm: pool www
  19.            ├─122098 php-fpm: pool www
  20.            ├─122099 php-fpm: pool www
  21.            ├─122100 php-fpm: pool www
  22.            ├─122101 php-fpm: pool www
  23.            ├─122102 php-fpm: pool www
  24.            ├─122103 php-fpm: pool www
  25.            ├─122104 php-fpm: pool www
  26.            ├─122105 php-fpm: pool www
  27.            ├─122106 php-fpm: pool www
  28.            ├─122107 php-fpm: pool www
  29.            ├─122108 php-fpm: pool www
  30.            ├─122109 php-fpm: pool www
  31.            └─122110 php-fpm: pool www
  32. 8月 29 22:50:39 C7--02 systemd[1]: Starting SYSV: This script is for LNMP Management!...
  33. 8月 29 22:50:39 C7--02 lnmp[122085]: nginx is starting!
  34. 8月 29 22:50:39 C7--02 lnmp[122085]: php-fpm is starting!
  35. 8月 29 22:50:39 C7--02 systemd[1]: Started SYSV: This script is for LNMP Management!.
  36. [root@C7--02 ~]#
复造代码
   假如效劳启动毛病便杀逝世历程:[root@C7--02 ~]# killall -9 nginx   正在重启LNMP

   设置nginx撑持PHP剖析
     让Nginx可以剖析PHP网页有两种办法:一,会见PHP页里的Web恳求转交给其他效劳器(LAMP)去向置惩罚;两,利用PHP的FPM模块去挪用本机的PHP情况                        不管将PHP页里交给LAMP效劳器来剖析,仍是挪用本机的php-fpm历程停止剖析,皆需求正在“server { } ”设置段中增加location设置,以便指定会见php网页时采纳何种操纵
 办法一转交给LAMP效劳器:   (留意:\.php 输进毛病会招致会见失利)
  1. [root@C7--02 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. ......
  3. ...
  4.     server {
  5.         listen       80;
  6.         server_name  www.benet.com;
  7.         charset utf-8;
  8.         location ~ \.php$ {                          #会见.php页里的设置段
  9.             proxy_pass http://目的主机ip地点:80;      #apache效劳器的监听地点
  10.         }
复造代码
 办法两挪用本机的php-fpm历程剖析:(本次尝试利用办法两) (留意:\.php 输进毛病会招致会见失利)
  1. [root@C7--02 ~]# vim  /usr/local/nginx/conf/nginx.conf
  2. worker_processes  1;
  3. events {
  4.     use epoll;
  5.     worker_connections  4096;
  6. }
  7. http {
  8.     include       mime.types;
  9.     default_type  application/octet-stream;
  10.     log_format  main  &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
  11.                       &#39;$status $body_bytes_sent "$http_referer" &#39;
  12.                       &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;
  13.     access_log  logs/access.log  main;
  14.     sendfile        on;
  15.     keepalive_timeout  65;
  16.     server {
  17.         listen       80;
  18.         server_name  www.mac.com;
  19.         charset utf-8;
  20.         access_log logs/mac.access.log;
  21.         location / {
  22.             root   /usr/local/nginx/html/mac;
  23.             index  index.html index.php;
  24.         }
  25.     location ~ \.php$ {
  26.             root /usr/local/nginx/html/mac;
  27.             fastcgi_pass 127.0.0.1:9000;
  28.             fastcgi_index index.php;
  29.             include fastcgi.conf;
  30.         }
  31.     }
  32. }
复造代码
 创立测试php网页
  1. [root@C7--02 ~]# mkdir /usr/local/nginx/html/mac   #创立目次
  2. [root@C7--02 ~]# vim /usr/local/nginx/html/mac/test.php   #创立会见尾页mysql数据库用户名root暗码123.com
  3. <?php
  4. $link=mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;123.com&#39;);
  5. if($link) echo "<h1>会见数据库胜利 !!</h1>";
  6. mysqli_close($link);
  7. ?>
复造代码
115116itvvt5qn55atqycm.jpg

2、正在LNMP仄台中安排web使用

    LNMP仄台取LAMP仄台长短常类似的,区分正在于所用Web效劳硬件的差别,而那取利用PHP开辟的Web使用法式并没有太年夜干系,因而PHP使用的安排办法也是相似的
2.1安排法式代码

  1. [root@C7--02 ~]# yum -y install unzip
  2. [root@C7--02 ~]# unzip Discuz_X3.3_SC_UTF8.zip   #解压discuz!社区论坛
  3. [root@C7--02 ~]# mv upload/ /usr/local/nginx/html/mac/sqlt   #把upload剪切到LNMP效劳器的网站根目次下
  4. [root@C7--02 ~]# chown -R php:php /usr/local/nginx/html/mac/sqlt  #设置属主:属组,让nginx、php-fpm法式写进权限
  5. [root@C7--02 ~]# mysql -uroot -p123.com         #进进mysql数据库
  6. .......
  7. ...
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; to clear the current input statement.
  12. mysql> create database sqlt;             #创立库sqlt
  13. Query OK, 1 row affected (0.00 sec)
  14. mysql> grant all on sqlt.* to aaa@localhost identified by &#39;123.com&#39;;    #创立用户aaa暗码为123.com 能够对sqlt库具有一切权限
  15. Query OK, 0 rows affected (0.01 sec)
  16. mysql> quit          #退出
  17. Bye
复造代码
  输进: http://www.mac.com/sqlt/install/index.php
115116zyhr30nxy3497n7i.jpg

 
115116ywgg695l6n5eell0.jpg

 
115117ews5c0zww6u66w9r.jpg

   稍等片晌就能够会见  www.mac.com/sqlt/forum.php 
115117hu299usrls9h77rr.jpg

  办理背景:http://www.mac.com/sqlt/admin.php
115117epvjzvvaxqho8s0x.jpg

   输进之前给的办理员账号战密码就能够办理社区论坛了
115118iti2ft282kivz8ni.jpg



温习题

1、简述LNMP仄台的组成组件,和取LAMP仄台的区分 
LNMP仄台的组成:Linux效劳器、MySQL数据库、PHP剖析情况、Nginx
区分:正在于所用Web效劳硬件的差别
2、正在编译装置Nginx时经由过程指定甚么选项增加供给会见统计的stub_status模块?
指定:http_stub_status_module供给会见统计的stub_status模块
3、正在Linux体系中施行killall -s HUP nginx取killall -s QUIT nginx号令的感化别离是甚么?
-s HUP 相称于 -1:从头减载设置
-s QUIT 相称于 -3:截至效劳​​​​
4、正在Nginx的设置文件中,哪几个设置参数决议了一般效劳的毗连数?
  1. events {
  2. use   epoll; 
  3. worker_connections  1024;
  4. }
复造代码
5、正在Nginx设置文件的server { }设置段中,root语句的感化是甚么?
感化:网站根目次的地位,相对装置目次
6、利用Nginx的形态统计功用除启用内乱建模块中,借需求正在设置文件中增加哪些内乱容?
  1. 需求增加:
  2. location /status   {             #会见地位为/status
  3.      stub status on;             #翻开形态统计功用
  4.      access log off;              #封闭此地位的日记记载
  5. }
复造代码
7、简述Nginx设置假造主机的办法取流程
(1)筹办网站目次及测试文件  (2)调解nginx.conf设置文件  (3)测试假造Web主机
8、mysqladmin -u root password &#39;pwd123&#39;号令的感化甚么?
给mysqi数据库的root用户设置密码
9、Nginx对PHP的撑持能够经由过程哪两种方法完成?
1)把会见 PHP 页里的 Web 恳求转交给其他效劳器(LAMP)处理惩罚
2)利用PHP的FPM模块去挪用本机的 PHP 情况
10、写出php-fpm法式的启动办法取默许监听端心
启动办法:/usr/local/sbin/php-fpm
默许监听端心:9000

免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则