LAMP编译安装

部署lamp

(采用apache源码安装+mysql二进制安装+php源码安装)


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服务器如何处理客户端的请求

img

如上图所示

阶段①显示的是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通信

较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源

web工作流程

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

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

lamp平台构建(源码安装)

构建顺序  httpd --> mysql --> php

注意:php要求httpd使用prefork MPM

1 安装httpd

去apache官网下载和安装apr和apr-util以及httpd
[root@cjy ~]# wget https://downloads.apache.org/apr/apr-1.7.4.tar.gz
[root@cjy ~]# wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz
[root@cjy ~]# wget https://downloads.apache.org/httpd/httpd-2.4.58.tar.gz


创建apache服务的用户和组
[root@cjy ~]# useradd -r -M -s /sbin/nologin apache

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

解压安装包
[root@cjy ~]# ls
anaconda-ks.cfg  apr-1.7.4.tar.gz  apr-util-1.6.3.tar.gz  httpd-2.4.58.tar.gz
[root@cjy ~]# tar xf apr-1.7.4.tar.gz 
[root@cjy ~]# tar xf apr-util-1.6.3.tar.gz 
[root@cjy ~]# tar xf httpd-2.4.58.tar.gz 
[root@cjy ~]# ls
anaconda-ks.cfg  apr-1.7.4.tar.gz  apr-util-1.6.3.tar.gz  httpd-2.4.58.tar.gz
apr-1.7.4        apr-util-1.6.3    httpd-2.4.58
[root@cjy ~]# cd apr-1.7.4
[root@cjy apr-1.7.4]# ls
apr-config.in  build.conf        dso         libapr.rc     NOTICE         support
apr.dep        buildconf         emacs-mode  LICENSE       NWGNUmakefile  tables
apr.dsp        build-outputs.mk  encoding    locks         passwd         test
apr.dsw        CHANGES           file_io     Makefile.in   poll           threadproc
apr.mak        CMakeLists.txt    helpers     Makefile.win  random         time
apr.pc.in      config.layout     include     memory        README         tools
apr.spec       configure         libapr.dep  misc          README.cmake   user
atomic         configure.in      libapr.dsp  mmap          shmem
build          docs              libapr.mak  network_io    strings
[root@cjy apr-1.7.4]# vim configure

    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行

[root@cjy apr-1.7.4]# ./configure --prefix=/usr/local/apr
配置过程略...
[root@cjy apr-1.7.4]# make && make install
编译安装过程略...

[root@cjy apr-1.7.4]# cd ../apr-util-1.6.3
[root@cjy apr-util-1.6.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
配置过程略...
[root@cjy apr-util-1.6.3]# make && make install
编译安装过程略...

编译安装httpd
[root@cjy apr-util-1.6.3]# cd ../httpd-2.4.58
[root@cjy httpd-2.4.58]# ls
ABOUT_APACHE     CHANGES          httpd.mak       Makefile.in       ROADMAP
acinclude.m4     changes-entries  httpd.spec      Makefile.win      server
Apache-apr2.dsw  CMakeLists.txt   include         modules           srclib
Apache.dsw       config.layout    INSTALL         NOTICE            support
apache_probes.d  configure        InstallBin.dsp  NWGNUmakefile     test
ap.d             configure.in     LAYOUT          os                VERSIONING
build            docs             libhttpd.dep    README
BuildAll.dsp     emacs-style      libhttpd.dsp    README.CHANGES
BuildBin.dsp     httpd.dep        libhttpd.mak    README.cmake
buildconf        httpd.dsp        LICENSE         README.platforms
[root@cjy httpd-2.4.58]# ./configure --prefix=/usr/local/apache \
 --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@cjy httpd-2.4.38]# make && make install
编译安装过程略...

