LNMP环境应用实战

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

企业级LNMP环境应用实战:

面试必备:

(1)

LNMP的工作流程:

当LNMP工作的时候,首先是用户通过浏览器输入域名请求Nginx Web服务,如果是请求的是静态的资源,则由Nginx解析返回给用户。

如果是动态的资源,那么久通过Fast CGI接口发送给PHP引擎服务(Fast CGI进程php-fpm)进行解析。

如果这个动态的请求要读取数据库,那么PHP就会继续向后请求MySQL数据库,读取需要的数据。

最终通过Nginx服务把获取的数据返回给用户,这就是LNMP的基本流程。


(2)企业选用MySQL作为数据库的优点:

1.性能卓越,服务稳定,很少出现异常宕机。

2.开放源代码并且没有版权的限制,自主传播,使用成本低。

3.历史悠久,社区及用户非常活跃,遇到问题很快可以获取帮助。

4.软件体积小安装简单,并且易于维护,安装及维护的成本低。

5.支持多种操作系统,提供API接口。

6.品牌效应,使得企业无需考虑就直接使用。


(3)LNMP环境搭建问题:

当安装LNMP一体化环境的时候MySQL数据库要装在Nginx所在的服务器上,

如果MySQL和Nginx不在同一台机器上,那么Nginx服务器上的MySQL数据库软件只要解压移动安装目录中就行。

不需要对MySQL进行初始化配置。

在PHP5.3以上的版本中,Nginx服务器上安装了MySQL软件,只需要在编译PHP的时候指定相关参数即可。

编译参数:--with-mysql=mysqlnd

表示在编译的时候会调用内置的MySQL的库。


(4)什么是FCGI:

FastCGI是一个可伸缩的、高速的在HTTP服务器的动态脚本语言间通信的接口(在Linux下,FastCGI就是socket,这个socket可以是文件socket或IPsocket)。

FastCGI采用C/S架构,它可以将HTTP服务器和脚本服务器分开,同时还可以在脚本解析服务器上启动一个或多个服务器来解析守护进程。

当HTTP服务器遇到动态程序的时候,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一的处理静态的请求。

这会很高的提高整个应用系统的性能。


(5)FastCGI的重要特点:

1.HTTP服务器和动态脚本语言间通信的接口或工具。

2.可以把动态语言解析或HTTP服务器分离开。

3.Nginx、Apache、Lighttpd,以及多数动态语言都支持FastCGI。

4.PHP动态语言方式采用C/S结构,分为客户端(HTTP服务器)和服务器端(动态语言解析服务器)。

5.PHP动态语言服务器端可以启动多个FastCGI的守护进程。

6.HTTP服务器通过FastCGI客户端和动态语言FastCGI服务器端通信。


(6)Nginx FCGI运行原理:

Nginx不支持对外部动态程序的直接调用或者解析。所有的外部程序(包括PHP)必须通过FastCGI接口来调用。

FastCGI接口在Linux下是一个socket,为了调用CGI程序,还需要一个FastCGI的wrapper(可以理解为用户启动另一个程序的程序),这个wrapper绑定在某个固定的socket上。

当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接受到请求,然后派生出一个新的线程,这个线程调用解释器或外部的程序处理脚本来读取返回的数据。

然后wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx,最后Nginx将返回的数据发送给客户端。

FastCGI的主要优点就是把动态的语言和HTTP服务器分离开来,使Nginx专门处理静态的请求,动态的请求直接使用PHP/PHP-FPM服务器专门处理。


rpm安装MySQL数据库:

#创建MySQL账号

groupadd mysql #创建mysql组

useradd -s /sbin/nologin -g mysql -M mysql  #创建mysql用户,指定不可以登录,指定属组,指定不创建home目录

id mysql   #查看mysql用户信息

uid=500(mysql) gid=500(mysql) groups=500(mysql)

ll   #查看安装包

total 207M

-rw-r--r--. 1 7155 wheel  18M Nov 18  2013 MySQL-client-5.6.15-1.el6.x86_64.rpm

-rw-r--r--. 1 7155 wheel 3.2M Nov 18  2013 MySQL-devel-5.6.15-1.el6.x86_64.rpm

-rw-r--r--. 1 7155 wheel  81M Nov 18  2013 MySQL-embedded-5.6.15-1.el6.x86_64.rpm

