脚本安装lamp架构构建论坛
编写脚本
[root@100 ~]# vim lamp.sh
#!/bin/bash
# 配置yum源
echo "配置apache中"
echo "配置yum源中"
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &>/dev/null
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
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源配置完成"
# 清理缓存
echo "清理缓存中"
yum clean all &>/dev/null
yum makecache &>/dev/null
echo "清理完成"
# 安装环境
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ vim bzip2 wget make --allowerasing &>/dev/null
yum -y install ncurses-devel openssl cmake ncurses-compat-libs &>/dev/null
yum -y install bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel &>/dev/null
yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm &>/dev/null
yum -y install libxml2-devel &>/dev/null
echo "环境安装完成"
# 部署apr
echo "部署apr中"
cd /usr/src
tar -jxf apr-1.6.5.tar.bz2
cd apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr &>/dev/null
make &>/dev/null
make install &>/dev/null
echo "apr部署完成"
# 部署apr-util
echo "部署apr-util中"
cd /usr/src
tar -jxf apr-util-1.6.1.tar.bz2
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &>/dev/null
make &>/dev/null
make install &>/dev/null
echo "apr-util部署完成"
登录密码修改成功
# 部署httpd
echo "部署httpd中"
cd /usr/src
tar -jxf httpd-2.4.54.tar.bz2
cd httpd-2.4.54
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/http24 \
--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
echo "httpd部署完成"
#配置环境变量
echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
ln -s /usr/local/apache/include/ /usr/include/httpd
echo "MANDATORY_MANPATH /usr/local/apache/man" >>/etc/man_db.conf
useradd -M -r -s /sbin/nologin apache
# 配置服务
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=apache server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start httpd
echo "服务已启动"
echo "apache配置完成"
#配置mysql
echo "配置mysql中"
#关闭防火墙和selinux
echo "正在关闭防火墙和selinux"
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
echo "防火墙关闭"
setenforce 0
echo "selinux关闭"
#将mysql二进制包上传后解压
echo "解压mysql二进制包"
cd /opt
tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz &>/dev/null
echo "解压完成"
#创建mysql用户
echo "创建mysql用户中"
id mysql &>/dev/null
if [ $? -eq 0 ];then
echo "该用户已存在"
else
useradd -M -r -s /sbin/nologin mysql &>/dev/null
echo "用户创建完成"
fi
#创建软连接
echo "创建软链接"
ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
echo "创建完成"
#修改属主和属组
chown -R mysql.mysql mysql
chown -R mysql.mysql mysql-5.7.38-linux-glibc2.12-x86_64/
#配置环境变量
echo "配置环境变量中"
echo 'export PATH=$PATH:/opt/mysql/bin' >/etc/profile.d/mysql.sh
ln -s /opt/mysql/include /usr/include/mysql
echo '/opt/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
cat >> /etc/man_db.conf << EOF
MANDATORY_MANPATH /opt/mysql/man
EOF
source /etc/profile.d/mysql.sh
echo "环境变量配置完成"
#创建数据存放目录
mkdir /opt/mysql_data
chown -R mysql.mysql /opt/mysql_data
#初始化数据库
mysqld --initialize-insecure --user=mysql --datadir=/opt/mysql_data/ &>/dev/null
echo "数据库初始化完成"
#生成配置文件
echo "生成配置文件中"
cat > /etc/my.cnf << EOF
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
log-error=/var/log/mysqld.log
user = mysql
skip-name-resolve
EOF
echo "配置文件生成完毕"
#配置服务启动脚本
cp -a /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
cat >> /etc/init.d/mysqld <<EOF
basedir=/opt/mysql
datadir=/opt/mysql_data
EOF
cat > /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
echo "服务启动脚本配置完成"
systemctl daemon-reload
systemctl start mysqld
systemctl enable mysqld &>/dev/null
#修改数据库登录密码
read -p "请修改数据库登录密码:" sql_passwd
mysql -uroot -e "set password = password('"$sql_passwd"')"
echo "登录密码修改成功"
echo "mysql配置完成"
#配置php
echo "配置php中"
#安装php
cd /usr/src
tar xf php-7.4.30.tar.xz
cd php-7.4.30
echo "安装php中"
./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 \
--with-apxs2=/usr/local/apache/bin/apxs \
--enable-posix &>/dev/null
#编译安装
make &>/dev/null
make install &>/dev/null
echo "安装完成"
#环境变量
echo "配置php环境变量中"
echo "export PATH=$PATH:/usr/local/php7/bin" > /etc/profile.d/php.sh
source /etc/profile.d/php.sh
ln -s /usr/local/php7/include/ /usr/include/php &>/dev/null
echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php.conf
echo "php环境变量配置完成"
#配置php-fpm
echo "配置php-fpm中"
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
cat > /usr/lib/systemd/system/php-fpm.service << EOF
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
echo "php-fpm配置完成"
#启动php-fpm
systemctl daemon-reload
systemctl start php-fpm
systemctl enable php-fpm &>/dev/null
echo "php-fpm启动完成"
#启用代理模块
sed -i '/proxy_module/s/#//g' /etc/http24/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' /etc/http24/httpd.conf
#配置默认网页
rm -rf /usr/local/apache/htdocs/index.html
cat > /usr/local/apache/htdocs/index.php << EOF
<?php
phpinfo();
?>
EOF
chown -R apache.apache /usr/local/apache/htdocs/
echo "默认网页配置完成"
#修改httpd配置文件
echo "AddType application/x-httpd-php .php" >>/etc/http24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >>/etc/http24/httpd.conf
sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/http24/httpd.conf
systemctl restart httpd
echo "php配置完成"
#配置论坛
echo "配置论坛中"
yum -y install unzip &>/dev/null
unzip /opt/Discuz_X3.4_SC_UTF8_20220811.zip -d /opt/discuz &>/dev/null
cd /opt/discuz
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
systemctl restart httpd
mysql -uroot -p"$sql_passwd" -e "create database bbs;"
mysql -uroot -p"$sql_passwd" -e "grant all on bbs.* to 'bbsuser'@'%' identified by '123456';"
mysql -uroot -p"$sql_passwd" -e "flush privileges;"
echo "论坛配置完成"
运行脚本
[root@100 ~]# source lamp.sh
配置apache中
配置yum源中
yum源配置完成
清理缓存中
清理完成
环境安装完成
部署apr中
apr部署完成
部署apr-util中
apr-util部署完成
部署httpd中
httpd部署完成
服务已启动
apache配置完成
配置mysql中
正在关闭防火墙和selinux
防火墙关闭
selinux关闭
解压mysql二进制包
解压完成
创建mysql用户中
用户创建完成
创建软链接
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
创建完成
配置环境变量中
环境变量配置完成
数据库初始化完成
生成配置文件中
配置文件生成完毕
服务启动脚本配置完成
请修改数据库登录密码:123456
登录密码修改成功
mysql配置完成
配置php中
安装php中
安装完成
配置php环境变量中
php环境变量配置完成
配置php-fpm中
php-fpm配置完成
php-fpm启动完成
默认网页配置完成
php配置完成
配置论坛中
论坛配置完成
浏览器访问测试