如何编写服务器初始化脚本?

#!/bin/bash
更新包列表并升级所有已安装的软件包
echo "Updating package list and upgrading installed packages..."
sudo apt-get update && sudo apt-get upgrade -y
安装常用软件包和工具
echo "Installing essential software packages and tools..."
sudo apt-get install -y build-essential curl wget git unzip vim net-tools
设置时区为上海(可以根据需要更改)
echo "Setting timezone to Shanghai..."
sudo timedatectl set-timezone Asia/Shanghai
启用并启动SSH服务
echo "Enabling and starting SSH service..."
sudo systemctl enable ssh
sudo systemctl start ssh
安装并配置防火墙 (ufw)
echo "Installing and configuring UFW firewall..."
sudo apt-get install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
安装并配置NGINX
echo "Installing and configuring NGINX..."
sudo apt-get install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
创建一个简单的NGINX默认页面
echo "Creating a simple NGINX default page..."
cat <<EOL | sudo tee /var/www/html/index.html
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Server</title>
</head>
<body>
    <h1>Success! Your server is now running NGINX.</h1>
    <p>Your IP address is: $(hostname -I)</p>
</body>
</html>
EOL
安装并配置MySQL数据库
echo "Installing and configuring MySQL database..."
sudo debconf-set-selections <<<'EOF'
mysql-server mysql-server/root_password password your_secure_password
mysql-server mysql-server/root_password_again password your_secure_password
EOF
sudo apt-get install -y mysql-server
sudo systemctl enable mysql
sudo systemctl start mysql
安装PHP和一些常用的PHP扩展
echo "Installing PHP and common extensions..."
sudo apt-get install -y php-fpm php-mysql php-cli php-curl php-json php-mbstring php-xml php-gd
重启NGINX以应用PHP-FPM配置
echo "Restarting NGINX to apply PHP-FPM configuration..."
sudo systemctl restart nginx
安装Composer(PHP的依赖管理工具)
echo "Installing Composer for PHP..."
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RESULT=$?
rm composer-setup.php
if [ $RESULT -ne 0 ]
then
    echo 'ERROR: Composer installation failed'
    exit $RESULT
fi
清理临时文件和缓存
echo "Cleaning up temporary files and cache..."
sudo apt-get clean
sudo apt-get autoremove -y
sudo apt-get autoclean -y
sudo systemctl daemon-reload
echo "Server initialization script completed successfully!"

在运行此脚本之前,请确保将your_secure_password 替换为你的实际MySQL root密码,根据你的需求,你可能还需要进一步调整脚本内容。

服务器初始化脚本

各位小伙伴们,我刚刚为大家分享了有关“服务器初始化脚本”的知识,希望对你们有所帮助。如果您还有其他相关问题需要解决,欢迎随时提出哦!

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/655739.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seo的头像K-seoSEO优化员
Previous 2024-11-18 14:13
Next 2024-11-18 14:35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入