LNMP一体化部署

LNMP组合工作流程

在LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务,如果请求是静态资源,则由Nginx解析返回给用户;如果是动态请求(.php结尾),那么Nginx就会把它通过FastCGI接口(快速接口规则,生产常用方法)发送FastCGI数据包给PHP引擎服务(FastCGI进程php-fpm)进行解析,如果这个动态请求要读取数据库数据,那么PHP就会继续向后请求MySQL数据库,以读取需要的数据,并最终通过Nginx服务把获取的数据返回给用户,这就是LNMP环境的基本请求顺序流程。这个请求流程是企业使用LNMP环境的常用流程。
在这里插入图片描述
用户通过浏览器将请求发到nginx
(1)若请求为静态请求,location/{
(静态)
root html;
index index.html index.htm;

}
nginx就会去网页目录根据URL的URI部分取得网页,并将网页返回给用户。(磁盘IO读写),图片、视频在存储(NFS)中(代码不放在存储中),此时NFS需要挂载Nginx
(2) 若请求为动态请求,location ~* .(php|php5)${
(动态)
root html;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000
}
nginx就会将用户的请求转换为fastcgi格式的数据包推给fastcgi的服务端php-fpm(一个socket进程,端口为9000,加快了PHP解析速度),PHP激活JS代码
动态网页由实体文件index.php(放在内存中),通过php语言到数据库取得数据,并写回index.php文件,再打包给用户(代码不放在存储中)

  1. 分离式部署中,NGINX服务器和PHP服务器上网页、代码必须两者都有,否则会触发404

  2. nginx和Apache一样,若没有PHP支持,都处理不了所谓的动态请求,他们自身其实都只能出路静态,只是Apache转发动态数据包的速度快,但是只是单个包速度,Apache并发低。

  3. 读取网页时无需挂载存储NFS,但是动态(如上传图片)需要挂载存储NFS

  4. fastcgi快速接口有两端:
    (1)作为客户端的fastcgi_pass(Nginx安装包包含)
    (2)作为服务端的php-fpm(PHP安装包包含),一个socket进程,监听9000端口
    http数据的结构很松散,格式要求不够严谨,所以http协议的数据包特点:比较小,解析速度快,但是网络传输快
    fastcgi数据包格式非常严谨,PHP解析速度非常快,但是越严谨的数据包越大,所以fsatcgi数据包大小 要绝对大于http数据包

  5. 优化:往往与nginx的内存和php的内存性能有关

  6. PHP解析器:php.ini
    php.ini解析php语言用于向MySQL发送SQL语句,MySQL返回数据后转换成静态页面,写回到磁盘,形成伪静态文件(需要设定有效期)

LNMP的部署方式:

(1)全都部署在一台服务器上
(2)全都不部署在一台服务器上(N+P+M)
(3)只分离MySQL(NP+M)

部署LNMP

将nginx和PHP的程序用户设为同一个

  1. 安装nginx
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.10.2/
[root@localhost ~]# mount /dev/sr0 /media/cdrom
[root@localhost nginx-1.10.2]# yum -y install pcre-devel openssl-devel
[root@localhost nginx-1.10.2]# useradd -s /sbin/nologin -M www
[root@localhost nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 
[root@localhost nginx-1.10.2]# make && make install
[root@localhost nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@localhost conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / { 
            root   html;
            index  index.html index.htm;
        }
    }
}
              
# nginx编译完毕
  1. 安装MySQL(PHP需要MySQL的支持环境)
[root@localhost ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
# 创建mysql程序用户
[root@localhost ~]# useradd -s /sbin/nologin -M mysql
[root@localhost mysql-5.5.32-linux2.6-x86_64]# echo "192.168.214.163 LNMP" >> /etc/hosts
[root@localhost mysql-5.5.32-linux2.6-x86_64]# ln -s /usr/local/mysql-5.5.32-linux2.6-x86_64/ /usr/local/mysql
# 初始化MySQL配置文件my.conf
[root@localhost ~]# cd /usr/local/mysql-5.5.32-linux2.6-x86_64/
[root@localhost mysql-5.5.32-linux2.6-x86_64]# /bin/cp support-files/my-small.cnf  /etc/my.cnf 
# 初始化mysql
[root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql
[root@localhost mysql]# yum -y install libaio
[root@localhost mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!


初始化故障排错集锦

错误示例1:

usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared ob

#错误原因是没有libaio函数库的支持。需要
yum -y install libaio

错误示例2:

WARNING:The host'mysql'could not be looked up with resolveip

#需要修改主机名解析,使其和uname -n一样,修改后的结果如下:
[root@localhost ~] # grep `uname -n` /etc/hosts

错误示例3:

ERROR:1004Can't create fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值