LAMP架构平台的简介与搭建
文章目录
1. LANP架构
L:Linux
A:apache 提供网页
M:mysql 数据库,存储
P:php 处理动态的网页,也就是用PHP语言所开发的网页
2. web服务器工作流程
CGI:通用网关接口,是Web 服务器运行时外部程序的规范
FastCGI:CGI的改良版
web服务器的资源分为两种,静态资源和动态资源
- 静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
- 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
ps:动态资源是要执行的,用什么语言开发的网页就用什么语言解析执行
图片,视频,是静态资源
如上图所示
阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行
阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
3. lamp平台搭建
lamp平台软件安装顺序:httpd→mysql→php
epel源下载地址
注意:php要求httpd使用prefork MPM
源配置,除了配置国内的yum源,还要配epel源,过程略。
[root@localhost ~]# dnf clean all
[root@localhost ~]# dnf makecache
[root@localhost ~]# dnf -y install wget vim
以上基础操作就不细说
//安装开发工具包
[root@localhost ~]# yum groups mark install 'Development Tools'
3.1 安装apache
//创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
//-r系统用户,-M不设置家目录,-s不允许登录
//安装依赖包
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
//下载和安装apr和apr-util,以及编译安装httpd
安装包怎么下载见我之前的文章,这里就不细说了
[root@localhost ~]# ls
anaconda-ks.cfg httpd-2.4.53.tar.gz
apr-1.7.0.tar.bz2
apr-util-1.6.1.tar.gz
[root@localhost ~]# yum -y install bzip2 //系统默认没有解压bz2格式压缩包的工具
[root@localhost ~]# tar xf apr-util-1.6.1.tar.gz
[root@localhost ~]# tar xf httpd-2.4.53.tar.gz
[root@localhost ~]# tar xf apr-1.7.0.tar.bz2
[root@localhost ~]# cd apr-1.7.0
[root@localhost apr-1.7.0]# vim configure
找到$RM "cfgfile",删除或注释它
[root@localhost apr-1.7.0]#./configure -- prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make //编译
[root@localhost apr-1.7.0]# make install //make install安装,将编译好的二进制文件拷贝到指定的安装路径下,自动创建目录
[root@localhost ~]# 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
[root@localhost apr-util-1.6.1]# make install
[root@localhost ~]# cd httpd-2.4.53
[root@localhost httpd-2.4.53]# ./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@localhost httpd-2.4.53]# make
[root@localhost httpd-2.4.53]# make install
[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# ls
bin cgi-bin error icons logs manual
build conf htdocs include man modules
1. 设置环境变量bin
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@localhost ~]# source /etc/profile.d/apache.sh
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd
2. 有include头文件,做映射关系
[root@localhost ~]# ln -s /usr/local/apache/include /usr/include/apache
3. 有man文档
[root@localhost ~]# 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/apache/man
4.
[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
......
#ServerName www.example.com:80
找到这一行并把注释取消掉,保存
[root@localhost ~]# systemctl restart httpd //服务重启
5. 设置apache服务开机自启,配置service文件
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service httpd.service
[root@localhost system]# vim httpd.service
[Unit]
Description=httpd server daemon
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@localhost system]# systemctl daemon-reload //重新加载服务文件
[root@localhost system]# cd
[root@localhost ~]# systemctl status httpd
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled>
Active: inactive (dead)
[root@localhost ~]# systemctl enable --now httpd //设置开机自启并立马启动
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl status httpd
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled;>
Active: active (running) since Fri 2022-04-22 04:53:29 CST; 1mi>
......
6. 关闭防火墙:
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0 //设置enforce为零
[root@localhost ~]# vim /etc/selinux/config
......
SELINUX=enforcing 改为:SELINUX=disabled
......
3.2 安装mysql
详见我之前的文章mysql基础
3.3 安装php
按照上面的图下载PHP的软件包:
[root@localhost ~]# wget https://www.php.net/distributions/php-7.4.29.tar.xz
[root@localhost ~]# tar xf php-7.4.29.tar.xz
[root@localhost ~]# cd php-7.4.29
安装依赖包:
[root@localhost php-7.4.29]# yum -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
[root@localhost php-7.4.29]# dnf list all|grep mysql|grep php
php-mysqlnd.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream
[root@localhost php-7.4.29]# dnf -y install php-mysqlnd
编译安装php:
[root@localhost php-7.4.29]# ./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@localhost php-7.4.29]# dnf -y install sqlite-devel
[root@localhost php-7.4.29]# dnf list all|grep oniguruma
oniguruma.i686 6.8.2-2.el8 AppStream
oniguruma.x86_64 6.8.2-2.el8 AppStream
去pkgs.org网站上找到oniguruma.devel并下载安装:
[root@localhost php-7.4.29]# dnf -y install https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@localhost php-7.4.29]# dnf -y install libzip-devel
[root@localhost php-7.4.29]# ./configure --help|grep jpeg
--with-jpeg GD: Enable JPEG support (only for bundled libgd)
[root@localhost php-7.4.29]# ./configure --help|grep freetype
--with-freetype GD: Enable FreeType 2 support (only for bundled
[root@localhost php-7.4.29]# ./configure --help|grep zip
--with-zip Include Zip read/write support
利用这些信息对上面的配置进行调整,上面已经被我调整过了
[root@localhost php-7.4.29]# make
[root@localhost php-7.4.29]# make install
配置环境变量:
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost ~]# source /etc/profile.d/php7.sh
[root@localhost php-7.4.29]# which php
/usr/local/php7/bin/php
启用代理模块:
[root@localhost ~]# cd /usr/local/apache/conf/
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# vim httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
//取消这两行的注释
配置虚拟主机:
[root@localhost conf]# cd ..
[root@localhost apache]# ls
bin cgi-bin error icons logs manual
build conf htdocs include man modules
[root@localhost apache]# cd htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mkdir test.com/
[root@localhost htdocs]# cd test.com/
[root@localhost test.com]# vim index.php
<?php
phpinfo;
?>
[root@localhost apache]# vim conf/httpd.conf
//在配置文件的最后加入以下内容
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/test.com"
ServerName test.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test.com/$1
<Directory "/usr/local/apache/htdocs/test.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
......
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //添加此行
AddType application/x-httpd-php-source .php //添加此行
......
DirectoryIndex index.php index.html
//添加index.php
[root@localhost php-7.4.29]# php -v
PHP 7.4.29 (cli) (built: Apr 22 2022 07:20:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
[root@localhost php-7.4.29]# \cp php.ini-production /etc/php.ini
[root@localhost php-7.4.29]# cp sapi/fpm/php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.29]# ll /etc/init.d/php-fpm
-rwxr-xr-x. 1 root root 54696280 Apr 22 07:52 /etc/init.d/php-fpm
[root@localhost php-7.4.29]# cd /usr/local/php7/
[root@localhost php7]# ls
bin etc include lib php sbin var
[root@localhost php7]# cd etc
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf www.conf.default
未完待续