lamp架构

lamp架构介绍

现如今打开浏览器,搜索LAMP关键词,出现大量的关于LAMP的介绍,包括LAMP的一键脚本、LAMP的yum安装、LAMP的编译安装,但是对于一个非开发或非专业人员有可能根据网络参考资源实现LAMP的搭建并成功运行各种服务,也有部分人员完全照搬某些博客知识进行搭建,最后以失败告终,因此抱怨互联网资源不够成熟,其实根本原因并非如此,主要原因如下:

初学者对LAMP架构原理不熟悉

初学者实验时所用系统和软件版本和某些博客资源并不相同

因此导致大量初学者以失败告终,其实只有了解并掌握LAMP的工作原理才能轻松的搭建成功,其次参数的配置都是次要因素,因为互联网上拥有大量的参考资料供查询,下面将逐一介绍

L:很显然L代表Linux系统,但此L需注意系统的版本号,如Centos6.9或Centos7.3;

A:表示apache,在传统行业中,多数采用Apache服务器,因此也很有必要了解学习Apache;

M:表示数据库,多数采用mysql或mariadb,作为专业的数据库工程师需经多年的历练;

P:表示PHP、python、perl等等编程语言。

在这里注意讲解Linux+apache+mariadb+PHP组合的架构,架构图如下:

lamp架构_vim

 

根据上图中访问数据流可知,处理一次动态页面请求,服务器主要经历:Apache处理请求——通过CGI接口访问PHP的的应用程序——PHP应用程序调用PHP解释器执行PHP代码——PHP程序访问调用数据库——最后给客户做反馈。

故在LAMP的环境机构中,apache、mariadb和php的主要功能分别如下。

lamp架构_php程序_02

 

apache主要实现如下功能:

第一:处理http的请求、构建响应报文等自身服务;

第二:配置让Apache支持PHP程序的响应(通过PHP模块或FPM);

第三:配置Apache具体处理php程序的方法,如通过反向代理将php程序交给fcgi处理。

mariadb主要实现如下功能:

第一:提供PHP程序对数据的存储;

第二:提供PHP程序对数据的读取(通常情况下从性能的角度考虑,尽量实现数据库的读写分离)。

php主要实现如下功能:

第一:提供apache的访问接口,即CGI或Fast CGI(FPM);

第二:提供PHP程序的解释器;

第三:提供mairadb数据库的连接函数的基本环境。

由此可知,要实现LAMP在配置每一个服务时,安装功能需求进行配置,即可实现LAMP的架构,当然apache、mariadb和php服务都可配置为独立服务,安装在不同服务器之上。


lapm架构安装流程

首先下载最新版本

[root@localhost ~]# wget https://dlcdn.apache.org/httpd/httpd-2.4.49.tar.gz
--2021-09-24 12:13:22-- https://dlcdn.apache.org/httpd/httpd-2.4.49.tar.gz
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:9421895 (9.0M) [application/x-gzip]
正在保存至: “httpd-2.4.49.tar.gz”

httpd-2.4.49.tar.gz 100%[======================>] 8.99M 2.09MB/s 用时 4.3s

2021-09-24 12:13:47 (2.09 MB/s) - 已保存 “httpd-2.4.49.tar.gz” [9421895/9421895])

[root@localhost ~]# wget https://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
--2021-09-24 12:14:13-- https://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1093896 (1.0M) [application/x-gzip]
正在保存至: “apr-1.7.0.tar.gz”

apr-1.7.0.tar.gz 14%[==> ] 150.97K 45.3KB/s 用时 3.3s

2021-09-24 12:20:09 (45.3 KB/s) - 在 154593/1093896 字节处发生读取错误 (在 pull 函数中出错。)。重试中。

--2021-09-24 12:20:10-- (尝试次数: 2) https://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... 失败:拒绝连接。
正在连接 dlcdn.apache.org (dlcdn.apache.org)|2a04:4e42::644|:443... 失败:网络不可达。
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 206 Partial Content
长度:1093896 (1.0M),剩余 939303 (917K) [application/x-gzip]
正在保存至: “apr-1.7.0.tar.gz”

apr-1.7.0.tar.gz 100%[+++===================>] 1.04M 130KB/s 用时 7.0s

2021-09-24 12:20:33 (130 KB/s) - 已保存 “apr-1.7.0.tar.gz” [1093896/1093896])

