LNMP架构 (Ⅰ)——nginx、php-fpm

LNMP架构 (Ⅰ)

一、 LNMP架构介绍

上一课学习了LAMP架构,现在流行LNMP架构;他们的区别就是提供web服务为apache和nginx,目前nginx发展迅速,取代apache指日可待。

  • LAMP=linux+apache+mysql+php
  • LNMP=linux+nginx+mysql+php

当我们讲LAMP的时候PHP是作为一个模块在Apache中,但是在LNMP中,PHP是一个服务,当用户请求的时候nginx会把它交给php 然后对mysql进行交互!像这种静态的,例如图片或者html,nginx会直接处理,从而加快访问速度! 谈到速度,其实如果一个普通的站点你是看不到什么效果的,但是如果要是访问一个纯静态站点,nginx就能体现出它的优势!nginx的另外一个优势得益于它的高并发支持!

和LAMP不同的是,提供web服务的是Nginx 并且php是作为一个独立服务存在的,这个服务叫做php-fpm Nginx直接处理静态请求,动态请求会转发给php-fpm

二、MySQL安装

无论LAMP和LNMP架构中,M代表MySQL,都是独立存在于架构中;因此LNMP的MySQL安装步骤和LAMP一样的;

详细查看:Linux 第17课 三、MySQL安装

三、PHP安装

这里的PHP安装和LAMP安装PHP方法有差别,需要开启php-fpm服务

3.1 php编译、安装

首先把之前编译过的php配置删除;删除后就相当于php包刚解压的时候;

