LNMP基础架构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP基础架构相关的知识,希望对你有一定的参考价值。

[toc]

LNMP基础架构

12.1 LNMP架构介绍

架构原理:

LAMP=linux+apache+mysql+php

LNMP=linux+nginx+mysql+php

1.初识LNMP

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

技术分享图片

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

2.MYSQL 安装过程

2.1 获取安装包,并解压

[[email protected] ~] ps aux |grep mysql //安装前先确定mysql是否开启
root       1082  0.0  0.0 115392  1672 ?        S    21:09   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/xavi.pid
mysql      1375  3.8 24.2 1302876 452908 ?      Sl   21:09   0:03 /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/xavi.err --pid-file=/data/mysql/xavi.pid
root       2645  0.0  0.0 112680   976 pts/0    R+   21:10   0:00 grep --color=auto mysql

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# rm -rf /usr/local/mysql //把之前的删除
[[email protected] src]# rm -rf /etc/init.d/mysqld
[[email protected] src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
[[email protected] src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[[email protected] src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql

[[email protected] src]# ls /usr/local/mysql/
bin      data  include  man         README   share      support-files
COPYING  docs  lib      mysql-test  scripts  sql-bench

2.2 进入mysql目录下,删除之前/data/mysql/下的文件

[[email protected] ~]# cd /usr/local/mysql

[[email protected] mysql]# ls /data/
mariadb  mysql  wwwroot
[[email protected] mysql]# id mysql
uid=1004(mysql) gid=1005(mysql) 组=1005(mysql)
[[email protected] mysql]# rm -rf /data/mysql/*

2.3 配置文件./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

出现两个OK

技术分享图片

2.4 拷贝编辑cp support-files/my-default.cnf /etc/my.cnf

技术分享图片

2.5 拷贝启动脚本并编辑 cp support-files/mysql.server /etc/init.d/mysqld

  • 编辑 vim /etc/init.d/mysqld

vim编辑下面两行basedir和datadir配置
basedir=/usr/local/mysql
datadir=/data/mysql

技术分享图片

2.6 启动mysqld,/etc/init.d/mysqld start

报错:

[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to ‘/data/mysql/xavi.err‘.
.. ERROR! The server quit without updating PID file (/data/mysql/xavi.pid).

sql启动报错参考:http://blog.51cto.com/732233048/1636409

2.7 kill -9 PID 杀死占用mysql的进程,然后开启

查找出之前报错的原因,mysql进程没有关闭

[[email protected] mysql]# ps -ef|grep mysqld
root       1082      1  0 21:09 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/xavi.pid
mysql      1375   1082  0 21:09 ?        00:00:13 /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/xavi.err --pid-file=/data/mysql/xavi.pid
root       4433   2598  0 22:23 pts/0    00:00:00 grep --color=auto mysqld
[[email protected] mysql]# kill -9 1082
[[email protected] mysql]# ps -ef|grep mysqld
mysql      1375      1  0 21:09 ?        00:00:13 /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/xavi.err --pid-file=/data/mysql/xavi.pid
root       4444   2598  0 22:23 pts/0    00:00:00 grep --color=auto mysqld
[[email protected] mysql]# kill -9 1375
[[email protected] mysql]# ps -ef|grep mysqld
root       4447   2598  0 22:24 pts/0    00:00:00 grep --color=auto mysqld
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 

将mysql添加到开机启动项

[[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig mysqld on
[[email protected] mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS! 
[[email protected] mysql]# service mysqld start
Starting MySQL. SUCCESS! 

12.3 PHP安装

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

1. 配置并编译:

进入src目录下,查看之前已经下载了哪些安装包

[[email protected] src]cd /usr/local/src/
[[email protected] src]# ls
apr-1.6.3
apr-1.6.3.tar.gz
apr-util-1.6.1
apr-util-1.6.1.tar.gz
httpd-2.4.29
httpd-2.4.29.tar.gz
mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
mysql-5.5.55
mysql-5.5.55.tar.gz
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
php-5.6.30
php-5.6.30.tar.bz2
php-5.6.30.tar.gz
php-5.6.30.tar.gz.1
php-7.1.6
php-7.1.6.tar.bz2
phpredis-develop
phpredis-develop.zip

打开php-5.6.50源码包(之前LAMP架构中已经下载),make clean

[[email protected] src]# cd php-5.6.30/
[[email protected] php-5.6.30]# ls
[[email protected] php-5.6.30]# 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命名为php-fpm,添加配置文件

[[email protected] php-5.6.30]# ls /usr/local/
apache2.4  apr-util  etc    include  lib64    mariadb  php   sbin   src
apr        bin       games  lib      libexec  mysql    php7  share

[[email protected] php-5.6.30]#useradd -s /sbin/nologin php-fpm

[[email protected] 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

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

报错处理,安装devel安装包,安装完重新加载配置文件,然后make&make install

<curl-dir>

configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/
  • 安装 :yum install libcurl-devel

3 PHP信息:

[[email protected] php-5.6.30]# ls /usr/local/php-fpm 
bin  etc  include  lib  php  sbin  var
[[email protected] php-5.6.30]# ls /usr/local/php
bin  etc  include  lib  php

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

sbin:实际上就是启动php-fpm服务的目录

var:实际上就是存放PHP日志的,当然这个我们可以指定的

查看PHP信息:/usr/local/php-fpm/sbin/php-fpm -i

查看PHP模块:/usr/local/php-fpm/sbin/php-fpm -m

测试PHP配置语法:/usr/local/php-fpm/sbin/php-fpm -t

4. 配置PHP:

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

//牢记此命令,后期会经常使用到

4.1 编辑: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

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

4.2拷贝启动脚本,先到源码包目录,脚本为sapi/fpm/init.d.php-fpm

[[email protected] php-5.6.30]# cd /usr/local/src/php-5.6.30/
[[email protected] php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

赋予权限,修改开机启动项

[[email protected] php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[[email protected] php-5.6.30]# chkconfig --add php-fpm
[[email protected] php-5.6.30]# chkconfig php-fpm on
[[email protected] php-5.6.30]# service php-fpm start
Starting php-fpm  done

技术分享图片

以上是关于LNMP基础架构的主要内容,如果未能解决你的问题,请参考以下文章

LNMP架构搭建(基础入门级)

LNMP基础架构

LNMP架构

LNMP架构概述

LNMP架构概述

Linux三阶段之九:期中架构LNMP章节