lamp分离部署

lamp分离部署

本次部署环境说明:

系统平台 主机的IP地址 需要安装的服务
redhat8 192.168.153.11 httpd
redhat8 192.168.153.12 mysql
redhat8 192.168.153.13 php

准备工作:

配置yum源"阿里云官方镜像网站"

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

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

//清理yum缓存,建立缓存
[root@httpd ~]# yum clean all
[root@httpd ~]# yum makecach

其他两台主机做同样的操作即可

安装httpd

准备工作

安装开发工具包

[root@httpd ~]# yum -y groups mark install 'Development Tools'
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:04:30 ago on Fri 08 Jan 2021 12:00:48 AM CST.
Dependencies resolved.
====================================================================================
 Package            Architecture      Version              Repository          Size
====================================================================================
Installing Groups:
 Development Tools                                                                 

Transaction Summary
====================================================================================

Complete!

创建apache服务的用户和组

[root@httpd ~]# useradd -r -M -s /sbin/nologin apache
[root@httpd ~]# id apache
uid=48(apache) gid=48(apache) groups=48(apache)

安装依赖包

[root@httpd ~]# yum -y install bzip2  vim make wget openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libxml2-devel
  
  pkgconf-pkg-config-1.4.2-1.el8.x86_64                                             
  xz-devel-5.2.4-3.el8.x86_64                                                       
  zlib-devel-1.2.11-16.el8_2.x86_64                                                 

Complete!

下载源码包

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

开始安装

解压下载好的源码包

