LAMP架构是目前成熟的企业网站应用模式之一,指的是协同工作的一整台系统和相关软件,能够提供动态web站点服务及其应用开发环境.
LAMP指的是Linux
(操作系统)、Apache
(HTTP
服务器)、MySQL
(也指MariaDB
,数据库软件)和PHP
(有时也是指Perl
或Python
)的第一个字母,一般用来建立web
应用平台。
httpd --> mysql --> php
注意:php要求httpd使用prefork MPM
举例:
比如需要部署apache服务
需要到官网下载软件包后解压
创建系统用户
下载依赖包
编译
配置环境变量等
然后启动
开启三台主机
node1
部署apache
node2
部署mysql
node3
部署PHP
主机名 | ip | 系统平台 |
---|---|---|
node1(apache) | 192.168.229.148 | centos8/redhat8 |
node2(mysql) | 192.168.229.150 | centos8/redhat8 |
node3(PHP) | 192.168.229.151 | centos8/redhat8 |
阿里云官网:https://developer.aliyun.com/
# 删除自带yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# ls
CentOS-Stream-AppStream.repo CentOS-Stream-HighAvailability.repo CentOS-Stream-RealTime.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-Media.repo CentOS-Stream-ResilientStorage.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-NFV.repo CentOS-Stream-Sources.repo
CentOS-Stream-Extras.repo CentOS-Stream-PowerTools.repo
[root@localhost yum.repos.d]# rm -rf *
# 配置基础源
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
# 清除缓存
[root@localhost yum.repos.d]# dnf clean all
# 建立缓存
[root@localhost yum.repos.d]# dnf makecache
# 配置epel源
[root@localhost yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
# 清除缓存
[root@localhost yum.repos.d]# dnf clean all
0 files removed
# 建立缓存
[root@localhost yum.repos.d]# dnf makecache
# 关闭防火墙和SElinux
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# head -7 /etc/selinux/config | tail -1
SELINUX=disabled # 把它修改为disabled
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
apache官网: https://apache.org/
# 下载软件包
[root@node1 ~]# cd /usr/src/
[root@node1 src]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@node1 src]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@node1 src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
# 解压软件包
[root@node1 src]# tar xf apr-1.7.0.tar.gz
[root@node1 src]# tar xf apr-util-1.6.1.tar.gz
[root@node1 src]# tar httpd-2.4.54.tar.gz
# 创建一个系统用户
[root@node1 ~]# useradd -r -M -s /sbin/nologin apache
[root@node1 ~]# id apache
uid=995(apache) gid=992(apache) groups=992(apache)
# 安装开发工具包(标志安装)
[root@node1 ~]# dnf -y groups mark install "Development Tools"
# 安装依赖包
[root@node1 ~]# dnf -y install openssl-devel pcre-devel expat-devel boost-devel --allowerasing make gcc gcc-c++
因为安装gcc报错所以需要下载 boost-devel --allowerasing
#需要进去apr-1.7.0里面的configure
[root@node1 ~]# cd /usr/src/apr-1.7.0
[root@node1 apr-1.7.0]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
#/cfgfile 左斜杠查找cfgfile
# 编译Apr
[root@node1 ~]# cd /usr/src/apr-1.7.0
[root@node1 apr-1.7.0]# ./configure --prefix=/usr/local/apr
......省略N
[root@node1 apr-1.7.0]# make
......省略N
[root@node1 apr-1.7.0]# make install
......省略N
# 编译Apr-util
[root@node1 apr-1.7.0]# cd ../apr-util-1.6.1
[root@node1 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
....省略N
[root@node1 apr-util-1.6.1]# make
......省略N
[root@node1 apr-util-1.6.1]# make install
......省略N
# 编译httpd
[root@node1 apr-util-1.6.1]# cd ../httpd-2.4.54
[root@node1 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
......省略N
[root@node1 httpd-2.4.54]# make
......省略N
[root@node1 httpd-2.4.54]# make install
# 设置环境变量
[root@node1 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@node1 ~]# source /etc/profile.d/apache.sh
[root@node1 ~]# which httpd
/usr/local/apache/bin/httpd
# 配置头文件
[root@node1 ~]# ln -s /usr/local/apache/include /usr/include/apache
# 设置man文档
[root@node1 ~]# 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 # 添加
# 配置service 文件,做这一步是为了能够开机自启
[root@node1 ~]# cd /usr/lib/systemd/system
[root@node1 system]# cp sshd.service httpd.service
[root@node1 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@node1 ~]# systemctl daemon-reload
# 设置开机自启
[root@node1 ~]# systemctl enable --now httpd
MySQL官网:https://downloads.mysql.com/
找到 [MySQL Community (GPL) Downloads »](https://dev.mysql.com/downloads/)
# 安装依赖包
[root@node2 ~]# dnf -y install ncurses-compat-libs openssl-devel openssl cmake mariadb-devel
# 创建mysql系统用户
[root@node2 ~]# useradd -r -M -s /sbin/nologin mysql
[root@node2 ~]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
# 下载二进制格式的mysql软件包
[root@node2 ~]# cd /usr/src/
[root@node2 src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
# 解压软件至/usr/local/
[root@node2 ~]# cd /usr/src/
[root@node2 src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
# 重命名为 mysql
[root@node2 ~]# cd /usr/local/
[root@node2 local]# mv mysql-5.7.37-linux-glibc2.12-x86_64 mysql
[root@node2 local]# ll mysql/ -d
drwxr-xr-x. 9 root root 129 Jul 4 19:09 mysql/
# 修改目录/usr/local/mysql的属主属组
[root@node2 local]# chown -R mysql.mysql mysql
[root@node2 local]# ll mysql/ -d
drwxr-xr-x. 9 mysql mysql 129 Jul 4 19:09 mysql/
# 添加环境变量
[root@node2 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@node2 ~]# source /etc/profile.d/mysql.sh
[root@node2 ~]# which mysqld
/usr/local/mysql/bin/mysqld
# 配置include
## 软链接到/usr/include/mysql,叫mysql
[root@node2 ~]# ln -s /usr/local/mysql/include /usr/include/mysql
# 配置 man 文档
[root@node2 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man # 添加这条
# 映射库文件
[root@node2 ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@node2 ~]# ldconfig # 让其生效
# 建立数据存放目录
[root@node2 ~]# mkdir /opt/data
[root@node2 ~]# chown -R mysql.mysql /opt/data/
[root@node2 ~]# ll /opt/data/ -d
drwxr-xr-x. 2 mysql mysql 6 Jul 4 19:20 /opt/data/
# 初始化数据库
[root@node2 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
......省略
root@localhost: V2hQugC(hAh9
# 请注意,这个命令的最后会生成一个临时密码,此处密码是V2hQugC(hAh9ik
# 再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
# 生成配置文件
[root@node2 ~]# 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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 配置服务启动脚本
[root@node2 ~]# cd /usr/local/mysql/support-files/
[root@node2 support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
# 复制
[root@node2 support-files]# cp mysql.server mysqld
[root@node2 support-files]# ll mysqld -d
-rwxr-xr-x. 1 root root 10576 Jul 4 19:37 mysqld
# 设置所有者所属组为mysqld
[root@node2 support-files]# chown -R mysql.mysql mysqld
[root@node2 support-files]# ll mysqld -d
-rwxr-xr-x. 1 mysql mysql 10576 Jul 4 19:37 mysqld
[root@node2 support-files]# vim mysqld
basedir=/usr/local/mysql # 找到这两行添加/usr/local/mysql
datadir=/opt/data # 找到这两行添加/opt/data
# 启动mysql
[root@node2 ~]# /usr/local/mysql/support-files/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 *:*
# 修改密码
# 使用临时密码登录
[root@node2 ~]# mysql -uroot -p
Enter password: # 输入密码
......省略
# 设置新密码
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 退出登录验证密码
[root@node2 ~]# mysql -uroot -pruntime123!
....省略
mysql>
# 配置service 让它开机自启
[root@node2 ~]# cd /usr/lib/systemd/system
[root@node2 system]# cp sshd.service mysqld.service
[root@node2 system]# vim mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
# 开机自启
[root@node2 ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
PHP官网:https://www.php.net/
# 下载 PHP
[root@node3 ~]# cd /usr/src/
[root@node3 src]# wget https://www.php.net/distributions/php-7.4.29.tar.xz
........省略
# 解压
[root@node3 ~]# cd /usr/src/
[root@node3 src]# tar xf php-7.4.29.tar.xz
[root@node3 src]# ls
debug kernels php-7.4.29 php-7.4.29.tar.xz
# 这是查找MySQL跟PHP打交道的包名
[root@node3 ~]# dnf list all | grep mysql | grep php
php-mysqlnd.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream
# 下载编译器
[root@node3 ~]# dnf -y install boost-devel --allowerasing gcc gcc-c++ make
# 安装依赖包
[root@node3 ~]# dnf -y install 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 sqlite-devel libzip-devel php-mysqlnd
安装过程略....
# 安装 oniguruma 包
[root@node3 ~]# 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
[root@node3 ~]# cd /usr/src/php-7.4.29
[root@node3 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
# make
[root@node3 php-7.4.29]# make
....编译过程略
[root@node3 php-7.4.29]# make install
.....安装过程略
# 安装后配置
[root@node3 ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@node3 ~]# source /etc/profile.d/php7.sh
[root@node3 ~]# which php
/usr/local/php7/bin/php
# 配置头文件
[root@node3 ~]# ln -s /usr/local/php7/include /usr/include/php7
# 配置库文件
[root@node3 ~]# echo '/usr/local/php7/lib' > /etc/ld.so.conf.d/php7.conf
[root@node3 ~]# ldconfig
# 查看版本
[root@node3 ~]# php -v
PHP 7.4.29 (cli) (built: Jul 4 2022 21:07:32) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
# 配置启动服务脚本php-fpm
[root@node3 ~]# cd /usr/src/php-7.4.29
[root@node3 php-7.4.29]# \cp php.ini-production /etc/php.ini # 这个文件已存在所以要加上\表示覆盖
[root@node3 php-7.4.29]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@node3 php-7.4.29]# chmod +x /etc/init.d/php-fpm
[root@node3 ~]# cd /usr/local/php7/etc
[root@node3 etc]# cp php-fpm.conf.default php-fpm.conf
[root@node3 etc]# cd php-fpm.d
[root@node3 php-fpm.d]# cp www.conf.default www.conf
# 编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
# 配置fpm的相关选项为你所需要的值:
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50 ;最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 ;启动时启动5个进程
pm.min_spare_servers = 2 ;最小空闲进程数
pm.max_spare_servers = 8 ;最大空闲进程数
[root@node3 ~]# tail /usr/local/php7/etc/php-fpm.conf
; file.
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
# 查看端口号是否监听在9000上
[root@node3 php-fpm.d]# pwd
/usr/local/php7/etc/php-fpm.d
[root@node3 php-fpm.d]# vim www.conf
listen = 127.0.0.1:9000 # 查看端口号是否监听在9000上
listen = 192.168.229.151:9000 # 添加PHP的IP
;listen.allowed_clients = 127.0.0.1
;listen.allowed_clients = 192.168.229.148 # 添加apache的ip
# 启动php-fpm
# 默认情况下,fpm监听在127.0.0.1的9000端口但是使用三台主机部署LAMP所以需要让它监听本机ip,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@node3 ~]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@node3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 192.168.229.151:9000
LISTEN 0 128 0.0.0.0:22
LISTEN 0 128 [::]:22
# 杀掉进程
[root@node3 ~]# pkill php-fpm
[root@node3 ~]# 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 [::]:*
# 配置service 文件
[root@node3 ~]# cd /usr/lib/systemd/system
[root@node3 system]# cp sshd.service php.service
[root@node3 system]# cat php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/usr/bin/kill -9 $MAINPID
ExecReload=/usr/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@node3 system]# systemctl daemon-reload # 让文件生效
# 启动、并开机自启
[root@node3 system]# systemctl enable --now php
[root@node3 system]# systemctl status php
● php.service - php7 server daemon
Loaded: loaded (/usr/lib/systemd/system/php.service; enabled; vendor pr>
Active: active (running) since Mon 2022-07-25 10:45:01 CST; 1min 54s ago
Main PID: 146870 (php-fpm)
[root@node3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 192.168.229.151:9000
LISTEN 0 128 0.0.0.0:22
LISTEN 0 128 [::]:22
# 创建虚拟主机目录并生成php测试页面
[root@node3 ~]# mkdir /test.com
[root@node3 ~]# cd /test.com
[root@node3 test.com]# vim index.php
# 重启php
[root@node3 ~]# systemctl restart php
# 启用httpd的相关模块
[root@node1 ~]# cd /usr/local/apache/conf/
[root@node1 conf]# vim httpd.conf #把下面两条代码的注释取消掉
LoadModule proxy_module modules/mod_proxy.so # 取消前面的注释
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so # 取消前面的注释
# 配置虚拟主机
[root@node1 ~]# cd /usr/local/apache/conf/extra/
[root@node1 extra]# ls
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf
[root@node1 extra]# vim httpd-vhosts.conf
DocumentRoot "/usr/local/apache/htdocs"
ServerName test.com
ErrorLog "logs/test.com-error_log"
CustomLog "logs/test.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.229.151:9000/test.com/$1
Options none
AllowOverride none
Require all granted
# 包含虚拟主机
[root@node1 ~]# cd /usr/local/apache/conf/
[root@node1 conf]# vim httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf # 把前面的注释取消
[root@node1 ~]# cd /usr/local/apache/conf/
[root@node1 conf]# ls
extra httpd.conf magic mime.types original
[root@node1 conf]# vim httpd.conf
DirectoryIndex index.php index.html # 添加 index.php
# 搜索index.html,添加以下内容
[root@node1 conf]# pwd
/usr/local/apache/conf
[root@node1 conf]# vim httpd.conf
#Listen 12.34.56.78:80
Listen 192.168.229.148:80 # 在80的前面添加本机ip就是apache的
[root@node1 ~]# vim /usr/local/apache/conf/httpd.conf
# 搜索AddType,添加以下内容
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php #添加此行
AddType application/x-httpd-php-source .phps #添加此行
# 重启apache服务
[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 192.168.229.148:80
LISTEN 0 128 0.0.0.0:22
LISTEN 0 128 [::]:22
ip 访问
域名访问