12.1 LNMP架构介绍

  • 和LAMP不同的是,提供web服务的是Nginx

  • 并且php是作为一个独立服务存在的,这个服务叫做php-fpm

  • Nginx直接处理静态请求,动态请求会转发给php-fpm

【0514】LNMP 架构(上)_第1张图片

用户并发支持很大,可以上好几万,Apache做不到


12.2 MySQL安装

1、进到 /usr/local/src 下,下载二进制免编译包,并解压

[root@arslinux-01 ~]# cd /usr/local/src/
[root@arslinux-01 src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@arslinux-01 src]# tar xvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz

2、移动解压出的目录到 /usr/local/ 下,并改名为 mysql,进入到目录下

[root@arslinux-01 src]# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql
[root@arslinux-01 src]# cd /usr/local/mysql/

3、创建用户 mysql,创建数据存放目录 /data/

[root@arslinux-01 mysql]# useradd mysql
[root@arslinux-01 mysql]# mkdir /data/

4、安装 perl-Data-Dumper.x86_64 包后,进行初始化

[root@arslinux-01 mysql]# yum install -y perl-Data-Dumper.x86_64
[root@arslinux-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/

5、拷贝配置模板到 /etc/ 下并改名为 my.cnf

[root@arslinux-01 mysql]# cp support-files/my-default.cnf /etc/my.cnf

6、编辑配置文件 my.cnf

[root@arslinux-01 mysql]# vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /data/mysql
socket = /tmp/mysql.sock

7、拷贝启动程序到 /etc/init.d/ 下,并改名为 mysqld

[root@arslinux-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

8、安装完成,启动 mysqld

[root@arslinux-01 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/arslinux-01.err'.
SUCCESS!
[root@arslinux-01 mysql]# ps aux|grep mysqld
root      10829  0.2  0.1 113308  1632 pts/1    S    21:55   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/arslinux-01.pid
mysql     10979  7.4 45.2 1302740 450800 pts/1  Sl   21:55   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/data/mysql/arslinux-01.pid --socket=/tmp/mysql.sock
root      11003  0.0  0.0 112724   988 pts/1    R+   21:55   0:00 grep --color=auto mysqld
[root@arslinux-01 mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7446/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7575/master
tcp6       0      0 :::3306                 :::*                    LISTEN      10979/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      7446/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7575/master

9、将 mysql 加入 chkconfig 服务,并加入开机启动

[root@arslinux-01 mysql]# chkconfig --add mysqld
[root@arslinux-01 mysql]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。

123            0:关1:关2:开3:开4:开5:开6:关
mysqld         0:关1:关2:开3:开4:开5:开6:关
netconsole     0:关1:关2:关3:关4:关5:关6:关
network        0:关1:关2:开3:开4:开5:开6:关
[root@arslinux-01 mysql]# chkconfig mysqld on
[root@arslinux-01 mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!


12.3/12.4 PHP安装

  • LNMP下的PHP安装和LAMP下PHP安装有差别,需要开启php-fpm服务

  • 如果之前编译过php,那么进到源码包中,用make clean清除之前make过的文件

1、移动到 /usr/local/src 下,下载php源码包,并解压

[root@arslinux-01 mysql]# cd /usr/local/src/
[root@arslinux-01 src]# wget http://cn2.php.net/distributions/php-5.6.39.tar.bz2
[root@arslinux-01 src]# tar xvf php-5.6.39.tar.bz2

2、创建用户 php-fpm,并指定家目录为 /sbin/nologin

[root@arslinux-01 src]# useradd -s /sbin/nologin php-fpm

3、进到 php 包目录下,编译

[root@arslinux-01 src]# cd php-5.6.39/
[root@arslinux-01 php-5.6.39]# ./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

4、make && make install

[root@arslinux-01 php-5.6.39]# make && make install

5、/usr/local/php-fpm/sbin/php-fpm -m 查看模块

6、/usr/local/php-fpm/sbin/php-fpm -i 查看信息

7、/usr/local/php-fpm/sbin/php-fpm -t 语法检测

8、拷贝配置文件模板到 /usr/local/php-fpm/etc/ 并改名为 php.ini

[root@arslinux-01 php-5.6.39]# cp php.ini-production /usr/local/php-fpm/etc/php.ini

9、编辑配置文件 php-fpm.conf

[root@arslinux-01 php-5.6.39]# cd /usr/local/php-fpm/etc
[root@arslinux-01 etc]# vim 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.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

10、拷贝启动脚本,更改权限

[root@arslinux-01 etc]# cp /usr/local/src/php-5.6.39/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@arslinux-01 etc]# chmod 755 /etc/init.d/php-fpm

11、加入到服务列表

[root@arslinux-01 etc]# chkconfig --add php-fpm
[root@arslinux-01 etc]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。

123            0:关1:关2:开3:开4:开5:开6:关
mysqld         0:关1:关2:开3:开4:开5:开6:关
netconsole     0:关1:关2:关3:关4:关5:关6:关
network        0:关1:关2:开3:开4:开5:开6:关
php-fpm        0:关1:关2:开3:开4:开5:开6:关

12、启动 php-fpm 服务

[root@arslinux-01 etc]# service php-fpm start
Starting php-fpm  done
[root@arslinux-01 etc]# ps aux |grep php-fpm
root      33424  0.5  0.4 125924  4964 ?        Ss   23:34   0:00 php-fpm: master process (/usr/local/php-fpm/etcphp-fpm.conf)
php-fpm   33425  0.0  0.4 125924  4724 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33426  0.0  0.4 125924  4724 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33427  0.0  0.4 125924  4724 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33428  0.0  0.4 125924  4724 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33429  0.0  0.4 125924  4728 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33430  0.0  0.4 125924  4728 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33431  0.0  0.4 125924  4728 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33432  0.0  0.4 125924  4728 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33433  0.0  0.4 125924  4728 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33434  0.0  0.4 125924  4728 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33435  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33436  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33437  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33438  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33439  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33440  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33441  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33442  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33443  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
php-fpm   33444  0.0  0.4 125924  4732 ?        S    23:34   0:00 php-fpm: pool www
root      33446  0.0  0.0 112724   988 pts/1    R+   23:34   0:00 grep --color=auto php-fpm


配置文件地址:http://note.youdao.com/noteshare?id=8751926db1d0a1c1e01d12a37c3a2bbf&sub=05D9B235441F48D496A460CEB23B6E67

(listen也可以为 127.0.0.1:9000,监听内部网络,listen.mode = 666 定义 /tmp/php-fcgi.sock 的权限为 666)


  • 问题汇总:

1、xml2-config not found.

需要安装 libxml2 和 libxml2-devel

2、少cURL

安装 libcurl-devel

3、少OpenSSL's

安装 openssl、openssl-devel

4、jpeglib.h not found.

安装 libjpeg-devel

5、png.h not found.

安装 libpng-devel

6、freetype-config not found.

安装 freetype-devel

7、mcrypt.h not found.

安装 libmcrypt-devel

资料:CentOS下编译php时的一些典型错误及解决办法:http://yanue.net/post-127.html


12.5 Nginx介绍

  • Nginx官网 nginx.org

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

  • Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并

  • Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty

Nginx 静态文件处理能力比 Apache 强很多

参考http://jinnianshilongnian.iteye.com/blog/2280928


12.6 Nginx安装

1、下载 nginx 源码包,并解压缩

[root@arslinux-01 ~]# cd /usr/local/src/
[root@arslinux-01 src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@arslinux-01 src]# tar xvf nginx-1.14.2.tar.gz
[root@arslinux-01 src]# cd nginx-1.14.2/

2、编译、安装

[root@arslinux-01 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
[root@arslinux-01 nginx-1.14.2]# make && make install

3、编辑启动脚本,并更改 755 权限

[root@arslinux-01 nginx-1.14.2]# 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@arslinux-01 nginx-1.14.2]# chmod 755 /etc/init.d/nginx

4、Nginx 支持配置文件语法检测

[root@arslinux-01 nginx-1.14.2]# /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、加入服务列表,设置开机启动

[root@arslinux-01 nginx-1.14.2]# chkconfig --add nginx
[root@arslinux-01 nginx-1.14.2]# chkconfig nginx on

6、编辑配置文件

[root@arslinux-01 nginx-1.14.2]# cd /usr/local/nginx/conf/
[root@arslinux-01 conf]# mv nginx.conf nginx.conf.bak
[root@arslinux-01 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;
        }
    }
}

7、启动服务

[root@arslinux-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@arslinux-01 conf]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10654/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7449/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7776/master
tcp6       0      0 :::3306                 :::*                    LISTEN      7690/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      7449/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7776/master
[root@arslinux-01 conf]# ps aux|grep nginx
root      10654  0.0  0.0  20548   624 ?        Ss   21:18   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    10655  0.0  0.3  22992  3208 ?        S    21:18   0:00 nginx: worker process
nobody    10656  0.0  0.3  22992  3208 ?        S    21:18   0:00 nginx: worker process
root      10666  0.0  0.0 112724   988 pts/0    S+   21:24   0:00 grep --color=auto nginx

上方有两个子进程 worker process,由配置文件中的worker_processes定义的


user                               定义上传等操作完成的用户

worker_processes              定义子进程的数量

error_log                          错误日志

pid                                  pid号

worker_rlimit_nofile          指定nginx最多打开多少文件

use epoll                          使用epoll模式

worker_connections         进程最大的连接数

fastcgi_pass                      如果监听端口是9000,可以写为127.0.0.1:9000这是两种不同方式


[root@arslinux-01 conf]# curl localhost



Welcome to nginx!



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 nginx.org.
Commercial support is available at Thank you for using nginx.

上方欢迎语由/usr/local/nginx/html/index.html,而为什么能访问到index.html由nginx.conf定义


  • 解析 php

[root@arslinux-01 conf]# vim /usr/local/nginx/html/1.php
 
  


扩展

Nginx为什么比Apache Httpd高效:

原理篇  http://www.toxingwang.com/linux-unix/linux-basic/1712.html

https://www.zhihu.com/question/64727674

apache和nginx工作原理比较  http://www.server110.com/nginx/201402/6543.html

概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM  https://www.awaimai.com/371.html