12.1 LNMP架构介绍
- linux+Nginx+MySQL+PHP LNMP
- nginx 和Apache 在架构里面担任的角色是类似的
- 在LAMP里,PHP和Apache是一个整体,php的解析是交给Apache来执行的,只不过需要加载一个php 的模块libphp.so
- 在LNMP里,PHP会启动一个服务(php-fpm),Nginx会把用户请求的php交给php-fpm服务去进行处理(用户数据与mysql的交互就是由php-fpm来做的,处理好结果以后再告诉Nginx,然后通过Nginx告诉用户的浏览器),用户的静态请求将由Nginx直接进行处理(Nginx在处理静态数据的性能上要比Apache快),
- Nginx对静态为主的网站,处理用户并发会很大,速度也会快很多 可以参照下图进行理解:
12.2 MySQL安装
删除之前安装的mysql
删除整个安装过的目录
/usr/local/mysql
删除启动脚本
/etc/init.d/mysqld
开始安装
进入下载包的目录
/usr/local/src/
解压 mysql的包
[root@aminglinux-02 src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
将解压好的包移动到/usr/local/ 并改名为mysql
[root@aminglinux-02 src]# lsmysql-5.6.35-linux-glibc2.5-x86_64 mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz php-7.1.6.tar.bz2[root@aminglinux-02 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql[root@aminglinux-02 src]# cd !$cd /usr/local/mysql[root@aminglinux-02 mysql]# lsbin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
创建mysql 用户
[root@aminglinux-02 mysql]# useradd mysql[root@aminglinux-02 mysql]# id mysqluid=1000(mysql) gid=1000(mysql) 组=1000(mysql)
创建数据库目录
[root@aminglinux-02 mysql]# mkdir /data/[root@aminglinux-02 mysql]# ls -a /. .. bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
初始化配置
[root@aminglinux-02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqlFATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
因为是干净的虚拟机,所以提示缺少 Perl 模块
[root@aminglinux-02 mysql]# yum list |grep -i Perl|grep -i Dumperperl-Data-Dumper.x86_64 2.145-3.el7 baseperl-Data-Dumper-Concise.noarch 2.020-6.el7 epelperl-Data-Dumper-Names.noarch 0.03-17.el7 epelperl-XML-Dumper.noarch 0.81-17.el7 base[root@aminglinux-02 mysql]# yum install -y perl-Data-Dumper.x86_64
安装好模块以后,重新初始化
[root@aminglinux-02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqlInstalling MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory[root@aminglinux-02 mysql]# yum install -y libaio
安装好模块以后,重新初始化
[root@aminglinux-02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqlInstalling MySQL system tables...2017-08-09 20:16:29 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2017-08-09 20:16:29 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.2017-08-09 20:16:29 0 [Note] ./bin/mysqld (mysqld 5.6.35) starting as process 13773 ...2017-08-09 20:16:29 13773 [Note] InnoDB: Using atomics to ref count buffer pool pages2017-08-09 20:16:29 13773 [Note] InnoDB: The InnoDB memory heap is disabled2017-08-09 20:16:29 13773 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins2017-08-09 20:16:29 13773 [Note] InnoDB: Memory barrier is not used2017-08-09 20:16:29 13773 [Note] InnoDB: Compressed tables use zlib 1.2.3......WARNING: Found existing config file ./my.cnf on the system.Because this file might be in use, it was not replaced,but was used in bootstrap (unless you used --defaults-file)and when you later start the server.The new default config file was created as ./my-new.cnf,please compare it with your file and take the changes you need.WARNING: Default config file /etc/my.cnf exists on the systemThis file will be read by default by the MySQL serverIf you do not want to use this, either remove it, or use the--defaults-file argument to mysqld_safe when starting the server[root@aminglinux-02 mysql]# echo $?0
初始化成功,这次没有问题了
拷贝配置文件[root@aminglinux-02 mysql]# ls support-files/binary-configure magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server[root@aminglinux-02 mysql]# cp support-files/my-default.cnf /etc/my.cnfcp:是否覆盖"/etc/my.cnf"? y
拷贝启动脚本
root@aminglinux-02 mysql]# ls support-files/binary-configure magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server[root@aminglinux-02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
配置启动脚本
basedir=/usr/local/mysqldatadir=/data/mysql
定义配置文件
在[mysqld] 下 增加datadir=/data/mysqlsocket=/tmp/mysql.sock
可以试试是否能开机
第一次开启的时候出错[root@aminglinux-02 mysql]# /etc/init.d/mysqld startStarting MySQL.Logging to '/data/msyql/aminglinux-02.err'. ERROR! The server quit without updating PID file (/data/msyql/aminglinux-02.pid).
查了几次都没能查出解决办法,之后突然想到会不会是没有生成才aminglinux-02.err 这个文件才会出错,然后尝试进行目录修改,最后才发现“/data/msyql/ ”,这个目录错了,马上去/etc/my.cnf里进行修改,然后问题解决
[root@aminglinux-02 mysql]# /etc/init.d/mysqld startStarting MySQL.Logging to '/data/mysql/aminglinux-02.err'. SUCCESS![root@aminglinux-02 mysql]# ps aux|grep mysqldroot 2536 0.0 0.0 113256 1588 pts/0 S 20:43 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-02.pidmysql 2671 0.1 24.4 973048 458392 pts/0 Sl 20:43 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/aminglinux-02.err --pid-file=/data/mysql/aminglinux-02.pid --socket=/tmp/mysql.sockroot 2702 0.0 0.0 112664 976 pts/0 S+ 20:48 0:00 grep --color=auto mysqld
将mysql 加入到服务列表
[root@aminglinux-02 mysql]# chkconfig --add mysqld
配置开机启动
[root@aminglinux-02 mysql]# chkconfig mysqld on
加入服务列表以后就用使用服务命令进行开关机
[root@aminglinux-02 mysql]# service mysqld stopShutting down MySQL.. SUCCESS![root@aminglinux-02 mysql]# service mysqld startStarting MySQL. SUCCESS!
12.3/12.4 PHP安装
-
因为php在LNMP和LAMP有差别,在LAMP的时候,他是Apache的模块,需要用apxs2 指定Apache的路径,使用apxs的工具进行自动配置模块的加载
-
在LNMP里,需要指定mysql的路径,但是不需要制动Nginx的路径,因为是LNMP中,PHP是作为一个独立的服务在运行的,和Nginx没有直接的关系,所以他也不需要依赖Nginx的什么东西
因为之前编译过PHP,所以只需要进入PHP的目录下执行make clean 把之前编译过的那些文件,全部删掉
执行make clean之后,这里的php相当于是刚解压 准备工作 添加用户useradd -s /sbin/nologin php-fpm
初始化配置
./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 的路径--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: error: mcrypt.h not found. Please reinstall libmcrypt.
缺少包“libmcrypt.”
干脆两个一起安装上,因为都常用的库“libmcrypt.x86_64” 、“libmcrypt-devel.x86_64”[root@aminglinux-02 php-5.6.30]# yum install -y libmcrypt.x86_64[root@aminglinux-02 php-5.6.30]# yum install -y libmcrypt-devel.x86_64
重新初始化
提示:configure: error: Please reinstall the libcurl distribution - easy.h should be in/include/curl/
缺少“libcurl-devel ”
[root@aminglinux-02 php-5.6.30]# yum install -y libcurl-devel
重新初始化
config.status: creating sapi/fpm/status.htmlconfig.status: creating sapi/cgi/php-cgi.1config.status: creating ext/phar/phar.1config.status: creating ext/phar/phar.phar.1config.status: creating main/php_config.hconfig.status: executing default commands[root@aminglinux-02 php-5.6.30]# echo $?0
开始编译
make && make install
安装完毕
查看目录
[root@aminglinux-02 php-fpm]# lsbin etc include lib php sbin var
对比
[root@aminglinux-02 php-fpm]# ls /usr/local/php/bin etc include lib php
少了2个目录
该目录下的php-fpm文件,就是用来启动php-fpm服务的[root@aminglinux-02 ~]# ls /usr/local/php-fpm/sbin/php-fpm
存放日志的目录/usr/local/php-fpm/var/log
[root@aminglinux-02 php-fpm]# pwd/usr/local/php-fpm[root@aminglinux-02 php-fpm]# ls var/log/
存放PID的目录
/usr/local/php-fpm/var/run
日志路径和PID路径都可以指定
/usr/local/php-fpm/sbin/php-fpm 和 /usr/local/php-fpm/bin/php 的用法是基本一致的
测试配置文件语法问题
[root@aminglinux-02 php-fpm]# /usr/local/php-fpm/sbin/php-fpm -t[10-Aug-2017 00:12:46] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)[10-Aug-2017 00:12:46] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'[10-Aug-2017 00:12:46] ERROR: FPM initialization failed
提示配置文件不存在
拷贝配置文件[root@aminglinux-02 php-5.6.30]# pwd/usr/local/src/php-5.6.30[root@aminglinux-02 php-5.6.30]# lsacinclude.m4 footer Makefile.gcov README.GIT-RULES run-tests.phpaclocal.m4 generated_lists Makefile.global README.input_filter sapibuild genfiles Makefile.objects README.MAILINGLIST_RULES scriptsbuildconf header makerpm README.md server-tests-config.phpbuildconf.bat include missing README.namespaces server-tests.phpCODING_STANDARDS INSTALL mkinstalldirs README.NEW-OUTPUT-API snapshotconfig.guess install-sh modules README.PARAMETER_PARSING_API stamp-h.inconfig.log libs netware README.REDIST.BINS stub.cconfig.nice libtool NEWS README.RELEASE_PROCESS testsconfig.status LICENSE pear README.SELF-CONTAINED-EXTENSIONS travisconfig.sub ltmain.sh php5.spec README.STREAMS TSRMconfigure main php5.spec.in README.SUBMITTING_PATCH UPGRADINGconfigure.in makedist php.gif README.TESTING UPGRADING.INTERNALSCREDITS Makefile php.ini-development README.TESTING2 vcscleanext Makefile.frag php.ini-production README.UNIX-BUILD-SYSTEM win32EXTENSIONS Makefile.fragments README.EXT_SKEL README.WIN32-BUILD-SYSTEM Zendcp 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@aminglinux-02 etc]# lspear.conf php-fpm.conf.default php.ini[root@aminglinux-02 etc]#vim 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 //监听的地址# linsten = 127.0.0.1:9000listen.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 //进程相关信息
linsten 可以监听sock、tcp 可以写成 linsten = 127.0.0.1:9000(默认端口就是9000,可自定义),一遍监听的到是“127.0.0.1 ”IP,因为php-fpm这个服务是针对内部使用的、在本机上用的,也就是说Nginx和php通常是在一台机器上,两者之间通信直接使用内部网络就可以了;在这块选择的linsten配置不同,到之后的配置Nginx配置,也就是告诉Nginx到哪里找php的配置语句也是不同;
拷贝启动脚本
[root@aminglinux-02 php-5.6.30]# pwd/usr/local/src/php-5.6.30[root@aminglinux-02 php-5.6.30]# lsacinclude.m4 footer Makefile.gcov README.GIT-RULES run-tests.phpaclocal.m4 generated_lists Makefile.global README.input_filter sapibuild genfiles Makefile.objects README.MAILINGLIST_RULES scriptsbuildconf header makerpm README.md server-tests-config.phpbuildconf.bat include missing README.namespaces server-tests.phpCODING_STANDARDS INSTALL mkinstalldirs README.NEW-OUTPUT-API snapshotconfig.guess install-sh modules README.PARAMETER_PARSING_API stamp-h.inconfig.log libs netware README.REDIST.BINS stub.cconfig.nice libtool NEWS README.RELEASE_PROCESS testsconfig.status LICENSE pear README.SELF-CONTAINED-EXTENSIONS travisconfig.sub ltmain.sh php5.spec README.STREAMS TSRMconfigure main php5.spec.in README.SUBMITTING_PATCH UPGRADINGconfigure.in makedist php.gif README.TESTING UPGRADING.INTERNALSCREDITS Makefile php.ini-development README.TESTING2 vcscleanext Makefile.frag php.ini-production README.UNIX-BUILD-SYSTEM win32EXTENSIONS Makefile.fragments README.EXT_SKEL README.WIN32-BUILD-SYSTEM Zend[root@aminglinux-02 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
更改启动脚本权限
[root@aminglinux-02 php-5.6.30]# chmod 755 /etc/init.d/php-fpm
将php-fpm加入到服务列表里
[root@aminglinux-02 php-5.6.30]# chkconfig --add php-fpm
配置开机启动php-fpm
[root@aminglinux-02 php-5.6.30]# chkconfig php-fpm on
开启服务器
[root@aminglinux-02 php-5.6.30]# service php-fpm startStarting php-fpm done
查看进程,服务是否开启
[root@aminglinux-02 php-5.6.30]# ps aux |grep php-fpmroot 54745 0.0 0.2 123048 4936 ? Ss 00:41 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)php-fpm 54746 0.0 0.2 123048 4700 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54747 0.0 0.2 123048 4700 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54748 0.0 0.2 123048 4700 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54749 0.0 0.2 123048 4700 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54750 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54751 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54752 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54753 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54754 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54755 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54756 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54757 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54758 0.0 0.2 123048 4704 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54759 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54760 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54761 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54762 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54763 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54764 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwphp-fpm 54765 0.0 0.2 123048 4708 ? S 00:41 0:00 php-fpm: pool wwwroot 54767 0.0 0.0 112664 976 pts/1 R+ 00:41 0:00 grep --color=auto php-fpm
www是php-fpm.conf 里面的模块名字
pool 表示池子 php-fpm,php-fpm.conf 里面定义好的开启服务的用户 可以看看sock的权限是否和配置里的一样[root@aminglinux-02 php-5.6.30]# ls -lhtr /tmp/php-fcgi.socksrw-rw-rw- 1 root root 0 8月 10 00:41 /tmp/php-fcgi.sock
权限为666和配置里的一样
12.5 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虽然功能不多,但可以去扩展一些第三方的模块进来