lamp

1. LAMP架构介绍

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

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

2. web服务器工作流程

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

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

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

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

在这里插入图片描述
如上图所示:

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

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

2.1 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上下文切换而导致耗时

2.2 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通信

较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源
三种方式的特点:

以CGI方式运行PHP,由于CGI是非常驻内存集,每次Webserver接受客户端的HTTP请求,然后建立进程执行CGI程序,客户端的请求被传递给CGI程序,CGI执行后结果再返回Webserver。 每次浏览页面都要重复上面的动作,会有非常大的消耗。
以mod_php模式运行PHP,意味着php是作为apache的一个模块来启动的,因此只有在apache启动的时候会加载扩展模块,在apache运行期间是不会再去读取和加载扩展模块的。显然使用mod_php的方式运行PHP效率比CGI方式更高。
FastCGI方式,使用php-fpm单独管理php进程池,PHP-FPM简单可靠的 FastCGI 进程管理器(FastCGI Process Manager),FastCGI是一个常驻型的CGI,可以一直执行,只要激活后,而且还支持分布式运算(使得php程序解释执行可以单独交给php服务器),即可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。而mod_php与fastcgi相比,俩者都有进程池的概念,但是,fastcgi将服务器端动、静态请求更好的分离。php进程除了问题不会将web服务器也当掉。

2.3 web工作流程

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

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

3. lamp平台构建

环境说明:

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

lamp平台软件安装次序:

  • httpd --> mysql --> php

注意:php要求httpd使用prefork MPM

3.1 安装httpd

安装yum源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
0 files removed
[root@localhost ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@localhost ~]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo      epel.repo                  redhat.repo
epel-modular.repo     epel-testing-modular.repo
epel-playground.repo  epel-testing.repo
[root@localhost yum.repos.d]# vi CentOS-Base.repo                 将里面的$releasever改成8
[root@localhost yum.repos.d]# vi epel.repo                                  将里面的$releasever改成8
[root@localhost yum.repos.d]# cd
[root@localhost ~]# yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
34 files removed
[root@localhost ~]# yum makecache
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
CentOS-8 - Base - mirrors.aliyu 386 kB/s | 2.2 MB     00:05    
CentOS-8 - Extras - mirrors.ali  31 kB/s | 8.1 kB     00:00    
CentOS-8 - AppStream - mirrors. 741 kB/s | 5.8 MB     00:08    
Extra Packages for Enterprise L 168 kB/s |  97 kB     00:00    
Extra Packages for Enterprise L 3.0 MB/s | 8.2 MB     00:02    
Metadata cache created.


安装开发工具包
[root@localhost ~]# yum groups mark install "Development Tools"


创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin -g apache apache
[root@localhost ~]# id apache
uid=994(apache) gid=48(apache) groups=48(apache)


安装依赖包
[root@localhost ~]# yum -y install  make openssl-devel pcre-devel expat-devel libtool gcc gcc-c++


下载和安装apr以及apr-util
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz
[root@localhost src]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@localhost src]# wget https://downloads.apache.org/httpd/httpd-2.4.46.tar.bz2
[root@localhost src]# ls
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  debug  httpd-2.4.46.tar.bz2  kernels
[root@localhost src]# yum -y install bzip2
[root@localhost src]# tar xf apr-1.6.5.tar.gz 
[root@localhost src]# tar xf apr-util-1.6.1.tar.gz 
[root@localhost src]# tar xf httpd-2.4.46.tar.bz2 
[root@localhost src]# ls
apr-1.6.5         apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.bz2
apr-1.6.5.tar.gz  debug                  kernels
apr-util-1.6.1    httpd-2.4.46
[root@localhost src]# cd apr-1.6.5
[root@localhost apr-1.6.5]# vim configure
    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行


配置,编译安装
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.6.5]# make
[root@localhost apr-1.6.5]# make install

[root@localhost apr-1.6.5]# cd ../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
[root@localhost apr-util-1.6.1]# make install

[root@localhost apr-util-1.6.1]# cd ../httpd-2.4.46
[root@localhost 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@localhost httpd-2.4.46]# make
[root@localhost httpd-2.4.46]# make install


写环境变量
[root@localhost httpd-2.4.46]# ls /usr/local/apache/
bin    cgi-bin  htdocs  include  man     modules
build  error    icons   logs     manual
[root@localhost httpd-2.4.46]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost httpd-2.4.46]# source /etc/profile.d/httpd.sh
[root@localhost httpd-2.4.46]# cd
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd


