lamp架构的部署

lamp架构的部署

1.什么是lamp架构

所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。

web服务器工作流程

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

2.lamp平台构建

环境说明

系统平台 IP 所需安装的服务
centos8 192.168.169.139 httpd-2.4
mysql-5.7
php
php-mysql

安装顺序

httpd ——> mysql ——> php

2.1 安装httpd

//配置yum源
[root@zzd139 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@zzd139 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@zzd139 ~]# dnf clean all
[root@zzd139 ~]# dnf list all

//配置epel源
[root@zzd139 ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@zzd139 ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@zzd139 ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@zzd139 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  epel-modular.repo  epel.repo  epel-testing-modular.repo  epel-testing.repo
[root@zzd139 ~]# dnf clean all
[root@zzd139 ~]# dnf makecache

//安装依赖包和工具包
[root@zzd139 ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ vim make wget

//创建apache服务的用户
[root@zzd139 ~]# useradd -r -M -s /sbin/nologin apache

//下载和apr、apr-util、httpd源码包
[root@zzd139 ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@zzd139 ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@zzd139 ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz

//编译安装apr
[root@zzd139 ~]# tar xf apr-1.7.0.tar.gz 
[root@zzd139 ~]# cd apr-1.7.0/
[root@zzd139 apr-1.7.0]# vim configure
	cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    #$RM "$cfgfile"	 	将这行注释掉或者删掉
[root@zzd139 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@zzd139 apr-1.7.0]# make && make install

//编译安装apr-util
[root@zzd139 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@zzd139 apr-util-1.6.1]# make && make install

//编译安装httpd
[root@zzd139 ~]# tar xf httpd-2.4.54.tar.gz 
[root@zzd139 ~]# cd httpd-2.4.54/
[root@zzd139 httpd-2.4.54]# ./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@zzd139 httpd-2.4.54]# make && make install

//配置apache全局环境变量
[root@zzd139 local]# echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@zzd139 local]# source /etc/profile.d/httpd.sh
[root@zzd139 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin

//配置apache的include头文件,能够让系统识别
[root@zzd139 local]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@zzd139 local]# ll /usr/include/httpd
lrwxrwxrwx. 1 root root 26 Aug  2 21:19 /usr/include/httpd -> /usr/local/apache/include/

//编辑apache的man文档
[root@zzd139 local]# vim /etc/man_db.conf
//添加如下内容
	MANDATORY_MANPATH                       /usr/local/apache/man

//将主配置文件里的'#ServerName'行的注释去掉
[root@zzd139 apache]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf

//使用systemd风格管理apache
[root@zzd139 ~]# cd /usr/lib/systemd/system
[root@zzd139 system]# cp sshd.service httpd.service
[root@zzd139 system]# vim httpd.service 
    [Unit]
    Description=web server daemon
    Documentation=man:httpd(5)
    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@zzd139 system]# 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@zzd139 system]# vim /etc/selinux/config 
	SELINUX=enforcing——>SELINUX=disabled
[root@zzd139 system]# setenforce  0

//重新加载服务
[root@zzd139 system]# systemctl daemon-reload

//启动httpd服务
[root@zzd139 system]# systemctl start httpd.service 
[root@zzd139 system]# systemctl status httpd.service 
● httpd.service - web server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-08-02 21:30:42 CST; 26s ago
     Docs: man:httpd(5)
  Process: 49156 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 49159 (httpd)
    Tasks: 6 (limit: 5770)
   Memory: 4.8M
   CGroup: /system.slice/httpd.service
           ├─49159 /usr/local/apache/bin/httpd -k start
           ├─49160 /usr/local/apache/bin/httpd -k start
           ├─49161 /usr/local/apache/bin/httpd -k start
           ├─49162 /usr/local/apache/bin/httpd -k start
           ├─49163 /usr/local/apache/bin/httpd -k start
           └─49164 /usr/local/apache/bin/httpd -k start

Aug 02 21:30:42 zzd139 systemd[1]: Starting web server daemon...
Aug 02 21:30:42 zzd139 systemd[1]: Started web server daemon.
[root@zzd139 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               128                                  *:80                                 *:*                              
LISTEN          0               128                               [::]:22                              [::]:* 

//设置httpd开机自启
[root@zzd139 ~]# systemctl enable httpd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

2.2 安装mysql

//卸载mariadb
[root@zzd139 ~]# rpm -qa | grep mariadb
mariadb-connector-c-devel-3.1.11-2.el8_3.x86_64
mariadb-connector-c-config-3.1.11-2.el8_3.noarch
mariadb-devel-10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-connector-c-3.1.11-2.el8_3.x86_64
[root@zzd139 ~]# dnf -y remove mariadb*

//安装依赖包
[root@zzd139 ~]# dnf -y install ncurses-compat-libs

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

//下载mysql-5.7二进制安装包
[root@zzd139 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

//将mysql解压安装到/usr/local下
[root@zzd139 ~]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@zzd139 ~]# cd /usr/local/
[root@zzd139 local]# mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@zzd139 local]# chown -R mysql.mysql /usr/local/mysql/
[root@zzd139 local]# ll -d mysql/
drwxr-xr-x. 9 mysql mysql 129 Aug  2 21:39 mysql/

//配置环境变量
[root@zzd139 ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
[root@zzd139 ~]# source /etc/profile.d/mysql.sh 
[root@zzd139 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/usr/local/mysql/bin

//让系统可以识别mysql的lib库文件
[root@zzd139 ~]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
[root@zzd139 ~]# ldconfig 

//让系统可以识别mysql的include文件
[root@zzd139 ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql

//配置man文档
[root@zzd139 ~]# vim /etc/man_db.conf 
//添加如下内容
	MANDATORY_MANPATH                       /usr/local/mysql/man

//创建mysql数据存放目录
[root@zzd139 ~]# mkdir -p /opt/data
[root@zzd139 ~]# chown -R mysql.mysql /opt/data/
[root@zzd139 ~]# ll -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 Aug  2 21:51 /opt/data/

//初始化数据库
[root@zzd139 ~]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/
2022-08-02T13:53:55.291599Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-02T13:53:55.454892Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-02T13:53:55.479049Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-02T13:53:55.532736Z 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: 8cbc24af-126a-11ed-af9b-000c29f177ce.
2022-08-02T13:53:55.533262Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-08-02T13:53:55.960572Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T13:53:55.960585Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T13:53:55.960970Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-02T13:53:55.999193Z 1 [Note] A temporary password is generated for root@localhost: h#fimeZ?q8dB
//将初始密码保存到本地
[root@zzd139 ~]# echo 'h#fimeZ?q8dB' > ~/pass
[root@zzd139 ~]# cat ~/pass 
h#fimeZ?q8dB

//编辑mysql配置文件
[root@zzd139 ~]# vim /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@zzd139 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@zzd139 ~]# vim /etc/init.d/mysqld
//找到一下两行,此为更改后的内容
    basedir=/usr/local/mysql
    datadir=/opt/data
[root@zzd139 ~]# chmod +x /etc/init.d/mysqld

//开启mysqld服务
[root@zzd139 ~]# service mysqld start 
Starting MySQL.Logging to '/opt/data/zzd139.err'.
 SUCCESS! 
 [root@zzd139 ~]# 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               80                                   *:3306                               *:*                              
LISTEN          0               128                                  *:80                                 *:*                              
LISTEN          0               128                               [::]:22                              [::]:* 
 //设置mysql开机自启
 [root@zzd139 ~]# chkconfig --add mysqld 
[root@zzd139 ~]# chkconfig --list 

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

//登录mysql并修改密码
[root@zzd139 ~]# mysql -uroot -p'h#fimeZ?q8dB'

mysql> set password = password('123456!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@zzd139 ~]# mysql -uroot -p123456!

2.3 安装php

//安装依赖包
[root@zzd139 ~]# 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 php-mysqlnd libsqlite3x-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm libzip-devel

//下载php源码包
[root@zzd139 ~]# wget https://www.php.net/distributions/php-7.4.30.tar.xz

//解压php
[root@zzd139 ~]# tar xf php-7.4.30.tar.xz
[root@zzd139 ~]# cd php-7.4.30

//编译安装php
[root@zzd139 php-7.4.30]# ./configure --prefix=/usr/local/php7  \
> --with-config-file-path=/etc \
> --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
…………
+--------------------------------------------------------------------+
| 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@zzd139 php-7.4.30]# make && make install

//配置环境变量
[root@zzd139 php7]# echo "export PATH=$PATH:/usr/local/php7/bin" > /etc/profile.d/php7.sh
[root@zzd139 php7]# source /etc/profile.d/php7.sh 
[root@zzd139 php7]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/usr/local/mysql/bin:/usr/local/php7/bin
[root@zzd139 php7]# which php
/usr/local/php7/bin/php
[root@zzd139 php7]# php -v
PHP 7.4.30 (cli) (built: Aug  2 2022 23:07:12) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

//配置头文件
[root@zzd139 php7]# ln -s /usr/local/php7/include/ /usr/include/php7

//配置库文件
[root@zzd139 php7]# echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php7.conf
[root@zzd139 php7]# ldconfig 

//配置php-fpm
[root@zzd139 ~]# cd ~/php-7.4.30/
[root@zzd139 php-7.4.30]# cp php.ini-production  /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@zzd139 php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@zzd139 php-7.4.30]# chmod +x /etc/init.d/php-fpm 
[root@zzd139 php-7.4.30]# cd /usr/local/php7/etc/
[root@zzd139 etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@zzd139 etc]# cp php-fpm.conf.default php-fpm.conf
[root@zzd139 etc]# cd php-fpm.d/
[root@zzd139 php-fpm.d]# ls
www.conf.default
[root@zzd139 php-fpm.d]# cp www.conf.default www.conf

//启动php-fpm
[root@zzd139 php-fpm.d]# service php-fpm start 
Starting php-fpm  done
//默认情况下,监听9000端口
[root@zzd139 php-fpm.d]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                    Peer Address:Port          Process          
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                              [::]:*  

//设置php-fpm开机自启
[root@zzd139 php-fpm.d]# chkconfig --add php-fpm 
[root@zzd139 php-fpm.d]# chkconfig --list 

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
php-fpm        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

2.4 安装php过程中会遇到的报错信息以及解决方法

//报错信息
error: Package requirements (sqlite3 > 3.7.4) were not met:
//解决方法
[root@zzd139 php-7.4.30]# dnf -y install libsqlite3x-devel

//报错信息
error: Package requirements (oniguruma) were not met:
//解决方法
[root@zzd139 php-7.4.30]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

//报错信息
error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
//解决方法
[root@zzd139 php-7.4.30]# dnf -y install libzip-devel

2.5 配置apache

2.5.1 启动代理模块

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

[root@zzd139 ~]# cd /usr/local/apache/conf/
[root@zzd139 conf]# vim httpd.conf 
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

2.5.2 配置虚拟主机

//创建虚拟主机的目录并生成php测试页面
[root@zzd139 conf]# cd /usr/local/apache/htdocs/
[root@zzd139 htdocs]# ls
index.html
[root@zzd139 htdocs]# mkdir zzd
[root@zzd139 htdocs]# cd zzd/
[root@zzd139 zzd]# cat  > index.php << EOF
> <?php
>    phpinfo();
> ?>
> EOF
[root@zzd139 zzd]# cat index.php 
<?php
   phpinfo();
?>
[root@zzd139 zzd]# chown -R apache.apache /usr/local/apache/htdocs/

//配置虚拟主机
[root@zzd139 zzd]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/zzd"
    ServerName zzd.example.com
    ErrorLog "logs/zzd.example.com-error_log"
    CustomLog "logs/zzd.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/zzd/$1
    <Directory "/usr/local/apache/htdocs/zzd">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

[root@zzd139 zzd]# vim /usr/local/apache/conf/httpd.conf 
    Include conf/extra/httpd-vhosts.conf #去掉此行注释
//搜索AddType,加上以下内容
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php       #添加此行   
    AddType application/x-httpd-php-source .phps   #添加此行
//在'DirectoryIndex index.html'后面添加index.php
	DirectoryIndex index.html ——> DirectoryIndex index.php index.html

//重启httpd服务
[root@zzd139 zzd]# systemctl restart httpd.service 
[root@zzd139 zzd]# 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               128                          127.0.0.1:9000                         0.0.0.0:*                              
LISTEN          0               128                                  *:80                                 *:*                              
LISTEN          0               128                               [::]:22                              [::]:*                              
LISTEN          0               80                                   *:3306                               *:*  

2.6 验证

//在物理机C:\Windows\System32\drivers\etc\hosts文件下添加域名映射
192.168.169.139 zzd.example.com

在浏览器进行访问验证,输入域名或IP地址

posted @ 2022-08-02 23:32  Zic师傅  阅读(74)  评论(0编辑  收藏  举报