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

三期总目录链接

目录

一、Nginx服务

1、安装及运行控制

2、配置文件nginx.conf

 2.1全局配置   解释:

 2.2  I/O事件配置   解释:

 2.3 HTTP配置    解释:

 3、访问状态统计及虚拟主机应用

二、LNMP架构及应用部署

 1、搭建LNMP网站平台

1.1、安装MYSQL数据库(三章———Mysql数据库系统3.1)

1.2、安装PHP解析环境

1.3配置nginx支持PHP环境

2、在LNMP平台中部署web应用

2.1部署程序代码

复习题


一、Nginx服务

Nginx (engine x) 是一款轻量级的HTTP服务器软件,优点:稳定性好、丰富的功能集、简单的配置文件和低系统资源的消耗,以及占有内存少,并发能力强(单台物理服务器可支持30000~50000个并发请求)正因如此,大量提供社交网络、新闻资讯、电子商务的企业纷纷选择 Nginx来提供服务    例:百度、京东、新浪、网易、腾讯、淘宝等

1、安装及运行控制

[root@C7--01 ~]# yum -y install pcre-devel zlib-devel       #安装支持软件  (提供相应的库 和头文件)
.........
....

[root@C7--01 ~]# useradd -M -s /sbin/nologin nginx          #创建运行用户,组
[root@C7--01 ~]# tar xf nginx-1.12.0.tar.gz -C /usr/src/    #解压nginx
[root@C7--01 ~]# cd /usr/src/nginx-1.12.0/                  #进入目录
[root@C7--01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
............
.....

安装目录设置为/usr/loca/nginx ,运行用户,组设置为nginx,启用  --with-http_stub_status_module   支持状态统计,便于查看服务器的连接信息

 问题:在进行解析安装时出现错误

[root@C7--01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

解决方法:安装 gcc  openssl-devel

[root@C7--01 nginx-1.12.0]# yum -y install gcc pcre-devel zlib-devel openssl-devel         

[root@C7--01 nginx-1.12.0]# make && make install     #编译安装
 
[root@C7--01 nginx-1.12.0]# ls /usr/local/nginx/     #验证安装
conf  html  logs  sbin

优化执行(方便nginx 运行)

[root@C7--01 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/    #软连接
[root@C7--01 nginx-1.12.0]# ls -l /usr/local/sbin/
总用量 0
lrwxrwxrwx 1 root root 27 8月  28 00:13 nginx -> /usr/local/nginx/sbin/nginx

查看nginx帮助命令 

[root@C7--01 ~]# nginx -h
nginx version: nginx/1.12.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

 1.1启动、停止

[root@C7--01 ~]# nginx       #启动nginx服务
[root@C7--01 ~]# netstat -utpln | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      44886/nginx: master 

 访问测试

 在字符界面可以使用文本浏览器查看

  -dump:将HTML文档以纯文本的方式打印到标准输出设备

[root@C7--01 ~]# yum  -y install elinks         #安装文本浏览器
[root@C7--01 ~]# elinks  --dump  http://192.168.1.1
                               Welcome to nginx!

   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.

   For online documentation and support please refer to [1]nginx.org.
   Commercial support is available at [2]nginx.com.

   Thank you for using nginx.

References

   Visible links
   1. http://nginx.org/
   2. http://nginx.com/

 主程序Nginx支持标准的进程信号,通过kill或killall命令发送HUP信号表示重载配置,QUIT信号表示退出进程,KILL信号表示杀死进程,最小化安装的centos 系统默认没有安装killall命令,需要先安装

[root@C7--01 ~]# yum -y install psmisc          #安装killall命令
...........
.....


[root@C7--01 ~]# killall -s HUP  nginx          # 重载配置 相当于  killall  -1  nginx
[root@C7--01 ~]# netstat -utpln | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46095/nginx: master 

[root@C7--01 ~]# killall -s QUIT  nginx         # 停止服务  相当于 killall -3 nginx   (退出进程)
[root@C7--01 ~]# netstat -utpln | grep nginx

 编写nginx服务脚本使用 systemctl 工具进行管理

[root@C7--01 ~]# vi /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
    $NP;
    if [ $? -eq 0 ]
    then
      echo "nginx is starting!! "
    fi
  ;;
  stop)
    kill -s QUIT $(cat $NPF)
    if [ $? -eq 0 ]
    then
    echo "nginx is stopping!! "
    fi
  ;;
  restart)
    $0 stop
    $0 start
  ;;
  reload)
    kill -s HUP $(cat $NPF)
    if [ $? -eq 0 ]
    then
      echo "nginx config file is reload! "
    fi
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
esac
exit 0



