lamp架构平台创建

本文档详细介绍了在CentOS8系统上搭建lamp架构的步骤,包括安装Apache、MySQL和PHP,以及配置动态资源处理、网络源、环境变量和权限。整个过程涉及到动态网站服务器的各个组件,如httpd、MySQL和PHP的编译安装,以及配置文件的修改和服务器的启动。此外,还展示了如何创建虚拟主机和验证安装成功。
摘要由CSDN通过智能技术生成

1、lamp架构含义
lamp架构:就是由Linux、Apache、Mysql/MariaDB、Php/Python的一组动态网站或者服务器的开源软件(LAMP),组成了一个强大的web网站。

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

3、lamp平台构建 [centos8]
lamp架构中的安装顺序:

httpd → MySQL → php

一台虚拟机上就可完成搭建。

① 安装httpd:

  • 安装阿里云centos8镜像网站:https://mirrors.aliyun.com
...yum网络源安装(不能有本地源存在):
[root@king ~]# cd /etc/yum.repos.d/
[root@king yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@king yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

...安装阿里云网络epel源(本地源下载的包有问题)[root@king yum.repos.d]# vi epel.repo
[root@king yum.repos.d]# cat epel.repo
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8

...安装完成需清理缓存和重构缓存:
[root@king yum.repos.d]# yum clean all
[root@king yum.repos.d]# yum makecache

...安装常用的工具:
[root@king ~]# yum -y install wget vim

...安装开发工具包(Development Tools)[root@king ~]# yum grouplist
[root@king ~]# yum -y groups mark install 'Development Tools'

...创建apache的用户和组:
//创建用户apache是系统用户、不给指定家目录、指定给它一个登录shell、不允许它以用户方式登录
[root@king ~]# useradd -r -M -s /sbin/nologin apache
[root@king ~]# id apache
uid=994(apache) gid=991(apache) groups=991(apache)
[root@king ~]# grep 'apache' /etc/passwd //此时过滤查看它有家目录,实际是假的存在
apache:x:994:991::/home/apache:/sbin/nologin

...httpd需要二进制进行编译安装,所以需要安装以下依赖包:
[root@king ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++

...下载安装apr和apr-util:
第一种安装(从网络安装但很慢):
[...]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz
[...]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

第二种安装(直接先下载到本地,再通过xftp上传到xshell用户目录下,比较快):
在这里插入图片描述

...查看上传到目录下的lamp架构相关安装包:
[root@king ~]# ls
 apr-1.7.0.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.43.tar.bz2  mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@king ~]# yum -y install bzip2 //因压缩包是bz2本地没,所以下载bzip2包
...解压两个压缩包(x:解压文件;f:指定文件名)[root@king ~]# tar xf apr-1.7.0.tar.bz2 
[root@king ~]# tar xf apr-util-1.6.1.tar.bz2 

...进行apr配置:
[root@king ~]# cd apr-1.7.0
[root@king apr-1.7.0]# ls
configure //找到它
[root@king apr-1.7.0]# vim configure
cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile" //找到这一行注释#或删除,避免影响之后编译失败
//配置 指定安装包的位置安装存放第三方程序的/usr/local/apr它名为apr
[root@king apr-1.7.0]# ./configure --prefix=/usr/local/apr 
[root@king apr-1.7.0]# yum -y install make  //安装make
[root@king apr-1.7.0]# make && make install //编译(成功基础上)&&并且编译安装

...进行apr-util配置:
[root@king ~]# cd apr-util-1.6.1
[root@king apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr 
👆//两个包是依赖的,所以要告诉apr-util它,apr存放位置
[root@king apr-util-1.6.1]# make && make install

...编译安装httpd:
(也可网络下载[root@king ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.38.tar.gz)
[root@king ~]# tar xf httpd-2.4.43.tar.bz2
[root@king ~]# cd httpd-2.4.43
[root@king httpd-2.4.43]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--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
👆//httpd需要配置一些命令(复制执行就可)
↑↑↑解释命令含义:
【[root@king httpd-2.4.43]# ./configure --prefix=/usr/local/apache \ (指定安装路径位置;反斜杠\表示转义继续输入命令)
> --sysconfdir=/etc/httpd24 \ (二进制安装的配置文件存放位置)
> --enable-so \ 
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \  (enable都是要打开的功能)
> --with-zlib \
> --with-pcre \   (zlib,pcre 需要库文件)
> --with-apr=/usr/local/apr \   
> --with-apr-util=/usr/local/apr-util/ \(需要告诉它apr和apr-util的具体安装路径位置)
> --enable-modules=most \  
> --enable-mpms-shared=all \ (打开这两个模块)
> --with-mpm=prefork (php要求httpd使用prefork MPM模块)】
[root@king httpd-2.4.43]# make && make install //编译+安装

...安装完成后配置:
[root@king ~]# which apachectl
/usr/bin/which: no apachectl in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) //找不到apchectl路径
[root@king ~]# cd /usr/local/apache
[root@king apache]# ls
bin  
[root@king apache]# cd bin
[root@king bin]# ls
apachectl  
...添加环境变量,把apache路径覆盖写到重新指定的创建文件脚本中(脚本必须以.sh结尾): 
[root@king bin]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@king bin]# source /etc/profile.d/httpd.sh //让脚本生效
//查看是否生效
[root@king bin]# which apachectl
/usr/local/apache/bin/apachectl  //绝对路径出现存在
[root@king bin]# echo $PATH
/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin  //变量映射绝对路径已存在

//把include文件指向httpd文件做软链接
[root@king ~]# ln -s /usr/local/apache/include/ /usr/include/httpd  
[root@king ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
...删掉Severname前面的注释#:
[root@king ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

...开启apachectl服务(二进制安装使用脚本方式启动):
[root@king ~]# apachectl start
[root@king ~]# ss -antl
State       Recv-Q       Send-Q     Local Address:Port     Peer Address:Port                                
LISTEN        0          128         *:80  //端口已开        *:*                  

② 安装MySQL

...安装依赖包:
[root@king httpd]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

...创建mysql的系统用户、不给指定家目录、指定给它一个登录shell、不允许它以用户方式登录:
[root@king ~]# useradd -r -M -s /sbin.nologin mysql
[root@king ~]# id mysql
uid=993(mysql) gid=990(mysql) groups=990(mysql)
...解压本地mysql包并-C指定解压后位置(或网络安装)[root@king ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local
【]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz】
[root@king ~]# cd /usr/local/
[root@king local]# ll
total 0
drwxr-xr-x.  9 root root 129 Aug  3 10:58 mysql-5.7.30-linux-glibc2.12-x86_64
//解压后名字过于长,给它做一个软链接映射到它
[root@king local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@king local]# ll
total 0
lrwxrwxrwx.  1 root root  36 Aug  3 11:08 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Aug  3 10:58 mysql-5.7.30-linux-glibc2.12-x86_64
//修改数据库的属主和属组为mysql系统用户(*表mysql下所有有关的)
[root@king local]# chown -R mysql.mysql mysql*
[root@king local]# ll
total 0
lrwxrwxrwx.  1 mysql mysql  36 Aug  3 11:08 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 mysql mysql 129 Aug  3 10:58 mysql-5.7.30-linux-glibc2.12-x86_64
...添加指定路径环境变量:
[root@king bin]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@king bin]# source /etc/profile.d/mysql.sh   //生效
//查看是否生效
[root@king bin]# which mysql
/usr/local/mysql/bin/mysql
[root@king bin]# which mysqld
/usr/local/mysql/bin/mysqld
[root@king bin]# echo $PATH  //变量又新增mysql
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

...创建数据库的存放目录:
[root@king ~]# mkdir /opt/data
[root@king ~]# chown -R mysql.mysql /opt/data
[root@king ~]# ll /opt
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug  3 11:30 data

...对数据库进行初始化:
[root@king ~]# mysqld --initialize --user=mysql --datadir=/opt/data
(末尾👉)root@localhost: %fw!hsZp1:wt //初始化完会生成一个数据库临时密码,在初登录mysql时会使用!!!
 
 ...生成配置文件(写入以下配置)[root@king etc]# 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@king ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@king ~]# ls /etc/init.d
functions  mysqld  README
[root@king ~]# vim /etc/init.d/mysqld  //进入配置文件修改
basedir=/usr/local/mysql   //添加数据库位置
datadir=/opt/data     //添加数据库数据要存放的位置

...创建软连接指向该目录并做映射:
[root@king ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql/include' -> '/usr/local/mysql/include/'
[root@king ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@king ~]# ldconfig  //生效
//配置文件添加路径
[root@king ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                      /usr/local/mysql/man   //添加的路径
[root@king ~]# service mysqld start   //开启mysql服务
Starting MySQL.Logging to '/opt/data/king.err'.
 SUCCESS! 
[root@king ~]# ss -antl
State        Recv-Q        Send-Q       Local Address:Port     Peer Address:Port    
LISTEN        0            80           *:3306                   *:*

...用初始密码登录,进入修改密码:
【登录时如报错:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory 
应该是和系统版本兼容有关,此时只需下载① yum -y install libncurses*,解决问题;
或者② yum provides libncurses.so.5 查看所需依赖包,然后yum下载,解决问题】
[root@king ~]# mysql -uroot -p
Enter password:  //初始密码
mysql> set password= password('admin123');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> exit
Bye
[root@king ~]# mysql -uroot -p'admin123'
mysql> 

③ 安装PHP
[到centos8版本就不用二进制编译安装]

...直接yum安装就可:
[root@king ~]# yum -y insatll php*
[root@king ~]# ss -antl  //查看PHP端口号发现不存在 
...添加php端口号9000:
[root@king ~]# vim /etc/php-fpm.d/www.conf
;listen = /run/php-fpm/www.sock //把原本监听的注释(;)掉
listen=127.0.0.1:9000 //新添加一行IP:端口号
//开启服务:
[root@king ~]# systemctl restart php-fpm
[root@king ~]# 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:*
3.1、在apache中启动httpd的代理模块
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so:
[root@king ~]# vim /etc/httpd24/httpd.conf  //要启动这两个模块,需取消这两模块前注释#
  LoadModule proxy_module modules/mod_proxy.so
  LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
[root@king ~]# apachectl restart  //重启服务

3.2、配置虚拟机

  • 网页上apache内容已知从哪输出:
    在这里插入图片描述
    在这里插入图片描述
...创建虚拟主机目录生成PHP测试界面:
[root@king htdocs]# mkdir test
[root@king htdocs]# ls
index.html  test
[root@king htdocs]# cd test/
[root@king test]# vim index.php  //配置动态资源界面,添加以下三行内容
<?php
   phpinfo();
?>
...修改目录下所有属主和属组:
[root@king test]# chown -R apache.apache /usr/local/apache/htdocs/
[root@king test]# ll /usr/local/apache/htdocs/
total 4
-rw-r--r--. 1 apache apache 45 Jun 12  2007 index.html
drwxr-xr-x. 2 apache apache 23 Aug  3 17:39 test
[root@king test]# vim /etc/httpd24/httpd.conf
//在末行添加以下内容(注意网址添加的位置)
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/test"
    ServerName www.test.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/$1
    <Directory "/usr/local/apache/htdocs/test">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
[root@king test]# vim /etc/httpd24/httpd.conf
</VirtualHost>
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz  //找到此之后添加这两行进去
    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>
[root@king ~]# apachectl restart  //重启服务

4、登录界面验证是否搭建成功:
在这里插入图片描述
[已成功]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值