lamp
1.lamp简介
lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。 LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。2.web服务器工作流程
2.1 cgi与fastcgi
在说lamp架构平台的搭建前,我们先来了解下什么是CGI,什么是FastCGI
- CGI(Common Gateway Interface,通用网关接口)
CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。 - FastCGI(Fast Common Gateway Interface)
是CGI的改良版,CGI是通过启用一个解释器进程来处理每个请求,耗时且耗资源,而FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求,这样就避免了重复的生成和杀死进程带来的频繁cpu上下文切换而导致耗时。
web服务器的资源分为两种,静态资源和动态资源
- 静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
- 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求
如上图所示
阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行
阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
2.2httpd与php结合的方式
httpd与php结合的方式有以下三种:
- modules:php将以httpd的扩展模块形式存在,需要加载动态资源时,httpd可以直接通过php模块来加工资源并返回给客户端
httpd prefork:libphp5.so(多进程模型的php)
httpd event or worker:libphp5-zts.so(线程模型的php) - CGI:httpd需要加载动态资源时,通过CGI与php解释器联系,获得php执行的结果,此时httpd负责与php连接的建立和断开等
- FastCGI:利用php-fpm机制,启动为服务进程,php自行运行为一个服务,https通过socket与php通信
较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源
2.3 web工作流程
- 客户端通过http协议请求web服务器资源
- web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
若是静态资源则直接从本地文件系统取之返回给客户端。
否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,网站服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给网站,网站将从php服务器收到的执行结果封装成http响应报文响应给客户端。
3. lamp平台构建
系统平台 | IP | 需要安装的服务 |
---|---|---|
centos8 redhat7 | 192.168.249.141 | httpd-2.4 mysql-5.7 php php-phpmysql |
注意服务安装是有次序的。
lamp平台软件安装次序:
httpd --> mysql --> php
php要求httpd使用prefork MPM
3.1 安装httpd
//安装epel和vim
[root@localhost ~]# dnf -y install epel-release vim
//查看一下包组并安装开发工具“Development Tools”
[root@localhost ~]# dnf grouplist
Extra Packages for Enterprise Linux Modular 8 - x86_64 71 kB/s | 559 kB 00:07
Extra Packages for Enterprise Linux 8 - x86_64 11 kB/s | 9.4 MB 14:13
上次元数据过期检查:0:00:04 前,执行于 2021年05月12日 星期三 22时17分29秒。
可用环境组:
带 GUI 的服务器
服务器
工作站
KDE Plasma 工作区
虚拟化主机
定制操作系统
已安装的环境组:
最小安装
可用组:
容器管理
.NET 核心开发
RPM 开发工具
开发工具
图形管理工具
无头系统管理
传统 UNIX 兼容性
网络服务器
科学记数法支持
安全性工具
智能卡支持
系统工具
Fedora Packager
Xfce
[root@localhost ~]# dnf -y groups mark install "Development Tools"
//创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
//安装依赖包
[root@localhost ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
//下载和安装apr以及apr-util、httpd,这里我们下载好了直接xftp上传
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
//安装一个bzip2,不然不能解压
[root@localhost ~]# dnf -y install bzip2
//分别解压
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
[root@localhost ~]# tar xf apr-1.7.0.tar.bz2
[root@localhost ~]# tar xf apr-util-1.6.1.tar.bz2
[root@localhost ~]# tar xf httpd-2.4.43.tar.bz2
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
apr-1.7.0 apr-util-1.6.1 httpd-2.4.43
[root@localhost ~]# cd apr-1.7.0
[root@localhost apr-1.7.0]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
//配置编译安装
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make && make install
[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
[root@localhost 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
[root@localhost httpd-2.4.43]# 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 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/share/man
//加上这个路径
MANDATORY_MANPATH /usr/local/apache/man
//取消ServerName前面的注释
[root@localhost ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
//启动apache
[root@localhost ~]# apachectl start
[root@localhost ~]# 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 [::]:22 [::]:*
LISTEN 0 128 *:80 *:*
//关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
//直接设置永久关闭
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled //这一行改成这样
3.2安装mysql
//安装依赖包
[root@localhost ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=993(mysql) gid=990(mysql) 组=990(mysql)
//下载二进制格式的mysql安装包
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
apr-1.7.0 apr-util-1.6.1 httpd-2.4.43 mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
//解压软件至/usr/local/
[root@localhost ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
//名字太长可以改个名字
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache apr-util etc include lib64 mysql-5.7.33-linux-glibc2.12-x86_64 share
apr bin games lib libexec sbin src
[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls
apache apr apr-util bin etc games include lib lib64 libexec mysql sbin share src
//修改属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll
drwxr-xr-x. 9 mysql mysql 129 5月 12 23:38 mysql
//添加环境变量
[root@localhost local]# cd
[root@localhost ~]# ls /usr/local/mysql/
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/lib
[root@localhost ~]# ldconfig
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/apache/man
MANDATORY_MANPATH /usr/local/mysql/man
//建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
//初始化
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2021-05-12T15:59:24.645780Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-12T15:59:24.986224Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-12T15:59:25.037931Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-12T15:59:25.103732Z 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: 060f2640-b33b-11eb-9e5d-000c2948795d.
2021-05-12T15:59:25.105883Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-12T15:59:25.500300Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-12T15:59:25.843397Z 1 [Note] A temporary password is generated for root@localhost: FqYzKscpN9+O //密码
//生成配置文件,没有实质性的东西,直接覆盖,要是之前有,先备份
[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
[root@localhost ~]# cat /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@localhost ~]# ls /usr/local/mysql/
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# ls /usr/local/mysql/support-files/
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql //添加它的安装目录
datadir=/opt/data //添加数据存放目录
//启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# 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 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
//修改密码
//使用临时密码登录
root@localhost ~]# mysql -uroot -p'FqYzKscpN9+O'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@localhost ~]# dnf whatprovides libncurses.so.5
上次元数据过期检查:1:55:39 前,执行于 2021年05月12日 星期三 22时17分29秒。
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
仓库 :baseos
匹配来源:
提供 : libncurses.so.5
//报错,安装这个包
[root@localhost ~]# dnf -y install ncurses-compat-libs
//再次登录就可以了
[root@localhost ~]# mysql -uroot -p'FqYzKscpN9+O'
mysql: [Warning] Using a password on the command line interface can be insecure.
//设置密码
mysql> set password = password('lixirong123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//新密码登录
[root@localhost ~]# mysql -uroot -p'lixirong123!'
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql>
//设置开机自启
[root@localhost ~]# chkconfig --add mysqld
3.3 安装php
//yum安装php
[root@localhost ~]# dnf -y install php*
//上面安装会装httpd,所以移除
[root@localhost ~]# rpm -e --nodeps httpd
//安装依赖包
[root@localhost ~]# 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-fpm
[root@localhost ~]# systemctl start php-fpm
//设置开机自启
[root@localhost ~]# systemctl enable php-fpm
[root@localhost ~]# ls /etc/php-fpm.d/
www.conf
[root@localhost ~]# vim /etc/php-fpm.d/www.conf
;listen = /run/php-fpm/www.sock //找到这个位置在下面添加如下端口号
listen = 0.0.0.0:9000
//重新启动,就可以看到端口号了
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# 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 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
3.4 配置apache
3.4.1 启用代理模块
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:
- LoadModule proxy_module modules/mod_proxy.so
- LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
//找到这两行取消注释
[root@localhost ~]# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
3.4.2 配置虚拟主机
//创建虚拟主机目录并生成php测试页面
[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# cd /usr/local/apache/htdocs
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mkdir lixirong.com
[root@localhost htdocs]# cd lixirong.com/
[root@localhost lixirong.com]# vim index.php
<?php
phpinfo();
?>
//修改属主属组
[root@localhost lixirong.com]# chown -R apache.apache /usr/local/apache/htdocs/
[root@localhost lixirong.com]# ll /usr/local/apache/
drwxr-xr-x. 3 apache apache 44 5月 13 01:19 htdocs
//修改主配置文件
[root@localhost httpd24]# 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 //插入此行
<IfModule dir_module>
DirectoryIndex index.php index.html //插入index.php
#Include /etc/httpd24/extra/httpd-default.conf
Include /etc/httpd24/extra/vhosts.conf //插入此行,取消注释
//配置虚拟机
[root@localhost ~]# cd /etc/httpd24/
[root@localhost httpd24]# ls
extra httpd.conf magic mime.types original
[root@localhost httpd24]# cd extra/
[root@localhost extra]# ls
httpd-autoindex.conf httpd-info.conf httpd-mpm.conf httpd-userdir.conf
httpd-dav.conf httpd-languages.conf httpd-multilang-errordoc.conf httpd-vhosts.conf
httpd-default.conf httpd-manual.conf httpd-ssl.conf proxy-html.conf
[root@localhost extra]# vim vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/lixirong.com"
ServerName www.lixirong.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/lixirong.com/$1
<Directory "/usr/local/apache/htdocs/lixirong.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//重启apache服务
[root@localhost ~]# pkill httpd
[root@localhost ~]# 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 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@localhost ~]# apachectl start
[root@localhost ~]# 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 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
3.5 验证
1.在电脑主机修改C:\Windows\System32\drivers\etc\hosts文件,添加域名与IP的映射。(注意将文件hosts拖到桌面修改之后再拖回)
192.168.249.141 www.lixirong.com
2.在浏览器上进行访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作。
IP访问
域名访问