[root@localhost ~]# wget https://dlcdn.apache.org/apr/apr-util-1.6.1.tar.gz
--2021-09-24 12:20:46-- https://dlcdn.apache.org/apr/apr-util-1.6.1.tar.gz
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:554301 (541K) [application/x-gzip]
正在保存至: “apr-util-1.6.1.tar.gz”

apr-util-1.6.1.tar.gz 100%[======================>] 541.31K 903KB/s 用时 0.6s

2021-09-24 12:20:48 (903 KB/s) - 已保存 “apr-util-1.6.1.tar.gz” [554301/554301])

[root@localhost ~]# ls
公共 图片 音乐 apr-1.7.0.tar.gz initial-setup-ks.cfg
模板 文档 桌面 apr-util-1.6.1.tar.gz
视频 下载 anaconda-ks.cfg httpd-2.4.49.tar.gz

解压

[root@localhost src]# tar xf apr-1.7.0.tar.gz
[root@localhost src]# tar xf apr-util-1.6.1.tar.gz
[root@localhost src]# tar xf httpd-2.4.49.tar.gz
[root@localhost src]# ls
apr-1.7.0 apr-util-1.6.1 debug httpd-2.4.49.tar.gz
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.49 kernels

[root@localhost apr-1.7.0]# vim configure
31878 cfgfile=${ofile}T
31879 trap "$RM \"$cfgfile\"; exit 1" 1 2 15
31880 # $RM "$cfgfile" 将此行加上注释,或者删除此行
31881
31882 cat <<_LT_EOF >> "$cfgfile"

[root@localhost apr-1.7.0]# ls /etc/yum.repos.d/epel-modular.repo epel-testing-modular.repo zs.repo
epel-playground.repo epel-testing.repo
epel.repo redhat.repo
[root@localhost apr-1.7.0]# 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.
33 文件已删除
[root@localhost apr-1.7.0]# 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.
Extra Packages for Enterprise Linux 563 kB/s | 955 kB 00:01
Extra Packages for Enterprise Linux 616 kB/s | 10 MB 00:17
BaseOS 164 MB/s | 2.3 MB 00:00
AppStream 126 MB/s | 5.8 MB 00:00
元数据缓存已建立。

// 安装开发工具包
[root@localhost apr-1.7.0]# yum groups mark install 'Development Tools' -y
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:01:26 前,执行于 2021年09月23日 星期四 17时49分10秒。
依赖关系解决。
=====================================================================
软件包 架构 版本 仓库 大小
=====================================================================
安装组:
Development Tools


事务概要
=====================================================================

完毕!

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

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

