LAMP

1.LAMP架构

1.1 LAMP分别代表什么?

在这里插入图片描述

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

1.2 Apache/MySQL/PHP各自有什么作用?

1.2.1 Apache(httpd)-----像极了饭店前台

在这里插入图片描述

  • 作用:提供web服务,接受用户的连接请求
  • 注意:Apache或Nginx都只支持静态页面的解析

当客户端请求的是静态资源时,web服务(httpd程序),会直接返回静态资源给客户端

①静态网页

  • 静态网页指使用HTML(超文本标记语言)编写,一般后缀为.htm,.html等;网页文件中没有程序代码。
  • 静态页面,用户双击打开,看到的效果与web服务器是相同的,因为网页的内容在用户访问之前就已经确定。

②动态网页

  • 动态网页指网站使用特定的编程语言编写,网页文件中除了HTML标记以外,还包括一些实现特定功能的程序代码。
  • 服务端可以根据客户端的不同请求动态产生网页内容
  • 动态网页后缀一般为.php .asp .aspx .cgi .perl .jsp等
  • 常见的留言板,论坛,注册,发帖都是用动态网页实现的

1.2.2 MySQL数据库 - 像极了厨师

在这里插入图片描述

  • 作用:MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品。其主要作用用于永久的存储数据

1.2.3 PHP - 像极了服务生

在这里插入图片描述

  • 作用:PHP主要负责PHP脚本程序的解析以及实现与MySQL数据库的交互工作,动态页面中的注册/登陆/下单/支付等大多数功能都是基于PHP+MySQL进行实现。PHP是一种通用开源脚本语言。
    当客户端请求的是动态资源时,apache(httpd程序)会调用libphpX.so模块进行相应的解析
    如果解析处理需要用到后台数据库相关数据,此时php程序也会连接后台数据库
    最终php程序将解析后的结果返回给apache(httpd程序),让apache返回给客户端

1.2.4 LAMP架构是什么?- 像极了饭店

在这里插入图片描述
在这里插入图片描述

1.3 web服务器工作流程

在这里插入图片描述

1.3.1 web工作流程

  • 客户端通过http协议请求web服务器资源
  • web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
    • 若是静态资源则直接从本地文件系统取之返回给客户端。
    • 否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。
    • 在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。

2.LAMP平台构建

2.1 环境说明

系统平台IP服务
redhat8192.168.129.250httpd mysql php

2.2 项目实施

2.2.1 安装httpd

apache官网

1)关闭防火墙

[root@localhost ~]# systemctl disable --now firewalld.service 
[root@localhost ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config 
[root@localhost ~]# setenforce 0

2)配置基础环境

//配置yum源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

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

//安装包组
[root@localhost ~]# yum groups mark install 'Development Tools'
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache

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


//下载及解压apr包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz 
[root@localhost src]# tar -xf apr-1.7.0.tar.gz

//下载及解压apr-util包
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz 
[root@localhost src]# tar -xf apr-util-1.6.1.tar.gz

//下载及解压httpd包
[root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.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

3)安装apr

//删除configure中此行
[root@localhost src]# cd apr-1.7.0
[root@localhost apr-1.7.0]# sed -i '/$RM "$cfgfile"/d' configure

//编译
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
编译过程略...

//安装
[root@localhost apr-1.7.0]# make && make install
安装过程略...

4)安装apr-util

//编译
[root@localhost apr-1.7.0]# 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
安装过程略...

5)安装httpd

//编译
[root@localhost apr-util-1.6.1]# cd ../httpd-2.4.49
[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
安装过程略...

6)配置文件

//配置环境变量
[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 ~]# which httpd
/usr/local/apache/bin/httpd

//头文件、man帮助
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config

//取消ServerName前面的注释,避免出现报错
[root@localhost ~]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf

//编写服务启动文件.service文件
[root@localhost ~]# cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=Httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl start
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

