lamp


1. lamp简介

LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写:

  • Linux,操作系统
  • Apache,网页服务器
  • MariaDB或MySQL,数据库管理系统(或者数据库服务器)
  • PHP、Perl或Python,脚本语言

一般用来建立 web 应用平台

2. lamp组件

Linux 是一种自由和开发源代码的类 UNIX 操作系统,目前运用最广泛使用人数最多的操作系统。本实验为大家提供的就是Linux 操作系统。

Apache 是LAMP架构最核心的 Web Server,开源、稳定、模块丰富是 Apache 的优势。但 Apache 的缺点是有些臃肿,内存和 CPU 开销大,性能上有损耗,不如一些轻量级的 Web 服务(例如 nginx)高效,轻量级的 Web 服务器对于静态文件的响应能力来说远高于 Apache 服务器。Apache 做为 Web Server 是负载 PHP 的最佳选择,如果流量很大的话,可以采用 nginx 来负载非 PHP的Web 请求。

PHP 是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Per1 的特点,利于学习,使用广泛,主要适用于 Web 开发领域。PHP 独特的语法混合了 C、Java、Perl以及 PHP 自创的语法。它可以比 CGI 或者 Perl 更快速地执行动态网页。用 PHP 做出的动态页面与其他的编程语言相比,PHP 是将程序嵌入到 HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成 HTML 标记的 CGI 要高许多;PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。

MySQL 在开源数据库中,性能、稳定性和功能上是首选,可以达到百万级别的数据存储,网站初期可以将 MySQL 和 Web 服务器放在一起,但是当访问量达到一定规模后,应该将 MySQL 数据库从 Web Server 上独立出来,在单独的服务器上运行,同时保持 Web Server 和 MySQL 服务器的稳定连接。

3. LAMP有什么优势

和 Java/J2EE 架构相比, LAMP 具有 Web 资源丰富、轻量、快速开发等特点;
与微软的 .NET 架构相比,LAMP具有通用、跨平台、高性能、低价格的优势
因此 LAMP 无论是性能、质量还是价格都是企业搭建网站的首选平台

4. lamp平台构建

lamp平台软件安装次序:httpd --> mysql --> php

注意:php要求httpd使用prefork MPM

安装httpd

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

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

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

//下载和安装apr以及apr-util
[root@z1 ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@z1 ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@z1 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz
[root@z1 ~]# tar xf apr-1.7.0.tar.gz 
[root@z1 ~]# tar xf apr-util-1.6.1.tar.gz 
[root@z1 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz
apr-1.7.0        apr-util-1.6.1
[root@z1 ~]# cd apr-1.7.0
[root@z1 apr-1.7.0]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    $RM "$cfgfile"    //将此行加上注释,或者删除此行
[root@z1 apr-1.7.0]# ./configure --prefix=/usr/local/apr
配置过程略...
[root@z1 apr-1.7.0]# make 
[root@z1 apr-1.7.0]# make install
编译安装过程略...

[root@z1 apr-1.7.0]# cd ../apr-util-1.6.1
[root@z1 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
配置过程略...
[root@z1 apr-util-1.6.1]# make
[root@z1 apr-util-1.6.1]# make install
编译安装过程略...

//编译安装httpd
[root@z1 ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
[root@z1 ~]# tar xf httpd-2.4.54.tar.gz 
[root@z1 ~]# cd httpd-2.4.54
[root@z1 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@z1 httpd-2.4.54]# make && make install
编译安装过程略...

//安装后配置
[root@z1 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@z1 ~]# source /etc/profile.d/httpd.sh
[root@z1 ~]# which httpd
/usr/local/apache/bin/httpd
[root@z1 ~]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@z1 ~]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man  //添加这行


//启动apache并设置开机自启
[root@z1 ~]# cd /usr/lib/systemd/system
[root@z1 system]# cp sshd.service httpd.service
[root@z1 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@z1 ~]# systemctl daemon-reload
[root@z1 ~]# systemctl start httpd
[root@z1 ~]# 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               [::]:*              
[root@z1 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@z1 ~]# systemctl status httpd
● httpd.service - web server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor pr>
   Active: active (running) since Wed 2022-08-03 08:54:31 CST; 38s ago
     Docs: man:httpd(5)
 Main PID: 48142 (httpd)
    Tasks: 6 (limit: 4744)
   Memory: 5.0M
   CGroup: /system.slice/httpd.service
           ├─48142 /usr/local/apache/bin/httpd -k start
           ├─48148 /usr/local/apache/bin/httpd -k start
           ├─48149 /usr/local/apache/bin/httpd -k start
           ├─48150 /usr/local/apache/bin/httpd -k start
           ├─48151 /usr/local/apache/bin/httpd -k start
           └─48152 /usr/local/apache/bin/httpd -k start

Aug 03 08:54:16 z1 systemd[1]: Starting web server daemon...
Aug 03 08:54:31 z1 apachectl[48139]: AH00558: httpd: Could not reliably dete>
Aug 03 08:54:31 z1 systemd[1]: Started web server daemon.

安装mysql

//安装依赖包
[root@z1 ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

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

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


//解压软件至/usr/local/
[root@z1 ~]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@z1 ~]# cd /usr/local/
[root@z1 local]# ls
apache    bin    include  libexec                              share
apr       etc    lib      mysql-5.7.38-linux-glibc2.12-x86_64  src
apr-util  games  lib64    sbin
[root@z1 local]# mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
[root@z1 local]# ls
apache  apr-util  etc    include  lib64    mysql  share
apr     bin       games  lib      libexec  sbin   src

//修改目录/usr/local/mysql的属主属组
[root@z1 local]# chown -R mysql.mysql mysql
[root@z1 local]# ll
total 0
drwxr-xr-x. 14 root  root  164 Aug  3 08:41 apache
drwxr-xr-x.  6 root  root   58 Aug  3 08:20 apr
drwxr-xr-x.  5 root  root   43 Aug  3 08:24 apr-util
drwxr-xr-x.  2 root  root    6 May 19  2020 bin
drwxr-xr-x.  2 root  root    6 May 19  2020 etc
drwxr-xr-x.  2 root  root    6 May 19  2020 games
drwxr-xr-x.  2 root  root    6 May 19  2020 include
drwxr-xr-x.  2 root  root    6 May 19  2020 lib
drwxr-xr-x.  3 root  root   17 Aug  3 04:02 lib64
drwxr-xr-x.  2 root  root    6 May 19  2020 libexec
drwxr-xr-x.  9 mysql mysql 129 Aug  3 09:07 mysql
drwxr-xr-x.  2 root  root    6 May 19  2020 sbin
drwxr-xr-x.  5 root  root   49 Aug  3 04:02 share
drwxr-xr-x.  2 root  root    6 May 19  2020 src

//配置mysql
[root@z1 local]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@z1 local]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@z1 local]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@z1 local]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man
MANDATORY_MANPATH                       /usr/local/mysql/man

