12.1 LNMP架构介绍

12.2 MySQL安装

12.3/12.4 PHP-fpm安装

12.5 Nginx介绍

扩展

Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html

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

mod_php 和 mod_fastcgi以及php-fpm的比较 http://dwz.cn/1lwMSd

概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM http://www.nowamagic.net/librarys/veda/detail/1319/ https://www.awaimai.com/371.html

12.1 LNMP架构介绍:

~1.和LAMP不同的是,提供web服务的是Nginx

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

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

~4.与Apache相比,Nginx在静态文件的交互上会比Apache快很多。用户并发支持会很大,上好几万,Apache做不到

46.LNMP架构介绍 mysql及PHP-fpm安装 Nginx介绍-LMLPHP

12.2 MySQL安装:

安装方法与LAMP里的mysql方法一样

~1.cd /usr/local/src

~2.wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.43-linux-glibc2.5-x86_64.tar.gz

~3.tar zxvf mysql-5.6.43-linux-glibc2.5-x86_64.tar.gz

~4.mv mysql-5.6.43-linux-glibc2.5-x86_64 /usr/local/mysql

~5.cd /usr/local/mysql

~6.useradd mysql

~7.mkdir /data/

.~8./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

~9.cp support-files/my-default.cnf /etc/my.cnf 拷贝配置文件

~10.vi /etc/my.cnf

~11.定义datadir和socket

~12.cp support-files/mysql.server /etc/init.d/mysqld 拷贝启动脚本

~13.vi /etc/init.d/mysqld

~14.定义basedir和datadir

~15/etc/init.d/mysqld start

我们这里先把之前安装的mysql删除,再重新安装

[root@localhost ~]# cd /usr/local/src 先cd进src

[root@localhost src]# ps aux | grep mysql 检查mysql是否运行

root 1089 0.0 0.0 115432 1704 ? S 19:39 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/axinlinux-01.pid

mysql 1296 3.8 24.2 1302732 453888 ? Sl 19:39 0:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=axinlinux-01.err --pid-file=/data/mysql/axinlinux-01.pid

root 1599 0.0 0.0 112720 980 pts/0 S+ 19:41 0:00 grep --color=auto mysql

[root@localhost src]# service mysql stop 关闭mysql

Redirecting to /bin/systemctl stop mysql.service

[root@localhost src]# ps aux | grep mysql 再次检查

root 1633 0.0 0.0 112720 980 pts/0 R+ 19:42 0:00 grep --color=auto mysql

[root@localhost src]# rm -rf /usr/local/mysql 删除mysql目录

[root@localhost src]# rm -rf /etc/init.d/mysqld 删除启动脚本

[root@localhost src]# vim /etc/my.cnf 系统会自带一个my.cnf,所以暂且保留

[root@localhost src]# tar -xvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz 解压

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

apache2/ apr-util/ etc/ include/ lib64/ mariadb/ php7/ share/ 看一下有没有应存在的mysql目录

apr/ bin/ games/ lib/ libexec/ php/ sbin/ src/

[root@localhost src]# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql 挪到local下,并改名字叫mysql

[root@localhost src]# ls /usr/local/mysql/ 看一下是不是这几个文件

bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

[root@localhost mysql]# useradd mysql

[root@localhost mysql]# rm -rf /data/mysql/* 因为之前创建过,过意直接删除mysql目录下的左右

[root@localhost mysql]# id mysql 查看一下mysql这个用户

uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 初始化,过程其实就是他启动所需要的目录生成

[root@localhost mysql]# echo $? 要出现两个OK,或者echo $?看一下

0

[root@localhost mysql]# vim /etc/my.cnf 因为之前拷贝过配置文件,所以直接vim定义了datadir和socket

basedir =/usr/local/mysql
datadir = /data/mysql

# port = .....

# server_id = .....

socket = /tmp/mysql.sock

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld 拷贝启动脚本

[root@localhost mysql]# ls support-files
binary-configure  magic  my-default.cnf  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost mysql]# ls /etc/init.d/
functions  mysqld  netconsole  network  README

