LAMP 概述,及源码安装apache(附带安装脚本)

 

 

一: LAMP 架构

1.1  什么是LAMP

LAMP架构是目前成熟的企业网站应用模式之一,指的是协同工作的一整套系统和相关软件,能够提供动态Web站点服务及其应用开发环境。LAMP是一个缩写词,具体包括Linux操作系统、Apache网站服务器、MySQL数据库服务器、PHP(或Perl、Python)网页编程语言。

image-20210808213931318

 

1.2 LAMP 组件

(平台)Linux:作为LAMP架构的基础,提供用于支撑Web站点的操作系统,能够与其他三个组件提供更好的稳定性,兼容性(AMP组件也支持Windows、UNIX等平台) 。

 

(前台)Apache:作为LAMP架构的前端,是一款功能强大,稳定性好的Web服务器程序,该服务器直接面向用户提供网站访问,发送网页,图片等文件内容。

 

(后台)MySQL:作为LAMP架构的后端,是一款流行的开源关系数据库系统。在企业网站、业务系统等应用中,各种账户信息、产品信息,客户资料、业务数据等都可以存储到MySQL数据库,其他程序可以通过SQL语句来查询,更改这些信息。

 

(中间连接)PHP/Perl/Python:作为三种开发动态网页的编程语言,负责解释动态网页文件,负责沟通Web服务器和数据库系统以协同工作,并提供Web应用程序的开发和运行环境。其中PHP是一种被广泛应用的开放源代码的多用途脚本语言,它可以嵌入到HTML中,尤其适合于Web应用开发

 

注意:在构建LAMP平台时,各组件的安装顺序依次为Linux、Apache、MySQL、PHP。其中Apache和MySQL的安装并没有严格的顺序,PHP环境的安装一般放到最后安装,负责沟通 Web 服务器和数据库系统以协同工作。

 

二:源码编译安装Apache

2.1 关闭防火墙和selinux .将Apache 所需要的软件包传到 /opt目录下

 systemctl stop firewalld
 systemctl disable firewalld
 setenforce 0
 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

 

image-20210808163611593

软件包:

httpd-2.4.29.tar.gz

apr-1.6.2.tar.gz

apr-util-1.6.0.tar.gz

注:apr组件包用于支持Apache上层应用跨平台,提供底层接口库,能有效的降低并发连接数、降低进程和减少访问堵塞。

 

 

2.2 安装环境依赖包

 yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl    

image-20210808164234762

注意:

如果不安装 pcre-devel 软件包则报错:configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

 

2.3 配置软件模块

--prefix=/usr/local/httpd #指定将 httpd 服务程序的安装路径

--enable-so #启用动态加载模块支持,使 httpd 具备进一步扩展功能的能力

--enable-rewrite #启用网页地址重写功能,用于网站优化、防盗链及目录迁移维护

--enable-charset-lite #启动字符集支持,以便支持使用各种字符集编码的页面

--enable-cgi #启用CGI(通用网关接口)脚本程序支持,便于网站的外部扩展应用访问能力

 cd /opt/
 tar zxvf apr-1.6.2.tar.gz
 tar zxvf apr-util-1.6.0.tar.gz
 tar jxvf httpd-2.4.29.tar.bz2
 ​
 mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
 mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-utili
 ​
 ​
 cd /opt/httpd-2.4.29/
 ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi

 


 

 

2.4 编辑及安装

 make  -j 2  &&  make install 
 #make -j 2 表示两核同时进行编译

 

 