[root@httpd ~]# tar xf apr-1.7.0.tar.gz
[root@httpd ~]# tar xf apr-util-1.6.1.tar.gz 
[root@httpd ~]# tar xf httpd-2.4.46.tar.bz2 
[root@httpd ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.bz2
apr-1.7.0        apr-util-1.6.1    httpd-2.4.46

安装apr源码包

[root@httpd ~]# cd apr-1.7.0
[root@httpd apr-1.7.0]# vim configure
 
 cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile"      //把这一行用#注释或者删除
[root@httpd apr-1.7.0]# make&&make install

安装apr-util源码包

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

安装httpd源码包

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

安装成功后配置一下

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

//查看apachectl命令
[root@httpd ~]# which apachectl
/usr/local/apache/bin/apachectl

//映射一个软连接
[root@httpd ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@httpd ~]# 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

取消ServerName前面的注释

[root@httpd ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80   //已经修改成功

启动apache服务

[root@httpd ~]# apachectl start
[root@httpd ~]# 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                   [::]:* 

安装httpd

安装依赖包

[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
  
  pkgconf-m4-1.4.2-1.el8.noarch                                                     
  pkgconf-pkg-config-1.4.2-1.el8.x86_64                                             
  zlib-devel-1.2.11-16.el8_2.x86_64                                                 

Complete!

创建mysql的用户和组

[root@mysql ~]# useradd -r -M -s /sbin/nologin mysql
[root@mysql ~]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)

下载源码包

[root@mysql ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

mysql-5.7.31-linux-g 100%[======================>] 359.09M  11.2MB/s    in 35s     

2021-01-08 00:50:09 (10.2 MB/s) - ‘mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz’ saved [376537503/376537503]
[root@mysql ~]# ls
anaconda-ks.cfg  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

解压mysql源码包到/usr/local

[root@mysql ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.31-linux-glibc2.12-x86_64  share

设置软链接并修改属主和属组

[root@mysql ~]# ln -s  /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysql ~]# chown -R mysql.mysql /usr/local/mysql*
[root@mysql ~]# ll /usr/local/
total 0
drwxr-xr-x. 2 root  root    6 Aug 12  2018 bin
drwxr-xr-x. 2 root  root    6 Aug 12  2018 etc
drwxr-xr-x. 2 root  root    6 Aug 12  2018 games
drwxr-xr-x. 2 root  root    6 Aug 12  2018 include
drwxr-xr-x. 2 root  root    6 Aug 12  2018 lib
drwxr-xr-x. 2 root  root    6 Aug 12  2018 lib64
drwxr-xr-x. 2 root  root    6 Aug 12  2018 libexec
lrwxrwxrwx. 1 mysql mysql  46 Jan  8 00:54 mysql -> /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jun  2  2020 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 Aug 12  2018 sbin
drwxr-xr-x. 5 root  root   49 Jan  6 21:19 share
drwxr-xr-x. 2 root  root    6 Aug 12  2018 src

添加环境变量

[root@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql ~]# source /etc/profile.d/mysql.sh 
[root@mysql ~]# which mysql
/usr/local/mysql/bin/mysql

建立数据存放目录

[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data
[root@mysql ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jan  8 00:55 data

初始化数据库

[root@mysql ~]# mysqld --initialize --user=mysql --datadir=/opt/data
2021-01-07T16:56:34.666225Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-07T16:56:34.819286Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-07T16:56:34.843325Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-07T16:56:34.897956Z 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: 4cbd9118-5109-11eb-97f0-000c29d3a0b6.
2021-01-07T16:56:34.898711Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-07T16:56:35.292878Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-07T16:56:35.342864Z 1 [Note] A temporary password is generated for root@localhost: HJt_/;L3l>EE

//把这个生成的临时密码写到a文件中,方便等下查看
[root@mysql ~]# echo 'HJt_/;L3l>EE' > a
[root@mysql ~]# ls
a  anaconda-ks.cfg  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

编辑/etc/my.cnf

[root@mysql ~]# 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@mysql ~]# vim /etc/init.d/mysqld
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql
datadir=/opt/data

启动mysql,设置密码

[root@mysql ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/mysql.err'.
 SUCCESS! 
[root@mysql ~]# cat a
HJt_/;L3l>EE
[root@mysql ~]# mysql -uroot -p'HJt_/;L3l>EE'
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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

配置文件

[root@mysql ~]# vim /etc/man_db.conf 

#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@mysql ~]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib
[root@mysql ~]# ldconfig

安装httpd

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

Transaction Summary
====================================================================================

Complete!

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

  xz-devel-5.2.4-3.el8.x86_64                                                       
  zlib-devel-1.2.11-16.el8_2.x86_64                                                 

Complete!

//安装php
[root@php ~]# yum -y install php-*
  
  tokyocabinet-1.4.48-10.el8.x86_64                                                 
  unixODBC-2.3.7-1.el8.x86_64                                                       

Complete!

//启动php
[root@php ~]# systemctl start php-fpm

配置apache服务和php服务

apache服务

编辑/etc/httpd24/httpd.conf文件

//取消注释,启动需要的模块
[root@httpd ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@httpd ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf

配置虚拟主机

[root@httpd ~]# vim /etc/httpd24/httpd.conf
//在文件的最后一行加上
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/"
    ServerName leidazhuang.com    //域名可以自己设置
    ProxyRequests Off    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.153.13:9000/var/www/html/$1      //这里配置的php主机的IP地址
    <Directory "/usr/local/apache/htdocs/">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

//添加这一行
<IfModule dir_module>
    DirectoryIndex index.php index.html   //在index.html前面加上index.php优先访问前者
</IfModule>


//继续添加下面两行配置
    AddType application/x-httpd-php .php     
    AddType application/x-httpd-php-source .phps

php服务

修改/etc/php-fpm.d/www.conf

[root@php ~]# vim /etc/php-fpm.d/www.conf    

//搜索listen =进行修改
listen = 0.0.0.0:9000								//修改成0.0.0.0:9000
//搜索allowed_进行修改
listen.allowed_clients = 192.168.153.11   //修改成httpd主机的IP地址

创建php测试页面

[root@php ~]# vim /var/www/html/index.php 

<?php
        phpinfo();
?>
[root@php ~]# chown -R apache.apache /var/www/html/
[root@php ~]# ll /var/www/html/
total 4
-rw-r--r--. 1 apache apache 21 Jan  8 01:38 index.php

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

关闭防火墙,selinux,重启httpd和php服务

[root@httpd ~]# apachectl restart
[root@httpd ~]# setenforce 0

[root@php ~]# systemctl restart php-fpm
[root@php ~]# setenforce 0

测试

lamp分离部署

上一篇:vsftpd服务器配置与使用


下一篇:CentOS7.3学习笔记总结(十九)