• lamp


    lamp简介

    所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

    LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。

    web服务器工作流程

    在说lamp架构平台的搭建前,我们先来了解下什么是CGI,什么是FastCGI,什么是......

    web服务器的资源分为两种,静态资源和动态资源

    • 静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
    • 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端

    那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求

    如上图所示

    阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行

    阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互

    cgi与fastcgi

    上图阶段①中提到了FastCGI,下面我们来了解下CGI与FastCGI。

    CGI(Common Gateway Interface,通用网关接口),CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。

    FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通过启用一个解释器进程来处理每个请求,耗时且耗资源,而FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求,这样就避免了重复的生成和杀死进程带来的频繁cpu上下文切换而导致耗时

    httpd与php结合的方式

    httpd与php结合的方式有以下三种:

    • modules:php将以httpd的扩展模块形式存在,需要加载动态资源时,httpd可以直接通过php模块来加工资源并返回给客户端
      • httpd prefork:libphp5.so(多进程模型的php)
      • httpd event or worker:libphp5-zts.so(线程模型的php)
    • CGI:httpd需要加载动态资源时,通过CGI与php解释器联系,获得php执行的结果,此时httpd负责与php连接的建立和断开等
    • FastCGI:利用php-fpm机制,启动为服务进程,php自行运行为一个服务,https通过socket与php通信

    web工作流程

    通过上面的图说明一下web的工作流程:

             客户端通过http协议请求web服务器资源

      web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
      • 若是静态资源则直接从本地文件系统取之返回给客户端。
      • 否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。

    lamp平台构建

    环境说明:

     

    系统平台 IP 需要安装的服务
    centos8 redhat8 192.168.220.40 httpd mysql php php-mysql

    1.安装HTTP: //安装需要的包[root@chouyu ~]# yum -y install wget bzip2 gcc gcc-c++ make pcre-devel expat-devel libxml2-devel openssl-devel

    [root@chouyu ~]# yum groups mark install 'Development Tools'
    
    //下载和安装apache,apr和apr-util
    [root@chouyu~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
     
    [root@chouyu~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
     
    [root@chouyu~]# wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
     
    //创建apache用户和组
    [root@chouyu~]# useradd  -r -M -s /sbin/nologin apache
    
    //解压
    [root@chouyu ~]# tar -xf apr-1.7.0.tar.gz
    [root@chouyu~]# tar -xf httpd-2.4.46.tar.gz
    [root@chouyu~]# tar -xf apr-util-1.6.1.tar.gz
    
    [root@chouyu~]# cd apr-1.7.0
    [root@chouyuapr-1.7.0]# vim configure
        cfgfile="${ofile}T"
        trap "$RM "$cfgfile"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行
    
    
    [root@localhost apr-1.7.0]# ./configure  --prefix=/usr/local/apr
    ....................................................    //配置过程
    [root@localhost apr-1.7.0]# make && make install
    ...................................     //编译安装过程
     
    [root@localhost apr-1.7.0]# cd  /root/apr-util-1.6.1
    [root@localhost apr-util-1.6.1]# ./configure  --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    ....................................    //配置过程
    [root@localhost apr-util-1.6.1]# make && make install
    .........................................    //编译安装过程
    
    //apache安装
    [root@chouyu apr-util-1.6.1]# cd /root/httpd-2.4.46
    [root@chouyu httpd-2.4.46]# ./configure --prefix=/usr/local/apache 
    > --sysconfdir=/etc/httpd24 
    > --enable-so 
    > --enable-ssl 
    > --enable-cgi 
    > --enable-rewrite 
    > --with-zlib 
    > --with-pcre 
    > --with-apr=/usr/local/apr 
    > --with-apr-util=/usr/local/apr-util/ 
    > --enable-modules=most 
    > --enable-mpms-shared=all 
    > --with-mpm=prefork
    ....................................配置过程
    
    [root@chouyu httpd-2.4.46]# make && make install
    ............................编译安装过程
    //安装后配置
    [root@localhost ~]#  echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
    [root@localhost ~]# source /etc/profile.d/httpd.sh
    [root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
    [root@localhost ~]# vim /etc/man_db.conf
    MANDATORY_MANPATH                       /usr/man
    MANDATORY_MANPATH                       /usr/share/man
    MANDATORY_MANPATH                       /usr/local/share/man
    MANDATORY_MANPATH                       /usr/local/apache/man     //添加此行
    [root@chouyu ~]# vim /etc/httpd24/httpd.conf
    #
    #ServerName www.example.com:80  //取消前面的注释
    
    //启动
    [root@chouyu httpd-2.4.46]#  apachectl start
    
    
    [root@chouyu httpd-2.4.46]# ss -antl
    State         Recv-Q          Send-Q                    Local Address:Port                   Peer Address:Port         
    LISTEN        0               128                             0.0.0.0:22                          0.0.0.0:*            
    LISTEN        0               128                                [::]:22                             [::]:*            
    LISTEN        0               128                                   *:80                                *:*            
    [root@chouyu httpd-2.4.46]# 
    
    
    //安装依赖包
    [root@localhost ~]#  yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
     
    //创建用户和组
    [root@localhost ~]# useradd  -r -M -s /sbin/nologin  mysql
     
    //下载二进制格式mysql软件包
    [root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
     
    //解压软件
    [root@localhost ~]# tar  -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
     
    [root@localhost ~]# ln -sv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/  /usr/local/mysql
    '/usr/local/mysql' -> '/usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/'
     
    [root@localhost local]# chown -R mysql.mysql mysql*
     
    //添加环境变量
    [root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh
    [root@localhost local]# source /etc/profile.d/myslq.sh
    [root@localhost local]# echo $PATH
    /usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost ~]# ln -s /usr/local/mysql/include/  /usr/include/mysql
    [root@localhost ~]# vim /etc/man_db.conf 
    MANDATORY_MANPATH           /usr/local/apache/man          
    MANDATORY_MANPATH           /usr/local/mysql/man  //添加man文件
    [root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
    /usr/local/mysql/lib
    [root@localhost ~]# ldconfig 
    #创建数据存放目录
    [root@localhost local]# mkdir  /mydata
    [root@localhost local]# chown  -R mysql.mysql  /mydata/
    
    //初始化数据库
    [root@chouyu local]#  mysqld  --initialize  --user=mysql   --datadir=/mydata
    2021-01-03T12:26:58.269415Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2021-01-03T12:26:58.522285Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2021-01-03T12:26:58.546744Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2021-01-03T12:26:58.602304Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f943bc2c-4dbe-11eb-8248-000c29aa2fbf.
    2021-01-03T12:26:58.602972Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2021-01-03T12:26:58.917180Z 0 [Warning] CA certificate ca.pem is self signed.
    2021-01-03T12:26:59.390382Z 1 [Note] A temporary password is generated for root@localhost: Ewl1guldYj>!
    
    #修改my.cnf配置文件
    [root@localhost ~]# vim /etc/my.cnf
    #添加以下内容
    [mysqld]
    basedir = /usr/local/mysql
    datadir = /mydata
    socket = /tmp/mysql.sock
    port = 3306
    pid-file = /mydata/mysql.pid
    user = mysql
    skip-name-resolve
     
    [client-server]
     
    #配置服务启动脚本
    [root@localhost ~]# cd /usr/local/mysql/support-files/
    [root@localhost support-files]# ls
    magic  mysqld_multi.server  mysql-log-rotate  mysql.server
    [root@localhost support-files]# cp mysql.server  /etc/init.d/mysqld
    
    [root@localhost support-files]# vim /etc/init.d/mysqld
    # If you change base dir, you must also change datadir. These may get
    # overwritten by settings in the MySQL configuration files.
     
    basedir=/usr/local/mysql
    datadir=/mydata
     
    #启动mysql如果报错最下面有解决办法
    [root@localhost ~]# service mysqld start
    Starting MySQL. SUCCESS!
    
    [root@chouyu support-files]# ss -antl
    State         Recv-Q          Send-Q                    Local Address:Port                   Peer Address:Port         
    LISTEN        0               128                             0.0.0.0:22                          0.0.0.0:*            
    LISTEN        0               128                                [::]:22                             [::]:*            
    LISTEN        0               80                                    *:3306                              *:*            
    LISTEN        0               128                                   *:80                                *:*            
    [root@chouyu support-files]# mysql -uroot -p'Ewl1guldYj>!'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 2
    Server version: 5.7.31
    
    Copyright (c) 2000, 2020, 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> set password = password('123321123');
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> 
    //安装依赖包
    [root@chouyu ~]#  yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd
     
    [root@chouyu ~]#  yum -y install php-*
    [root@chouyu ~]# php -v
    [root@chouyu ~]# php -v
    PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
        with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies
    [root@chouyu ~]# 
    
    [root@chouyu ~]# vim /etc/php-fpm.d/www.conf
    ;listen = /run/php-fpm/www.sock #注释此行
    listen = 127.0.0.1:9000  #添加监听端口
    
    [root@chouyu ~]# systemctl start php-fpm
    [root@chouyu ~]# ss -antl
    State         Recv-Q          Send-Q                    Local Address:Port                   Peer Address:Port         
    LISTEN        0               128                             0.0.0.0:22                          0.0.0.0:*            
    LISTEN        0               128                           127.0.0.1:9000                        0.0.0.0:*            
    LISTEN        0               128                                [::]:22                             [::]:*            
    LISTEN        0               80                                    *:3306                              *:*            
    LISTEN        0               128                                   *:80                                *:*            
    [root@chouyu ~]# 
    
    [root@localhost ~]# mkdir  /usr/local/apache/htdocs/ceshi
    [root@localhost ~]# vim /usr/local/apache/htdocs/ceshi/index.php
    <?php
            phpinfo();
    ?>
     
    [root@localhost ~]# chown -R apache.apache  /usr/local/apache/htdocs/
     
    [root@localhost ~]# vim /etc/httpd24/httpd.conf
    #在配置文件的最后加入以下内容
    <VirtualHost *:80>
            DocumentRoot "/usr/local/apache/htdocs/ceshi"
            ServerName  www.ceshi11.com
            ProxyRequests   Off
            ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/ceshi/$1
            <Directory "/usr/local/apache/htdocs/ceshi">
                    Options none
                    AllowOverride none
                    Require all granted
            </Directory>
    </VirtualHost>
    
    
    [root@localhost ~]# vim /etc/httpd24/httpd.conf
        # If the AddEncoding directives above are commented-out, then you
        # probably should define those extensions to indicate media types:
        #
        AddType application/x-compress .Z
        AddType application/x-gzip .gz .tgz
        AddType application/x-httpd-php .php      #添加此行
        AddType application/x-httpd-php-source .phps   #添加此行
    .......................................................
    # is requested.
    #
    <IfModule dir_module>
        DirectoryIndex index.php  index.html   #添加index.php
     
    #重启报错的话后面有解决办法
    [root@localhost
    ~]# apachectl restart

    验证

    mysql重启报错解决:

    重启报错解决
    
    解决的方法就在httpd这个配置文件里,找到以下两条:
    
    #LoadModule proxy_module modules/mod_proxy.so
    
    #LoadModule proxy_http_module modules/mod_proxy_http.so
  • 相关阅读:
    Java中Runnable和Thread的区别
    JAVA Swing 事件监听
    java 监听机制模拟(JButton按钮监听机制)
    java事件处理机制(自定义事件)
    oracle之检查点(Checkpoint)
    linux内核值shmmax问题
    如何在VMware虚拟机间建立共享磁盘?
    Mysql 不同版本 说明
    mysql 概念和逻辑架构
    mysql 在大型应用中的架构演变
  • 原文地址:https://www.cnblogs.com/lichouluoyu/p/14226859.html
Copyright © 2020-2023  润新知