目录
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
上图阶段①中提到了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平台构建
3.1.环境说明
系统版本 | IP | 需要的服务 |
Rocky Linux 9-1 | 192.168.50.150 | httpd-2.4.58 mysql-8.0.35 php-8.3.0 |
3.2.部署Apache
//安装依赖包
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
gcc gcc-c++ make wget
//下载apr、apr-util、httpd软件包
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.4.tar.gz apr-util-1.6.3.tar.gz httpd-2.4.58.tar.gz
//解压apr、apr-util、httpd软件包
[root@localhost ~]# tar xf apr-1.7.4.tar.gz
[root@localhost ~]# tar xf apr-util-1.6.3.tar.gz
[root@localhost ~]# tar xf httpd-2.4.58.tar.gz
//安装apr
[root@localhost ~]# cd apr-1.7.4
[root@localhost apr-1.7.4]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
$RM "$cfgfile" //删除这一行,否则会出现问题
//源码安装三部曲
[root@localhost apr-1.7.4]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.4]# make
[root@localhost apr-1.7.4]# make install
//安装apr-util
[root@localhost ~]# cd apr-util-1.6.3
[root@localhost apr-util-1.6.3]# ./configure --perfix=/usr/local/apr-util \
--with-apr=/usr/local/apr //apr安装后位置
[root@localhost apr-util-1.6.3]# make
[root@localhost apr-util-1.6.3]# make install
apr和apr-util是http编译需要的,所以要先行安装
//编译安装http
[root@localhost ~]# cd httpd-2.4.58
[root@localhost httpd-2.4.58]# ./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.58]# make
[root@localhost httpd-2.4.58]# 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@localhost ~]# vim /usr/lib/systemd/system/httpd.service
[root@localhost ~]# 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@localhost ~]# systemctl daemon-reload
[root@localhost bin]# systemctl enable --now httpd
[root@localhost ~]# 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
[::]:*
3.3.部署Mysql
//安装mysql
[root@localhost ~]# ls
anaconda-ks.cfg apr-util-1.6.3 apr-1.7.4 httpd-2.4.58.tar.gz
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
//创建MySQL用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=991(mysql) gid=991(mysql) groups=991(mysql)
//解压mysql软件包包/usr/local/下并将属主属组更改为mysqls
[root@localhost ~]# tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv mysql-8.0.35-linux-glibc2.28-x86_64 mysql
[root@localhost local]# ls
apache apr-util etc include lib64 apr bin games lib mysql share libexec sbin src
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
total 0
drwxr-xr-x. 9 mysql mysql 129 Dec 11 15:30 mysql
//配置环境变量
[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 mysql]# which mysql
/usr/local/mysql/bin/mysql
[root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql/
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# vim /etc/man_db.conf
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/apache/man
MANDATORY_MANPATH /usr/local/mysql/man
//创建数据存放目录
[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data
//初始化数据库
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data
2023-12-11T07:41:06.735856Z 0 [System] [MY-013169] [Server]
/usr/local/mysql/bin/mysqld (mysqld 8.0.35) initializing of server in progress as
process 99661
2023-12-11T07:41:06.745146Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization
has started.
2023-12-11T07:41:07.037139Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization
has ended.
2023-12-11T07:41:09.713727Z 6 [Note] [MY-010454] [Server] A temporary password is
generated for root@localhost: H)u=xRh?e4)T
//注意这里有初始化后的数据库密码,这里的密码是H)u=xRh?e4)T,建议保存这个密码,否则后期会无法登录数据库
//再次注意,这个密码是随机的,每个人都不一样,一定要记住这个密码
[root@localhost ~]# echo 'H)u=xRh?e4)T' > pass
[root@localhost ~]# cat pass
H)u=xRh?e4)
//再次提醒,这个密码很重要
//生成配置文件
[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-resolv
//配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ls
mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# mkdir /etc/init.d
[root@localhost support-files]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost support-files]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data
//启动mysql服务
[root@localhost ~]# service mysqld start
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
使用systemctl去启动MySQL服务
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.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
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
//修改密码
[root@localhost ~]# cat pass
H)u=xRh?e4)T
[root@localhost ~]# mysql -uroot -p'H)u=xRh?e4)T' //这就是刚刚初始化后显示的密码,如果没有保存那只能遗憾了
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 8
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 '1';
Query OK, 0 rows affected (0.01 sec)
[root@localhost ~]# mysql -uroot -p1
mysql>
3.4.部署PHP
//安装依赖包
[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 readline readline-devel libxslt libxslt-devel php-mysqlnd epel-release libsqlite3x-devel
//下载并解压php安装包
[root@localhost ~]# ls
anaconda-ks.cfg apr-util-1.6.3 apr-1.7.4 x86_64.tar.xz
apr-1.7.4.tar.gz httpd-2.4.58 php-8.3.0.tar.xz
[root@localhost ~]# tar xf php-8.3.0.tar.xz
[root@localhost php-8.3.0]# ls
appveyor configure.ac benchmark build buildconf buildconf.bat CODEOWNERS docs ext EXTENSIONS LICENSE CODING_STANDARDS.md main configure NEWS pear SECURITY.md CONTRIBUTING.md php.ini-devel opment tests php.ini-production README.md README.REDIST.BINS run-tests.php sapi scripts travis TSRM UPGRADING UPGRADING.INTERNALS win32 zend
//编译安装PHP
[root@localhost php-8.3.0]# [root@localhost php-8.3.1]# ./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@localhost php-8.3.1]# make
[root@localhost php-8.3.1]# make install
可能遇到的问题
问题
configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:
Package ‘libxml-2.0’, required by ‘virtual:world’, not found
解决方案
[root@localhostphp-8.3.0]# yum -y install libxml2-devel
问题
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
Package ‘sqlite3’, required by ‘virtual:world’, not found
解决方案
[root@localhost 8.3.0]# yum -y install sqlite-devel
问题
configure: error: Package requirements (oniguruma) were not met:
Package ‘oniguruma’, required by ‘virtual:world’, not found
解决方案
[root@localhost ~]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@localhost ~]# tar xf oniguruma-6.9.4.tar.gz
[root@localhost ~]# cd oniguruma-6.9.4/
[root@localhost oniguruma-6.9.4]# ./autogen.sh && ./configure --prefix=/usr
[root@localhost oniguruma-6.9.4]# make && make install
问题
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 ~]# wget https://libzip.org/download/libzip-1.3.2.tar.gz
[root@localhost ~]# tar xf libzip-1.3.2
[root@localhost ~]# cd libzip-1.3.2
[root@localhost libzip-1.3.2]# ./configure
[root@localhost libzip-1.3.2]# make && make install
//配置环境变量
[root@localhost php-8.3.1]# cd /usr/local/php8/
[root@localhost php8]# ls
bin etc include lib php sbin var
[root@localhost php8]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@localhost php8]# source /etc/profile.d/php8.sh
[root@localhost php8]# which php
/usr/local/php8/bin/php
//配置php-fpm
[root@localhost php-8.3.1]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? yes
[root@localhost php-8.3.1]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.3.1]# chmod +x /etc/init.d/php-fpm
[root@localhost php-8.3.1]# ll -d /etc/init.d/php-fpm
-rwxr-xr-x. 1 root root 2402 Jan 15 16:08 /etc/init.d/php-fpm
[root@localhost php8]# 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服务开机自启
root@localhost ~]# vim /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@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
3.5.配置apache
[root@localhost ~]# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so //添加以下两行
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
//编写php测试页面
[root@localhost ~]# mkdir /usr/local/apache/htdocs/test
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# ls
index.html test
[root@localhost htdocs]# cd test
[root@localhost test]# vim index.php
<?php
phpinfo();
?>
//配置虚拟主机
//在配置文件的最后加入以下内容
[root@localhost ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/test"
ServerName www.yjh.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@localhost conf]# vim httpd.conf
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //添加以下两行内容
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.html index.php //添加index.php
</IfModule
//重启服务
//重启服务
[root@localhost conf]# systemctl restart httpd
[root@localhost conf]# systemctl restart php-fpm
3.6.验证
1.修改/etc/hosts文件,添加域名与IP的映射,如此才能使用设置的域名访问
基本上能看到这个页面lamp架构就没有什么问题