//重新加载
[root@localhost ~]# systemctl daemon-reload

//开启httpd
[root@localhost ~]# systemctl enable --now httpd
[root@localhost ~]# systemctl status httpd
● httpd.service - Httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-09-24 00:43:27 CST; 4s ago
  Process: 153508 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 153524 (httpd)
    Tasks: 6 (limit: 11300)
   Memory: 5.0M
   CGroup: /system.slice/httpd.service
           ├─153524 /usr/local/apache/bin/httpd -k start
           ├─153525 /usr/local/apache/bin/httpd -k start
           ├─153526 /usr/local/apache/bin/httpd -k start
           ├─153527 /usr/local/apache/bin/httpd -k start
           ├─153528 /usr/local/apache/bin/httpd -k start
           └─153529 /usr/local/apache/bin/httpd -k start

Sep 24 00:43:27 localhost systemd[1]: Starting Httpd server daemon...
Sep 24 00:43:27 localhost systemd[1]: Started Httpd server daemon.

//查看端口
[root@localhost ~]# ss -anlt
State           Recv-Q          Send-Q                   Local Address:Port                    Peer Address:Port          
LISTEN          0               128                            0.0.0.0:22                           0.0.0.0:*             
LISTEN          0               128                                  *:80                                 *:*             
LISTEN          0               128                               [::]:22                              [::]:*       

2.2.2安装mysql

MYSQL官网

1)安装依赖包

[root@localhost ]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel  

//安装软件包才可以使用MySQL命令
[root@localhost ]# yum -y install ncurses-compat-libs

//创建用户
[root@localhost ]# useradd -r -M -s /sbin/nologin mysql

//下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls mysql*
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

