zoukankan      html  css  js  c++  java
  • Web实战搭建,通过NFS存储数据

    基于LNMP架构部署WordPRess,后端构建NFS服务存储用户上传的图片及数据


    #配置NFS服务
    #安装NFS服务
    yum -y install nfs-utils

    #配置共享目录
    cat > /etc/exports << EOF
    /data/uploads 10.0.0.0/24(rw,sync,no_root_squash)
    EOF

    #启动NFS服务病共享
    systemctl enable nfs-server.service
    systemctl start nfs-server.service
    exportfs -r

     


    安装基于LAMP架构的wordpress
    vim wordpress_install.sh
    #!/bin/bash
    #wordpress自动安装

    function install_httpd() {
    #下载httpd源码包
    target_dir=/usr/local/src
    install_dir=/usr/local/httpd
    download_url=https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
    file_name=${download_url##*/}
    uncompress_dir=${file_name%.tar*}
    rpm -q wget || yum install -y wget
    [ -f $target_dir/$file_name ] || wget -O $target_dir/$file_name $download_url

    #下载apr和apr-util源码包
    apr_download_url=https://mirror.bit.edu.cn/apache/apr/apr-1.7.0.tar.bz2
    apr_file_name=${apr_download_url##*/}
    apr_uncompress_dir=${apr_file_name%.tar*}
    [ -f $target_dir/$apr_file_name ] || wget -O $target_dir/$apr_file_name $apr_download_url
    apr_util_download_url=https://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
    apr_util_file_name=${apr_util_download_url##*/}
    apr_util_uncompress_dir=${apr_util_file_name%.tar*}
    [ -f $target_dir/$apr_util_file_name ] || wget -O $target_dir/$apr_util_file_name $apr_util_download_url

    #安装依赖包
    yum install -y gcc make pcre-devel openssl-devel redhat-rpm-config expat-devel

    #添加apache用户
    id apache &> /dev/null || useradd -r -u 80 -d /var/www -s /sbin/nologin apache

    #解压源码包
    tar xf $target_dir/$file_name -C $target_dir
    tar xf $target_dir/$apr_file_name -C $target_dir
    mv $target_dir/$apr_uncompress_dir $target_dir/$uncompress_dir/srclib/apr
    tar xf $target_dir/$apr_util_file_name -C $target_dir
    mv $target_dir/$apr_util_uncompress_dir $target_dir/$uncompress_dir/srclib/apr-util

    #编译安装
    cd $target_dir/$uncompress_dir
    ./configure --prefix=$install_dir
    --sysconfdir=/etc/httpd
    --enable-so
    --enable-ssl
    --enable-cgi
    --enable-rewrite
    --enable-modules=most
    --enable-mpms-shared=all
    --with-zlib
    --with-pcre
    --with-included-apr
    --with-mpm=event
    make -j`lscpu | grep "^CPU(s)" | awk '{print $NF}'` && make install

    #设置环境变量
    echo 'PATH='$install_dir'/bin:$PATH' > /etc/profile.d/httpd.sh
    source /etc/profile.d/httpd.sh

    #修改配置文件
    sed -ri 's#(User )daemon#1apache#' /etc/httpd/httpd.conf
    sed -ri 's#(Group )daemon#1apache#' /etc/httpd/httpd.conf

    #启动httpd服务
    cat > /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=/usr/local/httpd/bin/apachectl start
    ExecReload=/usr/local/httpd/bin/apachectl graceful
    ExecStop=/usr/local/httpd/bin/apachectl stop
    KillSignal=SIGCONT
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target
    EOF
    systemctl daemon-reload
    systemctl enable httpd.service
    systemctl start httpd.service

    #检查firewalld状态
    firewall_status=`firewall-cmd --state`
    if [ $firewall_status = running ];then
    echo "防火墙已启用,开放端口"
    firewall-cmd --permanent --add-service=http --add-service=https
    firewall-cmd --reload
    fi
    }

    function install_mysql() {
    target_dir=/usr/local
    download_url=http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
    file_name=${download_url##*/}
    uncompress_dir=${file_name%.tar*}

    #安装依赖包
    yum -y install libaio-devel numactl-libs

    #创建mysql用户
    id mysql &> /dev/null || useradd -r -s /sbin/nologin mysql

    #下载解压安装包并创建数据目录
    rpm -q wget || yum install -y wget
    [ -f $target_dir/src/$file_name ] || wget -O $target_dir/src/$file_name $download_url
    tar xf $target_dir/src/$file_name -C $target_dir
    ln -s $target_dir/$uncompress_dir $target_dir/mysql
    chown -R root.root $target_dir/$uncompress_dir
    [ -d /data/mysql ] || mkdir -p /data/mysql
    chown -R mysql.mysql /data/mysql

    #设置环境变量
    cat > /etc/profile.d/mysql.sh << EOF
    export PATH=/usr/local/mysql/bin:$PATH
    EOF
    source /etc/profile.d/mysql.sh

    #生成数据库配置文件
    mv /etc/my.cnf /etc/my.cnf.bak
    cat > /etc/my.cnf << EOF
    [mysqld]
    port=3306
    socket=/data/mysql/mysql.sock
    datadir=/data/mysql
    log-error=/data/mysql/mysql.log
    character-set-server=utf8mb4
    #skip_name_resolve
    max_connections=1000
    max_connect_errors=1000

    [client]
    port=3306
    socket=/data/mysql/mysql.sock
    default-character-set=utf8mb4
    EOF

    #mysql初始化
    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

    #创建启动脚本并启动服务
    cat > /etc/systemd/system/mysqld.service << EOF
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target

    [Install]
    WantedBy=multi-user.target

    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
    LimitNOFILE = 5000
    EOF
    systemctl daemon-reload
    systemctl enable mysqld.service
    systemctl start mysqld.service

    #检查防火墙状态
    firewall_status=`firewall-cmd --state`
    if [ $firewall_status = running ];then
    echo "防火墙已启用,开放端口"
    firewall-cmd --permanent --add-port=3306/tcp
    firewall-cmd --reload
    fi

    #修改数据库root密码
    mysql -uroot -h 127.0.0.1 -e 'update mysql.user set authentication_string=password("my123456") where user="root" and Host="localhost";'
    mysql -uroot -h 127.0.0.1 -e 'flush privileges;'
    sed -ri 's@#(skip_name_resolve)@1@' /etc/my.cnf
    systemctl restart mysqld.service
    }

    function install_php() {
    #下载源码包
    target_dir=/usr/local
    install_dir=/usr/local/php
    download_url=http://mirrors.sohu.com/php/php-7.4.10.tar.bz2
    file_name=${download_url##*/}
    uncompress_dir=${file_name%.tar*}
    rpm -q wget || yum install -y wget
    [ -f $target_dir/src/$file_name ] || wget -O $target_dir/src/$file_name $download_url

    #安装依赖包
    yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel

    #解压源码包
    tar xf $target_dir/src/$file_name -C $target_dir

    #编译安装
    cd $target_dir/$uncompress_dir
    ./configure --prefix=$install_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`lscpu | grep "^CPU(s)" | awk '{print $NF}'` && make install

    #设置环境变量
    echo 'PATH='$install_dir'/bin:$PATH' > /etc/profile.d/php.sh
    source /etc/profile.d/php.sh

    #生成配置文件和启动文件
    cp php.ini-production /etc/php.ini
    cp $install_dir/etc/php-fpm.conf.default $install_dir/etc/php-fpm.conf
    cp $install_dir/etc/php-fpm.d/www.conf.default $install_dir/etc/php-fpm.d/www.conf
    #cp $target_dir/$uncompress_dir/sapi/fpm/php-fpm.service /lib/systemd/system/
    cat > /lib/systemd/system/php-fpm.service << EOF
    [Unit]
    Description=The PHP FastCGI Process Manager
    After=syslog.target network.target

    [Service]
    Type=simple
    PIDFile=/var/run/php-fpm.pid
    ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
    ExecReload=/bin/kill -USR2 $MAINPID
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target
    EOF

    #修改配置文件
    sed -ri 's#(user = )nobody#1apache#' $install_dir/etc/php-fpm.d/www.conf
    sed -ri 's#(group = )nobody#1apache#' $install_dir/etc/php-fpm.d/www.conf
    sed -ri 's#;(pm.status_path = )/status#1/php-fpm_status#' $install_dir/etc/php-fpm.d/www.conf
    sed -ri 's#;(ping.path = /ping)#1#' $install_dir/etc/php-fpm.d/www.conf
    mkdir /etc/php.d
    cat > /etc/php.d/opcache.ini << EOF
    [opacache]
    zend_extensio=opcache.so
    opcache.enable=1
    EOF
    systemctl daemon-reload
    systemctl enable php-fpm.service
    systemctl start php-fpm.service

    #修改httpd配置,支持php-fpm
    sed -ri 's@#(LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so)@1@' /etc/httpd/httpd.conf
    sed -ri 's@#(LoadModule proxy_module modules/mod_proxy.so)@1@' /etc/httpd/httpd.conf
    sed -ri 's@(DirectoryIndex )(index.html)@1index.php 2@' /etc/httpd/httpd.conf
    sed -ri '$aAddType application/x-httpd-php .php ProxyRequests Off' /etc/httpd/httpd.conf
    cat >> /etc/httpd/httpd.conf << EOF

    <VirtualHost *:80>
    ServerName localhost
    DocumentRoot /data/wordpress
    <Directory "/data/wordpress">
    Require all granted
    </Directory>
    ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
    ProxyPassMatch ^/(php-fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
    CustomLog "logs/access_wordpress_log" common
    </VirtualHost>
    EOF
    systemctl restart httpd.service
    }

    function install_wordpress() {
    #下载wordpress安装包
    target_dir=/usr/local/src
    install_dir=/data
    download_url=https://cn.wordpress.org/wordpress-5.4.2-zh_CN.zip
    file_name=${download_url##*/}
    uncompress_dir=${file_name%%-*}
    rpm -q wget || yum install -y wget
    [ -f $target_dir/$file_name ] || wget -O $target_dir/$file_name $download_url

    #解压安装包
    unzip $target_dir/$file_name -d $target_dir
    mv $target_dir/$uncompress_dir $install_dir/
    chown -R apache:apache $install_dir/$uncompress_dir

    #创建数据库和用户
    mysql -uroot -pmy123456 -e 'create database wordpress;'
    mysql -uroot -pmy123456 -e 'grant all on wordpress.* to wpuser@"192.168.1.%" identified by "wppasswd";'
    }

    function install_nfs_client() {
    nfs_server=10.0.0.0
    nfs_share_dir=/data/uploads
    pic_upload_dir=/data/wordpress/wp-content/uploads
    [ -d $pic_upload_dir ] || mkdir -p $pic_upload_dir

    #安装NFS相关工具
    yum -y install nfs-utils

    #挂载NFS共享目录
    cat >> /etc/fstab << EOF
    $nfs_server:$nfs_share_dir $pic_upload_dir nfs defaults,_netdev 0 0
    EOF
    mount -a
    chown -R apache:apache $pic_upload_dir
    }

    install_httpd
    install_mysql
    install_php
    install_wordpress
    install_nfs_client
    执行bash wordpress_install.sh自动完成wordpress安装配置,图片上传目录已挂载到NFS服务器/data/uploads目录

    把生命浪费在美好的事物上
  • 相关阅读:
    树的递归
    《计算机存储与外设》 3二级存储器
    《计算机存储与外设》 2主存储器
    《码农翻身:用故事给技术加点料》阅读有感
    分析"傍富婆发财"
    《第22条军规》Catch-22
    《编译原理》4
    《编译原理》3
    《血酬定律》
    linux下netcore程序部署
  • 原文地址:https://www.cnblogs.com/tz66/p/13707293.html
Copyright © 2011-2022 走看看