LAMP(8.0编译)

安装LAMP

LAMP分别代表什么?

LAMP(8.0编译)

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

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

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

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

①静态网页

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

②动态网页

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

MySQL数据库 - 像极了厨师

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

PHP - 像极了服务生

  • 作用:PHP主要负责PHP脚本程序的解析以及实现与MySQL数据库的交互工作,动态页面中的注册/登陆/下单/支付等大多数功能都是基于PHP+MySQL进行实现。PHP是一种通用开源脚本语言。
    LAMP(8.0编译)

1、当客户端请求的是动态资源时,apache(httpd程序)会调用libphpX.so模块进行相应的解析
2、如果解析处理需要用到后台数据库相关数据,此时php程序也会连接后台数据库
3、最终php程序将解析后的结果返回给apache(httpd程序),让apache返回给客户端

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

LAMP(8.0编译)
LAMP(8.0编译)

web服务器工作流程

LAMP(8.0编译)

web工作流程

通过上面的图说明一下web的工作流程:

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

LAMP部署

环境说明:

系统平台 IP 需要安装的服务
redhat8 192.168.129.250 httpd mysql php

安装httpd

配置源和工具

[root@localhost ~]# wget -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

创建apache服务的用户和组

[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

安装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
编译安装过程略...

安装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
编译安装过程略...

安装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
编译安装过程略...

配置

#配置环境变量
[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' /etc/httpd24/httpd.conf 

关闭防火墙

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

编写服务启动文件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

#开机自启
[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                              [::]:*       

安装mysql

安装依赖包

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

创建用户

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

下载二进制格式的mysql软件包

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls mysql*
mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

解压软件至/usr/local/

[root@localhost src]# tar xf mysql-5.7.35-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.35-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.35-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.35-linux-glibc2.12-x86_64/'
[root@localhost local]# ll
total 0
drwxr-xr-x. 14 root root 164 Sep 24 00:25 apache
drwxr-xr-x.  6 root root  58 Sep 24 00:20 apr
drwxr-xr-x.  5 root root  43 Sep 24 00:22 apr-util
drwxr-xr-x.  2 root root   6 Aug 12  2018 bin
drwxr-xr-x.  2 root root   6 Aug 12  2018 etc
drwxr-xr-x.  2 root root   6 Aug 12  2018 games
drwxr-xr-x.  2 root root   6 Aug 12  2018 include
drwxr-xr-x.  2 root root   6 Aug 12  2018 lib
drwxr-xr-x.  2 root root   6 Aug 12  2018 lib64
drwxr-xr-x.  2 root root   6 Aug 12  2018 libexec
lrwxrwxrwx.  1 root root  36 Sep 24 10:24 mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Sep 24 10:24 mysql-5.7.35-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root   6 Aug 12  2018 sbin
drwxr-xr-x.  5 root root  49 Jul 12 15:16 share
drwxr-xr-x.  2 root root   6 Aug 12  2018 src

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

[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]#  ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Sep 24 10:24 /usr/local/mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/

添加环境变量

[root@localhost local]# ls /usr/local/mysql
LICENSE  README  bin  docs  include  lib  man  share  support-files
[root@localhost local]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/httpd.sh
[root@localhost local]# 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

配置service服务启动文件

[root@localhost ~]# sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g"  /usr/local/mysql/support-files/mysql.server
[root@localhost ~]# sed -ri "s#^(datadir=).*#\1/opt/datar#g"  /usr/local/mysql/support-files/mysql.server
[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                              [::]:*

修改密码

[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

安装php

安装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

下载php软件包并解压

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/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

cd /usr/src/php-8.0.10
[root@localhost ~]# cd /usr/src/php-8.0.10
[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[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 $MAINPID
> 
> [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                   [::]:* 

配置httpd配置文件

启用httpd的相关模块

#取消注释
[root@localhost ~]# sed -i '/proxy_module/s/#//g' /usr/local/httpd/conf/httpd.conf
[root@localhost ~]# sed -i '/proxy_fcgi_module/s/#//g' /usr/local/httpd/conf/httpd.conf

创建虚拟主机目录并生成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
ll /usr/local/apache/htdocs/ -d

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

[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       			 #添加此行
    AddType application/x-httpd-php-source .phps        #添加此行
EOF 

添加index.php

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

访问

重启服务

[root@localhost ~]# systemctl restart httpd

LAMP(8.0编译)
LAMP(8.0编译)
LAMP(8.0编译)

上一篇:LAMP源码安装图文详解(超详细)


下一篇:航班信息管理系统(JDBC)