安装后配置
[root@cjy ~]# ls /usr/local/apache/
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
[root@cjy ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh

[root@cjy ~]# ln -s /usr/local/apache/include /usr/include/apache
[root@cjy ~]# vim /etc/man_db.conf 
增加这条内容
MANDATORY_MANPATH                       /usr/local/apache/man
[root@cjy ~]# source /etc/profile.d/httpd.sh
[root@cjy ~]# vim /usr/local/apache/conf/httpd.conf 
取消下面ServerName这行前面的注释
ServerName www.example.com:80

关闭防火墙启动apache
[root@cjy ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@cjy ~]# setenforce 0
[root@cjy ~]# vim /etc/selinux/config 
将enforcing改成disabled
[root@cjy ~]# apachectl start
[root@cjy ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                  
LISTEN    0         511                      *:80                     *:*                  
LISTEN    0         128                   [::]:22                  [::]:*           

发现可以访问了

在这里插入图片描述

再设置开机自启

[root@cjy ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/httpd.service
[root@cjy ~]# vim /usr/lib/systemd/system/httpd.service 
[root@cjy ~]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@cjy ~]# systemctl daemon-reload
[root@cjy ~]# systemctl status httpd
○ httpd.service - httpd server daemon
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
[root@cjy ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@cjy ~]# systemctl status httpd
○ httpd.service - httpd server daemon
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: inactive (dead) since Mon 2023-12-11 15:39:01 CST; 11s ago
    Process: 62554 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/>
    Process: 62557 ExecStop=/usr/local/apache/bin/apachectl stop (code=exited, status=0/SU>
        CPU: 21ms

2.安装mysql

先去官网下载mariadb-devel
[root@cjy ~]# yum -y install https://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/mariadb-devel-10.5.16-2.el9.x86_64.rpm

创建用户和组
[root@cjy ~]# useradd -r -M -s /sbin/nologin mysql

去官网下载二进制格式的mysql软件包
[root@cjy ~]# wget https://downloads.mysql.com/archives/get/file/mysql-8.0.35-linux-glibc2.28-x86_64.tar
[root@cjy ~]# ls
anaconda-ks.cfg   apr-util-1.6.3         httpd-2.4.58.tar.gz
apr-1.7.4         apr-util-1.6.3.tar.gz  mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
apr-1.7.4.tar.gz  httpd-2.4.58

解压软件至/usr/local/
[root@cjy ~]# ls
anaconda-ks.cfg   apr-util-1.6.3         httpd-2.4.58.tar.gz
apr-1.7.4         apr-util-1.6.3.tar.gz  mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
apr-1.7.4.tar.gz  httpd-2.4.58
[root@cjy ~]# tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz -C /usr/local/
[root@cjy ~]# cd /usr/local/
[root@cjy local]# ls
apache  apr-util  etc    include  lib64    mysql-8.0.35-linux-glibc2.28-x86_64  share
apr     bin       games  lib      libexec  sbin                                 src
[root@cjy local]# mv mysql-8.0.35-linux-glibc2.28-x86_64 mysql
[root@cjy local]# ls
apache  apr-util  etc    include  lib64    mysql  share
apr     bin       games  lib      libexec  sbin   src

修改目录/usr/local/mysql的属主属组
[root@cjy local]# chown -R mysql.mysql mysql
[root@cjy local]# ll
total 0
drwxr-xr-x. 14 root  root  164 Dec 11 14:58 apache
drwxr-xr-x.  6 root  root   58 Dec 11 14:40 apr
drwxr-xr-x.  5 root  root   43 Dec 11 14:48 apr-util
drwxr-xr-x.  2 root  root    6 May 16  2022 bin
drwxr-xr-x.  2 root  root    6 May 16  2022 etc
drwxr-xr-x.  2 root  root    6 May 16  2022 games
drwxr-xr-x.  2 root  root    6 May 16  2022 include
drwxr-xr-x.  2 root  root    6 May 16  2022 lib
drwxr-xr-x.  3 root  root   17 Dec  4 15:58 lib64
drwxr-xr-x.  2 root  root    6 May 16  2022 libexec
drwxr-xr-x.  9 mysql mysql 129 Dec 11 15:52 mysql
drwxr-xr-x.  2 root  root    6 May 16  2022 sbin
drwxr-xr-x.  5 root  root   49 Dec  4 15:58 share
drwxr-xr-x.  2 root  root    6 May 16  2022 src

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

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

初始化数据库
[root@cjy ~]# mysqld --initialize --user mysql --datadir /opt/data
2023-12-11T07:59:19.785768Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.35) initializing of server in progress as process 67519
2023-12-11T07:59:19.799194Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-12-11T07:59:20.176444Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-12-11T07:59:22.428386Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: PF9:7yt,7u3-
[root@cjy ~]# echo 'PF9:7yt,7u3-' > pass
[root@cjy ~]# ls
anaconda-ks.cfg   apr-util-1.6.3         httpd-2.4.58.tar.gz
apr-1.7.4         apr-util-1.6.3.tar.gz  mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
apr-1.7.4.tar.gz  httpd-2.4.58           pass


配置mysql,生成配置文件
[root@cjy ~]# vim /etc/my.cnf
[root@cjy ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[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
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

root@cjy ~]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@cjy ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@cjy ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@cjy ~]# cat /etc/ld.so.conf.d/mysql.conf 
/usr/local/mysql/lib
[root@cjy ~]# vim /etc/man_db.conf    
将下行内容加入
MANDATORY_MANPATH                       /usr/local/mysql/man

配置服务启动脚本
[root@cjy ~]# ls /usr/local/mysql/support-files/
mysqld_multi.server  mysql-log-rotate  mysql.server
[root@cjy ~]# vim /usr/local/mysql/support-files/mysql.server 
在basedir和datadir后面填写路径
basedir=/usr/local/mysql
datadir=/opt/data

启动mysql
[root@cjy ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/cjy.err'.
 SUCCESS! 
[root@cjy ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                  
LISTEN    0         128                   [::]:22                  [::]:*       

LISTEN    0         511                      *:80                     *:*           
LISTEN    0         70                       *:33060                  *:*                  
LISTEN    0         151                      *:3306                   *:*                 
设置开机自启
[root@cjy ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@cjy ~]# vim /usr/lib/systemd/system/mysqld.service 
[root@cjy ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@cjy ~]# systemctl daemon-reload
[root@cjy ~]# systemctl status mysqld
○ mysqld.service - mysqld server daemon
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; preset: disabled)
     Active: inactive (dead)
[root@cjy ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@cjy ~]# systemctl status mysqld
× mysqld.service - mysqld server daemon
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: failed (Result: exit-code) since Mon 2023-12-11 16:22:02 CST; 6s ago
   Duration: 191ms
    Process: 67906 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exite>
   Main PID: 67919 (code=exited, status=1/FAILURE)
        CPU: 198ms

Dec 11 16:22:01 cjy systemd[1]: Starting mysqld server daemon...
Dec 11 16:22:01 cjy mysql.server[67906]: Starting MySQL SUCCESS!
Dec 11 16:22:01 cjy systemd[1]: Started mysqld server daemon.
Dec 11 16:22:02 cjy mysql.server[67919]: 2023-12-11T08:22:02.005090Z mysqld_safe A mysqld >
Dec 11 16:22:02 cjy systemd[1]: mysqld.service: Main process exited, code=exited, status=1>
Dec 11 16:22:02 cjy systemd[1]: mysqld.service: Failed with result 'exit-code'.

使用临时密码登录,修改密码
[root@cjy ~]# cat pass 
PF9:7yt,7u3-
[root@cjy ~]# mysql -uroot -p'PF9:7yt,7u3-'
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 9
Server version: 8.0.35

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> alter user root@'localhost' identified with mysql_native_password by 'Passw0rd@_';
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye
[root@cjy ~]# mysql -uroot -pPassw0rd@_
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 10
Server version: 8.0.35 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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.安装php

去官网下载php包再解压
[root@cjy ~]# wget https://www.php.net/distributions/php-8.3.0.tar.xz
[root@cjy ~]# ls
anaconda-ks.cfg   apr-util-1.6.3.tar.gz                       pass
apr-1.7.4         httpd-2.4.58                                php-8.3.0.tar.xz
apr-1.7.4.tar.gz  httpd-2.4.58.tar.gz
apr-util-1.6.3    mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
[root@cjy ~]# tar xf php-8.3.0.tar.xz 
[root@cjy ~]# ls
anaconda-ks.cfg   apr-util-1.6.3.tar.gz                       pass
apr-1.7.4         httpd-2.4.58                                php-8.3.0
apr-1.7.4.tar.gz  httpd-2.4.58.tar.gz                         php-8.3.0.tar.xz
apr-util-1.6.3    mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
[root@cjy ~]# cd php-8.3.0
[root@cjy php-8.3.0]# ls
appveyor             configure.ac     pear                 SECURITY.md
benchmark            CONTRIBUTING.md  php.ini-development  tests
build                docs             php.ini-production   travis
buildconf            ext              README.md            TSRM
buildconf.bat        EXTENSIONS       README.REDIST.BINS   UPGRADING
CODEOWNERS           LICENSE          run-tests.php        UPGRADING.INTERNALS
CODING_STANDARDS.md  main             sapi                 win32
configure            NEWS             scripts              Zend

安装依赖包
[root@cjy php-8.3.0]# yum list all|grep php|grep mysql
php-mysqlnd.x86_64                                                            
8.0.30-1.el9_2                                 appstream          
[root@cjy php-8.3.0]# 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@cjy ~]# yum -y install libsqlite3x-devel
忽略依赖安装 rpm --nodeps -hiv 后面接安装的包名
[root@cjy ~]# cd /etc/yum.repos.d/
[root@cjy yum.repos.d]# ls
epel-cisco-openh264.repo  epel-testing.repo  rocky-devel.repo   rocky.repo
epel.repo                 rocky-addons.repo  rocky-extras.repo
[root@cjy yum.repos.d]# vim rocky-devel.repo 
将enabled改成1才能过滤到
[root@cjy yum.repos.d]# yum list all | grep oniguruma
oniguruma.i686                                                                           6.9.6-1.el9.5                                  appstream          
oniguruma.x86_64                                                                         6.9.6-1.el9.5                                  appstream          
oniguruma.x86_64                                                                         6.9.6-1.el9.5                                  devel              
oniguruma-devel.x86_64                                                                   6.9.6-1.el9.5                                  devel              
[root@cjy yum.repos.d]# yum -y install oniguruma-devel
[root@cjy yum.repos.d]# yum list all | grep libzip
libzip.i686                                                                              1.7.3-7.el9                                    appstream          
libzip.x86_64                                                                            1.7.3-7.el9                                    appstream          
libzip.x86_64                                                                            1.7.3-7.el9                                    devel              
libzip-devel.x86_64                                                                      1.7.3-7.el9                                    devel              
libzip-tools.x86_64                                                                      1.7.3-7.el9                                    devel              
[root@cjy yum.repos.d]# yum -y install libzip-devel

编译安装php
[root@cjy]# cd php-8.3.0
[root@cjy php-8.3.0]# ./configure --prefix=/usr/local/php8  \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@cjy php-8.3.0]# make 
[root@cjy php-8.3.0]# make install
安装过程略


安装后配置
[root@cjy ~]# cd php-8.3.0
[root@cjy php-8.3.0]# ls /usr/local/php8/
bin  etc  include  lib  php  sbin  var
[root@cjy php-8.3.0]# echo 'export PATH=/usr/local/php8/bin:/usr/local/php8/sbin:$PATH' > /etc/profile.d/php.sh
[root@cjy php-8.3.0]# source /etc/profile.d/php.sh
[root@cjy php-8.3.0]# ln -s /usr/local/php8/include /usr/include/php
[root@cjy php-8.3.0]# vim /etc/ld.so.conf.d/php.conf
[root@cjy php-8.3.0]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php8/lib
[root@cjy php-8.3.0]# which php
/usr/local/php8/bin/php
[root@cjy php-8.3.0]# php -v
PHP 8.3.0 (cli) (built: Dec 11 2023 17:09:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0, Copyright (c) Zend Technologies
[root@cjy php-8.3.0]# pwd
/root/php-8.3.0

配置php-fpm
[root@cjy php-8.3.0]# ls
appveyor             configure.ac     Makefile.fragments   scripts
benchmark            CONTRIBUTING.md  Makefile.objects     SECURITY.md
build                docs             modules              tests
buildconf            ext              NEWS                 travis
buildconf.bat        EXTENSIONS       pear                 TSRM
CODEOWNERS           include          php.ini-development  UPGRADING
CODING_STANDARDS.md  libs             php.ini-production   UPGRADING.INTERNALS
config.log           libtool          README.md            win32
config.nice          LICENSE          README.REDIST.BINS   Zend
config.status        main             run-tests.php
configure            Makefile         sapi
[root@cjy php-8.3.0]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@cjy php-8.3.0]# ls sapi/fpm/
config.m4       init.d.php-fpm.in  php-fpm.8        php-fpm.service     tests
CREDITS         LICENSE            php-fpm.8.in     php-fpm.service.in  www.conf
fpm             Makefile.frag      php-fpm.conf     status.html         www.conf.in
init.d.php-fpm  php-fpm            php-fpm.conf.in  status.html.in
[root@cjy php-8.3.0]# mkdir /etc/init.d
[root@cjy php-8.3.0]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@cjy php-8.3.0]# ll /etc/init.d/
total 4
-rw-r--r--. 1 root root 2402 Dec 11 17:20 php-fpm
[root@cjy php-8.3.0]# chmod +x /etc/init.d/php-fpm 
[root@cjy php-8.3.0]# ll /etc/init.d
total 4
-rwxr-xr-x. 1 root root 2402 Dec 11 17:20 php-fpm
[root@cjy php-8.3.0]# cd /usr/local/php8/
[root@cjy php8]# ls
bin  etc  include  lib  php  sbin  var
[root@cjy php8]# cd etc
[root@cjy etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@cjy etc]# cp php-fpm.conf.default php-fpm.conf
[root@cjy etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@cjy etc]# cd php-fpm.d/
[root@cjy php-fpm.d]# ls
www.conf.default
[root@cjy php-fpm.d]# cp www.conf.default www.conf
[root@cjy php-fpm.d]# ls
www.conf  www.conf.default
[root@cjy php-fpm.d]# cd

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

设置开机自启
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php server daemon
After=network.target 

[Service]
Type=forking
ExecStart=service php-fpm start
ExecStop=service php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

[root@cjy ~]# service  php-fpm stop
Gracefully shutting down php-fpm . done
[root@cjy ~]# systemctl enable --now php-fpm
[root@cjy ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                  
LISTEN    0         4096             127.0.0.1:9000             0.0.0.0:*                  
LISTEN    0         511                      *:80                     *:*                  
LISTEN    0         128                   [::]:22                  [::]:*                  
LISTEN    0         70                       *:33060                  *:*                  
LISTEN    0         151                      *:3306                   *:*       

[root@cjy ~]# systemctl status php-fpm
● php-fpm.service - php server daemon
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; preset: disabled)
     Active: active (running) since Mon 2023-12-11 17:51:07 CST; 59s ago
    Process: 214583 ExecStart=service php-fpm start (code=exited, status=0/SUCCESS)
   Main PID: 214589 (php-fpm)
      Tasks: 3 (limit: 22879)
     Memory: 7.2M
        CPU: 44ms
     CGroup: /system.slice/php-fpm.service
             ├─214589 "php-fpm: master process (/usr/local/php8/etc/php-fpm.conf)"
             ├─214590 "php-fpm: pool www"
             └─214591 "php-fpm: pool www"

Dec 11 17:51:07 cjy systemd[1]: Starting php server daemon...
Dec 11 17:51:07 cjy service[214587]: Starting php-fpm  done
Dec 11 17:51:07 cjy systemd[1]: Started php server daemon.

4.配置apache

启用代理模块

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

[root@cjy ~]# vim /usr/local/apache/conf/httpd.conf
  • LoadModule proxy_module modules/mod_proxy.so
  • LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
创建虚拟主机目录并生成php测试页面
[root@cjy ~]# cd /usr/local/apache/
[root@cjy apache]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
[root@cjy apache]# cd htdocs/
[root@cjy htdocs]# ls
index.html
[root@cjy htdocs]# mkdir test
[root@cjy htdocs]# ls
index.html  test
[root@cjy htdocs]# cd test/
[root@cjy test]# ls
[root@cjy test]# vim index.php
[root@cjy test]# cat index.php 
<?php
    phpinfo();
?>
[root@cjy ~]# vim /usr/local/apache/conf/httpd.conf
在里面加入下列这行
Include conf/extra/vhosts.conf

[root@cjy ~]# cd /usr/local/apache/conf/
[root@cjy conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@cjy conf]# cd extra/
[root@cjy extra]# ls
httpd-autoindex.conf  httpd-languages.conf           httpd-ssl.conf
httpd-dav.conf        httpd-manual.conf              httpd-userdir.conf
httpd-default.conf    httpd-mpm.conf                 httpd-vhosts.conf
httpd-info.conf       httpd-multilang-errordoc.conf  proxy-html.conf
[root@cjy extra]# vim vhosts.conf
[root@cjy extra]# cat vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/test"
    ServerName www.shuijiao.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/$1
    <Directory "/usr/local/apache/htdocs/test">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>  
    
[root@cjy extra]# cd ..
[root@cjy conf]# pwd
/usr/local/apache/conf
[root@cjy conf]# ls
extra  httpd.conf  magic  mime.types  original
[root@cjy conf]# vim httpd.conf 
搜索AddType,添加以下内容
# 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        #添加此行
再搜索index.html并在其后添加index.php 
    DirectoryIndex index.html index.php

重启apache和php服务
[root@cjy ~]# systemctl restart httpd
[root@cjy ~]# systemctl restart php-fpm
[root@cjy ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*                  
LISTEN    0         4096             127.0.0.1:9000             0.0.0.0:*                  
LISTEN    0         511                      *:80                     *:*                  
LISTEN    0         128                   [::]:22                  [::]:*                  
LISTEN    0         70                       *:33060                  *:*                  
LISTEN    0         151                      *:3306                   *:*                  
[root@cjy ~]# cd /usr/local/apache/htdocs/
[root@cjy htdocs]# ls
index.html  test
[root@cjy htdocs]# cp index.html test/
[root@cjy htdocs]# ls test/
index.html  index.php

验证

1.修改/etc/hosts文件,添加域名与IP的映射
2.在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值