//添加环境变量
[root@z1 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@z1 ~]# source /etc/profile.d/mysql.sh 
[root@z1 ~]# which mysql
/usr/local/mysql/bin/mysql


//建立数据存放目录
[root@z1 ~]# mkdir -p /opt/data
[root@z1 ~]# chown -R mysql.mysql /opt/data/
[root@z1 ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug  3 09:17 data


//初始化数据库
[root@z1 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-08-03T01:19:59.639725Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-03T01:19:59.640021Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-03T01:19:59.713231Z 1 [Note] A temporary password is generated for root@localhost: 2!>srargX1nU   
[root@z1 ~]# echo '2!>srargX1nU' > pass

//卸载mariadb的所有包
[root@z1 ~]# dnf -y remove mariadb*

//生成配置文件
[root@z1 ~]# 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@z1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@z1 ~]# vi /etc/init.d/mysqld
basedir=/usr/local/mysql    
datadir=/opt/data
[root@z1 ~]# chmod +x /etc/init.d/mysqld 

//启动mysql 并设置开机自启
[root@z1 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/z1.err'.
 SUCCESS! 
[root@z1 ~]# 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               [::]:*  
[root@z1 ~]# chkconfig --add mysqld
[root@z1 ~]# 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


//修改密码
//使用临时密码登录
[root@z1 ~]# cat pass 
2!>srargX1nU
[root@z1 ~]# mysql -uroot -p'2!>srargX1nU'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38

Copyright (c) 2000, 2022, 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.00 sec)

安装php

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

//安装依赖包
[root@z1 ~]# 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

//编译安装php
[root@z1 ~]# tar xf php-7.4.30.tar.xz 
[root@z1 ~]# cd php-7.4.30
[root@z1 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
[root@z1 php-7.4.30]# make
编译过程略
[root@z1 php-7.4.30]# make install
安装过程略


//安装后配置
[root@z1 php-7.4.30]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@z1 php-7.4.30]# source /etc/profile.d/php7.sh 
[root@z1 php-7.4.30]# which php
/usr/local/php7/bin/php
[root@z1 php-7.4.30]# php -v
PHP 7.4.30 (cli) (built: Aug  3 2022 10:32:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

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

//启动php-fpm
[root@z1 ~]# service php-fpm start
Starting php-fpm  done

//默认情况下,fpm监听在127.0.0.19000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@z1 ~]# 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                *:*  

//设置开机自启
[root@z1 ~]# chkconfig --add php-fpm
[root@z1 ~]# 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

配置apache

启用代理模块
[root@z1 ~]# vim /usr/local/apache/conf/httpd.conf 
//取消这二行注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 
配置虚拟主机
//创建虚拟主机目录并生成php测试页面
[root@z1 conf]# mkdir -p /usr/local/apache/htdocs/zzz
[root@z1 conf]# cd /usr/local/apache/htdocs/zzz/
[root@z1 zzz]# vim index.php
<?php
    phpinfo()
?> 

//修改配置文件内容
[root@z1 extra]# cd /usr/local/apache/conf/extra/
[root@z1 extra]# vi httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/zzz"
    ServerName zzz.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/zzz/$1
    <Directory "/usr/local/apache/htdocs/zzz">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

[root@z1 conf]# cd /usr/local/apache/conf/
[root@z1 conf]# vim httpd.conf 
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php           //添加此行
    AddType application/x-httpd-php-source .phps   //添加此行

Include conf/extra/httpd-vhosts.conf   //取消注释

<IfModule dir_module>
    DirectoryIndex index.php index.html     //修改此行
</IfModule>

//重启apache服务
[root@z1 conf]# systemctl restart httpd
[root@z1 conf]# ss -anlt
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                *:*  

验证

在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作

安装报错问题

//登录mysql报错
[root@z1 ~]# mysql -uroot -p'2!>srargX1nU'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@z1 ~]# dnf -y install ncurses-compat-libs    //安装这个包
//编译安装php报错
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:

Package 'sqlite3', required by 'virtual:world', not found
[root@z1 php-7.4.30]# dnf -y install libsqlite3x-devel  //安装这个包
//编译安装php报错
configure: error: Package requirements (oniguruma) were not met:

Package 'oniguruma', required by 'virtual:world', not found
[root@z1 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
//安装这个包

//编译安装php报错
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
Package 'libzip', required by 'virtual:world', not found
[root@z1 ~]# dnf -y install libzip-devel   //安装这个包


posted @   世界的尽头*  阅读(313)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示