shell脚本lamp-php架构

lamp-php架构

#!/bin/bash

#关闭防火墙和selinux
echo "正在关闭防火墙和selinux"
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
setenforce 0
echo "已经关闭防火墙和selinux"

#配置yum网络源
echo "正在配置yum源"
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &>/dev/null
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm &>/dev/null
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

echo  "正在清理缓存"
yum clean all && yum makecache &>/dev/null


#部署lamp-apache
echo"正在安装需要的工具包"
yum -y install openssl-devel zip  pcre-devel expat-devel libtool gcc gcc-c++ bzip2 vim make wget libxml2-devel 
echo "安装完成"

#解压并安装apr包
cd /opt
tar -xzf apr-1.7.0.tar.gz  &>/dev/null
cd /opt/apr-1.7.0
sed -i '/$RM "$cfgfile"/d' configure
echo "正在编译安装apr"
./configure --prefix=/usr/local/apr &>/dev/null
make &>/dev/null
make install &>/dev/null
echo "apr部署完成"

#解压并安装apr-util
cd /opt
tar -xzf apr-util-1.6.1.tar.gz  &>/dev/null
cd /opt/apr-util-1.6.1
echo "正在编译安装apr-util"
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &>/dev/null
make  &>/dev/null
make install &>/dev/null
echo "apr-util部署完成" 

#解压并安装httpd
cd  /opt
tar -xzf httpd-2.4.54.tar.gz
cd /opt/httpd-2.4.54

echo "正在编译httpd"
./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 &>/dev/null
make &>/dev/null 
make install &>/dev/null
/usr/local/apache/bin/apachectl start
echo "httpd 编译完成"

#部署lamp-mysql
echo "---正在部署mysql---"
#创建mysql用户
echo "正在验证mysql用户..."
id mysql &> /dev/null
if [ $? -eq 0 ];then
	echo "mysql用户已存在"
else
	echo "正在创建mysql用户..."
        useradd -r -M -s /sbin/nologin mysql
	echo "mysql用户创建成功"
fi
#安装依赖包
echo "正在安装依赖包"
yum -y install libncurses* &>/dev/null
echo "安装完成"

#配置mysql
echo "正在配置mysql..."

tar -xzf /opt/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
ln -sv /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql &>/dev/null
chown -R mysql.mysql /usr/local/mysql &>/dev/null
mkdir -p /opt/data &>/dev/null
chown -R mysql.mysql /opt/data/ &>/dev/null

#数据库初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ &>/dev/null
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql &>/dev/null
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf &>/dev/null
ldconfig &> /dev/null

#编辑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

sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server

#定义mysql服务可以使用systemd来进行管理
cat > /usr/lib/systemd/system/mysqld.service  <<EOF
[Unit]
Description=mysql server daemon
After=network.targe

[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
EOF
systemctl daemon-reload && echo "*****重新加载成功*****"
systemctl enable --now mysqld && echo "*****开机自启mysql成功*****"

#设置数据库密码
read -p "请输入您要设置的数据库密码:" mysql_passwd
/usr/local/mysql/bin/mysql -uroot -e "set password=password('$mysql_passwd')"

#做一个链接,添加mysq命令的变量到环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
echo "-----mysql部署完成-----"

#部署lamp-php
#安装依赖包,安装GD库和GD库关联程序,用来处理和生成图片
echo "正在安装依赖包"
yum -y install \
          libjpeg \
          libjpeg-devel \
          libpng libpng-devel \
          freetype freetype-devel \
          libxml2 \
          libxml2-devel \
          zlib zlib-devel \
          curl curl-devel &>/dev/null
echo "依赖包已经安装完成"

#解压php包文件
cd /opt
tar -xzf php-7.1.10.tar.gz
echo "解压php包文件成功"

#配置编译安装php
cd php-7.1.10
echo "正在配置php"
./configure \
      --prefix=/usr/local/php \
      --with-apxs2=/usr/local/apache/bin/apxs \
      --with-mysql-sock=/tmp/mysql.sock \
      --with-mysqli \
      --with-zlib \
      --with-curl \
      --with-gd \
      --with-jpeg-dir \
      --with-png-dir \
      --with-freetype-dir \
    --with-openssl \
      --enable-mbstring \
       --enable-xml \
      --enable-session \
      --enable-ftp \
       --enable-pdo \
       --enable-tokenizer \
       --enable-zip &>/dev/null 

echo "正在编译安装php"
make &>/dev/null
make install &>/dev/null
echo "php编译安装完成"

#配置php
echo "正在配置php"

#测试环境时使用php.ini-development文件,生产环境使用php.ini-production文件
cp php.ini-development /usr/local/php/lib/php.ini

#修改php配置文件,设置时区
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini

#修改httpd服务配一文件,让apache支持php
echo "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf

sed -i 's/index.html/index\.php index\.html/' /etc/httpd24/httpd.conf

#验证php测试页
rm -f /usr/local/apache/htdocs/index.html

cat > /usr/local/apache/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF

echo "php配置完成"

#启动apache服务
/usr/local/apache/bin/apachectl restart
echo "lamp部署完成"

#创建数据库,并进行授权
mysql -u root -p"$mysql_passwd" -e  'create database bbs;'
mysql -u root -p"$mysql_passwd" -e "grant all on bbs.* to 'bbsuser'@'%' identified by 'redhat';"
mysql -u root -p"$mysql_passwd" -e 'flush privileges;'

echo "bss数据库配置完成"
#解压论坛压缩包
unzip /opt/Discuz_X3.4_SC_UTF8_20220811.zip -d /opt/dis &>/dev/null
cd /opt/dis
cp -r upload /usr/local/apache/htdocs/bbs

#更改论坛目录的属主
cd /usr/local/apache/htdocs/bbs
chown -R daemon ./config
chown -R daemon ./data
chown -R daemon ./uc_client
chown -R daemon ./uc_server/data

/usr/local/apache/bin/apachectl restart
echo "完成"

#输入source +文件名 执行脚本
执行脚本在编译完httpd后会发生报错
AH00558: httpd:Could not reliably determine the server's futly quslf1ed domain name  , using localhost.localdomain. Set the ‘ServerName' directive globally to suppress thls message
解决:另起一个终端
find / -name httpd.conf //查找httpd.conf配置文件
我的在/etc/httpd24/httpd.conf下
找到ServerName www.example.com:80 去掉注释 把它改为ServerName localhost:80
保存退出

#php编译完成之后会报mysql密码不安全之类 那不是报错不用管
编译完成之后可以登录mysql查看一下数据库里面有没有bbs,如果没有就是有问题

#验证 
在浏览器访问
192.168.253.139/bbs

shell脚本lamp-php架构_第1张图片

你可能感兴趣的:(php,架构,linux)