12.1 LNMP架构介绍
LNMP架构介绍目录概要
- 和LAMP不同的是,提供web服务的是Nginx
- 并且php是作为一个独立服务存在的,这个服务叫做php-fpm
- Nginx直接处理静态请求,动态请求会转发给php-fpm
LNMP架构
- LAMP和LNMP两个架构类似
- 在LAMP架构中,PHP和Apache是一个整体,php解析是交给Apache来执行的,只不过需要加一个php的模块libphp.so
- 在LNMP架构中,提供web服务的是Nginx,PHP会启动一个php-fpm服务,Nginx会把用户请求的php交给php-fpm服务去进行处理(用户数据与mysql的交互就是由php-fpm来做的,处理好的结果在高速Nginx,然后通过Nginx告诉用户的浏览器),用户的静态请求将由Nginx直接进行处理(Nginx在处理静态数据的性能上要比Apache快),
- Nginx对静态为主的网站,处理用户并发会很大,速度也会快很多
12.2 MySQL安装
MySQL安装目录概要
- cd /usr/local/src
- wget
- tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
- mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
- cd /usr/local/mysql
- useradd mysql
- mkdir /data/
- ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
- cp support-files/my-default.cnf /etc/my.cnf
- cp support-files/mysql.server /etc/init.d/mysqld
- vi /etc/init.d/mysqld
- 定义basedir和datadir
- /etc/init.d/mysqld start
搭建LNMP环境
LAMP架构下,然后搭建LNMP架构1.首先查看mysql是否启动ps aux |grep mysql2.删除目录rm -rf /usr/local/mysql/3.删除启动的脚本rm -rf /etc/init.d/mysqld4.然后其他步骤相同
- 这里新建一个虚拟环境(这里不是在lamp架构上搭建的,而是新建的一个环境)
- 然后进入到/usr/local/src目录下
[root@hanfeng ~]# cd /usr/local/src[root@hanfeng src]#
- 下载
[root@hanfeng src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
- 然后解压安装包
[root@hanfeng src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
- 移动目录并修改名称——>这里移动目录到/usr/local/下时,一定不能有mysql目录,(若已经有mysql目录时,再去移动则会放到mysql目录下面去,而不是去移动并修改名称了)
[root@hanfeng src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql[root@hanfeng src]#
- 移动完后,检查目录
[root@hanfeng src]# ls /usr/local/mysqlbin data include man README share support-filesCOPYING docs lib mysql-test scripts sql-bench[root@hanfeng src]#
- 进入到/usr/local/mysql目录下
[root@hanfeng src]# cd /usr/local/mysql[root@hanfeng mysql]#
- 新建mysql用户和/data/目录——>这里若是在lamp之前的基础上做的话,需要rm -rf /data/mysql/*清空内容,(直接删除mysql目录也可以。它会自动创建)
[root@hanfeng mysql]# useradd mysql[root@hanfeng mysql]# mkdir /data/[root@hanfeng mysql]#
- 初始化./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
- 初始化的过程目的,就是把mysql启动所需要的目录生成
[root@hanfeng mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
- 错误
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
- 解决方法
yum install -y perl-Data-Dumper
- 错误
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
- 解决方法
yum install -y libaio
- 然后可以echo $?检查,或者看初始化的过程中是否有两个OK
[root@hanfeng mysql]# echo $?0[root@hanfeng mysql]#
- 拷贝配置文件cp support-files/my-default.cnf /etc/my.cnf
- 未拷贝之前的环境配置cat /etc/my.cnf
[root@hanfeng mysql]# cat /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d
拷贝后
[root@hanfeng mysql]# cp support-files/my-default.cnf /etc/my.cnf
- 拷贝文件cp support-files/mysql.server /etc/init.d/mysqld
[root@hanfeng mysql]# cp support-files/mysql.server /etc/init.d/mysqld
- 编辑文件 /etc/init.d/mysqld,并配置
[root@hanfeng mysql]# vim /etc/init.d/mysqld在文件中配置basedir=/usr/local/mysqldatadir=/data/mysql然后保存退出
- 启动mysql服务
[root@hanfeng mysql]# /etc/init.d/mysqld startStarting MySQL.Logging to '/data/mysql/hanfeng.err'.. SUCCESS! [root@hanfeng mysql]#
- 查看服务是否启动成功
[root@hanfeng mysql]# ps aux |grep mysqlroot 2295 0.0 0.1 113252 1608 pts/0 S 22:41 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/hanfeng.pidmysql 2403 1.9 44.6 973512 451180 pts/0 Sl 22:41 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=/data/mysql/hanfeng.err --pid-file=/data/mysql/hanfeng.pidroot 2429 0.0 0.0 112656 992 pts/0 R+ 22:41 0:00 grep --color=auto mysql[root@hanfeng mysql]#
- 将mysql服务加入到服务列表中去,并设置开机启动
[root@hanfeng mysql]# chkconfig --add mysqld[root@hanfeng mysql]# chkconfig mysqld on[root@hanfeng mysql]#
- 下次就可以直接使用service关闭或启动服务
[root@hanfeng mysql]# service mysqld stopShutting down MySQL.. SUCCESS! [root@hanfeng mysql]# service mysqld startStarting MySQL. SUCCESS!
12.3/12.4 PHP安装
PHP安装目录概要
- 和LAMP安装PHP方法有差别,需要开启php-fpm服务
- cd /usr/local/src/
- wget
- tar zxf php-5.6.30.tar.gz
- useradd -s /sbin/nologin php-fpm
- cd php-5.6.30
- ./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
- make && make install
- cp php.ini-production /usr/local/php-fpm/etc/php.ini
- vi /usr/local/php/etc/php-fpm.conf //
- cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
- chmod 755 /etc/init.d/php-fpm
- chkconfig --add php-fpm
- chkconfig php-fpm on
- service php-fpm start
- ps aux |grep php-fpm
PHP安装
- LAMP架构和LNMP架构中安装PHP方法是不同的
- 在LAMP中,php是作为Apache的一个模块存在的,需要用apxs2 指定Apache的路径,使用apxs的工具进行自动配置模块的加载
- 在LNMP里,需要指定mysql的路径,但是不需要制动Nginx的路径,因为是LNMP中,PHP是作为一个独立的服务在运行的,和Nginx没有直接的关系,所以它也不需要依赖Nginx
- 若是在之前LAMP架构中,编译过PHP,所以只需要进入PHP的目录下执行make clean 把之前编译过的那些文件,全部删掉
- 首先进入到/usr/local/src/
[root@hanfeng ~]# cd /usr/local/src/[root@hanfeng src]#
- 下载
[root@hanfeng src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
- 解压php安装包
[root@hanfeng src]# tar zxf php-5.6.30.tar.gz[root@hanfeng src]# lsmysql-5.6.35-linux-glibc2.5-x86_64.tar.gz php-5.6.30 php-5.6.30.tar.gz[root@hanfeng src]#
- 增加用户并指定shell
- useradd -s 指定用户的shell
[root@hanfeng src]# useradd -s /sbin/nologin php-fpm[root@hanfeng src]#
- 切换到/usr/local/src/php-5.6.30
[root@hanfeng ~]# cd /usr/local/src/php-5.6.30[root@hanfeng php-5.6.30]#
- 初始化./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
- ./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 //指定 mysql 的路径
- --with-mysqli=/usr/local/mysql/bin/mysql_config //指定 mysqli 的路径
- --with-pdo-mysql=/usr/local/mysql //指定 pdo-mysql 的路径
[root@hanfeng php-5.6.30]# ./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
- 错误
checking for cc... nochecking for gcc... noconfigure: error: in `/usr/local/src/php-5.6.30':configure: error: no acceptable C compiler found in $PATHSee `config.log' for more details
- 解决方法
yum install -y gcc
- 错误
configure: error: xml2-config not found. Please check your libxml2 installation.
- 解决方法
[root@hanfeng php-5.6.30]# yum install -y libxml2-devel
- 错误
configure: error: Cannot find OpenSSL's
- 解决方法
yum install -y openssl-devel
- 错误
configure: error: Please reinstall the libcurl distribution - easy.h should be in/include/curl/
- 解决方法
yum install -y libcurl-devel或yum install -y curl curl-devel
- 错误
configure: error: jpeglib.h not found.
- 解决方法
yum install -y libjpeg-devel
- 错误
configure: error: png.h not found.
- 解决方法
yum install -y libpngyum install -y libpng-devel
- 错误
configure: error: freetype-config not found.
- 解决方法
yum install -y freetype-devel
- 错误
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
- 解决方法
yum install -y epel-releaseyum install -y libmcrypt-devel
- 可以echo $?检查是否初始化成功
[root@hanfeng php-5.6.30]# echo $?0[root@hanfeng php-5.6.30]#
- 然后make && make install
[root@hanfeng php-5.6.30]# make && make instal
- 用echo $?检查是否成功
[root@hanfeng php-5.6.30]# echo $?0[root@hanfeng php-5.6.30]#
- 查看/usr/local/php-fpm/目录——>于/usr/local/php/目录项比较多了 sbin 和 var 目录
[root@hanfeng php-5.6.30]# ls /usr/local/php-fpm/bin etc include lib php sbin var[root@hanfeng php-5.6.30]# ls /usr/local/php-fpm/sbin/ //在sbin目录下有一个php-fpm文件,就是用来启动php-fpm的php-fpm[root@hanfeng php-5.6.30]# ls /usr/local/php-fpm/var/ //在var目录下有 log 和 run 文件,log文件存放日志的,run存放pid的,日志和pid路径都是可以指定的log run[root@hanfeng php-5.6.30]#
- /usr/local/php-fpm/sbin/php-fpm 和 /usr/local/php-fpm/bin/php 的用法是基本一致的
/usr/local/php-fpm/sbin/php-fpm -m 查看模块/usr/local/php-fpm/sbin/php-fpm -i/usr/local/php-fpm/sbin/php-fpm -t 测试配置文件文件语法
- 用/usr/local/php-fpm/sbin/php-fpm -t 测试配置文件文件语法是否错误
[root@hanfeng php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t[03-Jan-2018 00:10:44] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)[03-Jan-2018 00:10:44] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'[03-Jan-2018 00:10:44] ERROR: FPM initialization failed[root@hanfeng php-5.6.30]#
- 这里会提示配置文件并不存在,若想要启动php-fpm服务,首先需要去配置配置文件
- 拷贝cp php.ini-production /usr/local/php-fpm/etc/php.ini文件
- 查看目录发现/usr/local/src/php-5.6.30 有2个配置文件
- php.ini-development 适合开发环境使用
- php.ini-production 适合生产环境使用
- 两者区别在于,错误日志不同
[root@hanfeng php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini[root@hanfeng php-5.6.30]#
- 进入到/usr/local/php-fpm/etc/目录下
[root@hanfeng php-5.6.30]# cd /usr/local/php-fpm/etc/[root@hanfeng etc]# lspear.conf php-fpm.conf.default php.ini[root@hanfeng etc]#
- 编辑配置文件vim php-fpm.conf,或者直接把php-fpm.conf.default文件改名字为php-fpm.cnf——>这里直接新建写入的
[root@hanfeng etc]# vim php-fpm.conf去https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/php-fpm.conf拷贝样例拷贝到文件中[global] //全局配置pid = /usr/local/php-fpm/var/run/php-fpm.pid //定义PIDerror_log = /usr/local/php-fpm/var/log/php-fpm.log //错误日志[www] //模块名字,启动服务的时候就能看到listen = /tmp/php-fcgi.sock //监听的地址listen.mode = 666 //当监听的是sock的时候,这个语句才会生效,这个语句用来定义sock文件权限的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然后保存退出
linsten 可以监听sock、tcp 可以写成 linsten = 127.0.0.1:9000(默认端口就是9000,可自定义),一般监听的到是“127.0.0.1 ”IP,因为php-fpm这个服务是针对内部使用的、在本机上用的,也就是说Nginx和php通常是在一台机器上,两者之间通信直接使用内部网络就可以了;在这块选择的linsten配置不同,到之后的配置Nginx配置,也就是告诉Nginx到哪里找php的配置语句也是不同
- 进入到/usr/local/src/php-5.6.30/下,拷贝启动的脚本
[root@hanfeng etc]# cd /usr/local/src/php-5.6.30/[root@hanfeng php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm[root@hanfeng php-5.6.30]#
- 加入到服务列表中,先更改权限,然后在chkconfig,最后在开机启动
[root@hanfeng php-5.6.30]# chmod 755 /etc/init.d/php-fpm[root@hanfeng php-5.6.30]# chkconfig --add php-fpm[root@hanfeng php-5.6.30]# chkconfig php-fpm on[root@hanfeng php-5.6.30]# service php-fpm startStarting php-fpm done[root@hanfeng php-5.6.30]# chkconfig --list注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。 欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。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:关[root@hf-01 php-5.6.30]#
- 查看php-fpm服务是否启动
[root@hanfeng php-5.6.30]# ps aux |grep php-fpmroot 2386 0.0 0.4 124184 4936 ? Ss 17:04 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)php-fpm 2387 0.0 0.4 124184 4700 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2388 0.0 0.4 124184 4700 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2389 0.0 0.4 124184 4700 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2390 0.0 0.4 124184 4700 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2391 0.0 0.4 124184 4704 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2392 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2393 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2394 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2395 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2396 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2397 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2398 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2399 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2400 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2401 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2402 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2403 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2404 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2405 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwphp-fpm 2406 0.0 0.4 124184 4708 ? S 17:04 0:00 php-fpm: pool wwwroot 2408 0.0 0.0 112676 988 pts/0 R+ 17:04 0:00 grep --color=auto php-fpm[root@hanfeng php-5.6.30]#
12.5 Nginx介绍
Nginx介绍
- Nginx官网 nginx.org,最新版1.13,最新稳定版1.12 (stable版本) //这个软件是有俄国人开发的,因为对静态文件的处理性能上比Apache强很多,所以在慢慢的取代Apache
- Nginx应用场景:web服务、反向代理、负载均衡
- Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
- Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,
- Nginx虽然功能不多,但可以去扩展一些第三方的模块进来