首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Debian-Apache-MySQL-PHP版本-不可知的环境引导程序

Debian-Apache-MySQL-PHP版本-不可知的环境引导程序
EN

Code Review用户
提问于 2019-11-27 13:31:29
回答 1查看 68关注 0票数 5

下面的程序由三个文件组成,目的是用作Debian PHP版本无关环境引导程序。

它的目标是用于原始Debian系统(没有安装OS附带的任何东西),并建立与web域相关的web应用程序。

程序

文件1

该文件包含基本安装和/或配置命令,由两部分组成:

  • 第一部分是cat .profile遗传工具,目的是声明一些全局模式、变量和函数(“全局”,用于影响所有shell会话),根据我的经验,这些模式、变量和函数是无害的,尽管是全局的。
  • 第二部分是.profile的“来源”,以确保变量在第一次shell会话中生效,在第一次shell会话中声明变量时,也在每次启动Debian之后。

文件1代码如下(代码块下面有说明)。

代码语言:javascript
运行
复制
#!/bin/bash

cat <<-EOF >> "$HOME"/.profile
    set -x
    complete -r

    export war="/var/www/html"
    export dmp="phpminiadmin"

    export -f war ssr tmd # Create execution shortcuts to the following functions:

    war() {
        cd $war/
    }

    ssr() {
        chown -R www-data:www-data "$war"/
        chmod -R a-x,a=rX,u+w "$war"/
        systemctl restart apache*
        chmod -R 000 "$war"/"$dmp"/
    }
    tmd() {
        chmod -R a-x,a=rX,u+w "$war"/"$dmp"/
        echo "chmod -R 000 "$war"/"$dmp"/" | at now + 1 hours
    }
EOF

source "$HOME"/.profile 2>/dev/null

文件1模式

  • 模式set -x意味着常量在完全调试模式下工作。
  • 模式complete -r意味着不断删除可编程完成(通过调用函数等)在完全调试模式中常见的杂乱输出。

文件1变量

  • war变量的值反映了用户首选的WebApplication根目录
  • dmp变量的值反映了用户首选的数据库管理程序(如phpMiniAdmin)

文件1函数

  • 函数war的意思是“轻松、快速地导航到网络应用程序根”
  • 函数ssr意味着Secured服务器重新启动:;也就是说,通过重复可能被错误更改的基本安全指令重新启动Secured服务器,并允许通过数据库管理程序临时管理MySQL数据库。
  • 函数tmd是指临时管理数据库,并在DB管理器安全锁后使用ssr()

文件2

此文件包含基本的应用程序安装和/或配置命令。

代码语言:javascript
运行
复制
#!/bin/bash

apt update -y
apt upgrades ufw sshguard unattended-upgrades wget curl git zip unzip tree -y
ufw --force enable
ufw allow 22,25,80,443

apt install lamp-server^ python-certbot-apache
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
a2enmod http2 deflate expires

文件3

此文件用于创建Apache虚拟主机和相关文件。

只有在MySQL程序之上创建一个CMS上下文数据库之后才能执行该文件,该数据库基于一个单一的数据模式,<#>the web域也用作以下名称:

  • Web应用程序数据库用户
  • Web应用程序DB实例
  • Web应用程序目录(在下一章中解释)

档案:

代码语言:javascript
运行
复制
#!/bin/bash

read -p "Have you created db credentials already?" yn
case $yn in
    [Yy]* ) break;;
    [Nn]* ) exit;;
    * ) echo "Please create db credentials and then comeback;";;
esac

function read_and_verify  {
    read -p "$1:" tmp1
    read -p "$2:" tmp2
    if [ "$tmp1" != "$tmp2" ]; then
        echo "Values unmatched. Please try again."; return 2
    else
        read "$1" <<< "$tmp1"
    fi
}

read_and_verify domain "Please enter the domain of your web application twice" 
read_and_verify dbrootp "Please enter the app DB root password twice" 
read_and_verify dbuserp "Please enter the app DB user password twice"

cat <<-EOF > /etc/apache2/sites-available/$domain_2.conf
    
        ServerAdmin admin@"$domain_2"
        ServerName ${domain_2}
        ServerAlias www.${domain_2}
        DocumentRoot $war/${domain_2}
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
EOF

ln -sf /etc/apache2/sites-available/"$domain_2".conf /etc/apache2/sites-enabled/
certbot --apache -d "$domain_2" -d www."$domain_2"

可能的附录-安装

由于使用本地Drush安装由编写器驱动的drupal项目是该程序的最初目标,因此我附加如下:

代码语言:javascript
运行
复制
composer create-project drupal-composer/drupal-project "$war"/"$domain"
cp "$drt/$domain"/wp-config-sample.php "$war/$domain"/wp-config.php
drush --root="$war" --uri="$domain" pm install redirect token metatag draggableviews
drush --root="$war" --uri="$domain" en language content_translation redirect token metatag draggableviews

Notes

  • 该程序不包括备份,因为我个人认为,每个托管提供商,包括专用主机,无论是半(商业)还是全(私有),应该包括一个可靠的,最好是社区维护的自动每日备份机制,以及手动备份标准化例行程序(比如每季度或半年进行手动备份)。
  • 这个程序不包括web应用程序升级,因为我个人认为每个CMS都应该默认包含一个升级机制,而不需要用户从后端进行最大程度的升级自动化。

