yum部署LAMP架构
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服务器的资源分为两种,静态资源和动态资源
静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求

如上图所示
阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行
阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
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.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方式来加载动态资源
3.web工作流程
通过上面的图说明一下web的工作流程:
客户端通过http协议请求web服务器资源
web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
若是静态资源则直接从本地文件系统取之返回给客户端。
否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。
amp平台构建
环境说明:
| 系统 | IP | 需要安装的服务 |
|---|---|---|
| centos8 | 192.168.71.132 | httpd-2.4.53 mysql-5.7 php php-mysql |
lamp平台软件安装次序:
httpd --> mysql --> php
//创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
[root@localhost ~]# id apache
uid=995(apache) gid=992(apache) groups=992(apache)
//安装依赖包
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
//安装开发工具包
[root@localhost ~]# yum groups mark install 'Development Tools'
//安装常用工具
[root@localhost ~]# yum -y install vim wget make
//清空缓存,有利下载
[root@localhost ~]# yum clean all
//重新建立缓存
[root@localhost ~]# yum makecache
1.安装httpd
按照顺序先安装apr,然后装apr-util,接着安装httpd,因为它们是依赖关系
//下载安装包并解压
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
[root@localhost ~]# tar -xf apr-1.7.0.tar.gz
[root@localhost ~]# tar -xf apr-util-1.6.1.tar.gz
[root@localhost ~]# tar -xf httpd-2.4.53.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.53.tar.gz
apr-1.7.0 apr-util-1.6.1 httpd-2.4.53 initial-setup-ks.cfg
apr-1.7.0
[root@localhost ~]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# vim configure
# $RM "$cfgfile" //将此行加上注释,或者删除此行
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
//编译
[root@localhost apr-1.7.0]# make && make install
apr-util-1.6.1
[root@localhost apr-1.7.0]# cd
[root@localhost ~]# 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-2.4.53
[root@localhost httpd-2.4.53]# ./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.53]# make && make install
配置httpd
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@localhost ~]# source /etc/profile.d/apache.sh
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd
[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# ln -s /usr/local/apache/include /usr/include/apache
[root@localhost apache]# ll /usr/include/|grep apache
lrwxrwxrwx. 1 root root 25 4月 17 17:39 apache -> /usr/local/apache/include
//取消ServerName前面的注释
[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
ServerName gives the name and port that the server uses to identify itself.
//重启
[root@localhost ~]# systemctl status httpd.service
[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 --粘贴并修改/usr/local/apache/man
//设置开机自启 写一个server文件 可以直接复制其他的文件更改
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# ls sshd.service
sshd.service
[root@localhost system]# cp sshd.service httpd.service
[root@localhost system]# cat httpd.service
[Unit]
Description=httpd 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 daemon-reload
[root@localhost system]# cd
[root@localhost ~]# systemctl status httpd.service
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[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; vendor preset: disabled)
Active: active (running) since Thu 2022-04-21 18:29:19 CST; 1min 24s ago
Process: 378141 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 378144 (httpd)
Tasks: 6 (limit: 11070)
Memory: 5.2M
CGroup: /system.slice/httpd.service
├─378144 /usr/local/apache/bin/httpd -k start
├─378145 /usr/local/apache/bin/httpd -k start
├─378146 /usr/local/apache/bin/httpd -k start
├─378147 /usr/local/apache/bin/httpd -k start
├─378148 /usr/local/apache/bin/httpd -k start
[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 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
[root@localhost ~]# systemctl disable --now firewalld.service //关闭防火墙
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//设置永久生效
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled //将enforcing 修改为 disabled
[root@localhost ~]# setenforce 0

2.安装mysql
//下载mysql软件包
[root@localhost ~]# wget https://repo.mysql.com//mysql80-community-release-el7-5.noarch.rpm
//安装
[root@localhost ~]# rpm -ivh mysql80-community-release-el7-5.noarch.rpm
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Stream-AppStream.repo CentOS-Stream-HighAvailability.repo mysql-community.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-Media.repo mysql-community-source.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-PowerTools.repo
CentOS-Stream-Extras.repo CentOS-Stream-RealTime.repo
[root@localhost yum.repos.d]# vim mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1 //把0修改成1 打开
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0 //把1修改成0 关闭
//清理缓存
[root@localhost yum.repos.d]# dnf clean all
21 文件已删除
//建立缓存
[root@localhost yum.repos.d]# dnf makecache
[root@localhost yum.repos.d]# cd
[root@localhost ~]# dnf list all|grep '^mysql' 搜索以mysql开头的包
//安装依赖包
[root@localhost ~]# dnf -y install mysql mysql-common mysql-devel mysql-libs mysql-server
//使用yum安装会自动创建mysql用户,使用二进制安装需要手动创建用户
[root@localhost ~]# id mysql
uid=27(mysql) gid=27(mysql) 组=27(mysql)
//环境变量自动添加
[root@localhost ~]# which mysql
/usr/bin/mysql
//重启就会自动初始化
[root@localhost ~]# ls /var/lib/mysql //无内容
[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 -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 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
//此时已有内容
[root@localhost ~]# ls /var/lib/mysql
auto.cnf client-key.pem ib_logfile1 mysql.sock.lock public_key.pem
binlog.000001 '#ib_16384_0.dblwr' ibtmp1 mysql_upgrade_info server-cert.pem
binlog.index '#ib_16384_1.dblwr' '#innodb_temp' mysqlx.sock server-key.pem
ca-key.pem ib_buffer_pool mysql mysqlx.sock.lock sys
ca.pem ibdata1 mysql.ibd performance_schema undo_001
client-cert.pem ib_logfile0 mysql.sock private_key.pem undo_002
//创建时没有密码
[root@localhost ~]# grep 'password' /var/log/mysql/mysqld.log
2022-04-21T11:30:32.315346Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
//可以直接登录
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution
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>
//设置密码
mysql> ALTER USER root@localhost IDENTIFIED BY 'runtime123!';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
安装PHP
下载rpm包
[root@localhost ~]# dnf -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 ~]# wget https://www.php.net/distributions/php-7.4.29.tar.xz
注:如果遇到缺少部分包得到情况下就到https://pkgs.org/网站下载所需要的包
编译安装php
[root@localhost ~]# tar -xf php-7.4.29.tar.xz //解压文件
[root@localhost ~]# cd php-7.4.29 //进入文件
[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php7 \
--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
//开始编译
[root@localhost php-7.4.29]# make install
配置php
//设置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost ~]# source /etc/profile.d/php7.sh
[root@localhost ~]# which php
/usr/local/php7/bin/php
[root@localhost php-7.4.29]# php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
win10
为真机的host文件添加ip对应域名做映射
虚拟机ip :192.168.71.132
域名: test.example.com

配置apache
启用代理模块
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件
//启用httpd的相关模块
[root@localhost ~]# cd /usr/local/apache/conf/
[root@localhost conf]# vim httpd.conf
//取消以下两行内容的注释:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
配置虚拟主机
//创建虚拟主机目录并生成php测试页面
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mkdir test.com
[root@localhost htdocs]# ls
index.html test.com
[root@localhost htdocs]# cd test.com/
[root@localhost test.com]# ls
[root@localhost test.com]# vim index.php
[root@localhost test.com]# cat index.php
<?
phpinfo();
?>
//配置php-fpm
//因为php.ini默认是存在的,所以加上\覆盖
[root@localhost php-7.4.29]# \cp php.ini-production /etc/php.ini
[root@localhost php-7.4.29]# cp sapi/fpm/php-fpm /etc/init.d/php-fpm
[root@localhost etc]# pwd
/usr/local/php7/etc
[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-fpm
[root@localhost php-7.4.29]# service php-fpm start
Starting php-fpm done
[root@localhost php-7.4.29]# 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 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
[root@localhost php-7.4.29]# pkill php-fpm
[root@localhost php-7.4.29]# 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 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
//设置开机自启
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service php-fpm.service
[root@localhost system]# cat php-fpm.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target
[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl status php-fpm.service
● php-fpm.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:sshd(8)
man:sshd_config(5)
//启动php
[root@localhost system]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@localhost system]# 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 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
[root@localhost apache]# pwd
/usr/local/apache
[root@localhost apache]# vim conf/httpd.conf //在文件最后添加如下内容
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/test.com"
ServerName test.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test.com/$1
<Directory "/usr/local/apache/htdocs/test.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//搜索 AddType 在后面添加如下两行:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
//搜索 index.html ,在前面添加index.php,网页默认连接前面的
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>

浙公网安备 33010602011771号