[root@localhost mysql]# vim /etc/init.d/mysqld 定义启动脚本的basedir和datadir

basedir=/usr/local/mysql

datadir=/data/mysql

[root@localhost mysql]# chkconfig --add mysqld 加入到服务里表里面去

[root@localhost mysql]# chkconfig mysqld on 设置开机启动

[root@localhost mysql]# service mysqld stop 这时候可以用命令关闭

Shutting down MySQL.. SUCCESS!

[root@localhost mysql]# service mysqld start 也可以用命令开启

Starting MySQL. SUCCESS!

[root@localhost mysql]# ps aux |grep mysqld 检查是否用命令开启

root       1881  0.0  0.1  11816  1632 pts/0    S    10:14   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.pid
mysql      2032  8.6 45.3 1302744 452028 pts/0  Sl   10:14   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=localhost.err --pid-file=/data/mysql/localhost.pid --socket=/tmp/mysql.sock
root       2056  0.0  0.0 112724   988 pts/0    R+   10:14   0:00 grep --color=auto mysqld

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

12.3/12.4 PHP安装:

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

~1.cd /usr/local/src/

~2.wget http://cn2.php.net/distributions/php-5.6.32.tar.gz

~3.tar zxf php-5.6.32.tar.gz

~4.useradd -s /sbin/nologin php-fpm

~5.cd php-5.6.32

~6../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

~7.make && make install

~8.cp php.ini-production /usr/local/php-fpm/etc/php.ini

php.ini-development给开发用的或测试用的。php.ini-production适合生产环境用

~9.vi /usr/local/php-fpm/etc/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

~10.cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

~11.chmod 755 /etc/init.d/php-fpm

~12.chkconfig --add php-fpm

~13.chkconfig php-fpm on

~14.service php-fpm start

~15.ps aux |grep php-fpm

(/usr/local/php-fpm/sbin/php-fpm -m/-i与/usr/local/php-fpm/bin/php -m/-i是一样的,都可以查看。但是多了一个 -t

/usr/local/php-fpm/sbin/php-fpm -t是测试他自己配置文件语法的,makeinstall之后用-t就会提示没有配置文件也就是~10.)

实例:

因为我们之前安装过,所以直接make clean 清空之前的配置,回到刚解压的状态

[root@localhost php]# cd /usr/local/src/php-5.6.32/ 进入解压后的php5