我的问题

How您会修改这个版本不可知的环境引导程序吗?

EN

回答 1

Code Review用户

回答已采纳

发布于 2019-11-27 13:52:32

首先,在.profile开始的时候写一个Bash是错误的,原因有两个:

  1. 它不是一个可执行的脚本,而是打算从其他shell环境中获取
  2. .profile是由用户的shell提供的,它不一定是Bash。

如果可能的话,应该重写基本语言;不能的(如complete -r)应该迁移到.bash_profile

这些看起来有点奇怪:

chmod -R a-x,a=rX,u+w "$war"/"$dmp"/ echo "chmod -R 000 "$war"/"$dmp"/" | at now + 1 hours

在第一种情况下,不需要为/字符保留双引号字符串。在第二种情况下,我们肯定不希望变量中的参数分裂。我会把这两行改写成:

代码语言:javascript
运行
复制
    chmod -R a-x,a=rX,u+w "$war/$dmp/"
    echo "chmod -R 000 $war/$dmp/" | at now + 1 hours

curl -sS https://getcomposer.org/installer -o composer-setup.php composer-setup.php -install-dir=/usr/local/bin-filename=composer

首先,我们不检查curl是否成功,而是取决于其结果。以set -e为例。

第二,更重要的是,没有验证我们下载的内容是安全的。在执行该PHP程序之前,我希望至少有一个简单的SHA校验和。

阅读-p“您已经创建了db凭据吗?”yn大小写$yn in *)断开;;*)退出;;*)回显“请创建db凭据,然后返回;

首先,一个简单的拼写错误--“回来”--失去了它的空间。其次,在输入n时不使用消息退出似乎是错误的,但是在给出无效的答复时假设为n是错误的。

我希望这样的事情:

代码语言:javascript
运行
复制
read -p "Have you created db credentials already? [y/N] " yn
case "$yn" in
    [Yy]* ) true ;;
    [Nn]* ) echo >&2 "Please create db credentials and repeat this command;"; exit ;;
    * ) echo >&2 "Please respond with Y or N."; exit 1 ;;
esac

提示符在这里吗?

函数read_and_verify { read -p "$1:“tmp1 read -p "$2:”tmp2 if;然后回显“未匹配的”值。请再试一次。“;返回2条阅读"$1”<<< "$tmp1“fi } read_and_verify域”请两次输入web应用程序的域“”read_and_verify dbrootp“请输入应用程序DB根密码两次”read_and_verify dbuserp“请输入应用程序DB用户密码两次”

第一个读取(在其中提示用户使用变量名称)是意外的;这在用户的world模型中不是这样,第二个提示则要求相同的内容(用不同的话说)“两次”:

代码语言:javascript
运行
复制
domain:foo
Please enter the domain of your web application twice:foo

我认为每次都应该从$2生成提示符:

代码语言:javascript
运行
复制
read_and_verify()  {
    read -p "Please enter $2: " tmp1
    read -p "Please enter $2 again to confirm: " tmp2
    if [ "$tmp1" != "$tmp2" ]; then
        echo >&2 "Values unmatched. Please try again."; return 2
    fi

    declare "$1=$tmp1"
}

read_and_verify domain "the domain of your web application" 
read_and_verify dbrootp "the app DB root password" 
read_and_verify dbuserp "the app DB user password"
代码语言:javascript
运行
复制
Please enter the domain of your web application: foo
Please enter the domain of your web application again to confirm: foo

尽管如此,要求用户重新键入(或复制粘贴)这些值是很烦人的,而且不会比重新打印和请求确认更有效:

代码语言:javascript
运行
复制
read_and_verify()  {
    read -p "Please enter $2: " "$1"
    read -p "Please confirm $2: ${!1} [y/N] " yn
    case "$yn" in
        [Yy]*) true ;;
        *) echo >&2 "Cancelled." return 2
    esac
}
代码语言:javascript
运行
复制
Please enter the domain of your web application: foo
Please confirm the domain of your web application: foo [y/N] y

实际上,我可能会四处乱搞,收集所有的输入,并提供一个简单的摘要以供确认:

代码语言:javascript
运行
复制
ask_yn() {
    while read -p "$* [y/n] " -n1
    do
       case "$REPLY" in
            ?) echo ;;&
            [Yy]) return 0 ;;
            [Nn]) return 1 ;;
       esac
    done
}

until
    read -p 'Please enter the domain of your web application: ' domain
    read -p 'Please enter the app DB root password: ' dbrootp
    read -p 'Please enter the app DB user password: ' dbuserp
    printf '%s: "%s"\n' \
           'Application domain' "$domain" \
           'Database root password' "$dbrootp" \
           'Database user password' "$dbuserp"
    ask_yn 'Are these details correct?'
do
    true
done

有一种情况是,两次请求相同的输入是有价值的,当用户设置一个看不见的值时(比如使用passwd程序时)。那里的理由是,一个错误不能被发现或容易纠正后,事实(因为你被锁在你的帐户之外,直到/除非你可以重新创建相同的错误)。

在这里,我们只是存储值,如果我们发现值是错误的,我们可以很容易地修复它们。(实际上,我们甚至可以在接受设置之前测试密码,只需与数据库建立简单的连接)。

票数 6
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/233067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档