-rw-r--r--. 1 7155 wheel  52M Nov 18  2013 MySQL-server-5.6.15-1.el6.x86_64.rpm

-rw-r--r--. 1 7155 wheel 1.9M Nov 18  2013 MySQL-shared-5.6.15-1.el6.x86_64.rpm

-rw-r--r--. 1 7155 wheel 3.8M Nov 18  2013 MySQL-shared-compat-5.6.15-1.el6.x86_64.rpm

-rw-r--r--. 1 7155 wheel  48M Nov 18  2013 MySQL-test-5.6.15-1.el6.x86_64.rpm

rpm -Uvh ./MySQL-*   #采用升级安装MySQL


/etc/init.d/mysql start  #启动服务


采用二进制文件安装MySQL安装时,需要初始化数据库:

mkdir -p /app/mysql/data/   #创建数据目录

chown -R mysql.mysql /app/mysql/   #授权目录

/app/mysql/scripts/mysql_install_db --basedir=/app/mysql/ --datadir=/app/mysql/data/ --user=mysql  #初始化数据库


采用二进制安装配置MySQL命令使用全局路径:

echo 'export PATH=/app/mysql/bin:$PATH' >> /etc/profile

source /etc/profile   #刷新配置文件


采用二进制安装MySQL数据库的时候需要配置启动:

cp /tools/MySQL/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

MySQL二进制默认安装路径是/usr/local/mysql,启动脚本里是/usr/local/mysql的地方需要替换为MySQL的安装路径。


初始化数据库故障集锦:

(1)显示ERROR:1004 Can't create file '/tmp/#sql300e_1_0.frm'(errno:13)

原因是/tmp目录的权限有问题。

chomd -R 1777 /tmp/


(2)WARNING:The host 'mysql' could not be looked up with resoleip

需要修改主机名



netstat -tunlap | grep mysql  #查看MySQL数据库是否启动

tcp        0      0 :::3306                     :::*                        LISTEN      2992/mysqld


chkconfig mysql on   #设置开机自启动,也可以将启动命令放到/etc/rc.local文件里面

chkconfig mysql --list

mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off


初次使用MySQL数据库:

cd ~  #回到用户home目录

cat ./.mysql_secret  #查看MySQL的安全文件

mysql -uroot -pew4pubHF   #登录MySQL

mysql> set password for root@"localhost"=password("123456");

mysql> quit


PHP(FastCGI)服务安装:

yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel openssl-devel -y

yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel -y

这些lib库不是必须安装的,但是目前企业里边一般都需要安装,否则可能会导致PHP无法显示验证码等问题。

编译安装libiconv-devel:

tar -xf ./libiconv-1.15.tar.gz

cd ./libiconv-1.15

./configure --prefix=/usr/local/libicon

make && make install

libtool --finish /usr/local/libiconv/lib


编译安装libmcrypt:

libmcrypt是一个动态加载的模块化的limbcrypt,libmcrypt对于在程序运行时添加/移除算法是有用的。

libmcrypt-nm目前不再被官方支持,编译PHP的时候不是必须的包。

在CentOS中默认没有libmcrypt-devel,因此需要事先配置epel第三方yum源:

下载阿里云的epel的.repo源文件。

直接使用yum install libmcrypt -y安装。

tar -xf ./libmcrypt-2.5.8.tar.gz

cd ./libmcrypt-2.5.8

./configure --prefix=/usr/local/libmcrypt-devel

make && make install


编译安装mhash库:

mhash是基于离散数学原理的不可逆的PHP加密方式扩展库,其在默认情况下不会开启。

mhash可以创建校验值、消息摘要、消息认证码、以及无需原文的关键信息保存(如:密码)。

它为PHP提供了多种散列算法,如MD5,SHA1,GOST等。

可以通过MHASH_hashname()查看其支持的算法有哪些。

tar -zxvf ./mhash-0.9.9.9.tar.gz

cd ./mhash-0.9.9.9

./configure --prefix=/usr/local/mhash

make && make install

可以直接使用yum install mhash -y安装。

注意:

该扩展不可以提供最新的散列算法。

该算法原则上不可逆。


编译安装mcrypt加密扩展库:

mcrypt扩展库是实现加密解密的功能,既可以将明文加密,也可以将密文还原。

mcrypt是PHP里面的重要的加密文件支持扩展库,该库在默认情况下不开启。