[root@localhost 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/*

[root@localhost 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

在这里我们把php的名字设置成了--prefix=php-fpm(和Nginx对应的)

还有他的配置文件也改成和Nginx对应的--with-config-file-path=/usr/local/php-fpm/etc

注意要加上--enable-fpm,启动php-fpm,不加就没法启动这个服务。(在Nginx中php作为服务存在的)

--with-fpm-user=php-fpm指定用户

--with-fpm-group=php-fpm指定组

--with-mysql=/usr/local/mysql 指定路径

报错configure: error: Please reinstall the libcurl distribution

yum install curl curl-devel
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

[root@localhost php-5.6.32]# echo $?

0

[root@localhost php-5.6.32]# make

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
directorytreeiterator.inc
invertedregexiterator.inc
directorygraphiterator.inc
pharcommand.inc
phar.inc

[root@localhost php-5.6.32]# echo $?

0

[root@localhost php-5.6.32]# make install

Installing shared extensions:     /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary:        /usr/local/php-fpm/bin/
Installing PHP CLI man page:      /usr/local/php-fpm/php/man/man1/
Installing PHP FPM binary:        /usr/local/php-fpm/sbin/
Installing PHP FPM config:        /usr/local/php-fpm/etc/
Installing PHP FPM man page:      /usr/local/php-fpm/php/man/man8/
Installing PHP FPM status page:   /usr/local/php-fpm/php/php/fpm/
Installing PHP CGI binary:        /usr/local/php-fpm/bin/
Installing PHP CGI man page:      /usr/local/php-fpm/php/man/man1/
Installing build environment:     /usr/local/php-fpm/lib/php/build/
Installing header files:           /usr/local/php-fpm/include/php/
Installing helper programs:       /usr/local/php-fpm/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php-fpm/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php-fpm/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
/usr/local/src/php-5.6.32/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
ln -s -f phar.phar /usr/local/php-fpm/bin/phar
Installing PDO headers:           /usr/local/php-fpm/include/php/ext/pdo/

[root@localhost php-5.6.32]# echo $?

0

[root@localhost php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t 自带的-t测试,提示没有配置文件

[23-Jul-2019 10:43:22] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[23-Jul-2019 10:43:22] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[23-Jul-2019 10:43:22] ERROR: FPM initialization failed

[root@localhost php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini 拷贝配置文件,这里选php.ini-production生产环境用的

[root@localhost php-5.6.32]# vi /usr/local/php-fpm/etc/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 监听的地址。php-fpm可以监听socket可以监听tcp/ip,这里可以写socket也可以写下面的

#listen = 127.0.0.1:9000 一般都是监听这个IP。因为php-fpm是在内部使用的,在本机上用的,也就是Nginx和php通常是在一台机器上的。他们两者通信就可以用内部的网络。当然也可以监听其他的IP。prot默认是9000,也可以改成其他的

以上是两种不同的监听方法。这选择不同的话,后面php-fpm与nginx结合的时候,配置语句也是不同的

listen.mode = 666 socket的时候这个语句才会生效。就是上面选socket的时候定义它的权限

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]
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

[root@localhost php-5.6.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 拷贝启动脚本

[root@localhost php-5.6.32]# chmod 755 /etc/init.d/php-fpm

[root@localhost php-5.6.32]# chkconfig --add php-fpm

[[email protected]]# chkconfig php-fpm on

[root@localhost php-5.6.32]# service php-fpm start 执行到这一步有报错。是之前忘记创建用户

Starting php-fpm [23-Jul-2019 11:02:39] ERROR: [pool www] cannot get uid for user 'php-fpm'
[23-Jul-2019 11:02:39] ERROR: FPM initialization failed
 failed

[root@localhost php-5.6.32]# useradd php-fpm 创建php-fpm用户。不要忘记

[root@localhost php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t 检测成功

[23-Jul-2019 11:04:08] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@localhost php-5.6.32]# service php-fpm start 开启php-fpm

Starting php-fpm done

[root@localhost php-5.6.32]# ps aux |grep php-fpm

root     119564  0.0  0.4 125928  4952 ?        Ss   11:04   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm  119565  0.0  0.4 125928  4708 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119566  0.0  0.4 125928  4708 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119567  0.0  0.4 125928  4708 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119568  0.0  0.4 125928  4708 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119569  0.0  0.4 125928  4712 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119570  0.0  0.4 125928  4712 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119571  0.0  0.4 125928  4712 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119572  0.0  0.4 125928  4712 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119573  0.0  0.4 125928  4712 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119574  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119575  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119576  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119577  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119578  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119579  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119580  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119581  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119582  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119583  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
php-fpm  119584  0.0  0.4 125928  4716 ?        S    11:04   0:00 php-fpm: pool www
root     119586  0.0  0.0 112724   992 pts/0    R+   11:05   0:00 grep --color=auto php-fpm

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

12.5 Nginx介绍:

~1.Nginx官网 nginx.org,最新版1.13,最新稳定版(stable)1.12 (俄国人开发的)。他的静态文件处理能力比Apache强很多。尽量去选择最新的稳定版本(stable)。有些会另外安一个nginx来处理静态文件,比如,tomcat+nginx的Java应用

~2.小巧,功能也非常的强大。本身可能支持的功能不是很多,但是可扩展第三方的模块进来。比如支持https功能就要安装模块。模块跟Apache都是很类似的,因为框架结构都是一样的

~3. Nginx应用场景:web服务、反向代理(代理一台就叫反向代理)、负载均衡(代理两台就叫负载均衡)

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

像淘宝,页面图片很多,访问进去的话,一个页面请求就会很多。所以淘宝就想到吧这些小的js、css图片合并起来,一个请求就把他们处理完了。最终就搞了一个Tenging

~5. Nginx核心+lua(一种开发语言)相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928(作为扩展)

07-23 13:45