LAMP 架构介绍及环境搭建

2022-09-28 19:29:32 浏览数 (1)

1、LAMP 架构介绍及环境搭建

1.LAMP分别代表什么?

  • L代表服务器操作系统使用Linux
  • A代表网站服务使用的是Apache软件基金会中的httpd软件
  • M代表网站后台使用的数据库是MySQL数据库
  • P代表网站是使用PHP/Perl/Python等语言开发

2.web服务器工作流程介绍

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

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

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

如图所示

  • ①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行
  • ②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
2.1cgi与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上下文切换而导致耗时

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方式来加载动态资源

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

192.168.111.138

httpd-2.4 mysql-5.7 php php-mysql

lamp平台软件安装次序:

代码语言:javascript复制
    httpd --> mysql --> php

注意:php要求httpd使用prefork MPM

3.1 安装httpd
代码语言:javascript复制
##配置centos源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

##配置epel源
[root@100 ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@100 ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@100 ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

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

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

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

##下载源码包
[root@localhost ~]# cd /usr/src/
[root@localhost src]#wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2
[root@localhost src]#wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@localhost src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2
[root@localhost src]#ls
apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  debug  httpd-2.4.54.tar.bz2  kernels

##安装apr
[root@localhost src]#tar -xf apr-1.6.5.tar.bz2
[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 && make install

##安装apr-util
[root@localhost src]#tar -xf apr-util-1.6.1.tar.bz2 
[root@localhost src]#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 && make install

##安装httpd
[root@study src]#tar -xf httpd-2.4.54.tar.bz2 
[root@study src]#cd httpd-2.4.54/
[root@study httpd-2.4.54]#./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.54]# make && make install

##安装后配置
[root@localhost httpd-2.4.54]# echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@localhost httpd-2.4.54]# source /etc/profile.d/httpd.sh
[root@localhost httpd-2.4.54]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost httpd-2.4.54]# vim /etc/man_db.conf
.....
MANDATORY_MANPATH                       /usr/local/apache/man
.....

##取消ServerName前面的注释
[root@localhost httpd-2.4.54]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

##systemctl管理httpd服务
[root@localhost httpd-2.4.54]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service httpd.service
[root@localhost system]# vim httpd.service 
[Unit]
Description=apache server daemon
After=network.target sshd-keygen.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@localhost system]# systemctl start httpd
[root@localhost system]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost system]# ss -anlt | grep 80
LISTEN 0      128                *:80              *:* 

访问

安装完成之后,访问Apache服务,出现默认首页,说明安装成功。

3.2 安装mysql
代码语言:javascript复制
##安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake

##创建mysql用户和组
[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.38-linux-glibc2.12-x86_64.tar.gz

##解压软件至/usr/local/
[root@localhost src]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

##设置软链接并修改属主和属组
[root@localhost usr]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.37-linux-glibc2.12-x86_64/'
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll -d mysql*
lrwxrwxrwx. 1 mysql mysql  36 Aug  2 21:08 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Aug  2 21:05 mysql-5.7.37-linux-glibc2.12-x86_64

##添加环境变量
[root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh 
[root@localhost mysql]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin
#头文件—include
[root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql

#库文件—lib
[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@localhost mysql]# ldconfig

#man文件路径
[root@localhost mysql]# vim /etc/man_db.conf
......
MANDATORY_MANPATH                       /usr/local/mysql/man
......

##建立数据存放目录
[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/
[root@localhost mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug  2 21:16 data

##初始化数据库
[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2022-08-02T13:17:30.732811Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-02T13:17:30.907500Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-02T13:17:30.932432Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-02T13:17:30.997587Z 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: 76a6881f-1265-11ed-8422-000c29e16842.
2022-08-02T13:17:30.998294Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-08-02T13:17:31.495168Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T13:17:31.495186Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T13:17:31.495869Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-02T13:17:31.601283Z 1 [Note] A temporary password is generated for root@localhost: o!Ki52Mu_hhh
//请注意,这个命令的最后会生成一个临时密码,此处密码是o!Ki52Mu_hhh
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
[root@localhost mysql]# echo 'o!Ki52Mu_hhh' > passwd

##配置mysql
[root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# ldconfig 

##生成配置文件
[root@localhost mysql]# 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 mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data
[root@localhost mysql]# cd /usr/lib/systemd/system
[root@localhost system]# cp httpd.service mysqld.service
[root@localhost system]# vim mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

##启动mysql
[root@localhost system]# systemctl start mysqld
[root@localhost system]# systemctl enable mysqld
Synchronizing state of mysqld.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost system]# ss -anlt | grep 3306 
LISTEN 0      80                 *:3306            *:* 

##修改密码
[root@localhost mysql]# mysql -p 
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
//解决办法
[root@localhost mysql]# yum whatprovides libncurses.so.5
Last metadata expiration check: 1:12:26 ago on Tue 02 Aug 2022 08:23:55 PM CST.
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
Repo        : base
Matched from:
Provide    : libncurses.so.5

[root@localhost mysql]# yum -y install ncurses-compat-libs
//进入数据库设置新密码
[root@localhost ~]# mysql -uroot -p'o!Ki52Mu_hhh'
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.37

Copyright (c) 2000, 2022, 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> set password = password('Passwd123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
//开启远程访问权限
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH  PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user from user;
 ----------- --------------- 
| host      | user          |
 ----------- --------------- 
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
 ----------- --------------- 
3 rows in set (0.00 sec)
3.3 安装php
代码语言:javascript复制
##安装依赖包
[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

##下载php并解压
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-7.4.30.tar.xz
[root@localhost src]# tar -xf php-7.4.30.tar.xz 

##编译安装php
[root@study src]#cd php-7.4.30/
[root@localhost php-7.4.30]# ./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

遇到以下问题的解决方法

configure: error: Package requirements (sqlite3 > 3.7.4) were not met: Package 'sqlite3', required by 'virtual:world', not found

代码语言:javascript复制
[root@localhost php-7.4.30]# yum -y install sqlite-devel

configure: error: Package requirements (oniguruma) were not met: Package 'oniguruma', required by 'virtual:world', not found

代码语言:javascript复制
[root@localhost php-7.4.30]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

configure: error: Package requirements (libcurl >= 7.15.5) were not met: Package 'libcurl', required by 'virtual:world', not found

代码语言:javascript复制
[root@localhost php-7.4.30]# yum install -y libcurl-devel.x86_64

error: Please reinstall readline - I cannot find readline.h

代码语言:javascript复制
[root@localhost php-7.4.30]# yum -y install readline-devel

当出现如图所示情况

代码语言:javascript复制
//因版本过新导致可选编译项名称不一致
[root@localhost php-7.4.30]# ./configure --help | grep gd
  --with-gdbm[=DIR]       DBA: GDBM support
  --enable-gd             Include GD support
  --with-external-gd      Use external libgd
  --with-webp             GD: Enable WEBP support (only for bundled libgd)
  --with-jpeg             GD: Enable JPEG support (only for bundled libgd)
  --with-xpm              GD: Enable XPM support (only for bundled libgd)
                          libgd)
  --enable-gd-jis-conv    GD: Enable JIS-mapped Japanese font support (only
                          for bundled libgd)
[root@localhost php-7.4.30]# ./configure --help | grep jpeg
  --with-jpeg             GD: Enable JPEG support (only for bundled libgd)
[root@localhost php-7.4.30]# ./configure --help | grep png
[root@localhost php-7.4.30]# ./configure --help | grep freetype
  --with-freetype         GD: Enable FreeType 2 support (only for bundled
[root@localhost php-7.4.30]# ./configure --help | grep zip
  --with-zip              Include Zip read/write support
  
##找到相应的可选编译项后,继续编译
[root@localhost php-7.4.30]# ./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 
--enable-gd 
--with-jpeg 
--with-zlib-dir 
--with-freetype 
--with-gettext 
--enable-json 
--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
//报错
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:

Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found

解决方案

[root@localhost php-7.4.30]# yum -y install libzip-devel

如图表示PHP模块启用成功

继续编译安装

代码语言:javascript复制
[root@localhost php-7.4.30]# make && make install

安装后配置

代码语言:javascript复制
[root@localhost php-7.4.30]# cd /usr/local/php7/
[root@localhost php7]# ls
bin  etc  include  lib  php  sbin  var
[root@localhost php7]# echo 'export PATH=$PATH:/usr/local/php7/bin' > /etc/profile.d/php7.sh
[root@localhost php7]# source /etc/profile.d/php7.sh
[root@localhost php7]# ln -s /usr/local/php7/include/ /usr/include/php7
[root@localhost php7]# echo '/usr/local/php7/lib/' > /etc/ld.so.conf.d/php7.conf
[root@localhost php7]# ldconfig

##配置php-fpm
[root@localhost php7]# cd /usr/src/php-7.4.30
[root@localhost php-7.4.30]# cp php.ini-production /etc/php.ini 
[root@localhost php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.30]# chmod  x /etc/init.d/php-fpm
[root@study /usr/src/php-7.4.30]#cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@study /usr/src/php-7.4.30]#cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@study /usr/src/php-7.4.30]#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
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp httpd.service php-fpm.service
[root@localhost system]# vim php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target

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

[Install]
WantedBy=multi-user.target

[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start php-fpm
[root@localhost system]# systemctl enable php-fpm
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@localhost system]# ss -anlt | grep 9000
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*  

4.配置 apache

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
代码语言:javascript复制
##启用httpd的相关模块
[root@localhost ~]# vim /etc/httpd24/httpd.conf
//取消以下两行内容的注释
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
4.2 配置虚拟主机

在需要使用fcgi的虚拟主机中添加类似如下两行:

代码语言:javascript复制
ProxyRequests Off       //关闭正向代理
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

例如:

代码语言:javascript复制
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/var/www/html/idfsoft.com/$1

注意:

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

代码语言:javascript复制
##创建虚拟主机目录并生成php测试页面
[root@localhost ~]# mkdir /usr/local/apache/htdocs/runtime
[root@localhost ~]# vim /usr/local/apache/htdocs/runtime/index.php
<?php
   phpinfo();
?>
[root@localhost ~]#chown -R apache.apache /usr/local/apache/htdocs/runtime/
[root@localhost ~]#vim /etc/httpd24/httpd.conf
# Virtual hosts
Include /etc/httpd24/extra/httpd-vhosts.conf  #取消注释
[root@localhost ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/runtime"
    ServerName runtime.example.com
    ErrorLog "logs/runtime.example.com-error_log"
    CustomLog "logs/runtime.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/runtime/$1
    <Directory "/usr/local/apache/htdocs/runtime">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>


[root@localhost ~]# vim /etc/httpd24/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        #添加此行

//搜索dir_module,添加index.php
<IfModule dir_module>
    DirectoryIndex index.php index.html        
</IfModule>

[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# ss -anlt
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              127.0.0.1:9000              0.0.0.0:*                  
LISTEN    0         128                   [::]:22                   [::]:*                  
LISTEN    0         80                       *:3306                    *:*                  
LISTEN    0         128                      *:80                      *:*   

5.验证

修改/etc/hosts文件,添加域名与 IP 的映射。 将 Windows 系统下的C:WindowsSystem32driversetchosts文件移至桌面,使用记事本打开,添加192.168.111.138 runtime.example.com,再移回。

在浏览器上使用域名访问,若看到以下界面则表示 lamp 架构搭建成功

0 人点赞