2.5 .优化配置文件路径,并把httpd服务的可执行程序文件放入路径环境变量的目录中便于系统识别

 ln -s /usr/local/httpd/conf/httpd.conf /etc/
 ln -s /usr/local/httpd/bin/* /usr/local/bin/
 #源码编译安装的apache 配置文件在 安装目录的 conf 目录中,将其做软连接到 /etc/目录下
 #执行程序在 bin 目录 下,将其做软连接到 /usr/local/bin/  目录下,这样可以使用systemctl 来控制

 

 

2.6 添加httpd 系统服务

方法一:

 cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
 chmod +x /etc/init.d/httpd
 sed -i "1i #!/bin/bash\n# chkconfig: 35 85 21 \n" /etc/init.d/httpd
 chkconfig  --add httpd
 ​
 #完成之后,就可以使用systemctl 命令控制httpd服务
 # /etc/init.d/httpd                   # service 服务管理
 # #!/bin/bash                         #在第一行前插入新行,添加此2行内容
 # chkconfig: 35 85 21                 #3,5级别自动运行  第85个启动 第21个关闭

 

image-20210808174529354

 

方法二:

  cat >  /lib/systemd/system/httpd.service  <<EOF
 > [Unit]
 > Description=The Apache HTTP Server
 > After=network.target
 > [Service]
 > Type=forking
 > PIDFile=/usr/local/httpd/logs/httpd.pid
 > ExecStart=/usr/local/bin/apachectl $OPTIONS
 > ExecReload=/bin/kill -HUP $MAINPID
 > [Install]
 > WantedBy=multi-user.target
 > EOF
 

 

 

image-20210808180644223

 

 

7 修改httpd 配置文件

 cp -a /usr/local/httpd/conf /usr/local/httpd/conf.bak  #先备份所有的配置文件
 cp -p /usr/local/httpd/conf/httpd.conf{,.bak}        #复制住配置文件
 ​
 sed -i  's/Listen 80/Listen 192.168.23.103:80/' /usr/local/httpd/conf/httpd.conf.bak
 sed -i '197i ServerName www.kgc.com:80' /usr/local/httpd/conf/httpd.conf.bak
 ​
 /usr/bin/cp -fp /usr/local/httpd/conf.bak/httpd.conf  /usr/local/httpd/conf/httpd.conf

 

配置文件项说明

 vim /etc/httpd.conf
 
​
 Listen 192.168.23.103:80      #--197行-- 设置apache网站域名和访问端口
 ​
 ServerName www.mynet.com:80   #--221行--默认首页存放路径
 ​
 DocumentRoot "/usr/local/httpd/htdocs"   #--255行--默认首页文件名设置
 ​
 DirectoryIndex index.html    #设置首页为index.html

 

 

8 启动服务,并访问

检查配置文件语法是否正确

 httpd -t

 

image-20210808190026023

 

启动服务

 
systemctl start httpd
 netstat  -natp | grep 80

 

image-20210808190151432

 

访问验证

 ehco "192.168.23.103 www.mynet.com" >> /etc/hosts   # ip域名映射
 firefox http://192.168.23.103   #使用ip访问
 firefox http://www.mynet.com    #使用域名访问
 

image-20210808191603505

image-20210808191341589

 

 

 

 

附:

apache 主要目录结构及配置文件

如果没有tree 命令,可以先yum provides tree ,先查看提供命令的软件包,然后在

yum -y install tree 下载软件包

 

image-20210808170825570

 

 

进入到 apache 的安装目录下,使用 tree -L 1 查看

安装目录结构

image-20210808170950198

主要使用目录释义
bin 程序命令目录
conf 配置文件目录
htdocs 网站站点目录
logs 日志文件及pid 号文件存放目录
modules 模块目录

 

 

bin 目录结构

image-20210808171857484

bin 程序目录释义
ab apache http服务性能压力测试工具
apachectl apache 启动命令
apxs apache服务器编译和安装扩展的工具,在进行DSO方式模块编译时会用到例如编译PHP时就用到:--with-
htcacheclean 清理磁盘缓存区的命令,一般少用
htpasswd 建立更新基本认证文件
httpd 为apache的控制命令程序,apachectl执行的时候会调用到httpd
rotatelogs apache自带日志轮换工具 ,也可以用到cronolog代替。

 

 

conf 目录结构

image-20210808172714460

conf目录释义
extra 辅助apache 配置文件
httpd.conf 主配置文件

conf/extra目录

image-20210808172945215

conf/extra 目录释义
httpd-dav.conf dav支持配置
httpd-default.conf apache相关服务配置 如超时时间 保持连接时间
httpd-languages.conf 语言支持
httpd-mpm.conf 服务器池管理,如apache模式配置 连接等
httpd-ssl.conf 支持SSL加密配置
httpd-vhosts.conf 虚拟机配置文件

 

apache 源码包 一键安装脚本

(需要 配置好yum 源,并将软件包传到 /opt/目录下)

 #!/bin/bash
 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
 yum clean all && yum makecache
 systemctl stop firewalld
 systemctl disable firewalld   &>/dev/null
 setenforce 0  &> /dev/null
 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
 ​
 read -p  "请输入web服务器ip:" IP
 read -p "请输入对应的域名:" NAME
 ​
 yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl   &> /dev/null
 ​
 cd /opt/
 tar zxvf apr-1.6.2.tar.gz  &> /dev/null
 tar zxvf apr-util-1.6.0.tar.gz  &> /dev/null
 tar jxvf httpd-2.4.29.tar.bz2   &> /dev/nullmv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
 mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
 ​
 ​
 cd /opt/httpd-2.4.29/
 ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &> /dev/null
 ​
 ​
 make   &> /dev/null  
 make install  &> /dev/null
 ​
 ​
 ​
 ln -s /usr/local/httpd/conf/httpd.conf /etc/
 ln -s /usr/local/httpd/bin/* /usr/local/bin/
 ​
 cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd  &> /dev/null
 chmod +x /etc/init.d/httpd
 sed -i "1i #!/bin/bash\n# chkconfig: 35 85 21 \n" /etc/init.d/httpd
 chkconfig  --add httpd   &> /dev/null
  
 ​
 cp -a /usr/local/httpd/conf /usr/local/httpd/conf.bak   &> /dev/null
 cp -p /usr/local/httpd/conf/httpd.conf{,.bak}    &> /dev/null
 ​
 ​
 sed -i  "s/Listen 80/Listen $IP:80/" /usr/local/httpd/conf/httpd.conf.bak
 sed -i "197i ServerName $NAME:80" /usr/local/httpd/conf/httpd.conf.bak
 /usr/bin/cp -fp /usr/local/httpd/conf/httpd.conf.bak  /usr/local/httpd/conf/httpd.conf
 ​
 echo "$IP  $NAME" >> /etc/hosts
 systemctl restart httpd
 

 

 

 

posted @ 2021-08-15 06:05  知己一语  阅读(212)  评论(0编辑  收藏  举报