lamp简介
有了前面学习的知识的铺垫,今天可以来学习下第一个常用的web架构了。
所谓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服务器的资源分为两种,静态资源和动态资源
静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
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-2.4
mysql-5.7
php
php-mysql
系统平台 | IP |
---|---|
centos7 centos7 | 192.168.126.153 |
lamp平台软件安装次序:
httpd --> mysql --> php
注意:php要求httpd使用prefork MPM
安装httpd
//下载和解压httpd、apr以及apr-util
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.tar.gz
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@localhost src]# tar xf httpd-2.4.49.tar.gz
[root@localhost src]# tar xf apr-util-1.6.1.tar.gz
[root@localhost src]# tar xf apr-1.7.0.tar.gz
[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"
// 安装EPEL rpm 包
[root@localhost apr-1.7.0]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
[root@localhost apr-1.7.0]# ls /etc/yum.repos.d/epel-modular.repo epel-testing-modular.repo wjj.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
// 安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
// 下载mysql软件包
[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('zrz!');
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:*
文件
//创建虚拟主机目录并生成php测试页面
[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# cd htdocs/
[root@localhost htdocs]# mkdir mkf
[root@localhost htdocs]# cat mkf/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/zrz"
516 ServerName www.mkf.com
517 ProxyRequests Off
518 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/a pache/htdocs/zrz/$1
519 <Directory "/usr/local/apache/htdocs/zrz">
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