[root@ying01 ~]# cd /usr/local/src/
[root@ying01 src]# ls
apr-1.6.3                                     nginx-1.4.7
apr-1.6.3.tar.gz                              nginx-1.4.7.tar.gz
apr-util-1.6.1                                php-5.6.32
apr-util-1.6.1.tar.bz2                        php-5.6.32.tar.bz2
httpd-2.4.33                                  php-7.1.6
httpd-2.4.33.tar.gz                           php-7.1.6.tar.bz2
httpd-2.4.33.tar.gz.1                         phpredis-develop
mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz  phpredis-develop.zip
mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
[root@ying01 src]# cd php-5.6.32/
[root@ying01 php-5.6.32]#  make clean
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f 
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp5.la sapi/cli/php sapi/cgi/php-cgi    libphp5.la modules/* libs/*

此时的php-5.6.32目录,如同刚解压的时候时候;此时通过**./configure**进行定制相关功能,使其生成makefile;


[root@ying01 php-5.6.32]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl

相关名称释义:

  • --prefix=指定安装位置
  • --with-config-file-path=指定配置文件目录
  • --enable-fpm=启动fpm服务
  • --with-fpm-user=指定用户
  • --with-fpm-group=指定用户组
  • --with-mysql=指定mysql的路径

自动检查过程中,出现以下报错:

configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

需安装devel安装包,安装完重新加载配置文件;

[root@ying01 php-5.6.32]# yum install libcurl-devel

开始编译

[root@ying01 php-5.6.32]# make

编译完成后,开始安装

[root@ying01 php-5.6.32]# make install

3.2 php相关信息

此时会在/usr/local目录下,生成了php-fpm新目录;

[root@ying01 php-5.6.32]# ls /usr/local/php-fpm/              //比之前的php多了sbin和var目录
bin  etc  include  lib  php  sbin  var
[root@ying01 php-5.6.32]# ls /usr/local/php
bin  etc  include  lib  php
[root@ying01 php-5.6.32]# ls /usr/local/php-fpm/sbin/        //sbin下有php-fpm
php-fpm
[root@ying01 php-5.6.32]# ls /usr/local/php-fpm/var/         //var下有log日志目录和PID目录
log  run

安装下来之后我们发现其实比之前安装PHP要多了两个目录 sbin 和 var

  • sbin:实际上就是启动php-fpm服务的目录
  • var:实际上就是存放PHP日志的,当然这个我们可以指定的
    • log: php日志目录;
    • run:进程PID目录

php-fpm 选项参数用法:-i -m -t

[root@ying01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -i  //查看PHP信息

[root@ying01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -m  //查看PHP模块

[root@ying01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t  //测试PHP配置语法      

3.3 php配置

php解压目录中的php.ini-production文件复制到php-fpm/etc/目录中,并且重命名为php.ini

[root@ying01 php-5.6.32]# ls /usr/local/php-fpm/etc/
pear.conf  php-fpm.conf.default
[root@ying01 php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@ying01 php-5.6.32]# cd /usr/local/php-fpm/etc/
[root@ying01 etc]# ls
pear.conf  php-fpm.conf.default  php.ini                   //复制成功

在php-fpm/etc/下有默认的php-fpm.conf.default配置文件,现在我们需要重新复制一份,方便试验操作;

[root@ying01 etc]# vi php-fpm.conf   //新创建

....... 以下为配置文件内容

[global]                                                  
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
#listen =127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

......

相关名词释义

  • [global]=定义全局参数
  • [www]=自定义模块
  • listen = 监听的地址
  • 或者可以使用这样的方式↓
  • listen = 127.0.0.1:9000 (port默认为9000,也可以更改)
  • listen.mode = 666 用来定义listen = /tmp/php-fcgi.sock的权限,只有这个sock打开的情况下才生效!

把解压目录下的脚本sapi/fpm/init.d.php-fpm 复制到/etc/init.d下

[root@ying01 etc]# cd /usr/local/src/php-5.6.32/
[root@ying01 php-5.6.32]# ls sapi/
aolserver      apache2handler  cgi         embed  litespeed  phpdbg  roxen   tux
apache         apache_hooks    cli         fpm    milter     phttpd  tests   webjames
apache2filter  caudium         continuity  isapi  nsapi      pi3web  thttpd
[root@ying01 php-5.6.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

然后给予755权限,增加开机服务等操作

[root@ying01 php-5.6.32]# chmod 755 /etc/init.d/php-fpm   //给予755权限
[root@ying01 php-5.6.32]# chkconfig --add php-fpm         //增加服务
[root@ying01 php-5.6.32]# chkconfig php-fpm on            
[root@ying01 php-5.6.32]# service php-fpm start           //开启php-fpm服务
Starting php-fpm  done

查看php-fpm启动信息

[root@ying01 php-5.6.32]# ps aux |grep php-fpm
root     60635  0.0  0.2 123652  4940 ?        Ss   16:10   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm  60636  0.0  0.2 123652  4704 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60637  0.0  0.2 123652  4704 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60638  0.0  0.2 123652  4704 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60639  0.0  0.2 123652  4704 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60640  0.0  0.2 123652  4708 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60641  0.0  0.2 123652  4708 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60642  0.0  0.2 123652  4708 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60643  0.0  0.2 123652  4708 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60644  0.0  0.2 123652  4708 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60645  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60646  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60647  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60648  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60649  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60650  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60651  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60652  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60653  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60654  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
php-fpm  60655  0.0  0.2 123652  4712 ?        S    16:10   0:00 php-fpm: pool www
root     60663  0.0  0.0 112724   984 pts/0    S+   16:11   0:00 grep --color=auto php-fpm

[root@ying01 php-5.6.32]# ls -l /tmp/php-fcgi.sock               //sock的权限符合配置赋予的权限666
srw-rw-rw- 1 root root 0 7月   3 16:10 /tmp/php-fcgi.sock

四、Nginx介绍

因为nginx处理静态文件的能力要比apache好很多,所以很多企业在建站的时候一般都是用java写的,然后会选择tomcat,但是tomcat处理静态文件的能力不是太好就会叠加选择nginx。

nginx特点:

  • 体积小
  • 处理能力强
  • 并发高
  • 可扩展性好

Nginx应用场景:web服务、反向代理、负载均衡

Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并 Nginx核心+lua(开发语言)相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928

五、Nginx安装

5.1 Nginx编译、安装

下载配置安装Nginx,并解压

[root@ying01 php-5.6.32]# cd /usr/local/src/
[root@ying01 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
--2018-07-03 16:51:10--  http://nginx.org/download/nginx-1.4.7.tar.gz
正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ...
正在连接 nginx.org (nginx.org)|95.211.80.227|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:769153 (751K) [application/octet-stream]
正在保存至: “nginx-1.4.7.tar.gz”

100%[===============================================>] 769,153     34.6KB/s 用时 11s    

2018-07-01 16:51:21 (68.8 KB/s) - 已保存 “nginx-1.4.7.tar.gz” [769153/769153])

[root@ying01 src]# ls
apr-1.6.3                                     mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
apr-1.6.3.tar.gz                              nginx-1.4.7.tar.gz
apr-util-1.6.1                                php-5.6.32
apr-util-1.6.1.tar.bz2                        php-5.6.32.tar.bz2
httpd-2.4.33                                  php-7.1.6
httpd-2.4.33.tar.gz                           php-7.1.6.tar.bz2
httpd-2.4.33.tar.gz.1                         phpredis-develop
mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz  phpredis-develop.zip
[root@ying01 src]# tar zxf nginx-1.4.7.tar.gz                                //解压

进入解压目录,进行./configure定制服务,使其生成makefile文件;

[root@ying01 src]# cd nginx-1.4.7/
[root@ying01 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx

编译生成的makefile文件

[root@ying01 nginx-1.4.7]# make


开始安装

[root@ying01 nginx-1.4.7]# make install

5.2 Nginx相关目录信息

打开各层目录,查看相关子目录

[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/
conf  html  logs  sbin 
[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/conf/
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/html/
50x.html  index.html
[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/logs/
[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/sbin/
nginx
[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/sbin/nginx 
/usr/local/nginx/sbin/nginx

/usr/local/nginx目录下的子目录:

子目录释义
confnginx配置文件
html主页样例文件
logs站点日志
sbin核心进程文件

nginx -t 选项: 测试配置语法错误

[root@ying01 nginx-1.4.7]#  /usr/local/nginx/sbin/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

5.3 Nginx配置
  • 设置nginx脚本

在init.d目录下,新建nginx脚本文件

[root@ying01 nginx-1.4.7]# vim /etc/init.d/nginx

以下为脚本内容.....

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

然后增加权限,以及启动服务;

[root@ying01 nginx-1.4.7]# chmod 755 /etc/init.d/nginx
[root@ying01 nginx-1.4.7]# chkconfig --add nginx
[root@ying01 nginx-1.4.7]# chkconfig nginx on

  • 编辑配置文件

进入nginx/conf/目录下,把默认的配置文件作为备份;

[root@ying01 nginx-1.4.7]# cd /usr/local/nginx/conf/
[root@ying01 conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@ying01 conf]# mv nginx.conf nginx.conf.1                           //把原配置文件作为备份
[root@ying01 conf]# ls
fastcgi.conf            koi-utf             nginx.conf.1         uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default

新建nginx.conf配置文件,并按下面写入内容

[root@ying01 conf]# vim nginx.conf

以下为配置内容.....


user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

相关名词,释义

  • user nobody nobody; 运行服务的用户是谁
  • worker_processes 2;定义子进程的数量
  • worker_rlimit_nofile
  • 51200;最多可以打开多少个文件
  • worker_connections 6000;允许最大的连接数
  • server; 下面对应的就是虚拟主机配置
  • server_name localhost;定义网站的域名
  • root /usr/local/nginx/html;定义网站的根目录
  • location ~ .php$;配置解析PHP
  • fastcgi_pass unix:/tmp/php-fcgi.sock;监听端口或者监听socket,通过此命令去执行
  • fastcgi_pass 127.0.0.1:9000;(或者携程这种方式,服务器IP地址+端口)

可参考文章:http://www.okay686.cn/510.html

  • 启动nginx服务
[root@ying01 conf]# /usr/local/nginx/sbin/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@ying01 conf]# /etc/init.d/nginx start                                    //开启服务
Starting nginx (via systemctl):                            [  确定  ]
[root@ying01 conf]# ps aux |grep nginx                                        //查看相关服务
root     63447  0.0  0.0  24844   784 ?        Ss   17:20   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   63448  0.0  0.1  27148  3356 ?        S    17:20   0:00 nginx: worker process
nobody   63449  0.0  0.1  27148  3356 ?        S    17:20   0:00 nginx: worker process
root     63451  0.0  0.0 112720   980 pts/0    S+   17:20   0:00 grep --color=auto nginx
[root@ying01 conf]# 

  • 配置完nginx,进行测试
[root@ying01 conf]# curl localhost 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>                         
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

  • 解析PHP
[root@ying01 conf]# vim /usr/local/nginx/html/1.php     //新建1.php页面,写入以下内容

以下为1.php内容...

<?php
echo "hello world";

测试解析PHP

[root@ying01 conf]# curl localhost/1.php        //进行测试,成功解析
hello world[root@ying01 conf]# 

转载于:https://my.oschina.net/u/3851633/blog/1839744

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值