// 编译安装apr-1.7.0、apr-util-1.6.1、httpd-2.4.49
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make && make install
[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
[root@localhost httpd-2.4.49]# ./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@localhost httpd-2.4.49]# make && make install

// 安装后配置
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# vim /etc/man_db.conf
20 MANDATORY_MANPATH /usr/man
21 MANDATORY_MANPATH /usr/share/man
22 MANDATORY_MANPATH /usr/local/share/man
23 MANDATORY_MANPATH /usr/local/apache/man 把apache加进去

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

//启动apache
[root@localhost ~]# apachectl start
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 *:3306 *:*

// 配置开机自启
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/httpd.service
[root@localhost ~]# vim /usr/lib/systemd/system/httpd.service
[root@localhost ~]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=Httpd server daemon
Documentation=man:httpd(8)
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 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl status httpd.service
● httpd.service - Httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>
Active: active (running) since Thu 2021-09-23 18:47:11 CST; 10s a>
Docs: man:httpd(8)


安装mysql

下载mysql软件包

[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
[root@localhost src]# ls
apr-1.7.0 httpd-2.4.49
apr-1.7.0.tar.gz httpd-2.4.49.tar.gz
apr-util-1.6.1 kernels
apr-util-1.6.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
debug

创建用户和组

[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql

解压软件至/usr/local/

[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql

修改目录/usr/local/mysql的属主属组

[root@localhost local]# chown -R mysql.mysql mysql/

添加环境变量

[root@localhost ~]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# vim /etc/man_db.conf
20 MANDATORY_MANPATH /usr/man
21 MANDATORY_MANPATH /usr/share/man
22 MANDATORY_MANPATH /usr/local/share/man
23 MANDATORY_MANPATH /usr/local/apache/man
24 MANDATORY_MANPATH /usr/local/mysql/man 把mysql加进去
[root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql/

写一个配置文件,告诉库文件lib在/usr/local/mysql下面

[root@localhost ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ~]# ldconfig

建立数据存放目录

[root@localhost ~]# ldconfig
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/

初始化数据库

[root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data
2021-09-23T11:04:41.643189Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-23T11:04:41.799190Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-09-23T11:04:41.821815Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-09-23T11:04:41.875518Z 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: 0d6341e8-1c5e-11ec-b0eb-000c29f6d306.
2021-09-23T11:04:41.876088Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-09-23T11:04:42.525643Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-23T11:04:42.602903Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

配置mysql

[root@localhost ~]# cat /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 ~]# vim /usr/local/mysql/support-files/mysql.server
46 basedir=/usr/local/mysql
47 datadir=/opt/data
[root@localhost ~]# cp /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql 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

启动mysql

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now mysqld.service
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 *:3306 *:*
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; >
Active: active (running) since Thu 2021-09-23 19:14:13 CST; 13s a>
Process: 673923 ExecStart=/usr/local/mysql/support-files/mysql.ser>
Main PID: 673945 (mysqld_safe)

修改密码

[root@localhost ~]# yum -y install ncurses-compat-libs
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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('zs123!');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> exit
Bye


安装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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
[root@localhost ~]# yum -y install php-mysqlnd

在php.net网站上下载php软件包,然后传到/usr/src下面去,再解压

[root@localhost src]# ls
apr-1.7.0 httpd-2.4.49
apr-1.7.0.tar.gz httpd-2.4.49.tar.gz
apr-util-1.6.1 kernels
apr-util-1.6.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
debug php-8.0.10.tar.xz
[root@localhost src]# tar xf php-8.0.10.tar.xz

编译安装php

[root@localhost php-8.0.10]# yum -y install libsqlite3x-devel
[root@localhost src]# yum install autoconf automake libtool -y
[root@localhost src]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@localhost src]# tar xf oniguruma-6.9.4.tar.gz
[root@localhost src]# cd oniguruma-6.9.4/
[root@localhost src]# ./autogen.sh && ./configure --prefix=/usr
[root@localhost src]# make && make install
[root@localhost php-8.0.10]# yum -y install libzip-devel
[root@localhost php-8.0.10]# ./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
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.
[root@localhost php-8.0.10]# make && make install

安装后配置

[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost ~]# source /etc/profile.d/php.sh
[root@localhost php-8.0.10]# which php
/usr/local/php8/bin/php
[root@localhost php-8.0.10]# php -v
PHP 8.0.10 (cli) (built: Sep 23 2021 20:29:40) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies

配置php-fpm

[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@localhost php-8.0.10]# cd sapi/
[root@localhost sapi]# cp fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost sapi]# chmod +x /etc//init.d/php-fpm
[root@localhost php-8.0.10]# cd /usr/local/php8/
[root@localhost php8]# cd etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf

启动php-fpm

[root@localhost ~]# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now php-fpm
[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 80 *:3306 *:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*


配置apache

启用代理模块

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

启用httpd的相关模块

[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
119 #LoadModule remoteip_module modules/mod_remoteip.so
120 LoadModule proxy_module modules/mod_proxy.so 取消注释
121 #LoadModule proxy_connect_module modules/mod_proxy_connect.so
122 #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
123 #LoadModule proxy_http_module modules/mod_proxy_http.so
124 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 取消注释
125 #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so


配置虚拟主机

创建虚拟主机目录并生成php测试页面

[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# cd htdocs/
[root@localhost htdocs]# mkdir zs
[root@localhost htdocs]# cat zs/index.php
<?php
phpinfo();
?>
[root@localhost apache]# chown -R apache.apache /usr/local/apache/htdocs/

[root@localhost apache]# vim conf/httpd.conf

在配置文件的最后加入以下内容

514 <VirtualHost *:80>
515 DocumentRoot "/usr/local/apache/htdocs/wjj"
516 ServerName www.zs.com
517 ProxyRequests Off
518 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/a pache/htdocs/zs/$1
519 <Directory "/usr/local/apache/htdocs/zs">
520 Options none
521 AllowOverride none
522 Require all granted
523 </Directory>
524 </VirtualHost>

搜索AddType,添加以下内容

397 AddType application/x-compress .Z
398 AddType application/x-gzip .gz .tgz
399 AddType application/x-httpd-php .php
400 AddType application/x-httpd-php-source .phps

260 <IfModule dir_module>
261 DirectoryIndex index.php index.html 加上index.php
262 </IfModule>

重启apache服务

[root@localhost apache]# systemctl restart httpd.service


验证:

lamp架构_vim_03