lamp分离部署

lamp分离部署

本次部署环境说明

主机名 IP地址 需要安装的服务 系统版本
httpd 192.168.110.11 httpd redhat 8
mysql 192.168.110.12 mysql redhat 8
php 192.168.110.13 php redhat 8

准备工作

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

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo   
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo  
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
sed -i 's|$releasever|8|' /etc/yum.repos.d/epel* 

关闭防火墙和selinux

systemctl disable --now firewalld
setenforce 0
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

安装httpd

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

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

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

//下载源码包
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
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

//解压下载好的源码包
tar xf apr-1.7.0.tar.gz
tar xf apr-util-1.6.1.tar.gz 
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]# sed -i '/$RM "$cfgfile"/d' configure
 cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile"      //把这一行用#注释或者删除
[root@httpd apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@httpd apr-1.7.0]# make && make install

//安装apr-util源码包
[root@httpd apr-1.7.0]# 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 && make install

//安装httpd源码包
[root@httpd apr-util-1.6.1]# 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 httpd-2.4.46]#cd
[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/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@httpd ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

//启动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                 [::]:*

安装mysql并配置

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

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

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

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

//设置软链接并修改属主和属组
[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 ~]# 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 ~]# 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 ~]# cat a
HJt_/;L3l>EE

//编辑my.cnf文件
[root@mysql ~]# cat >> /etc/my.cnf <<EOF
[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
EOF

//配置服务启动脚本
[root@mysql ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# cat >> /etc/init.d/mysqld <<EOF
basedir=/usr/local/mysql
datadir=/opt/data
EOF

//启动mysql
[root@mysql ~]# service mysqld start
[root@mysql ~]# 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                      *:* 


//查看密码,登录数据库设置密码123456
[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');    //设置密码123456
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

//设置完后配置
[root@mysql ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql ~]# ldconfig 

安装php

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

//安装依赖包
[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

//安装php
[root@php ~]# yum -y install php-*

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

配置apache和php主机

//http主机
#取消注释,启动需要的模块
sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
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.110.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,添加以下内容
# 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主机
#修改/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.110.11   //修改成httpd主机的IP地址

#创建php测试页面
[root@php ~]# vim /var/www/html/index.php
<?php
        phpinfo();
?>
[root@php ~]# chown -R apache.apache /var/www/html/

重启服务测试

//重启http和php服务
[root@httpd ~]# apachectl restart
[root@php ~]# systemctl restart php-fpm

网页测试

image

posted @ 2021-01-08 02:18  我爱吃芹菜~  阅读(137)  评论(0编辑  收藏  举报
Title