做一个软连接
[root@localhost ~]# ln -s /usr/local/apache/include /usr/include/apache
[root@localhost ~]# ll /usr/include/
......
lrwxrwxrwx.  1 root root     25 Oct 30 14:42 apache -> /usr/local/apache/include
......


查找帮助文档
[root@localhost ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man_db.conf(在8的版本里不需要做此步骤就可以直接查找帮助文档)
[root@localhost ~]# man httpd



HTTPD(8)                      httpd                     HTTPD(8)

NAME
       httpd - Apache Hypertext Transfer Protocol Server

SYNOPSIS
       httpd  [ -d serverroot ] [ -f config ] [ -C directive ] [
       -c directive ] [ -D parameter ] [ -e level ] [ -E file  ]
       [ -k start|restart|graceful|stop|graceful-stop ] [ -h ] [
       -l ] [ -L ] [ -S ] [ -t ] [ -v ] [ -V ] [ -X ] [ -M  ]  [
       -T ]

       On  Windows  systems,  the following additional arguments
       are available:

       httpd [ -k install|config|uninstall ] [ -n name ] [ -w ]

SUMMARY
       httpd is the Apache HyperText  Transfer  Protocol  (HTTP)
       server  program. It is designed to be run as a standalone
       daemon process. When used like this it will create a pool
       of child processes or threads to handle requests.

       In  general,  httpd  should  not be invoked directly, but
       rather should be invoked via apachectl on Unix-based sys‐
       tems  or as a service on Windows NT, 2000 and XP and as a
       console application on Windows 9x and ME.

OPTIONS
       -d serverroot
              Set the initial value for the ServerRoot directive
 Manual page httpd(8) line 1 (press h for help or q to q


取消ServerName前面的注释
[root@localhost ~]# vim /etc/httpd24/httpd.conf 
ServerName www.example.com:80



启动apache
[root@localhost ~]# apachectl start
查看80端口已经起来
[root@localhost ~]# 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                  *:80                 *:*     
LISTEN  0       128               [::]:22              [::]:*     

此时可以访问网页
在这里插入图片描述

3.2 安装mysql

安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel


创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=993(mysql) gid=991(mysql) groups=991(mysql)


下载二进制格式的mysql软件包
[root@localhost ~]# ls
anaconda-ks.cfg   apr-util-1.6.1.tar.gz
apr-1.6.5         httpd-2.4.46
apr-1.6.5.tar.gz  httpd-2.4.46.tar.bz2
apr-util-1.6.1    mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz


解压软件至/usr/local/
[root@localhost ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
total 0
drwxr-xr-x. 13 root  root  152 Oct 30 14:37 apache
drwxr-xr-x.  6 root  root   58 Oct 30 14:25 apr
drwxr-xr-x.  5 root  root   43 Oct 30 14:30 apr-util
drwxr-xr-x.  2 root  root    6 Aug 12  2018 bin
drwxr-xr-x.  2 root  root    6 Aug 12  2018 etc
drwxr-xr-x.  2 root  root    6 Aug 12  2018 games
drwxr-xr-x.  2 root  root    6 Aug 12  2018 include
drwxr-xr-x.  2 root  root    6 Aug 12  2018 lib
drwxr-xr-x.  2 root  root    6 Aug 12  2018 lib64
drwxr-xr-x.  2 root  root    6 Aug 12  2018 libexec
lrwxrwxrwx.  1 mysql mysql  35 Oct 30 15:11 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x.  9  7161 31415 129 Jun  2 21:11 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root  root    6 Aug 12  2018 sbin
drwxr-xr-x.  5 root  root   49 Aug 30 14:26 share
drwxr-xr-x.  2 root  root    6 Aug 12  2018 src


添加环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh 
[root@localhost local]# cd
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql


建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data


初始化数据库
[root@localhost ~]# mysqld --initialize-insecure --datadir=/opt/data --user=mysql                 -insecure是不设密码


生成配置文件
此时打开配置文件已有内容,所以我们先把这内容备份,以防后面会用到
[root@localhost ~]# vim /etc/my.cnf

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost ~]# mv /etc/my.cnf{,-bak}
[root@localhost ~]# ls /etc/my.cnf*
/etc/my.cnf-bak

/etc/my.cnf.d:
client.cnf
此时再写配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve


做一个软连接
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib
[root@localhost ~]# ldconfig       重新读取配置文件



配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# ls
bin   include  LICENSE  README  support-files
docs  lib      man      share
[root@localhost mysql]# cd 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]# cd
[root@localhost ~]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql         添加这两行
datadir=/opt/data



启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# 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       80                   *:3306               *:*    
LISTEN  0       128                  *:80                 *:*    
LISTEN  0       128               [::]:22              [::]:*   



此时登陆Mysql会报错
[root@localhost ~]# mysql
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
安装这个包
[root@localhost ~]# yum -y install libncurses.so.5*



修改密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

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("maqiang123");
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
使用新密码登陆
[root@localhost ~]# mysql -uroot -pmaqiang123
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 3
Server version: 5.7.31 MySQL Community Server (GPL)

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> quit
Bye

3.3 安装php

配置yum源
[root@localhost ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@localhost ~]# rpm -Uvh remi-release-7.rpm
[root@localhost ~]# yum makecache --enablerepo=remi-php74
因为8的版本里有各种包就不可以忽略
[root@localhost ~]# yum list all|grep php
php.x86_64                                           7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-IDNA_Convert.noarch                              0.8.0-14.el8                                     epel         
php-adodb.noarch                                     5.20.6-9.el8                                     epel         
php-bcmath.x86_64                                    7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-cli.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-common.x86_64                                    7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-dba.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-dbg.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-devel.x86_64                                     7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-embedded.x86_64                                  7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-enchant.x86_64                                   7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-fpm.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-gd.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-gmp.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-intl.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-json.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-ldap.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-mbstring.x86_64                                  7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-mysqlnd.x86_64                                   7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-odbc.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-opcache.x86_64                                   7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-pdo.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-pear.noarch                                      1:1.10.5-9.module_el8.2.0+313+b04d0a66           AppStream    
php-pear-Auth-SASL.noarch                            1.1.0-6.el8                                      epel         
php-pear-Cache-Lite.noarch                           1.8.3-1.el8                                      epel         
php-pear-Date.noarch                                 1.4.7-22.el8                                     epel         
php-pear-HTTP-Request.noarch                         1.4.4-18.el8                                     epel         
php-pear-Mail.noarch                                 1.4.1-6.el8                                      epel         
php-pear-Net-SMTP.noarch                             1.9.0-1.el8                                      epel         
php-pear-Net-Socket.noarch                           1.2.2-6.el8                                      epel         
php-pear-Net-URL.noarch                              1.0.15-20.el8                                    epel         
php-pecl-apcu.x86_64                                 5.1.12-2.module_el8.2.0+313+b04d0a66             AppStream    
php-pecl-apcu-devel.x86_64                           5.1.12-2.module_el8.2.0+313+b04d0a66             AppStream    
php-pecl-zip.x86_64                                  1.15.3-1.module_el8.2.0+313+b04d0a66             AppStream    
php-pgsql.x86_64                                     7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-process.x86_64                                   7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-recode.x86_64                                    7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-snmp.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-soap.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-xml.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-xmlrpc.x86_64                                    7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
php-xmpphp.noarch                                    0.1-0.23.rc2.r77.el8                             epel         
sphinx-php.x86_64                                    2.2.11-15.el8                                    epel         


安装依赖包
[root@localhost ~]# 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@localhost ~]# yum list all|grep php|grep mysql
php-mysqlnd.x86_64                                   7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream    
[root@localhost ~]# yum -y install php-mysqlnd


下载php
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
[root@localhost ~]# ls
anaconda-ks.cfg        httpd-2.4.46
apr-1.6.5              httpd-2.4.46.tar.bz2
apr-1.6.5.tar.gz       install.sh
apr-util-1.6.1         mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.gz  php-7.2.8.tar.xz



编译安装php
[root@localhost src]# tar xf php-7.2.8.tar.xz
[root@localhost src]# cd php-7.2.8
[root@localhost php-7.2.8]# ./configure --prefix=/usr/local/php7  \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@localhost php-7.2.8]# make
[root@localhost php-7.2.8]# make install



配置环境变量
[root@localhost php-7.2.8]# ls /usr/local/php7/
bin  etc  include  lib  php  sbin  var
[root@localhost php-7.2.8]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost php-7.2.8]# source /etc/profile.d/php.sh 
[root@localhost php-7.2.8]# which php
/usr/local/php7/bin/php


配置php-fpm
[root@localhost php-7.2.8]# which php
/usr/local/php7/bin/php
[root@localhost php-7.2.8]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.2.8]# cd /usr/local/php7/
[root@localhost php7]# ls
bin  etc  include  lib  php  sbin  var
[root@localhost php7]# cd etc/
[root@localhost etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf  www.conf.default



编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf)
配置fpm的相关选项为你所需要的值:配置文件最后添加以下四行
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50    ;最多同时提供50个进程提供50个并发服务
pm.start_servers = 5    ;启动时启动5个进程
pm.min_spare_servers = 2    ;最小空闲进程数
pm.max_spare_servers = 8    ;最大空闲进程数


