兰生幽谷,不为莫服而不芳;
君子行义,不为莫知而止休。
1 准备工作
|------------------------------------------------------------------------------------------------|
| yum -y install wget git git clone https://github.com/teddysun/lamp.git cd lamp chmod 755 *.sh |
2 脚本
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #!/bin/bash #----------------------------------------------------------- # File Name: lamp.sh # Version: 1.0 # Created: 2025-05-18 22:27:20 # Author: 网工xxx # Contact: QQ15523232551 # Copyright(R):CQIE # Description: 综合前面的脚本编程结果,综合部署LAMP #先决条件:1.本例要centos8以上,顺利连通外网; # 2.做好yum源架设; # 3.Git上有很多资源,我们要善于利用互联网资源 # 先安装git工具,拉资源: # git clone https://github.com/teddysun/lamp.git # cd lamp # 4. 由于centos停更了,部署过程不顺利,最开始要提前 # 安装mtr, 然后就是要把remi-release-8.rpm改为remi-release-8.5.rpm # (后面标红了的地方,就是对原脚本加以改进了的。) #=========================================================== # This is a Shell script for VPS initialization and # LAMP (Linux + Apache + MariaDB + PHP) installation # # Supported OS: # Enterprise Linux 8 (CentOS 8, RHEL 8, Rocky Linux 8, AlmaLinux 8, Oracle Linux 8) # Enterprise Linux 9 (CentOS 9, RHEL 9, Rocky Linux 9, AlmaLinux 9, Oracle Linux 9) # # Copyright (C) 2013 - 2025 Teddysun <i@teddysun.com> # yum install -y mtr trap _exit INT QUIT TERM cur_dir="(cd -P -- "(dirname -- "0")" \&\& pwd -P)" _red() { printf '\\033\[1;31;31m%b\\033\[0m' "1" } _green() { printf '\0331;31;32m%b\\033\[0m' "$1" } _yellow() { printf '\\033\[1;31;33m%b\\033\[0m' "$1" } _printargs() { printf -- "%s" "\[$(date) " printf -- "%s" "1" printf "\\n" } _info() { _printargs "@" } _warn() { printf -- "%s" "$(date) " _yellow "1" printf "\\n" } _error() { printf -- "%s" "\[(date)] " _red "1" printf "\\n" exit 2 } _exit() { printf "\\n" _red "0 has been terminated." printf "\n" exit 1 } _exists() { local cmd="1" if eval type type \>/dev/null 2\>\&1; then eval type "cmd" >/dev/null 2>&1 elif command >/dev/null 2>&1; then command -v "cmd" \>/dev/null 2\>\&1 else which "cmd" >/dev/null 2>&1 fi local rt=? return {rt} } _error_detect() { local cmd="1" _info "{cmd}" if ! eval "{cmd}" 1\>/dev/null; then _error "Execution command ({cmd}) failed, please check it and try again." fi } check_sys() { local value="1" local release='' if \[ -f /etc/redhat-release \]; then release="rhel" elif grep -Eqi "debian" /etc/issue; then release="debian" elif grep -Eqi "ubuntu" /etc/issue; then release="ubuntu" elif grep -Eqi "centos\|red hat\|redhat" /etc/issue; then release="rhel" elif grep -Eqi "debian" /proc/version; then release="debian" elif grep -Eqi "ubuntu" /proc/version; then release="ubuntu" elif grep -Eqi "centos\|red hat\|redhat" /proc/version; then release="rhel" fi if \[ "{value}" == "{release}" \]; then return 0 else return 1 fi } get_char() { SAVEDSTTY=(stty -g) stty -echo stty cbreak dd if=/dev/tty bs=1 count=1 2>/dev/null stty -raw stty echo stty "{SAVEDSTTY}" } get_opsy() { \[ -f /etc/redhat-release \] \&\& awk '{print (1,3\~/\^\[0-9\]/?3:4)}' /etc/redhat-release \&\& return \[ -f /etc/os-release \] \&\& awk -F'\[= "\]' '/PRETTY_NAME/{print 3,4,5}' /etc/os-release && return -f /etc/lsb-release && awk -F'="+' '/DESCRIPTION/{print 2}' /etc/lsb-release \&\& return } get_rhelversion() { if check_sys rhel; then local version local code=1 local main_ver version=(get_opsy) main_ver=(echo "{version}" \| grep -oE "\[0-9.\]+") if \[ "{main_ver%%.*}" == "{code}" \]; then return 0 else return 1 fi else return 1 fi } get_debianversion() { if check_sys debian; then local version local code=1 local main_ver version=(get_opsy) main_ver=(echo "{version}" \| grep -oE "\[0-9.\]+") if \[ "{main_ver%%.*}" == "{code}" \]; then return 0 else return 1 fi else return 1 fi } get_ubuntuversion() { if check_sys ubuntu; then local version local code=1 version=(get_opsy) if echo "{version}" | grep -q "{code}"; then return 0 else return 1 fi else return 1 fi } version_ge() { test "(echo "@" \| tr " " "\\n" \| sort -rV \| head -n 1)" == "1" } check_kernel_version() { local kernel_version kernel_version=(uname -r \| cut -d- -f1) if version_ge "{kernel_version}" 4.9; then return 0 else return 1 fi } check_bbr_status() { local param param=(sysctl net.ipv4.tcp_congestion_control \| awk '{print 3}') if \[ "${param}" == "bbr" ]; then return 0 else return 1 fi } # Check user ${EUID} -ne 0 && _red "This script must be run as root!\n" && exit 1 # Check OS if ! get_rhelversion 8 && ! get_rhelversion 9; then _error "Not supported OS, please change OS to Enterprise Linux 8 or Enterprise Linux 9 and try again." fi # Choose MariaDB version while true; do _info "Please choose a version of the MariaDB:" _info "(_green 1). MariaDB 10.11" _info "(_green 2). MariaDB 11.4" read -r -p "$(date) Please input a number: (Default 2) " mariadb_version -z "${mariadb_version}" && mariadb_version=2 case "{mariadb_version}" in 1) mariadb_ver="10.11" break ;; 2) mariadb_ver="11.4" break ;; \*) _info "Input error! Please only input a number 1 2" ;; esac done _info "---------------------------" _info "MariaDB version = (_red "{mariadb_ver}")" _info "---------------------------" # Set MariaDB root password _info "Please input the root password of MariaDB:" read -r -p "\[(date)] (Default password: Teddysun.com):" db_pass if -z "${db_pass}" ; then db_pass="Teddysun.com" fi _info "---------------------------" _info "Password = (_red "{db_pass}")" _info "---------------------------" # Choose PHP version while true; do _info "Please choose a version of the PHP:" _info "(_green 1). PHP 7.4" _info "(_green 2). PHP 8.0" _info "(_green 3). PHP 8.1" _info "(_green 4). PHP 8.2" _info "(_green 5). PHP 8.3" _info "(_green 6). PHP 8.4" read -r -p "$(date) Please input a number: (Default 5) " php_version -z "${php_version}" && php_version=5 case "{php_version}" in 1) php_ver="7.4" break ;; 2) php_ver="8.0" break ;; 3) php_ver="8.1" break ;; 4) php_ver="8.2" break ;; 5) php_ver="8.3" break ;; 6) php_ver="8.4" break ;; \*) _info "Input error! Please only input a number 1 2 3 4 5 6" ;; esac done _info "---------------------------" _info "PHP version = (_red "{php_ver}")" _info "---------------------------" _info "Press any key to start...or Press Ctrl+C to cancel" char=(get_char) _info "Initialization start" _error_detect "rm -f /etc/localtime" _error_detect "ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime" _error_detect "dnf install -yq yum-utils epel-release" _error_detect "dnf config-manager --enable epel" if get_rhelversion 8; then dnf config-manager --enable powertools >/dev/null 2>&1 || dnf config-manager --enable PowerTools >/dev/null 2>&1 _info "Set enable PowerTools Repository completed" _error_detect "dnf install -yq https://dl.lamp.sh/linux/rhel/el8/x86_64/teddysun-release-1.0-1.el8.noarch.rpm" fi if get_rhelversion 9; then _error_detect "dnf config-manager --enable crb" _info "Set enable CRB Repository completed" echo "set enable-bracketed-paste off" >>/etc/inputrc _error_detect "dnf install -y https://dl.lamp.sh/linux/rhel/el9/x86_64/teddysun-release-1.0-1.el9.noarch.rpm" fi _error_detect "dnf makecache" _error_detect "dnf install -yq vim tar zip unzip net-tools bind-utils screen git virt-what wget whois firewalld traceroute iftop htop jq tree" _error_detect "dnf install -yq libnghttp2 libnghttp2-devel" # Replaced local curl from teddysun linux Repository _error_detect "dnf install -yq curl libcurl libcurl-devel" if -s "/etc/selinux/config" && grep 'SELINUX=enforcing' /etc/selinux/config; then sed -i 's@^SELINUX.*@SELINUX=disabled@g' /etc/selinux/config setenforce 0 _info "Disable SElinux completed" fi if -s "/etc/motd.d/cockpit" ; then rm -f /etc/motd.d/cockpit _info "Delete /etc/motd.d/cockpit completed" fi if systemctl status firewalld >/dev/null 2>&1; then default_zone="(firewall-cmd --get-default-zone)" firewall-cmd --permanent --add-service=https --zone="{default_zone}" >/dev/null 2>&1 firewall-cmd --permanent --add-service=http --zone="{default_zone}" \>/dev/null 2\>\&1 firewall-cmd --reload \>/dev/null 2\>\&1 sed -i 's@AllowZoneDrifting=yes@AllowZoneDrifting=no@' /etc/firewalld/firewalld.conf _error_detect "systemctl restart firewalld" _info "Set firewall completed" else _warn "firewalld looks like not running, skip setting up firewall" fi if check_kernel_version; then if ! check_bbr_status; then sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf sed -i '/net.core.rmem_max/d' /etc/sysctl.conf cat \>\>/etc/sysctl.conf \<\
3 验证
测试:
