lamp分离部署

lamp分离部署

准备三台主机

ip name 服务
192.168.23.133 yc2 httpd
192.168.23.134 yc3 mysql
192.168.23.135 yc4 php

准备环境

三台主机都需配置,这里只举yc2的示例

//配置yum源

[root@yc2 ~]# curl -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@yc2 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@yc2 ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo

//配置epel源

[root@yc2 ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@yc2 ~]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@yc2 ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@yc2 ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/epel*
[root@yc2 ~]# yum clean all
[root@yc2 ~]# yum makecache

//关闭防火墙和selinux

[root@yc2 ~]# systemctl stop firewalld
[root@yc2 ~]# setenforce 0

在yc2上安装httpd

//安装开发工具包
[root@yc2 ~]# yum -y groups mark install 'Development Tools'

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

//安装依赖包
[root@yc2 ~]# yum -y install bzip2 make openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libxml2-devel

//下载源码包
[root@yc2 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
[root@yc2 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
[root@yc2 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
[root@yc2 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.bz2

//安装apr
[root@yc2 ~]# tar xf apr-1.7.0.tar.gz
[root@yc2 ~]# cd apr-1.7.0
[root@yc2 apr-1.7.0]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"		//过滤到该位置,将此行加上注释,或者删除此行
[root@yc2 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@yc2 apr-1.7.0]# make
[root@yc2 apr-1.7.0]# make install

//安装apr-util

[root@yc2 ~]# tar xf apr-util-1.6.1.tar.gz 
[root@yc2 ~]# cd apr-util-1.6.1
[root@yc2 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@yc2 apr-util-1.6.1]# make
[root@yc2 apr-util-1.6.1]# make install

//安装httpd

[root@yc2 ~]# tar xf httpd-2.4.46.tar.bz2 
[root@yc2 ~]# cd httpd-2.4.46
[root@yc2 ~]# ./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@yc2 ~]# make
[root@yc2 ~]# make install

//安装后配置

[root@yc2 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@yc2 ~]# source /etc/profile.d/httpd.sh
[root@yc2 ~]# which apachectl
/usr/local/apache/bin/apachectl
[root@yc2 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@yc2 ~]# vim /etc/man_db.conf
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH                      /usr/src/pvm3/man
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man //添加这一行

//取消ServerName的注释
[root@yc2 ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

//启动apache
[root@yc2 ~]# apachectl start
[root@yc2 ~]# ss -antl
State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port    
LISTEN   0         128                 0.0.0.0:22               0.0.0.0:*       
LISTEN   0         128                       *:80                     *:*       
LISTEN   0         128                    [::]:22                  [::]:*       

在yc3上安装MySQL

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

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

//下载源码包
[root@yc3 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@yc3 ~]# ls
anaconda-ks.cfg  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@yc3 ~]# ls
anaconda-ks.cfg  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

//解压mysql到/usr/local/
[root@yc3 ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

//设置软链接
[root@yc3 ~]# ln -s  /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql

//修改属主和属组
[root@yc3 ~]# chown -R mysql.mysql /usr/local/mysql*

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

//创建数据库资源存放的目录
[root@yc3 ~]# mkdir /opt/mysql_data
[root@yc3 ~]#  chown -R mysql.mysql /opt/mysql_data

//初始化数据库
[root@yc3 ~]# mysqld --initialize --user=mysql --datadir=/opt/mysql_data
2021-01-07T17:08:33.191252Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-07T17:08:33.335292Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-07T17:08:33.364661Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-07T17:08:33.373273Z 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: f8fc2faf-510a-11eb-b86f-000c290bc351.
2021-01-07T17:08:33.374708Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-07T17:08:33.944679Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-07T17:08:34.055059Z 1 [Note] A temporary password is generated for root@localhost: BwV>%0CuMu1g  //临时密码,只能使用一次,需要记下来

//配置mysql

[root@yc3 ~]# vim /etc/my.cnf

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve


//配置服务启动脚本

[root@yc3 ~]# vim /etc/init.d/mysqld

......
//找到下面位置添加路径

basedir=/usr/local/mysql
datadir=/opt/mysql_data
......

//启动mysql

[root@yc3 ~]# service mysqld start
Starting MySQL.Logging to '/opt/mysql_data/yc3.err'.
 SUCCESS! 
[root@yc3 ~]# ss -antl
State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port    
LISTEN   0         128                 0.0.0.0:22               0.0.0.0:*       
LISTEN   0         128                    [::]:22                  [::]:*       
LISTEN   0         80                        *:3306                   *:*      

//使用临时密码进入mysql然后修改密码
[root@yc3 ~]# mysql -uroot -p'BwV>%0CuMu1g'
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.31

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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('yanchuang');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//更改配置文件
[root@yc3 ~]# vim /etc/man_db.conf

#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
# --------------------------------------------------------
# MANDATORY_MANPATH                     manpath_element
# MANPATH_MAP           path_element    manpath_element
# MANDB_MAP             global_manpath  [relative_catpath]
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH                      /usr/src/pvm3/man
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man //添加这一行内容

[root@yc3 ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib

在yc4上安装php

//安装开发工具包
[root@yc4 ~]# yum -y groups mark install 'Development Tools'

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

//安装php
[root@yc4 ~]# yum -y install php-*
    
//启动php
[root@yc4 ~]# systemctl start php-fpm

配置apache和php

apache配置

//httpd配置
[root@yc2 ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@yc2 ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf

//配置虚拟主机
[root@yc2 ~]# vim /etc/httpd24/httpd.conf

......

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/"
    ServerName yuqinghao.com
    ProxyRequests Off    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.100.4:9000/var/www/html/$1
    <Directory "/usr/local/apache/htdocs/">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

//搜索AddType,添加以下内容

[root@yc2 ~]# vim /etc/httpd24/httpd.conf 
......

 # 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        #添加此行
......

php配置

//修改配置文件
[root@php ~]# vim /etc/php-fpm.d/www.conf
······
listen=0.0.0.0:9000					 	//修改端口号0.0.0.0:9000
······
listen.allowed_clients = 192.168.100.2  //修改成httpd主机的ip地址

//创建php测试页面
[root@yc4 ~]# echo -e "<?php\n\tphpinfo();\n?>" > /var/www/html/index.php
[root@yc4 ~]# chown -R apache.apache /var/www/html/

//重启httpd和php主机
[root@yc2 ~]# apachectl restart
[root@yc4 ~]# systemctl restart php-fpm

验证

lamp分离部署

上一篇:MySQL数据库备份的4种方式


下一篇:Canvas 获得键盘焦点的方法