mcrypt库支持20多种加密算法和8种加密模式。具体可以通过mcrypt_list_algorithms()和mcrypt_list_modes()来显示。

使用命令yum install mhash -y安装。

export LD_LIBRARY_PATH=/usr/local/libmcrypt-devel/lib:/usr/local/mhash/lib

export LDFLAGS="-L/usr/local/mhash/lib/ -I/usr/local/mhash/include/"

export CFLAGS="-I/usr/local/mhash/include/"

./configure --prefix=/usr/local/mcrypt/ --with-libmcrypt-prefix=/usr/local/libmcrypt



编译安装PHP:

tar -zxvf ./php-5.4.24.tar.gz 

cd ./php-5.4.24

./configure \

--prefix=/app/php-5.4.24 \

--with-mysql=/app/mysql/mysql/ \

--with-iconv-dir=/usr/local/libiconv/ \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr/ \

--enable-xml \

--disable-rpath \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-inline-optimization \

--with-curl \

--with-curlwrappers \

--enable-mbstring \

--enable-fpm \

--enable-mbregex \

--with-mcrypt=/usr/local/mcrypt/ \

--enable-gd-jis-conv       \

--enable-gd-native-ttf \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-zip \

--enable-soap \

--enable-short-tags \

--enable-static \

--with-xsl \

--with-fpm-user=nginx \

--with-fpm-group=nginx \

--enable-ftp

PHPFastCGI模式说明,如果是PHP5.3及以上的版本,所使用的编译参数是:--enable-fpm,

如果是PHP5.2版本是--enable-fastcgi --enable-fpm --enable-force-cgi。


+--------------------------------------------------------------------+

| License:                                                           |

| This software is subject to the PHP License, available in this     |

| distribution in the file LICENSE.  By continuing this installation |

| process, you are bound by the terms of this license agreement.     |

| If you do not agree with the terms of this license, you must abort |

| the installation process at this point.                            |

+--------------------------------------------------------------------+


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


其他的MySQL包场景还需要使用如下的编译参数:

--enable-mysqlnd

--with-pdo-mysql=mysqlnd

--with-mysqli=mysqlnd


make

make install


报错集锦:


(1)解决 Cannot find OpenSSL's <evp.h>:


解决:没有安装oopenssl-devel的开发包导致的

yum install openssl openssl-devel

ln -s /usr/lib64/libssl.so /usr/lib/


(3)/tools/lnmp_soft/php-5.4.24/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory

make: *** [ext/phar/phar.php] Error 127

原因:PHP找不到指定库文件

解决:

ln -s /app/mysql/mysql/lib/libmysqlclient.so.18 /usr/lib64/


(4)Generating phar.phar