启动php-fpm
[root@localhost etc]# service php-fpm start
Starting php-fpm  done
[root@localhost etc]# cd
[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port            默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
LISTEN  0       128          127.0.0.1:9000         0.0.0.0:*     
LISTEN  0       128            0.0.0.0:22           0.0.0.0:*     
LISTEN  0       80                   *:3306               *:*     
LISTEN  0       128                  *:80                 *:*     
LISTEN  0       128               [::]:22              [::]:*     

3.4 配置apache

3.4.1 启用代理模块

在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:

  • LoadModule proxy_module modules/mod_proxy.so
  • LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
进入配置文件,将这两个注释取消掉
[root@localhost ~]# vim /etc/httpd24/httpd.conf 

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

3.4.2 配置虚拟主机

将这行注释取消
[root@localhost ~]# vim /etc/httpd24/httpd.conf 
Include /etc/httpd24/extra/httpd-vhosts.conf

注意:
这里写的/var/www/html/是yum源安装方式生成的网页存放目录,这里必须改成你编译安装指定的网页存放路径,禁止直接复制我这里的路径
这里的idfsoft.com是域名,你必须改成你所使用的域名,禁止直接复制此处的域名
这里的$1表示匹配所有以.php结尾的http请求

在配置文件里添加以下内容
[root@localhost ~]# vim /etc/httpd24/extra/httpd-vhosts.conf 

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs"
    ServerName www.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.example.com-error_log"
    CustomLog "logs/www.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
    <Directory "/usr/local/apache/htdocs">
        Require all granted
    </Directory>



</VirtualHost>


提供网站
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# vim index.php

<?php
    phpinfo();
?>


修改属主
[root@localhost htdocs]# cd ..
[root@localhost apache]# pwd
/usr/local/apache
[root@localhost apache]# chown -R apache.apache htdocs/
[root@localhost apache]# ll
total 36
drwxr-xr-x.  2 root   root    262 Oct 30 14:37 bin
drwxr-xr-x.  2 root   root    167 Oct 30 14:37 build
drwxr-xr-x.  2 root   root     78 Oct 30 14:37 cgi-bin
drwxr-xr-x.  3 root   root   4096 Oct 30 14:37 error
drwxr-xr-x.  2 apache apache   41 Oct 30 17:18 htdocs
drwxr-xr-x.  3 root   root   8192 Oct 30 14:37 icons
drwxr-xr-x.  2 root   root   4096 Oct 30 14:37 include
drwxr-xr-x.  2 root   root     58 Oct 30 14:49 logs
drwxr-xr-x.  4 root   root     30 Oct 30 14:37 man
drwxr-xr-x. 14 root   root   8192 Oct 30 14:37 manual
drwxr-xr-x.  2 root   root   4096 Oct 30 14:37 modules



[root@localhost ~]# vim /etc/httpd24/httpd.conf
//搜索AddType,添加以下内容
......
 AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php        #添加此行
    AddType application/x-httpd-php-source .phps        #添加此行

//搜索index.html
  DirectoryIndex index.php index.html    添加index.php



启动服务
[root@localhost ~]# apachectl restart
[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port  
LISTEN  0       128          127.0.0.1:9000         0.0.0.0:*     
LISTEN  0       128            0.0.0.0:22           0.0.0.0:*     
LISTEN  0       80                   *:3306               *:*     
LISTEN  0       128                  *:80                 *:*     
LISTEN  0       128               [::]:22              [::]:*   

3.5 验证

在这里插入图片描述

  • 必须有mysql,说明部署成功
    在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百慕卿君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值