架构题:前端有一个 LAMP 架构通过 wordpress 来部署,后端构建一个 NFS 服务器实现要求将用户上传的图片保存至后端 NFS 服务器上。
后端搭建nfs
#!/bin/bash
rpm -q nfs-utils || yum -y install nfs-utils
share_dir=/data/wordpress
[ -d $share_dir ] || mkdir $share_dir -pv
cat > /etc/exports.d/wordpress.exports <<EOF
$share_dir 10.0.0.0/24(rw,no_root_squash)
EOF
systemctl enable nfs-server.service
systemctl start nfs-server.service
exportfs -r
#建立与前端web相同uid的apache的账号
useradd -r -u 80 -d /var/www -s /sbin/nologin apache
前端搭建wordpress
#!/bin/bash
#创建目录
DOWNLOAD_DIR="/usr/local/src"
#相关变量
CPU=`lscpu|sed -n '/^CPU(s)/p'|tr -s " "|cut -d " " -f2`
install_httpd() {
#定义变量
APR_URL="https://mirrors.bfsu.edu.cn/apache//apr/apr-1.7.0.tar.bz2"
APR_UTIL_URL="https://mirrors.bfsu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2"
HTTPD_URL="https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.bz2"
APR_DIR=`echo ${APR_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
APR_UTIL_DIR=`echo ${APR_UTIL_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
HTTPD_DIR=`echo ${HTTPD_URL}|sed -rn 's#.*/(.*).tar.bz2$#1#p'`
INSTALL_HTTPD_DIR="/app/httpd"
[ -d ${INSTALL_HTTPD_DIR} ] || mkdir -p ${INSTALL_HTTPD_DIR}
#编译安装
yum -y install gcc make pcre-devel openssl-devel expat-devel wget
wget -P ${DOWNLOAD_DIR} ${APR_URL}
wget -P ${DOWNLOAD_DIR} ${APR_UTIL_URL}
wget -P ${DOWNLOAD_DIR} ${HTTPD_URL}
cd ${DOWNLOAD_DIR}
for file in *;do
tar xf $file
done
mv ${APR_DIR} ${HTTPD_DIR}/srclib/apr
mv ${APR_UTIL_DIR} ${HTTPD_DIR}/srclib/apr-util
cd ${HTTPD_DIR}/
./configure --prefix=/app/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make -j ${CPU} && make install
#建用户
id apache &> /dev/null || useradd -r -u 80 -d /var/www -s /sbin/nologin apache
#修改配置文件
sed -ri 's#^User.*#User apache#' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
sed -ri 's#^Group.*#Group apache#' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
echo PATH='/app/httpd/bin/:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
#创建service文件
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=${INSTALL_HTTPD_DIR}/bin/apachectl start
ExecReload=${INSTALL_HTTPD_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_HTTPD_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#启动
systemctl daemon-reload
systemctl enable --now httpd.service
}
#安装mysql5.7
MYSQL_URL="http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz"
MYSQL_RAR_NAME=`echo ${MYSQL_URL}|sed -rn 's#.*/(.*)$#1#p'`
MYSQL_DIR=`echo ${MYSQL_RAR_NAME}|sed -rn 's#(.*).tar.gz#1#p'`
MYSQL_ROOT_PASSWORD=magedu
INSTALL_MYSQL_DIR="/app/mysql"
COLOUR='echo -e 33[01;31m'
END=' 33[0m'
check_mysql (){
if [ $UID -ne 0 ]; then
$COLOUR"当前用户不是root,安装失败"$END
exit
fi
[ -d /usr/local/mysql ] && { $COLOUR"数据库已存在,安装失败"$END;exit; }
}
install_mysql (){
$COLOUR"开始安装MySQL数据库..."$END
yum install -y libaio numactl-libs perl-Data-Dumper make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl wget lrzsz vim libnuma* bzip2 xz
yum install libncurses* wget -y
cd $DOWNLOAD_DIR
wget $MYSQL_URL
tar xf $MYSQL_RAR_NAME -C /usr/local
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
id mysql &> /dev/null || useradd -s /sbin/nologin -r mysql
chown -R root.root /usr/local/mysql
echo 'PATH=/usr/local/mysql/bin/:/$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<EOF
[mysqld]
server-id=1
log-bin
socket=/data/mysql/mysql.sock
datadir=/data/mysql
character-set-server=utf8mb4
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4
EOF
[ -d /data/mysql ] || mkdir -p /data/mysql
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &> /dev/null
}
install_php() {
PHP_URL="http://mirrors.sohu.com/php/php-7.4.10.tar.bz2"
PHP_RAR_NAME=`echo ${PHP_URL}|sed -rn 's#.*/(.*)#1#p'`
PHP_DIR=`echo ${PHP_RAR_NAME}|sed -rn 's#(.*).t.*#1#p'`
INSTALL_PHP_DIR="/app/php"
INSTALL_HTTPD_DIR="/app/httpd"
CPU=`lscpu|sed -n '/^CPU(s)/p'|tr -s " "|cut -d " " -f2`
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel openssl-devel openssl wget
[ -f ${DOWNLOAD_DIR}/${PHP_RAR_NAME} ] || wget -P ${DOWNLOAD_DIR} ${PHP_URL}
cd ${DOWNLOAD_DIR}
tar xf ${PHP_RAR_NAME}
cd ${PHP_DIR}
./configure --prefix=${INSTALL_PHP_DIR} --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
make -j ${CPU} && make install
echo "PATH=${INSTALL_PHP_DIR}/bin:$PATH" > /etc/profile.d/php.sh
. /etc/profile.d/php.sh
cp php.ini-production /etc/php.ini
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cd ${INSTALL_PHP_DIR}/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d/
cp www.conf.default www.conf
sed -ri 's#(user = )nobody#1apache#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
sed -ri 's#(group = )nobody#1apache#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
sed -ri 's#;(pm.status_path = )/status#1/fpm_status#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
sed -ri 's#;(ping.path = /ping)#1#' ${INSTALL_PHP_DIR}/etc/php-fpm.d/www.conf
mkdir /etc/php.d -p
cat > /etc/php.d/opcache.ini << EOF
[opacache]
zend_extensio=opcache.so
opcache.enable=1
EOF
systemctl daemon-reload
systemctl enable --now php-fpm.service
sed -ri 's@#(LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so)@1@' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
sed -ri 's@#(LoadModule proxy_module modules/mod_proxy.so)@1@' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
sed -ri 's@(DirectoryIndex )(index.html)@1index.php 2@' ${INSTALL_HTTPD_DIR}/conf/httpd.conf
cat >> ${INSTALL_HTTPD_DIR}/conf/httpd.conf <<EOF
AddType application/x-httpd-php .php
ProxyRequests Off
<virtualhost *:80>
servername blog
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>
EOF
mkdir -pv /data/wordpress
systemctl restart httpd
systemctl restart php-fpm.service
}
install_wordpress(){
#网络下载比较慢,将包传到 DOWNLOAD_DIR
cd ${DOWNLOAD_DIR}
tar xf wordpress-5.4.2.tar.gz
mkdir /data
mv ${DOWNLOAD_DIR}/wordpress /data
chown -R apache:apache /data/wordpress
mysql -uroot -pmagedu -e 'create database wordpress;'
mysql -uroot -pmagedu -e 'grant all on wordpress.* to admin@"10.0.0.%" identified by "magedu";'
}
install_httpd
check_mysql
install_mysql
install_php
install_wordpress
在wordpress端
将图片目录挂载到nfs服务器
查看是否可以挂载
shoumount -e nfs服务器的ip地址
mount 10.0.0.72:/data/wordpress /data/wordpress/wp-content/uploads
df -h查看