[root@C7--01 ~]# chmod +x /etc/init.d/nginx    #赋予执行权限
[root@C7--01 ~]# chkconfig --add nginx         #添加为系统服务
[root@C7--01 ~]# systemctl status nginx        #查看nginx 服务状态
● nginx.service - SYSV: Nginx Server Control Script
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

2、配置文件nginx.conf

     Nginx服务器主配置文件:/usr/local/nginx/conf/nginx.conf;有三大块内容:全局配置、I/O事件配置、HIIP配置;配置语句的格式为“关键字 值;”(末尾以分号表示结束),“#”开始为注释

 2.1全局配置   解释:

[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;                           #运行用户
worker_processes  1;                     #工作进程数量;可参考CPU核心总数指定工作进程数,网站访问量越大,进程数设置越多

#error_log  logs/error.log;              #错误日志文件的位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;              #PID文件位置
......
..

 2.2  I/O事件配置   解释:

events {                       #使用events界定标记指定nginx进程的I/O响应模型,每个进程连接数等
    use epoll;                 #使用epoll模型,提高性能
    worker_connections  4096;  #每个进程处理4096个连接(默认为1024:每个进程的连接数量一般在50000以下根据实际情况设置)
}

 注意:如工作进程数是 8;每个进程处理4096 个连接,则Nginx 提供服务的连接数是(4096X8);具体看服务器硬件和网络带宽等物理条件性能等

 2.3 HTTP配置    解释:

[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
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  logs/access.log  main;            #访问日志位置

    sendfile        on;                            #开启高效传输文件模式
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;                         #连接保持超时

    #gzip  on;

    server {                                       #web服务的监听配置
        listen       80;                           #监听地址及端口
        server_name  localhost;                    #网站名称

        #charset koi8-r;                           #网页的默认字符集

        #access_log  logs/host.access.log  main;

        location / {                               #根目录配置
            root   html;                           #网站根目录的位置,相对于安装目录
            index  index.html index.htm;           #默认首页(搜引页)
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;   #内部错误的反馈页面
        location = /50x.html {                     #错误页面配置
            root   html;
        }
.........
....
    }
nginx中的location语法:location [  = / ~ / ~* / ^~]   /uri/  { … }
=开头表示精确匹配
~开头表示区分大小写的正则匹配
~*开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/通用匹配,任何请求都会匹配到
^~开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
匹配顺序仅供参考:首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 /通用         匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求

 3、访问状态统计及虚拟主机应用

 编辑配置文件:把原配置文件修改名字为nginx.conf.bak

[root@C7--01 ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak                        

新建nginx.conf配置文件进行编辑

[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf

 worker_processes  1;
events {
    use epoll;
    worker_connections  4096;
}
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  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.aaa.com;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        location /status {                        #访问位置:/status
                stub_status on;                   #打开状态统计功能
                access_log off;                   #关闭此位置的日志记录
        }                
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


[root@C7--01 ~]# nginx -t    # 检查配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@C7--01 ~]# systemctl start nginx           #启动nginx
[root@C7--01 ~]# systemctl status nginx          #查看状态
● nginx.service - SYSV: Nginx Server Control Script
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (exited) since 六 2021-08-28 00:36:18 CST; 19h ago
     Docs: man:systemd-sysv-generator(8)
  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读取状态  写入状态   等待状态
[root@C7--01 ~]# elinks --dump http://192.168.1.1/status
   Active connections: 1 server accepts handled requests 1 1 1 Reading: 0
   Writing: 1 Waiting: 0

 图形化测试

3.1部署NGINX虚拟主机

方法一:配置DNS服务

[root@C7--01 ~]# yum -y install bind    #安装dns服务
.......
...
[root@C7--01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33   #进入网卡
......
..
DNS1=192.168.1.1       #添加dns地址

保存退出

[root@C7--01 ~]# systemctl restart network        #重启网卡
[root@C7--01 ~]# cat /etc/resolv.conf             #查看dns
# Generated by NetworkManager
search 1
nameserver 192.168.1.1

修改主配置文件

[root@C7--01 ~]# vim /etc/named.conf
options {
        listen-on port 53 { 192.168.1.1; };                   #修改成地址为192.168.1.1
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };                            #允许全部网段访问
.......
....
zone "." IN {
        type hint;
        file "named.ca";
};


zone "mac.com" IN {          #添加mac.com
        type master;
        file "mac";          #文件 mac
};

zone "aaa.com" IN {          #添加aaa.com
      type master;
      file "aaa";            #文件aaa
};


保存退出

编辑区域配置文件

[root@C7--01 ~]# cd /var/named
[root@C7--01 named]# cp named.localhost aaa    #复制named.localhost,名称修改为aaa
[root@C7--01 named]# cp named.localhost mac    #复制named.localhost,名称修改为mac
[root@C7--01 named]# vim mac              #修改mac区域配置文件

$TTL 1D
@       IN SOA  mac.com. admin.mac.com. (
                                        2021082101      ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      www.mac.com.
        MX  10  mail.mac.com.
www     IN   A  192.168.1.1

保存退出

[root@C7--01 named]# vim aaa             #修改aaa区域文件

$TTL 1D
@       IN SOA  aaa.com. admin.aaa.com. (
                                        2021080902      ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      www.aaa.com.
        MX  10  mail.aaa.com.
www     IN   A  192.168.1.1

 更改区域文件和主配置文件的权限

[root@C7--01 named]# chown named:named aaa     #修改属主:属组
[root@C7--01 named]# chown named:named mac     #修改属主:属组
[root@C7--01 named]# chown named:named /etc/named.conf  #修改属主:属组
[root@C7--02 ~]# systemctl start named           #启动dns服务

测试:解析dns两个域名成功都是一个ip地址

C:\Users\wrzs0> nslookup  
默认服务器:  UnKnown
Address:  192.168.1.1

>
> www.aaa.com
服务器:  UnKnown
Address:  192.168.1.1

名称:    www.aaa.com
Address:  192.168.1.1

> www.mac.com
服务器:  UnKnown
Address:  192.168.1.1

名称:    www.mac.com
Address:  192.168.1.1

 方法二:进入hosts文件添加(hosts文件是负责ip地址与域名快速解析的文件

[root@C7--01 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.1 www.aaa.com
192.168.1.1 www.mac.com

准备网站目录和测试文件

[root@C7--01 ~]# cd /usr/local/nginx/html      #进入目录
[root@C7--01 html]# ls
50x.html  index.html
[root@C7--01 html]# mkdir aaa                  #新建html根目录aaa
[root@C7--01 html]# mkdir mac                  #新建html根目录mac
[root@C7--01 html]# ls
50x.html  aaa  index.html  mac

编辑html首页

[root@C7--01 html]# vim aaa/index.html

aaa <h1>今天天气真好<h1/>


[root@C7--01 html]# vim mac/index.html

mac <h3>欢迎来到王者峡谷<h3/>

 调整nginx.conf配置文件(建议删除访问状态统计配置

[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf

 worker_processes  1;
events {
    use epoll;
    worker_connections  4096;
}
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  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.aaa.com;                   #修改网站名称
        charset utf-8;
        location / {
            root   /usr/local/nginx/html/aaa;       #修改网站根目录
            index  index.html index.php;
        }
     }
    server {
        listen       80;
        server_name  www.mac.com;                   #修改网站名称
        charset utf-8;
        location / {
            root   /usr/local/nginx/html/mac;       #修改网站根目录
            index  index.html index.php;
        }
     }
}

[root@C7--01 ~]# nginx -t                         #检查配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@C7--01 ~]# elinks --dump http://www.aaa.com   #字符界面访问
   aaa

今天天气真好
[root@C7--01 ~]# elinks --dump http://www.mac.com
   mac

  欢迎来到王者峡谷

 图形化访问

 

二、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

[root@C7--02 ~]# yum -y install libxml2-devel gd zlib-devel libjpeg-devel libpng-devel
已加载插件:fastestmirror
aaa                                                                                                 | 3.6 kB  00:00:00  
......
...


[root@C7--02 ~]# tar xf php-5.5.38.tar.gz -C /usr/src
[root@C7--02 ~]# cd /usr/src/php-5.5.38/

[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           
......
...

[root@C7--02 php-5.5.38]# make && make install
.........
.....

[root@C7--02 php-5.5.38]# ls /usr/local/php5/
bin  etc  include  lib  php  sbin  var

安装后调整

[root@C7--02 php-5.5.38]# cp php.ini-development /usr/local/php5/php.ini               
[root@C7--02 ~]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@C7--02 ~]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/

安装ZendGuardLoader

[root@C7--02 ~]# tar xf zend-loader-php5.5-linux-x86_64_update1.tar.gz   #解压
[root@C7--02 ~]# cd zend-loader-php5.5-linux-x86_64

[root@C7--02 zend-loader-php5.5-linux-x86_64]# ls                        #查看
opcache.so  README.txt  ZendGuardLoader.so

[root@C7--02 zend-loader-php5.5-linux-x86_64]# cp ZendGuardLoader.so /usr/local/php5/lib/php/                    

[root@C7--02 zend-loader-php5.5-linux-x86_64]# cd
[root@C7--02 ~]# vim /usr/local/php5/php.ini                            #进入php.ini文件
.........
.....
;curl.cainfo =

; Local Variables:
; tab-width: 4
; End:
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so      #在最下面添加
zend_loader.enable=1                                            #在最下面添加

1.3配置nginx支持PHP环境

 启用php-fpm进程(默认端口号为9000)

[root@C7--02 ~]# cd /usr/local/php5/etc/
[root@C7--02 etc]# useradd -M -s /sbin/nologin php     #创建用户php

[root@C7--02 etc]# vim php-fpm.conf                    #创建新配置文件

[global]
pid = run/php-fpm.pid        #确认pid文件位置
[www]
listen = 127.0.0.1:9000
user = php                   #运行用户
group = php                  #运行组
pm = dynamic
pm.max_children = 50         #最多空闲进程数
pm.start_servers = 20        #启动时开启的进程数
pm.min_spare_servers = 5     #最少空闲进程数
pm.max_spare_servers = 35 

保存退出

[root@C7--02 etc]# /usr/local/php5/sbin/php-fpm      #启动进程
[root@C7--02 etc]# netstat -utpln | grep php         #查看
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      104914/php-fpm: mas 

[root@C7--02 ~]# killall -9 php-fpm         #关闭进程
[root@C7--02 ~]# netstat -utpln | grep php  #查看

新建LNMP启动脚本:可以在启动或停止 nginx 服务器时php-fpm进程也可以启动或停止

[root@C7--02 ~]# vim /etc/init.d/lnmp
#!/bin/bash
# chkconfig: 35 95 30
# description: This script is for LNMP Management!
NGF=/usr/local/nginx/sbin/nginx
NGP=/usr/local/nginx/logs/nginx.pid
FPMF=/usr/local/php5/sbin/php-fpm
FPMP=/usr/local/php5/var/run/php-fpm.pid
case $1 in 
   start)
      $NGF &&echo "nginx is starting! "
      $FPMF && echo "php-fpm is starting! "
   ;;
   stop)
      kill -QUIT $(cat $NGP) &&echo "nginx is stoped! "
      kill -QUIT $(cat $FPMP) &&echo "php-fpm is stoped! "
   ;;
   restart)
      $0 stop
      $0 start
   ;;
   reload)
      kill -HUP $(cat $NGP) 
      kill -HUP $(cat $FPMP)
   ;;
   status)
      netstat -utpln |grep nginx &>/dev/null 
      if [  $? -eq 0 ]
      then
         echo "nginx is running! "
      else
         echo "nginx is not running! "
      fi
      netstat -upltn |grep php-fpm &>/dev/null 
      if [ $? -eq 0 ]
      then
         echo "php-fpm is runing! "
      else
         echo "php-fpm is not running! "
      fi
   ;;
   *)
      echo "Usage $0 {start|stop|status|restart}"
      exit 1
   ;;
esac
exit 0

保存退出

[root@C7--02 ~]# chmod +x /etc/init.d/lnmp     #赋予执行权限
[root@C7--02 ~]# chkconfig --add lnmp          #加入到系统服务

确认php-fpm、nginx服务已停止

[root@C7--02 ~]# netstat -anput | grep php-fpm  #查看
[root@C7--02 ~]# nginx -s stop                  #停止nginx服务
[root@C7--02 ~]# netstat -naput | grep nginx    #查看

 同时启动php-fpm、nginx服务

[root@C7--02 ~]# systemctl start lnmp     #同时启动

[root@C7--02 ~]# systemctl status lnmp    #查看状态
● lnmp.service - SYSV: This script is for LNMP Management!
   Loaded: loaded (/etc/rc.d/init.d/lnmp; bad; vendor preset: disabled)
   Active: active (running) since 日 2021-08-29 22:50:39 CST; 21s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 122085 ExecStart=/etc/rc.d/init.d/lnmp start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/lnmp.service
           ├─122087 nginx: master process /usr/local/nginx/sbin/nginx
           ├─122089 nginx: worker process
           ├─122090 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)
           ├─122091 php-fpm: pool www
           ├─122092 php-fpm: pool www
           ├─122093 php-fpm: pool www
           ├─122094 php-fpm: pool www
           ├─122095 php-fpm: pool www
           ├─122096 php-fpm: pool www
           ├─122097 php-fpm: pool www
           ├─122098 php-fpm: pool www
           ├─122099 php-fpm: pool www
           ├─122100 php-fpm: pool www
           ├─122101 php-fpm: pool www
           ├─122102 php-fpm: pool www
           ├─122103 php-fpm: pool www
           ├─122104 php-fpm: pool www
           ├─122105 php-fpm: pool www
           ├─122106 php-fpm: pool www
           ├─122107 php-fpm: pool www
           ├─122108 php-fpm: pool www
           ├─122109 php-fpm: pool www
           └─122110 php-fpm: pool www

8月 29 22:50:39 C7--02 systemd[1]: Starting SYSV: This script is for LNMP Management!...
8月 29 22:50:39 C7--02 lnmp[122085]: nginx is starting!
8月 29 22:50:39 C7--02 lnmp[122085]: php-fpm is starting!
8月 29 22:50:39 C7--02 systemd[1]: Started SYSV: This script is for LNMP Management!.
[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 输入错误会导致访问失败)

[root@C7--02 ~]# vim /usr/local/nginx/conf/nginx.conf

......
...
    server {
        listen       80;
        server_name  www.benet.com;
        charset utf-8;
        location ~ \.php$ {                          #访问.php页面的配置段
            proxy_pass http://目标主机ip地址:80;      #apache服务器的监听地址
        }

 方法二调用本机的php-fpm进程解析:(本次实验使用方法二) (注意:\.php 输入错误会导致访问失败)

[root@C7--02 ~]# vim  /usr/local/nginx/conf/nginx.conf
 worker_processes  1;
events {
    use epoll;
    worker_connections  4096;
}
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  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.mac.com;
        charset utf-8;
        access_log logs/mac.access.log;
        location / {
            root   /usr/local/nginx/html/mac;
            index  index.html index.php;
        }
    location ~ \.php$ {
            root /usr/local/nginx/html/mac;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    }
}

 创建测试php网页

[root@C7--02 ~]# mkdir /usr/local/nginx/html/mac   #创建目录
[root@C7--02 ~]# vim /usr/local/nginx/html/mac/test.php   #创建访问首页mysql数据库用户名root密码123.com
<?php
$link=mysqli_connect('localhost','root','123.com');
if($link) echo "<h1>访问数据库成功 !!</h1>";
mysqli_close($link);
?>

2、在LNMP平台中部署web应用

  LNMP平台与LAMP平台是非常相似的,区别在于所用Web服务软件的不同,而这与使用PHP开发的Web应用程序并无太大关系,因此PHP应用的部署方法也是类似的

2.1部署程序代码

[root@C7--02 ~]# yum -y install unzip
[root@C7--02 ~]# unzip Discuz_X3.3_SC_UTF8.zip   #解压discuz!社区论坛
[root@C7--02 ~]# mv upload/ /usr/local/nginx/html/mac/sqlt   #把upload剪切到LNMP服务器的网站根目录下
[root@C7--02 ~]# chown -R php:php /usr/local/nginx/html/mac/sqlt  #设置属主:属组,让nginx、php-fpm程序写入权限


[root@C7--02 ~]# mysql -uroot -p123.com         #进入mysql数据库
.......
...
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database sqlt;             #创建库sqlt
Query OK, 1 row affected (0.00 sec)

mysql> grant all on sqlt.* to aaa@localhost identified by '123.com';    #创建用户aaa密码为123.com 可以对sqlt库拥有所有权限
Query OK, 0 rows affected (0.01 sec)

mysql> quit          #退出
Bye

输入: http://www.mac.com/sqlt/install/index.php

 

 

 稍等片刻就可以访问  www.mac.com/sqlt/forum.php 

管理后台:http://www.mac.com/sqlt/admin.php

 输入之前给的管理员账号和密码就可以管理社区论坛了

复习题

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的配置文件中,哪几个配置参数决定了正常服务的连接数?

events {
use   epoll; 
worker_connections  1024;
}

5、在Nginx配置文件的server { }配置段中,root语句的作用是什么?

作用:网站根目录的位置,相对于安装目录

6、使用Nginx的状态统计功能除了启用内建模块外,还需要在配置文件中添加哪些内容?

需要添加:
location /status   {             #访问位置为/status
     stub status on;             #打开状态统计功能
     access log off;              #关闭此位置的日志记录
}

7、简述Nginx配置虚拟主机的方法与流程

(1)准备网站目录及测试文件  (2)调整nginx.conf配置文件  (3)测试虚拟Web主机

8、mysqladmin -u root password 'pwd123'命令的作用什么?

给mysqi数据库的root用户设置密码

9、Nginx对PHP的支持可以通过哪两种方式实现?

1)把访问 PHP 页面的 Web 请求转交给其他服务器(LAMP)处理

2)使用PHP的FPM模块来调用本机的 PHP 环境

10、写出php-fpm程序的启动方法与默认监听端口

启动方法:/usr/local/sbin/php-fpm

默认监听端口:9000

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘浪初心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值