部署LAMP

搭建lamp架构

1.LAMP架构介绍

所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。

2.架构说明

LAMP架构说明.png

apache主要实现如下功能:

第一:处理http的请求、构建响应报文等自身服务;

第二:配置让Apache支持PHP程序的响应(通过PHP模块或FPM);

第三:配置Apache具体处理php程序的方法,如通过反向代理将php程序交给fcgi处理。

mariadb主要实现如下功能:

第一:提供PHP程序对数据的存储;

第二:提供PHP程序对数据的读取(通常情况下从性能的角度考虑,尽量实现数据库的读写分离)。

php主要实现如下功能:

第一:提供apache的访问接口,即CGI或Fast CGI(FPM);

第二:提供PHP程序的解释器;

第三:提供mairadb数据库的连接函数的基本环境。

由此可知,要实现LAMP在配置每一个服务时,安装功能需求进行配置,即可实现LAMP的架构,当然apache、mariadb和php服务都可配置为独立服务,安装在不同服务器之上。

3.lamp平台搭建

环境说明

系统平台 IP 需要安装的服务
centos8 192.168.142.130 http
mysql
php
php-mysql

lamp平台软件安装次序:

http - - > mysql - - > php			//顺序不可逆

阿里云

配置一下仓库源

[root@Soap ~]# cd /etc/yum.repo.d/
[root@Soap yum.repos.d]# rm -rf *
[root@Soap yum.repos.d]# cd
[root@Soap ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@Soap ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@Soap ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@Soap ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@Soap ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

3.1 编译安装httpd

//先下载需要的依赖包
[root@Soap ~]# dnf -y groupinstall "Development Tools" --allowerasing
[root@Soap ~]# dnf -y install wget openssl-devel pcre-devel expat-devel libtool libxml2-devel make gcc gcc-c++

//创建一个apache的用户
[root@Soap ~]# useradd -r -M -s /sbin/nologin apache
[root@Soap ~]# id apache
uid=994(apache) gid=991(apache) groups=991(apache)

//wget下载安装httpd所需要的包
[root@Soap ~]# wget http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz http://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz http://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
[root@Soap ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz

//解压并编译
[root@Soap ~]# tar xf apr-1.7.0.tar.gz 
[root@Soap ~]# tar xf apr-util-1.6.1.tar.gz 
[root@Soap ~]# tar xf httpd-2.4.54.tar.gz 
[root@Soap ~]# ls
anaconda-ks.cfg  apr-1.7.0  apr-1.7.0.tar.gz  apr-util-1.6.1  apr-util-1.6.1.tar.gz  httpd-2.4.54  httpd-2.4.54.tar.gz

//进入apr-1.7.0的configure删除这一行或者注释这一行
[root@Soap ~]# cd apr-1.7.0
[root@Soap apr-1.7.0]# vim configure
cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    #$RM "$cfgfile"			//注释这行

//编译apr
[root@Soap apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@Soap apr-1.7.0]# make && make install

//编译apr-util
[root@Soap httpd-2.4.54]# cd ../apr-util-1.6.1
[root@Soap apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@Soap apr-util-1.6.1]# make && make install

//编译httpd
[root@Soap ~]# cd httpd-2.4.54
[root@Soap 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-rpm=prefork
[root@Soap httpd-2.4.54]# make && make install

//配置环境变量
[root@Soap ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@Soap ~]# source /etc/profile.d/apache.sh
[root@Soap ~]# which apachectl
/usr/local/apache/bin/apachectl

//创建头文件软连接
[root@Soap ~]# ln -s /usr/local/apache/include/ /usr/include/apache

//添加帮助文档
[root@Soap ~]# 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		//添加

//取消警报
[root@Soap ~]# vim /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80 		//取消这行注释

//编写控制脚本
[root@Soap ~]# cd /usr/lib/systemd/system/
[root@Soap system]# cp sshd.service httpd.service
[root@Soap 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
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/apache/bin/apachectl stop

[Install]
WantedBy=multi-user.target
[root@Soap ~]# systemctl daemon-reload			//刷新一下

//设置开机自启动
[root@Soap ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@Soap ~]# 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                                    *:80                                  *:*                                
LISTEN           0                128                                 [::]:22                               [::]:*                                

//关闭防火墙和selinux
[root@Soap ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@Soap ~]# vim /etc/selinux/config 
SELINUX=disabled
[root@Soap ~]# setenforce 0

3.2 二进制安装mysql

//安装依赖包
[root@Soap ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

//创建用户
[root@Soap ~]# useradd -r -M -s /sbin/nologin mysql
[root@Soap ~]# id mysql
uid=993(mysql) gid=990(mysql) groups=990(mysql)

//下载二进制包
[root@Soap ~]# cd /usr/src/
[root@Soap src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

//解压软件包
[root@Soap src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

//更改软件名,更改属主属组
[root@Soap src]# cd /usr/local/
[root@Soap local]# mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
[root@Soap local]# chown -R mysql.mysql mysql*
[root@Soap local]# ll -d mysql
drwxr-xr-x. 9 mysql mysql 129 Aug  2 19:08 mysql

//配置环境变量
[root@Soap local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@Soap local]# source /etc/profile.d/mysql.sh

//创建头文件
[root@Soap local]# ln -s /usr/local/mysql/include /usr/include/mysql

//添加帮助文档
[root@Soap ~]# 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@Soap ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@Soap ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@Soap ~]# ldconfig 

//创建数据存放目录
[root@Soap ~]# mkdir /opt/data
[root@Soap ~]# chown -R mysql.mysql /opt/data/
[root@Soap ~]# ll -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 Aug  2 19:13 /opt/data/

//初始化
[root@Soap ~]# mysqld --initialize --user mysql --datadir /opt/data/
2022-08-02T11:33:30.837163Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-02T11:33:30.882575Z 1 [Note] A temporary password is generated for root@Soap: ,7//AkYZV3tZ		//最后一行会生成临时密码
[root@Soap ~]# echo ',7//AkYZV3tZ' > passwd		//记录一下密码

//编写配置文件
[root@Soap ~]# dnf -y remove mariadb*
[root@Soap ~]# 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@Soap ~]# cd /usr/lib/systemd/system/
[root@Soap system]# cp httpd.service mysqld.service
[root@Soap system]# vim mysqld.service 
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@Soap ~]# systemctl daemon-reload		//刷新一下

//设置开机自启
[root@Soap ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@Soap ~]# 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                80                                     *:3306                                *:*                                
LISTEN           0                128                                    *:80                                  *:*                                
LISTEN           0                128                                 [::]:22                               [::]:*                                

//设置密码
[root@Soap ~]# cat passwd 
,7//AkYZV3tZ
[root@Soap ~]# mysql -uroot -p',7//AkYZV3tZ'
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('Qwer1234!');  //修改密码, ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY 'Qwer1234!';(mysql8.0使用这条命令进行修改密码)
Query OK, 0 rows affected, 1 warning (0.00 sec)

//登录查看是否修改成功
[root@Soap ~]# mysql -uroot -pQwer1234!
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 3
Server version: 5.7.38 MySQL Community Server (GPL)

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> 

3.3 编译安装php

//下载php软件包
[root@Soap ~]# cd /usr/src/
[root@Soap src]# wget https://www.php.net/distributions/php-7.4.30.tar.gz

//解压软件包
[root@Soap src]# tar xf php-7.4.30.tar.gz 
[root@Soap src]# ls
debug  kernels  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  php-7.4.30  php-7.4.30.tar.gz

//安装依赖包
[root@Soap ~]# 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
[root@Soap ~]# 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
[root@Soap ~]# dnf -y install libsqlite3x-devel
[root@Soap ~]# dnf -y install libzip-devel

//编译安装
[root@Soap ~]# cd /usr/src/
[root@Soap src]# cd php-7.4.30
[root@Soap 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@Soap php-7.4.30]# make && make install

//配置环境变量
[root@Soap local]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php.sh
[root@Soap local]# source /etc/profile.d/php.sh
[root@Soap local]# which php
/usr/local/php7/bin/php
[root@Soap php-7.4.30]# php -v
PHP 7.4.30 (cli) (built: Aug  2 2022 22:27:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

//配置php-fpm
[root@Soap php-7.4.30]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@Soap php-7.4.30]# cd sapi/fpm/
[root@Soap fpm]# file init.d.php-fpm
init.d.php-fpm: POSIX shell script, ASCII text executable
[root@Soap fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@Soap fpm]# chmod +x /etc/init.d/php-fpm
[root@Soap fpm]# cd /usr/local/php7/
[root@Soap php7]# ls
bin  etc  include  lib  php  sbin  var
[root@Soap php7]# cd etc/
[root@Soap etc]# cp php-fpm.conf.default php-fpm.conf
[root@Soap etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@Soap etc]# cd php-fpm.d/
[root@Soap php-fpm.d]# ls
www.conf.default
[root@Soap php-fpm.d]# cp www.conf.default www.conf
[root@Soap php-fpm.d]# ls
www.conf  www.conf.default

//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@Soap ~]# vim /usr/local/php7/etc/php-fpm.conf
[root@Soap ~]# tail /usr/local/php7/etc/php-fpm.conf
; files from a glob(3) pattern. This directive can be used everywhere in the
; 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		//最多同时提供50个进程提供50个并发服务
pm.start_servers = 5		//启动时启动5个进程
pm.min_spare_servers = 2	//最小空闲进程数
pm.max_spare_servers = 8	//最大空闲进程数

//取消httpd.conf的注释,启动httpd的相关模块
[root@Soap php-fpm.d]# vim /usr/local/apache/conf/httpd.conf 
#LoadModule proxy_module modules/mod_proxy.so				//取消井号
#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so		//取消井号

//编写测试文件
[root@Soap ~]# mkdir -p /usr/local/apache/htdocs/runtime
[root@Soap ~]# vim /usr/local/apache/htdocs/runtime/index.php
<?php
   phpinfo();
?>
[root@Soap ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@Soap ~]# ll -d /usr/local/apache/htdocs/
drwxr-xr-x. 3 apache apache 39 Aug  2 22:45 /usr/local/apache/htdocs/

//配置虚拟主机
[root@Soap extra]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf 
//在配置文件的最后加入以下内容
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/runtime"
    ServerName runtime.example.com
    ErrorLog "logs/runtime.example.com-error_log"
    CustomLog "logs/runtime.example.com-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/runtime/$1
    <Directory "/usr/local/apache/htdocs/runtime">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>


[root@Soap ~]# vim /usr/local/apache/conf/httpd.conf 
//搜索ADDType,添加以下内容
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php				//添加此行
    AddType application/x-httpd-php-source .phps		//添加此行

 DirectoryIndex index.php index.html  		//在前面添加一个index.php

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

//启动php httpd
[root@Soap local]# service php-fpm start
[root@Soap ~]# chkconfig --add php-fpm		//开机自启动
[root@Soap local]# systemctl restart httpd
[root@Soap local]# ss -anlt
State            Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port           Process           
LISTEN           0                128                            127.0.0.1:9000                          0.0.0.0:*                                
LISTEN           0                128                              0.0.0.0:22                            0.0.0.0:*                                
LISTEN           0                80                                     *:3306                                *:*                                
LISTEN           0                128                                    *:80                                  *:*                                
LISTEN           0                128                                 [::]:22                               [::]:*                                

3.4 查看结果

4. lamp架构上部署phpmyadmin

4.1 先在数据库中创建一个用户并授权

[root@Soap ~]# mysql -uroot -pQwer1234!
mysql> create database admin;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on admin.* to 'admin'@'%' identified by 'Qwer1234!';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> grant all on admin.* to 'admin'@'Soap' identified by 'Qwer1234!';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4.2 下载phpmyadmin并解压和配置

[root@Soap ~]# wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.tar.gz
[root@Soap ~]# tar xf phpMyAdmin-5.2.0-all-languages.tar.gz -C /usr/local/apache/htdocs/runtime/
[root@Soap ~]# cd /usr/local/apache/htdocs/runtime/
[root@Soap runtime]# ls
index.php  phpMyAdmin-5.2.0-all-languages
[root@Soap runtime]# mv phpMyAdmin-5.2.0-all-languages/* .
mv: overwrite './index.php'? y
[root@Soap runtime]# cp config.sample.inc.php config.inc.php
[root@Soap runtime]# vim config.inc.php 
$cfg['Servers'][$i]['host'] = '127.0.0.1';		//把Soap改成为127.0.0.1
[root@Soap runtime]# systemctl restart httpd

4.3 查看结果

posted @ 2022-04-22 00:35  姜翎  阅读(62)  评论(0编辑  收藏  举报