//解压
[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# ls /usr/local/
apache  apr-util  etc    include  lib64    mysql-5.7.34-linux-glibc2.12-x86_64  share
apr     bin       games  lib      libexec  sbin                                 src

//创建软连接
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64/'
[root@localhost local]# ll mysql*
total 0
lrwxrwxrwx.  1 root root  36 Sep 24 10:24 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Sep 24 10:24 mysql-5.7.34-linux-glibc2.12-x86_64

//修改属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]#  ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Sep 24 10:24 /usr/local/mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/

//添加环境变量
[root@localhost ~]# ls /usr/local/mysql
LICENSE  README  bin  docs  include  lib  man  share  support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql

//头文件(include)、读取lib
[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig

//创建数据存放目录
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
total 0

//初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
2021-09-24T02:31:32.260452Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-24T02:31:32.442683Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-09-24T02:31:32.468911Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-09-24T02:31:32.485203Z 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: 87e4e2f0-1cdf-11ec-986c-000c29209bda.
2021-09-24T02:31:32.485988Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-09-24T02:31:32.987862Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-09-24T02:31:32.987895Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-09-24T02:31:32.988438Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-24T02:31:33.145541Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
#这种方法初始化完成后是没有密码

//生成配置文件
[root@localhost ~]# cat > /etc/my.cnf <<EOF
[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
EOF

//配置服务启动脚本
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//配置service服务启动文件
[root@localhost ~]# cat > /usr/lib/systemd/system/mysqld.service <<EOF
[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
EOF

//重新加载
[root@localhost ~]# systemctl daemon-reload

//启动mysql
[root@localhost ~]# systemctl enable --now mysqld

//查看端口
[root@localhost ~]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                    Peer Address:Port          
LISTEN          0               128                            0.0.0.0:22                           0.0.0.0:*             
LISTEN          0               80                                   *:3306                               *:*             
LISTEN          0               128                                  *:80                                 *:*             
LISTEN          0               128                               [::]:22                              [::]:*

2)修改mysql密码

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 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('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye

2.2.3 安装php

PHP官网

1)基础环境

//安装epel
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

//安装依赖包
[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 libzip-devel libsqlite3x libsqlite3x-devel oniguruma   libzip-devel    https://repo.almalinux.org/almalinux/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

2)下载php软件包并解压

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-8.0.10.tar.gz
[root@localhost src]# tar -xf php-8.0.10.tar.gz

[root@localhost ~]# cd /usr/src/php-8.0.10
[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
编译过程略...

//安装
[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 ~]# which php
/usr/local/php8/bin/php

//查看版本
[root@localhost ~]# php -v
PHP 8.0.10 (cli) (built: Sep 24 2021 12:24:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies

//配置php-fpm
[root@localhost ~]# cd /usr/src/php-8.0.10
[root@localhost php-8.0.10]# \cp php.ini-production /etc/php.ini
[root@localhost php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@localhost php-8.0.10]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.10]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

//配置.service服务启动文件
[root@localhost ~]# cat > /usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=Php server daemon
After=network.target  
 
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP  

[Install]
WantedBy=multi-user.target
EOF

//重新加载
[root@localhost ~]# systemctl daemon-reload

//启动服务
[root@localhost ~]# systemctl enable --now php-fpm
[root@localhost ~]# systemctl status php-fpm
● php-fpm.service - Php server daemon
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-09-24 12:33:19 CST; 6s ago
  Process: 528121 ExecStart=/etc/init.d/php-fpm start (code=exited, status=0/SUCCESS)
 Main PID: 528123 (php-fpm)
    Tasks: 3 (limit: 11300)
   Memory: 19.8M
   CGroup: /system.slice/php-fpm.service
           ├─528123 php-fpm: master process (/usr/local/php8/etc/php-fpm.conf)
           ├─528124 php-fpm: pool www
           └─528125 php-fpm: pool www

Sep 24 12:33:19 localhost systemd[1]: Starting Php server daemon...
Sep 24 12:33:19 localhost php-fpm[528121]: Starting php-fpm  done
Sep 24 12:33:19 localhost systemd[1]: Started Php server daemon.

//查看端口
[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         128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0         80                         *:3306                    *:*       
LISTEN    0         128                        *:80                      *:*       
LISTEN    0         128                     [::]:22                   [::]:* 

2.2.4 配置httpd配置文件

//启用httpd的相关模块
[root@localhost ~]# sed -i '/proxy_module/s/#//g' /usr/local/apache/conf/httpd.conf		#120行取消注释
[root@localhost ~]# sed -i '/proxy_fcgi_module/s/#//g' /usr/local/apache/conf/httpd.conf		#124行取消注释

//创建虚拟目录并生成php测试页面
[root@localhost ~]# mkdir -p /usr/local/apache/htdocs/hhr.com(写域名)
[root@localhost ~]# cat > /usr/local/apache/htdocs/hhr.com/index.php <<EOF
<?php
   phpinfo();
?>
EOF

//设置属主属组
[root@localhost ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@localhost ~]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x. 4 apache apache 53 Sep 27 20:38 /usr/local/apache/htdocs/

//在配置文件的最后加入以下内容
[root@localhost ~]# cat >> /usr/local/apache/conf/httpd.conf <<EOF
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/hhr.com"
    ServerName www.hhr.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/hhr.com/$1
    <Directory "/usr/local/apache/htdocs/hhr.com">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost> 
EOF

//创建一个html文件
[root@localhost ~]# echo 'hello world' > /usr/local/apache/htdocs/hhr.com/index.html

//搜索AddType,添加以下内容
[root@localhost ~]# cat >> /usr/local/apache/conf/httpd.conf <<EOF
    AddType application/x-httpd-php .php       			 #添加此行399
    AddType application/x-httpd-php-source .phps        #添加此行400
EOF 

//添加index.php
[root@localhost ~]# sed -i '/    DirectoryIndex/s/index.html/index.php index.html/g'  /usr/local/apache/conf/httpd.conf	//216行

//重启服务
[root@localhost ~]# systemctl restart httpd

2.2.5 访问

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值