chmod: cannot access `ext/phar/phar.phar': No such file or directory

make: [ext/phar/phar.phar] Error 1 (ignored)

Build complete.

Don't forget to run 'make test'.

原因:PHP本身的Bug创建所需的文件即可

解决:

touch ext/phar/phar.phar


配置PHP引擎配置文件php.ini:

设置软连接便于业务访问:

ln -s /app//php-5.4.24/ /app/php

ll

total 8.0K

drwxr-xr-x 4 mysql mysql 4.0K Sep 15 15:44 mysql

lrwxrwxrwx 1 root  root    17 Sep 16 12:13 php -> /app//php-5.4.24/

drwxr-xr-x 9 root  root  4.0K Sep 16 11:42 php-5.4.24

查看php的模板配置文件:

ll ./php*

-rw-r--r-- 1 root  root  1.5K Sep 16 11:29 ./php5.spec

-rw-r--r-- 1 nginx games 1.5K Jan  8  2014 ./php5.spec.in

-rw-r--r-- 1 nginx games 2.5K Jan  8  2014 ./php.gif

-rw-r--r-- 1 nginx games  66K Jan  8  2014 ./php.ini-development

-rw-r--r-- 1 nginx games  66K Jan  8  2014 ./php.ini-production

php.ini-development是开发环境,跟多的开启了日志、调试信息。

php.ini-production是生产环境,有些处于关闭的状态。


创建PHP的配置文件:

cp /tools/lnmp_soft/php-5.4.24/php.ini-production /app/php/lib/php.ini


配置PHP服务,使用FastCGI的方式,配置文件:php-fpm-conf:

cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf

可以先使用PHP默认的配置文件,后面有专门的PHP服务优化。


启动PHP服务:

/app/php/sbin/php-fpm 

netstat -tunlap | grep :9000

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      88555/php-fpm   


配置Nginx支持PHP程序请求访问:

/app/nginx/conf/extra/blog.conf:

worker_processes  1;  

pid        logs/nginx.pid;

events {

    worker_connections  1024;

}



http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on; 

    keepalive_timeout  65; 

    gzip  on; 

    server {

        listen       80; 

        server_name  www.blog.com;

        location / { 

            root   html;

            index  index.html index.htm;

        }   

        location ~ .*\.(php|php5)?$ {

            root html/blog;

            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;

            include fastcgi.conf;

        }   

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }   

    }   

}

测试配置文件:

/app/nginx/sbin/nginx -t

nginx: the configuration file /app/nginx-1.6.3//conf/nginx.conf syntax is ok

nginx: configuration file /app/nginx-1.6.3//conf/nginx.conf test is successful


mkdir /app/nginx/html/blog


测试LNMP环境是否成功:

vim /app/nginx/html/blog/test.php

<?php

 phpinfo();

?>


修改hosts文件,访问http://www.blog.com/test.php


部署web应用:

上传Web应用:

rz

登录MySQl 数据库:

mysql -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.6.37 MySQL Community Server (GPL)


Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 

创建WordPress专用的数据库:

mysql> create database wp;

Query OK, 1 row affected (0.01 sec)


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

| wp                 |

+--------------------+

5 rows in set (0.01 sec)


mysql> grant all on wp.* to wp@'localhost' identified by '123456';

Query OK, 0 rows affected (0.01 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> quit

Bye

解压博客文件:

tar -zxvf ./wordpress-4.8.1-zh_CN.tar.gz -C /app/nginx/html/blog/

mv /app/nginx/html/blog/wordpress/* /app/nginx/html/blog/

rm -rf /app/nginx/html/blog/wordpress/

设置应用的权限:

chown -R nginx.nginx /app/nginx/html/blog/

查看博客程序的文件权限:

ll -d /app/nginx/html/blog/

drwxr-xr-x 5 nginx nginx 4.0K Sep 16 13:44 /app/nginx/html/blog/

创建博客程序的配置文件:

cp /app/nginx/html/blog/wp-config-sample.php /app/nginx/html/blog/wp-config.php

vim /app/nginx/html/blog/wp-config.php

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //

/** WordPress数据库的名称 */

define('DB_NAME', 'wp');


/** MySQL数据库用户名 */

define('DB_USER', 'wp');


/** MySQL数据库密码 */

define('DB_PASSWORD', '123456');


/** MySQL主机 */

define('DB_HOST', 127.0.0.1');


/** 创建数据表时默认的文字编码 */

define('DB_CHARSET', 'utf8');


 本文转自 棋帅小七 51CTO博客,原文链接:http://blog.51cto.com/xvjunjie/1965854


相关文章
|
18天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
23 0
|
17天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
30 2
|
7月前
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。
199 0
|
4月前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
|
7月前
|
应用服务中间件 PHP nginx
基于Anolis OS 3快速搭建LNMP环境制作KodBox
本教程介绍如何搭建LNMP环境,其中本实验的LNMP分别代表Anolis OS 3、Nginx、Mariadb和PHP。
134 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
快速搭建LNMP环境
Nginx是一款小巧而高效的Web服务器软件,可帮您在Linux系统下快速方便地搭建出LNMP Web服务环境。本教程介绍如何搭建LNMP环境,其中LNMP分别代表Linux、Nginx、MySQL和PHP。
284 2
|
3月前
|
API PHP 数据库
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
29 0
|
8月前
|
关系型数据库 MySQL 应用服务中间件
centos7 配置LNMP环境
centos7 配置LNMP环境
|
6月前
|
关系型数据库 MySQL Linux
Linux环境下LNMP架构实战案例
Linux环境下LNMP架构实战案例
|
7月前
|
弹性计算 关系型数据库 MySQL
基于ROS快速部署LNMP环境(CentOS 7)
本教程提供在阿里云云服务器ECS上基于CentOS 7.9操作系统搭建LNMP环境的指引。LNMP是应用广泛的网站服务系统,由四种免费的开源软件Linux、Nginx、MySQL和PHP组成。搭建好LNMP环境后,您可以在该ECS实例上搭建网站、访问网站
406 0